blob: 6e5b7e4636989661e1e506993dce9f82faee3f35 [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
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500354 for (i = 0; i < io_ctl->num_pages; i++) {
355 clear_page_dirty_for_io(io_ctl->pages[i]);
356 set_page_extent_mapped(io_ctl->pages[i]);
357 }
358
Josef Bacika67509c2011-10-05 15:18:58 -0400359 return 0;
360}
361
362static void io_ctl_set_generation(struct io_ctl *io_ctl, u64 generation)
363{
364 u64 *val;
365
366 io_ctl_map_page(io_ctl, 1);
367
368 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400369 * Skip the csum areas. If we don't check crcs then we just have a
370 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400371 */
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400372 if (io_ctl->check_crcs) {
373 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
374 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
375 } else {
376 io_ctl->cur += sizeof(u64);
377 io_ctl->size -= sizeof(u64) * 2;
378 }
Josef Bacika67509c2011-10-05 15:18:58 -0400379
380 val = io_ctl->cur;
381 *val = cpu_to_le64(generation);
382 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400383}
384
385static int io_ctl_check_generation(struct io_ctl *io_ctl, u64 generation)
386{
387 u64 *gen;
388
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400389 /*
390 * Skip the crc area. If we don't check crcs then we just have a 64bit
391 * chunk at the front of the first page.
392 */
393 if (io_ctl->check_crcs) {
394 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
395 io_ctl->size -= sizeof(u64) +
396 (sizeof(u32) * io_ctl->num_pages);
397 } else {
398 io_ctl->cur += sizeof(u64);
399 io_ctl->size -= sizeof(u64) * 2;
400 }
Josef Bacika67509c2011-10-05 15:18:58 -0400401
Josef Bacika67509c2011-10-05 15:18:58 -0400402 gen = io_ctl->cur;
403 if (le64_to_cpu(*gen) != generation) {
404 printk_ratelimited(KERN_ERR "btrfs: space cache generation "
405 "(%Lu) does not match inode (%Lu)\n", *gen,
406 generation);
407 io_ctl_unmap_page(io_ctl);
408 return -EIO;
409 }
410 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400411 return 0;
412}
413
414static void io_ctl_set_crc(struct io_ctl *io_ctl, int index)
415{
416 u32 *tmp;
417 u32 crc = ~(u32)0;
418 unsigned offset = 0;
419
420 if (!io_ctl->check_crcs) {
421 io_ctl_unmap_page(io_ctl);
422 return;
423 }
424
425 if (index == 0)
426 offset = sizeof(u32) * io_ctl->num_pages;;
427
428 crc = btrfs_csum_data(io_ctl->root, io_ctl->orig + offset, crc,
429 PAGE_CACHE_SIZE - offset);
430 btrfs_csum_final(crc, (char *)&crc);
431 io_ctl_unmap_page(io_ctl);
432 tmp = kmap(io_ctl->pages[0]);
433 tmp += index;
434 *tmp = crc;
435 kunmap(io_ctl->pages[0]);
436}
437
438static int io_ctl_check_crc(struct io_ctl *io_ctl, int index)
439{
440 u32 *tmp, val;
441 u32 crc = ~(u32)0;
442 unsigned offset = 0;
443
444 if (!io_ctl->check_crcs) {
445 io_ctl_map_page(io_ctl, 0);
446 return 0;
447 }
448
449 if (index == 0)
450 offset = sizeof(u32) * io_ctl->num_pages;
451
452 tmp = kmap(io_ctl->pages[0]);
453 tmp += index;
454 val = *tmp;
455 kunmap(io_ctl->pages[0]);
456
457 io_ctl_map_page(io_ctl, 0);
458 crc = btrfs_csum_data(io_ctl->root, io_ctl->orig + offset, crc,
459 PAGE_CACHE_SIZE - offset);
460 btrfs_csum_final(crc, (char *)&crc);
461 if (val != crc) {
462 printk_ratelimited(KERN_ERR "btrfs: csum mismatch on free "
463 "space cache\n");
464 io_ctl_unmap_page(io_ctl);
465 return -EIO;
466 }
467
Josef Bacika67509c2011-10-05 15:18:58 -0400468 return 0;
469}
470
471static int io_ctl_add_entry(struct io_ctl *io_ctl, u64 offset, u64 bytes,
472 void *bitmap)
473{
474 struct btrfs_free_space_entry *entry;
475
476 if (!io_ctl->cur)
477 return -ENOSPC;
478
479 entry = io_ctl->cur;
480 entry->offset = cpu_to_le64(offset);
481 entry->bytes = cpu_to_le64(bytes);
482 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
483 BTRFS_FREE_SPACE_EXTENT;
484 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
485 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
486
487 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
488 return 0;
489
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400490 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400491
492 /* No more pages to map */
493 if (io_ctl->index >= io_ctl->num_pages)
494 return 0;
495
496 /* map the next page */
497 io_ctl_map_page(io_ctl, 1);
498 return 0;
499}
500
501static int io_ctl_add_bitmap(struct io_ctl *io_ctl, void *bitmap)
502{
503 if (!io_ctl->cur)
504 return -ENOSPC;
505
506 /*
507 * If we aren't at the start of the current page, unmap this one and
508 * map the next one if there is any left.
509 */
510 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400511 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400512 if (io_ctl->index >= io_ctl->num_pages)
513 return -ENOSPC;
514 io_ctl_map_page(io_ctl, 0);
515 }
516
517 memcpy(io_ctl->cur, bitmap, PAGE_CACHE_SIZE);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400518 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400519 if (io_ctl->index < io_ctl->num_pages)
520 io_ctl_map_page(io_ctl, 0);
521 return 0;
522}
523
524static void io_ctl_zero_remaining_pages(struct io_ctl *io_ctl)
525{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400526 /*
527 * If we're not on the boundary we know we've modified the page and we
528 * need to crc the page.
529 */
530 if (io_ctl->cur != io_ctl->orig)
531 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
532 else
533 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400534
535 while (io_ctl->index < io_ctl->num_pages) {
536 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400537 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400538 }
539}
540
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400541static int io_ctl_read_entry(struct io_ctl *io_ctl,
542 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400543{
544 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500545 int ret;
546
547 if (!io_ctl->cur) {
548 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
549 if (ret)
550 return ret;
551 }
Josef Bacika67509c2011-10-05 15:18:58 -0400552
553 e = io_ctl->cur;
554 entry->offset = le64_to_cpu(e->offset);
555 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400556 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400557 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
558 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
559
560 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400561 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400562
563 io_ctl_unmap_page(io_ctl);
564
Josef Bacik2f120c02011-11-10 20:45:05 -0500565 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400566}
567
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400568static int io_ctl_read_bitmap(struct io_ctl *io_ctl,
569 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400570{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400571 int ret;
572
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400573 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
574 if (ret)
575 return ret;
576
Josef Bacika67509c2011-10-05 15:18:58 -0400577 memcpy(entry->bitmap, io_ctl->cur, PAGE_CACHE_SIZE);
578 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400579
580 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400581}
582
Li Zefan0414efa2011-04-20 10:20:14 +0800583int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
584 struct btrfs_free_space_ctl *ctl,
585 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400586{
Josef Bacik9d66e232010-08-25 16:54:15 -0400587 struct btrfs_free_space_header *header;
588 struct extent_buffer *leaf;
Josef Bacika67509c2011-10-05 15:18:58 -0400589 struct io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400590 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400591 struct btrfs_free_space *e, *n;
Josef Bacik9d66e232010-08-25 16:54:15 -0400592 struct list_head bitmaps;
593 u64 num_entries;
594 u64 num_bitmaps;
595 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400596 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400597 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400598
599 INIT_LIST_HEAD(&bitmaps);
600
Josef Bacik9d66e232010-08-25 16:54:15 -0400601 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800602 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400603 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400604
605 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800606 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400607 key.type = 0;
608
609 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800610 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400611 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800612 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400613 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400614 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400615 }
616
Li Zefan0414efa2011-04-20 10:20:14 +0800617 ret = -1;
618
Josef Bacik9d66e232010-08-25 16:54:15 -0400619 leaf = path->nodes[0];
620 header = btrfs_item_ptr(leaf, path->slots[0],
621 struct btrfs_free_space_header);
622 num_entries = btrfs_free_space_entries(leaf, header);
623 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
624 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400625 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400626
627 if (BTRFS_I(inode)->generation != generation) {
628 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
Li Zefan0414efa2011-04-20 10:20:14 +0800629 " not match free space cache generation (%llu)\n",
Josef Bacik9d66e232010-08-25 16:54:15 -0400630 (unsigned long long)BTRFS_I(inode)->generation,
Li Zefan0414efa2011-04-20 10:20:14 +0800631 (unsigned long long)generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400632 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400633 }
634
635 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400636 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400637
Josef Bacika67509c2011-10-05 15:18:58 -0400638 io_ctl_init(&io_ctl, inode, root);
Josef Bacik9d66e232010-08-25 16:54:15 -0400639 ret = readahead_cache(inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800640 if (ret)
Josef Bacik9d66e232010-08-25 16:54:15 -0400641 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400642
Josef Bacika67509c2011-10-05 15:18:58 -0400643 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
644 if (ret)
645 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400646
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400647 ret = io_ctl_check_crc(&io_ctl, 0);
648 if (ret)
649 goto free_cache;
650
Josef Bacika67509c2011-10-05 15:18:58 -0400651 ret = io_ctl_check_generation(&io_ctl, generation);
652 if (ret)
653 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400654
Josef Bacika67509c2011-10-05 15:18:58 -0400655 while (num_entries) {
656 e = kmem_cache_zalloc(btrfs_free_space_cachep,
657 GFP_NOFS);
658 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400659 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400660
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400661 ret = io_ctl_read_entry(&io_ctl, e, &type);
662 if (ret) {
663 kmem_cache_free(btrfs_free_space_cachep, e);
664 goto free_cache;
665 }
666
Josef Bacika67509c2011-10-05 15:18:58 -0400667 if (!e->bytes) {
668 kmem_cache_free(btrfs_free_space_cachep, e);
669 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400670 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400671
Josef Bacika67509c2011-10-05 15:18:58 -0400672 if (type == BTRFS_FREE_SPACE_EXTENT) {
673 spin_lock(&ctl->tree_lock);
674 ret = link_free_space(ctl, e);
675 spin_unlock(&ctl->tree_lock);
676 if (ret) {
677 printk(KERN_ERR "Duplicate entries in "
678 "free space cache, dumping\n");
Josef Bacikdc89e982011-01-28 17:05:48 -0500679 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400680 goto free_cache;
681 }
Josef Bacika67509c2011-10-05 15:18:58 -0400682 } else {
683 BUG_ON(!num_bitmaps);
684 num_bitmaps--;
685 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
686 if (!e->bitmap) {
687 kmem_cache_free(
688 btrfs_free_space_cachep, e);
689 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400690 }
Josef Bacika67509c2011-10-05 15:18:58 -0400691 spin_lock(&ctl->tree_lock);
692 ret = link_free_space(ctl, e);
693 ctl->total_bitmaps++;
694 ctl->op->recalc_thresholds(ctl);
695 spin_unlock(&ctl->tree_lock);
696 if (ret) {
697 printk(KERN_ERR "Duplicate entries in "
698 "free space cache, dumping\n");
699 kmem_cache_free(btrfs_free_space_cachep, e);
700 goto free_cache;
701 }
702 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400703 }
704
Josef Bacika67509c2011-10-05 15:18:58 -0400705 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400706 }
707
Josef Bacik2f120c02011-11-10 20:45:05 -0500708 io_ctl_unmap_page(&io_ctl);
709
Josef Bacika67509c2011-10-05 15:18:58 -0400710 /*
711 * We add the bitmaps at the end of the entries in order that
712 * the bitmap entries are added to the cache.
713 */
714 list_for_each_entry_safe(e, n, &bitmaps, list) {
715 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400716 ret = io_ctl_read_bitmap(&io_ctl, e);
717 if (ret)
718 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400719 }
720
721 io_ctl_drop_pages(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400722 ret = 1;
723out:
Josef Bacika67509c2011-10-05 15:18:58 -0400724 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400725 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400726free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400727 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800728 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400729 goto out;
730}
731
Li Zefan0414efa2011-04-20 10:20:14 +0800732int load_free_space_cache(struct btrfs_fs_info *fs_info,
733 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400734{
Li Zefan34d52cb2011-03-29 13:46:06 +0800735 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800736 struct btrfs_root *root = fs_info->tree_root;
737 struct inode *inode;
738 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400739 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800740 bool matched;
741 u64 used = btrfs_block_group_used(&block_group->item);
742
743 /*
744 * If we're unmounting then just return, since this does a search on the
745 * normal root and not the commit root and we could deadlock.
746 */
David Sterba7841cb22011-05-31 18:07:27 +0200747 if (btrfs_fs_closing(fs_info))
Li Zefan0414efa2011-04-20 10:20:14 +0800748 return 0;
749
750 /*
751 * If this block group has been marked to be cleared for one reason or
752 * another then we can't trust the on disk cache, so just return.
753 */
754 spin_lock(&block_group->lock);
755 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
756 spin_unlock(&block_group->lock);
757 return 0;
758 }
759 spin_unlock(&block_group->lock);
760
761 path = btrfs_alloc_path();
762 if (!path)
763 return 0;
764
765 inode = lookup_free_space_inode(root, block_group, path);
766 if (IS_ERR(inode)) {
767 btrfs_free_path(path);
768 return 0;
769 }
770
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400771 /* We may have converted the inode and made the cache invalid. */
772 spin_lock(&block_group->lock);
773 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
774 spin_unlock(&block_group->lock);
775 goto out;
776 }
777 spin_unlock(&block_group->lock);
778
Li Zefan0414efa2011-04-20 10:20:14 +0800779 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
780 path, block_group->key.objectid);
781 btrfs_free_path(path);
782 if (ret <= 0)
783 goto out;
784
785 spin_lock(&ctl->tree_lock);
786 matched = (ctl->free_space == (block_group->key.offset - used -
787 block_group->bytes_super));
788 spin_unlock(&ctl->tree_lock);
789
790 if (!matched) {
791 __btrfs_remove_free_space_cache(ctl);
792 printk(KERN_ERR "block group %llu has an wrong amount of free "
793 "space\n", block_group->key.objectid);
794 ret = -1;
795 }
796out:
797 if (ret < 0) {
798 /* This cache is bogus, make sure it gets cleared */
799 spin_lock(&block_group->lock);
800 block_group->disk_cache_state = BTRFS_DC_CLEAR;
801 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800802 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800803
804 printk(KERN_ERR "btrfs: failed to load free space cache "
805 "for block group %llu\n", block_group->key.objectid);
806 }
807
808 iput(inode);
809 return ret;
810}
811
Josef Bacikc09544e2011-08-30 10:19:10 -0400812/**
813 * __btrfs_write_out_cache - write out cached info to an inode
814 * @root - the root the inode belongs to
815 * @ctl - the free space cache we are going to write out
816 * @block_group - the block_group for this cache if it belongs to a block_group
817 * @trans - the trans handle
818 * @path - the path to use
819 * @offset - the offset for the key we'll insert
820 *
821 * This function writes out a free space cache struct to disk for quick recovery
822 * on mount. This will return 0 if it was successfull in writing the cache out,
823 * and -1 if it was not.
824 */
Li Zefan0414efa2011-04-20 10:20:14 +0800825int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
826 struct btrfs_free_space_ctl *ctl,
827 struct btrfs_block_group_cache *block_group,
828 struct btrfs_trans_handle *trans,
829 struct btrfs_path *path, u64 offset)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400830{
831 struct btrfs_free_space_header *header;
832 struct extent_buffer *leaf;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400833 struct rb_node *node;
834 struct list_head *pos, *n;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400835 struct extent_state *cached_state = NULL;
Josef Bacik43be2142011-04-01 14:55:00 +0000836 struct btrfs_free_cluster *cluster = NULL;
837 struct extent_io_tree *unpin = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400838 struct io_ctl io_ctl;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400839 struct list_head bitmap_list;
840 struct btrfs_key key;
Josef Bacik43be2142011-04-01 14:55:00 +0000841 u64 start, end, len;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400842 int entries = 0;
843 int bitmaps = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -0400844 int ret;
845 int err = -1;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400846
Josef Bacik0cb59c92010-07-02 12:14:14 -0400847 INIT_LIST_HEAD(&bitmap_list);
848
Li Zefan0414efa2011-04-20 10:20:14 +0800849 if (!i_size_read(inode))
850 return -1;
Josef Bacik2b209822010-12-03 13:17:53 -0500851
Josef Bacika67509c2011-10-05 15:18:58 -0400852 io_ctl_init(&io_ctl, inode, root);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400853
Josef Bacik43be2142011-04-01 14:55:00 +0000854 /* Get the cluster for this block_group if it exists */
Li Zefan0414efa2011-04-20 10:20:14 +0800855 if (block_group && !list_empty(&block_group->cluster_list))
Josef Bacik43be2142011-04-01 14:55:00 +0000856 cluster = list_entry(block_group->cluster_list.next,
857 struct btrfs_free_cluster,
858 block_group_list);
859
860 /*
861 * We shouldn't have switched the pinned extents yet so this is the
862 * right one
863 */
864 unpin = root->fs_info->pinned_extents;
865
Josef Bacika67509c2011-10-05 15:18:58 -0400866 /* Lock all pages first so we can lock the extent safely. */
867 io_ctl_prepare_pages(&io_ctl, inode, 0);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400868
Josef Bacik0cb59c92010-07-02 12:14:14 -0400869 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
870 0, &cached_state, GFP_NOFS);
871
Josef Bacik43be2142011-04-01 14:55:00 +0000872 /*
873 * When searching for pinned extents, we need to start at our start
874 * offset.
875 */
Li Zefan0414efa2011-04-20 10:20:14 +0800876 if (block_group)
877 start = block_group->key.objectid;
Josef Bacik43be2142011-04-01 14:55:00 +0000878
Josef Bacikf75b1302011-10-05 10:00:18 -0400879 node = rb_first(&ctl->free_space_offset);
880 if (!node && cluster) {
881 node = rb_first(&cluster->root);
882 cluster = NULL;
883 }
884
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400885 /* Make sure we can fit our crcs into the first page */
886 if (io_ctl.check_crcs &&
887 (io_ctl.num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE) {
888 WARN_ON(1);
889 goto out_nospc;
890 }
891
Josef Bacika67509c2011-10-05 15:18:58 -0400892 io_ctl_set_generation(&io_ctl, trans->transid);
893
Josef Bacik0cb59c92010-07-02 12:14:14 -0400894 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400895 while (node) {
896 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400897
Josef Bacika67509c2011-10-05 15:18:58 -0400898 e = rb_entry(node, struct btrfs_free_space, offset_index);
899 entries++;
Josef Bacik43be2142011-04-01 14:55:00 +0000900
Josef Bacika67509c2011-10-05 15:18:58 -0400901 ret = io_ctl_add_entry(&io_ctl, e->offset, e->bytes,
902 e->bitmap);
903 if (ret)
904 goto out_nospc;
905
906 if (e->bitmap) {
907 list_add_tail(&e->list, &bitmap_list);
908 bitmaps++;
909 }
910 node = rb_next(node);
911 if (!node && cluster) {
912 node = rb_first(&cluster->root);
913 cluster = NULL;
914 }
915 }
916
917 /*
918 * We want to add any pinned extents to our free space cache
919 * so we don't leak the space
920 */
921 while (block_group && (start < block_group->key.objectid +
922 block_group->key.offset)) {
923 ret = find_first_extent_bit(unpin, start, &start, &end,
924 EXTENT_DIRTY);
925 if (ret) {
926 ret = 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400927 break;
928 }
929
Josef Bacika67509c2011-10-05 15:18:58 -0400930 /* This pinned extent is out of our range */
931 if (start >= block_group->key.objectid +
932 block_group->key.offset)
933 break;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400934
Josef Bacika67509c2011-10-05 15:18:58 -0400935 len = block_group->key.objectid +
936 block_group->key.offset - start;
937 len = min(len, end + 1 - start);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400938
Josef Bacika67509c2011-10-05 15:18:58 -0400939 entries++;
940 ret = io_ctl_add_entry(&io_ctl, start, len, NULL);
941 if (ret)
942 goto out_nospc;
Josef Bacik2f356122011-06-10 15:31:13 -0400943
Josef Bacika67509c2011-10-05 15:18:58 -0400944 start = end + 1;
945 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400946
947 /* Write out the bitmaps */
948 list_for_each_safe(pos, n, &bitmap_list) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400949 struct btrfs_free_space *entry =
950 list_entry(pos, struct btrfs_free_space, list);
951
Josef Bacika67509c2011-10-05 15:18:58 -0400952 ret = io_ctl_add_bitmap(&io_ctl, entry->bitmap);
953 if (ret)
954 goto out_nospc;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400955 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400956 }
957
Josef Bacik0cb59c92010-07-02 12:14:14 -0400958 /* Zero out the rest of the pages just to make sure */
Josef Bacika67509c2011-10-05 15:18:58 -0400959 io_ctl_zero_remaining_pages(&io_ctl);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400960
Josef Bacika67509c2011-10-05 15:18:58 -0400961 ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages,
962 0, i_size_read(inode), &cached_state);
963 io_ctl_drop_pages(&io_ctl);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400964 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
965 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
966
Josef Bacikc09544e2011-08-30 10:19:10 -0400967 if (ret)
Josef Bacik2f356122011-06-10 15:31:13 -0400968 goto out;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400969
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400970
Josef Bacik549b4fd2011-10-05 16:33:53 -0400971 ret = filemap_write_and_wait(inode->i_mapping);
972 if (ret)
973 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400974
975 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800976 key.offset = offset;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400977 key.type = 0;
978
Josef Bacika9b5fcd2011-08-19 12:06:12 -0400979 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400980 if (ret < 0) {
Josef Bacika67509c2011-10-05 15:18:58 -0400981 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400982 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
983 GFP_NOFS);
Josef Bacik2f356122011-06-10 15:31:13 -0400984 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400985 }
986 leaf = path->nodes[0];
987 if (ret > 0) {
988 struct btrfs_key found_key;
989 BUG_ON(!path->slots[0]);
990 path->slots[0]--;
991 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
992 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
Li Zefan0414efa2011-04-20 10:20:14 +0800993 found_key.offset != offset) {
Josef Bacika67509c2011-10-05 15:18:58 -0400994 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
995 inode->i_size - 1,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400996 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
997 NULL, GFP_NOFS);
David Sterbab3b4aa72011-04-21 01:20:15 +0200998 btrfs_release_path(path);
Josef Bacik2f356122011-06-10 15:31:13 -0400999 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001000 }
1001 }
Josef Bacik549b4fd2011-10-05 16:33:53 -04001002
1003 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001004 header = btrfs_item_ptr(leaf, path->slots[0],
1005 struct btrfs_free_space_header);
1006 btrfs_set_free_space_entries(leaf, header, entries);
1007 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1008 btrfs_set_free_space_generation(leaf, header, trans->transid);
1009 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001010 btrfs_release_path(path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001011
Josef Bacikc09544e2011-08-30 10:19:10 -04001012 err = 0;
Josef Bacik2f356122011-06-10 15:31:13 -04001013out:
Josef Bacika67509c2011-10-05 15:18:58 -04001014 io_ctl_free(&io_ctl);
Josef Bacikc09544e2011-08-30 10:19:10 -04001015 if (err) {
Josef Bacika67509c2011-10-05 15:18:58 -04001016 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001017 BTRFS_I(inode)->generation = 0;
1018 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001019 btrfs_update_inode(trans, root, inode);
Josef Bacikc09544e2011-08-30 10:19:10 -04001020 return err;
Josef Bacika67509c2011-10-05 15:18:58 -04001021
1022out_nospc:
1023 list_for_each_safe(pos, n, &bitmap_list) {
1024 struct btrfs_free_space *entry =
1025 list_entry(pos, struct btrfs_free_space, list);
1026 list_del_init(&entry->list);
1027 }
1028 io_ctl_drop_pages(&io_ctl);
1029 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1030 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
1031 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001032}
1033
1034int btrfs_write_out_cache(struct btrfs_root *root,
1035 struct btrfs_trans_handle *trans,
1036 struct btrfs_block_group_cache *block_group,
1037 struct btrfs_path *path)
1038{
1039 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1040 struct inode *inode;
1041 int ret = 0;
1042
1043 root = root->fs_info->tree_root;
1044
1045 spin_lock(&block_group->lock);
1046 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1047 spin_unlock(&block_group->lock);
1048 return 0;
1049 }
1050 spin_unlock(&block_group->lock);
1051
1052 inode = lookup_free_space_inode(root, block_group, path);
1053 if (IS_ERR(inode))
1054 return 0;
1055
1056 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
1057 path, block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001058 if (ret) {
Li Zefan0414efa2011-04-20 10:20:14 +08001059 spin_lock(&block_group->lock);
1060 block_group->disk_cache_state = BTRFS_DC_ERROR;
1061 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +08001062 ret = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -04001063#ifdef DEBUG
Li Zefan0414efa2011-04-20 10:20:14 +08001064 printk(KERN_ERR "btrfs: failed to write free space cace "
1065 "for block group %llu\n", block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001066#endif
Li Zefan0414efa2011-04-20 10:20:14 +08001067 }
1068
Josef Bacik0cb59c92010-07-02 12:14:14 -04001069 iput(inode);
1070 return ret;
1071}
1072
Li Zefan34d52cb2011-03-29 13:46:06 +08001073static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001074 u64 offset)
1075{
1076 BUG_ON(offset < bitmap_start);
1077 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001078 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001079}
1080
Li Zefan34d52cb2011-03-29 13:46:06 +08001081static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001082{
Li Zefan34d52cb2011-03-29 13:46:06 +08001083 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001084}
1085
Li Zefan34d52cb2011-03-29 13:46:06 +08001086static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001087 u64 offset)
1088{
1089 u64 bitmap_start;
1090 u64 bytes_per_bitmap;
1091
Li Zefan34d52cb2011-03-29 13:46:06 +08001092 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1093 bitmap_start = offset - ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001094 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
1095 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001096 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001097
1098 return bitmap_start;
1099}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001100
1101static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001102 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001103{
1104 struct rb_node **p = &root->rb_node;
1105 struct rb_node *parent = NULL;
1106 struct btrfs_free_space *info;
1107
1108 while (*p) {
1109 parent = *p;
1110 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1111
Josef Bacik96303082009-07-13 21:29:25 -04001112 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001113 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001114 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001115 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001116 } else {
1117 /*
1118 * we could have a bitmap entry and an extent entry
1119 * share the same offset. If this is the case, we want
1120 * the extent entry to always be found first if we do a
1121 * linear search through the tree, since we want to have
1122 * the quickest allocation time, and allocating from an
1123 * extent is faster than allocating from a bitmap. So
1124 * if we're inserting a bitmap and we find an entry at
1125 * this offset, we want to go right, or after this entry
1126 * logically. If we are inserting an extent and we've
1127 * found a bitmap, we want to go left, or before
1128 * logically.
1129 */
1130 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001131 if (info->bitmap) {
1132 WARN_ON_ONCE(1);
1133 return -EEXIST;
1134 }
Josef Bacik96303082009-07-13 21:29:25 -04001135 p = &(*p)->rb_right;
1136 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001137 if (!info->bitmap) {
1138 WARN_ON_ONCE(1);
1139 return -EEXIST;
1140 }
Josef Bacik96303082009-07-13 21:29:25 -04001141 p = &(*p)->rb_left;
1142 }
1143 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001144 }
1145
1146 rb_link_node(node, parent, p);
1147 rb_insert_color(node, root);
1148
1149 return 0;
1150}
1151
1152/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001153 * searches the tree for the given offset.
1154 *
Josef Bacik96303082009-07-13 21:29:25 -04001155 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1156 * want a section that has at least bytes size and comes at or after the given
1157 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001158 */
Josef Bacik96303082009-07-13 21:29:25 -04001159static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001160tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001161 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001162{
Li Zefan34d52cb2011-03-29 13:46:06 +08001163 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001164 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001165
Josef Bacik96303082009-07-13 21:29:25 -04001166 /* find entry that is closest to the 'offset' */
1167 while (1) {
1168 if (!n) {
1169 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001170 break;
1171 }
Josef Bacik96303082009-07-13 21:29:25 -04001172
1173 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1174 prev = entry;
1175
1176 if (offset < entry->offset)
1177 n = n->rb_left;
1178 else if (offset > entry->offset)
1179 n = n->rb_right;
1180 else
1181 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001182 }
1183
Josef Bacik96303082009-07-13 21:29:25 -04001184 if (bitmap_only) {
1185 if (!entry)
1186 return NULL;
1187 if (entry->bitmap)
1188 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001189
Josef Bacik96303082009-07-13 21:29:25 -04001190 /*
1191 * bitmap entry and extent entry may share same offset,
1192 * in that case, bitmap entry comes after extent entry.
1193 */
1194 n = rb_next(n);
1195 if (!n)
1196 return NULL;
1197 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1198 if (entry->offset != offset)
1199 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001200
Josef Bacik96303082009-07-13 21:29:25 -04001201 WARN_ON(!entry->bitmap);
1202 return entry;
1203 } else if (entry) {
1204 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001205 /*
Josef Bacik96303082009-07-13 21:29:25 -04001206 * if previous extent entry covers the offset,
1207 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001208 */
Josef Bacik96303082009-07-13 21:29:25 -04001209 n = &entry->offset_index;
1210 while (1) {
1211 n = rb_prev(n);
1212 if (!n)
1213 break;
1214 prev = rb_entry(n, struct btrfs_free_space,
1215 offset_index);
1216 if (!prev->bitmap) {
1217 if (prev->offset + prev->bytes > offset)
1218 entry = prev;
1219 break;
1220 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001221 }
Josef Bacik96303082009-07-13 21:29:25 -04001222 }
1223 return entry;
1224 }
1225
1226 if (!prev)
1227 return NULL;
1228
1229 /* find last entry before the 'offset' */
1230 entry = prev;
1231 if (entry->offset > offset) {
1232 n = rb_prev(&entry->offset_index);
1233 if (n) {
1234 entry = rb_entry(n, struct btrfs_free_space,
1235 offset_index);
1236 BUG_ON(entry->offset > offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001237 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001238 if (fuzzy)
1239 return entry;
1240 else
1241 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001242 }
1243 }
1244
Josef Bacik96303082009-07-13 21:29:25 -04001245 if (entry->bitmap) {
1246 n = &entry->offset_index;
1247 while (1) {
1248 n = rb_prev(n);
1249 if (!n)
1250 break;
1251 prev = rb_entry(n, struct btrfs_free_space,
1252 offset_index);
1253 if (!prev->bitmap) {
1254 if (prev->offset + prev->bytes > offset)
1255 return prev;
1256 break;
1257 }
1258 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001259 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001260 return entry;
1261 } else if (entry->offset + entry->bytes > offset)
1262 return entry;
1263
1264 if (!fuzzy)
1265 return NULL;
1266
1267 while (1) {
1268 if (entry->bitmap) {
1269 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001270 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001271 break;
1272 } else {
1273 if (entry->offset + entry->bytes > offset)
1274 break;
1275 }
1276
1277 n = rb_next(&entry->offset_index);
1278 if (!n)
1279 return NULL;
1280 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1281 }
1282 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001283}
1284
Li Zefanf333adb2010-11-09 14:57:39 +08001285static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001286__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001287 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001288{
Li Zefan34d52cb2011-03-29 13:46:06 +08001289 rb_erase(&info->offset_index, &ctl->free_space_offset);
1290 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001291}
1292
Li Zefan34d52cb2011-03-29 13:46:06 +08001293static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001294 struct btrfs_free_space *info)
1295{
Li Zefan34d52cb2011-03-29 13:46:06 +08001296 __unlink_free_space(ctl, info);
1297 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001298}
1299
Li Zefan34d52cb2011-03-29 13:46:06 +08001300static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001301 struct btrfs_free_space *info)
1302{
1303 int ret = 0;
1304
Josef Bacik96303082009-07-13 21:29:25 -04001305 BUG_ON(!info->bitmap && !info->bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001306 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001307 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001308 if (ret)
1309 return ret;
1310
Li Zefan34d52cb2011-03-29 13:46:06 +08001311 ctl->free_space += info->bytes;
1312 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001313 return ret;
1314}
1315
Li Zefan34d52cb2011-03-29 13:46:06 +08001316static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001317{
Li Zefan34d52cb2011-03-29 13:46:06 +08001318 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001319 u64 max_bytes;
1320 u64 bitmap_bytes;
1321 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001322 u64 size = block_group->key.offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001323 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1324 int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1325
1326 BUG_ON(ctl->total_bitmaps > max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001327
1328 /*
1329 * The goal is to keep the total amount of memory used per 1gb of space
1330 * at or below 32k, so we need to adjust how much memory we allow to be
1331 * used by extent based free space tracking
1332 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001333 if (size < 1024 * 1024 * 1024)
1334 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1335 else
1336 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1337 div64_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001338
Josef Bacik25891f72009-09-11 16:11:20 -04001339 /*
1340 * we want to account for 1 more bitmap than what we have so we can make
1341 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1342 * we add more bitmaps.
1343 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001344 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001345
Josef Bacik25891f72009-09-11 16:11:20 -04001346 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001347 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001348 return;
Josef Bacik96303082009-07-13 21:29:25 -04001349 }
Josef Bacik25891f72009-09-11 16:11:20 -04001350
1351 /*
1352 * we want the extent entry threshold to always be at most 1/2 the maxw
1353 * bytes we can have, or whatever is less than that.
1354 */
1355 extent_bytes = max_bytes - bitmap_bytes;
1356 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1357
Li Zefan34d52cb2011-03-29 13:46:06 +08001358 ctl->extents_thresh =
Josef Bacik25891f72009-09-11 16:11:20 -04001359 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
Josef Bacik96303082009-07-13 21:29:25 -04001360}
1361
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001362static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1363 struct btrfs_free_space *info,
1364 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001365{
Li Zefanf38b6e72011-03-14 13:40:51 +08001366 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001367
Li Zefan34d52cb2011-03-29 13:46:06 +08001368 start = offset_to_bit(info->offset, ctl->unit, offset);
1369 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001370 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001371
Li Zefanf38b6e72011-03-14 13:40:51 +08001372 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001373
1374 info->bytes -= bytes;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001375}
1376
1377static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1378 struct btrfs_free_space *info, u64 offset,
1379 u64 bytes)
1380{
1381 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001382 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001383}
1384
Li Zefan34d52cb2011-03-29 13:46:06 +08001385static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001386 struct btrfs_free_space *info, u64 offset,
1387 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001388{
Li Zefanf38b6e72011-03-14 13:40:51 +08001389 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001390
Li Zefan34d52cb2011-03-29 13:46:06 +08001391 start = offset_to_bit(info->offset, ctl->unit, offset);
1392 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001393 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001394
Li Zefanf38b6e72011-03-14 13:40:51 +08001395 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001396
1397 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001398 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001399}
1400
Li Zefan34d52cb2011-03-29 13:46:06 +08001401static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001402 struct btrfs_free_space *bitmap_info, u64 *offset,
1403 u64 *bytes)
1404{
1405 unsigned long found_bits = 0;
1406 unsigned long bits, i;
1407 unsigned long next_zero;
1408
Li Zefan34d52cb2011-03-29 13:46:06 +08001409 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001410 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001411 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001412
1413 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1414 i < BITS_PER_BITMAP;
1415 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1416 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1417 BITS_PER_BITMAP, i);
1418 if ((next_zero - i) >= bits) {
1419 found_bits = next_zero - i;
1420 break;
1421 }
1422 i = next_zero;
1423 }
1424
1425 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001426 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1427 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001428 return 0;
1429 }
1430
1431 return -1;
1432}
1433
Li Zefan34d52cb2011-03-29 13:46:06 +08001434static struct btrfs_free_space *
1435find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001436{
1437 struct btrfs_free_space *entry;
1438 struct rb_node *node;
1439 int ret;
1440
Li Zefan34d52cb2011-03-29 13:46:06 +08001441 if (!ctl->free_space_offset.rb_node)
Josef Bacik96303082009-07-13 21:29:25 -04001442 return NULL;
1443
Li Zefan34d52cb2011-03-29 13:46:06 +08001444 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001445 if (!entry)
1446 return NULL;
1447
1448 for (node = &entry->offset_index; node; node = rb_next(node)) {
1449 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1450 if (entry->bytes < *bytes)
1451 continue;
1452
1453 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001454 ret = search_bitmap(ctl, entry, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001455 if (!ret)
1456 return entry;
1457 continue;
1458 }
1459
1460 *offset = entry->offset;
1461 *bytes = entry->bytes;
1462 return entry;
1463 }
1464
1465 return NULL;
1466}
1467
Li Zefan34d52cb2011-03-29 13:46:06 +08001468static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001469 struct btrfs_free_space *info, u64 offset)
1470{
Li Zefan34d52cb2011-03-29 13:46:06 +08001471 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001472 info->bytes = 0;
Li Zefan34d52cb2011-03-29 13:46:06 +08001473 link_free_space(ctl, info);
1474 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001475
Li Zefan34d52cb2011-03-29 13:46:06 +08001476 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001477}
1478
Li Zefan34d52cb2011-03-29 13:46:06 +08001479static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001480 struct btrfs_free_space *bitmap_info)
1481{
Li Zefan34d52cb2011-03-29 13:46:06 +08001482 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001483 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001484 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001485 ctl->total_bitmaps--;
1486 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001487}
1488
Li Zefan34d52cb2011-03-29 13:46:06 +08001489static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001490 struct btrfs_free_space *bitmap_info,
1491 u64 *offset, u64 *bytes)
1492{
1493 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001494 u64 search_start, search_bytes;
1495 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001496
1497again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001498 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001499
Josef Bacik6606bb92009-07-31 11:03:58 -04001500 /*
1501 * XXX - this can go away after a few releases.
1502 *
1503 * since the only user of btrfs_remove_free_space is the tree logging
1504 * stuff, and the only way to test that is under crash conditions, we
1505 * want to have this debug stuff here just in case somethings not
1506 * working. Search the bitmap for the space we are trying to use to
1507 * make sure its actually there. If its not there then we need to stop
1508 * because something has gone wrong.
1509 */
1510 search_start = *offset;
1511 search_bytes = *bytes;
Josef Bacik13dbc082011-02-03 02:39:52 +00001512 search_bytes = min(search_bytes, end - search_start + 1);
Li Zefan34d52cb2011-03-29 13:46:06 +08001513 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
Josef Bacik6606bb92009-07-31 11:03:58 -04001514 BUG_ON(ret < 0 || search_start != *offset);
1515
Josef Bacik96303082009-07-13 21:29:25 -04001516 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001517 bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
Josef Bacik96303082009-07-13 21:29:25 -04001518 *bytes -= end - *offset + 1;
1519 *offset = end + 1;
1520 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001521 bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001522 *bytes = 0;
1523 }
1524
1525 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001526 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001527 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001528 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001529
Josef Bacik6606bb92009-07-31 11:03:58 -04001530 /*
1531 * no entry after this bitmap, but we still have bytes to
1532 * remove, so something has gone wrong.
1533 */
1534 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001535 return -EINVAL;
1536
Josef Bacik6606bb92009-07-31 11:03:58 -04001537 bitmap_info = rb_entry(next, struct btrfs_free_space,
1538 offset_index);
1539
1540 /*
1541 * if the next entry isn't a bitmap we need to return to let the
1542 * extent stuff do its work.
1543 */
Josef Bacik96303082009-07-13 21:29:25 -04001544 if (!bitmap_info->bitmap)
1545 return -EAGAIN;
1546
Josef Bacik6606bb92009-07-31 11:03:58 -04001547 /*
1548 * Ok the next item is a bitmap, but it may not actually hold
1549 * the information for the rest of this free space stuff, so
1550 * look for it, and if we don't find it return so we can try
1551 * everything over again.
1552 */
1553 search_start = *offset;
1554 search_bytes = *bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001555 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik6606bb92009-07-31 11:03:58 -04001556 &search_bytes);
1557 if (ret < 0 || search_start != *offset)
1558 return -EAGAIN;
1559
Josef Bacik96303082009-07-13 21:29:25 -04001560 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001561 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001562 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001563
1564 return 0;
1565}
1566
Josef Bacik2cdc3422011-05-27 14:07:49 -04001567static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1568 struct btrfs_free_space *info, u64 offset,
1569 u64 bytes)
1570{
1571 u64 bytes_to_set = 0;
1572 u64 end;
1573
1574 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1575
1576 bytes_to_set = min(end - offset, bytes);
1577
1578 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1579
1580 return bytes_to_set;
1581
1582}
1583
Li Zefan34d52cb2011-03-29 13:46:06 +08001584static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1585 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001586{
Li Zefan34d52cb2011-03-29 13:46:06 +08001587 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik96303082009-07-13 21:29:25 -04001588
1589 /*
1590 * If we are below the extents threshold then we can add this as an
1591 * extent, and don't have to deal with the bitmap
1592 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001593 if (ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001594 /*
1595 * If this block group has some small extents we don't want to
1596 * use up all of our free slots in the cache with them, we want
1597 * to reserve them to larger extents, however if we have plent
1598 * of cache left then go ahead an dadd them, no sense in adding
1599 * the overhead of a bitmap if we don't have to.
1600 */
1601 if (info->bytes <= block_group->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001602 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1603 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001604 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001605 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001606 }
1607 }
Josef Bacik96303082009-07-13 21:29:25 -04001608
1609 /*
1610 * some block groups are so tiny they can't be enveloped by a bitmap, so
1611 * don't even bother to create a bitmap for this
1612 */
1613 if (BITS_PER_BITMAP * block_group->sectorsize >
1614 block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08001615 return false;
1616
1617 return true;
1618}
1619
Josef Bacik2cdc3422011-05-27 14:07:49 -04001620static struct btrfs_free_space_op free_space_op = {
1621 .recalc_thresholds = recalculate_thresholds,
1622 .use_bitmap = use_bitmap,
1623};
1624
Li Zefan34d52cb2011-03-29 13:46:06 +08001625static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1626 struct btrfs_free_space *info)
1627{
1628 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001629 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08001630 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001631 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08001632 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001633
1634 bytes = info->bytes;
1635 offset = info->offset;
1636
Li Zefan34d52cb2011-03-29 13:46:06 +08001637 if (!ctl->op->use_bitmap(ctl, info))
1638 return 0;
1639
Josef Bacik2cdc3422011-05-27 14:07:49 -04001640 if (ctl->op == &free_space_op)
1641 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04001642again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04001643 /*
1644 * Since we link bitmaps right into the cluster we need to see if we
1645 * have a cluster here, and if so and it has our bitmap we need to add
1646 * the free space to that bitmap.
1647 */
1648 if (block_group && !list_empty(&block_group->cluster_list)) {
1649 struct btrfs_free_cluster *cluster;
1650 struct rb_node *node;
1651 struct btrfs_free_space *entry;
1652
1653 cluster = list_entry(block_group->cluster_list.next,
1654 struct btrfs_free_cluster,
1655 block_group_list);
1656 spin_lock(&cluster->lock);
1657 node = rb_first(&cluster->root);
1658 if (!node) {
1659 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001660 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001661 }
1662
1663 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1664 if (!entry->bitmap) {
1665 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001666 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001667 }
1668
1669 if (entry->offset == offset_to_bitmap(ctl, offset)) {
1670 bytes_added = add_bytes_to_bitmap(ctl, entry,
1671 offset, bytes);
1672 bytes -= bytes_added;
1673 offset += bytes_added;
1674 }
1675 spin_unlock(&cluster->lock);
1676 if (!bytes) {
1677 ret = 1;
1678 goto out;
1679 }
1680 }
Chris Mason38e87882011-06-10 16:36:57 -04001681
1682no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08001683 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04001684 1, 0);
1685 if (!bitmap_info) {
1686 BUG_ON(added);
1687 goto new_bitmap;
1688 }
1689
Josef Bacik2cdc3422011-05-27 14:07:49 -04001690 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
1691 bytes -= bytes_added;
1692 offset += bytes_added;
1693 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001694
1695 if (!bytes) {
1696 ret = 1;
1697 goto out;
1698 } else
1699 goto again;
1700
1701new_bitmap:
1702 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001703 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04001704 added = 1;
1705 info = NULL;
1706 goto again;
1707 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001708 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001709
1710 /* no pre-allocated info, allocate a new one */
1711 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05001712 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1713 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04001714 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001715 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001716 ret = -ENOMEM;
1717 goto out;
1718 }
1719 }
1720
1721 /* allocate the bitmap */
1722 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08001723 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001724 if (!info->bitmap) {
1725 ret = -ENOMEM;
1726 goto out;
1727 }
1728 goto again;
1729 }
1730
1731out:
1732 if (info) {
1733 if (info->bitmap)
1734 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001735 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001736 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001737
1738 return ret;
1739}
1740
Chris Mason945d8962011-05-22 12:33:42 -04001741static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001742 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001743{
Li Zefan120d66e2010-11-09 14:56:50 +08001744 struct btrfs_free_space *left_info;
1745 struct btrfs_free_space *right_info;
1746 bool merged = false;
1747 u64 offset = info->offset;
1748 u64 bytes = info->bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001749
Josef Bacik0f9dd462008-09-23 13:14:11 -04001750 /*
1751 * first we want to see if there is free space adjacent to the range we
1752 * are adding, if there is remove that struct and add a new one to
1753 * cover the entire range
1754 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001755 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001756 if (right_info && rb_prev(&right_info->offset_index))
1757 left_info = rb_entry(rb_prev(&right_info->offset_index),
1758 struct btrfs_free_space, offset_index);
1759 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001760 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001761
Josef Bacik96303082009-07-13 21:29:25 -04001762 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08001763 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001764 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001765 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001766 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001767 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001768 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001769 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001770 }
1771
Josef Bacik96303082009-07-13 21:29:25 -04001772 if (left_info && !left_info->bitmap &&
1773 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08001774 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001775 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001776 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001777 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001778 info->offset = left_info->offset;
1779 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001780 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001781 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001782 }
1783
Li Zefan120d66e2010-11-09 14:56:50 +08001784 return merged;
1785}
1786
Li Zefan581bb052011-04-20 10:06:11 +08001787int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
1788 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08001789{
1790 struct btrfs_free_space *info;
1791 int ret = 0;
1792
Josef Bacikdc89e982011-01-28 17:05:48 -05001793 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08001794 if (!info)
1795 return -ENOMEM;
1796
1797 info->offset = offset;
1798 info->bytes = bytes;
1799
Li Zefan34d52cb2011-03-29 13:46:06 +08001800 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08001801
Li Zefan34d52cb2011-03-29 13:46:06 +08001802 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08001803 goto link;
1804
1805 /*
1806 * There was no extent directly to the left or right of this new
1807 * extent then we know we're going to have to allocate a new extent, so
1808 * before we do that see if we need to drop this into a bitmap
1809 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001810 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08001811 if (ret < 0) {
1812 goto out;
1813 } else if (ret) {
1814 ret = 0;
1815 goto out;
1816 }
1817link:
Li Zefan34d52cb2011-03-29 13:46:06 +08001818 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001819 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05001820 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001821out:
Li Zefan34d52cb2011-03-29 13:46:06 +08001822 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001823
Josef Bacik0f9dd462008-09-23 13:14:11 -04001824 if (ret) {
Josef Bacik96303082009-07-13 21:29:25 -04001825 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001826 BUG_ON(ret == -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001827 }
1828
Josef Bacik0f9dd462008-09-23 13:14:11 -04001829 return ret;
1830}
1831
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001832int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1833 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001834{
Li Zefan34d52cb2011-03-29 13:46:06 +08001835 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001836 struct btrfs_free_space *info;
Josef Bacik96303082009-07-13 21:29:25 -04001837 struct btrfs_free_space *next_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001838 int ret = 0;
1839
Li Zefan34d52cb2011-03-29 13:46:06 +08001840 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001841
Josef Bacik96303082009-07-13 21:29:25 -04001842again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001843 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001844 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001845 /*
1846 * oops didn't find an extent that matched the space we wanted
1847 * to remove, look for a bitmap instead
1848 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001849 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04001850 1, 0);
1851 if (!info) {
Chris Mason24a70312011-11-21 09:39:11 -05001852 /* the tree logging code might be calling us before we
1853 * have fully loaded the free space rbtree for this
1854 * block group. So it is possible the entry won't
1855 * be in the rbtree yet at all. The caching code
1856 * will make sure not to put it in the rbtree if
1857 * the logging code has pinned it.
1858 */
Josef Bacik6606bb92009-07-31 11:03:58 -04001859 goto out_lock;
1860 }
Josef Bacik96303082009-07-13 21:29:25 -04001861 }
1862
1863 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1864 u64 end;
1865 next_info = rb_entry(rb_next(&info->offset_index),
1866 struct btrfs_free_space,
1867 offset_index);
1868
1869 if (next_info->bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08001870 end = next_info->offset +
1871 BITS_PER_BITMAP * ctl->unit - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001872 else
1873 end = next_info->offset + next_info->bytes;
1874
1875 if (next_info->bytes < bytes ||
1876 next_info->offset > offset || offset > end) {
1877 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1878 " trying to use %llu\n",
1879 (unsigned long long)info->offset,
1880 (unsigned long long)info->bytes,
1881 (unsigned long long)bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001882 WARN_ON(1);
1883 ret = -EINVAL;
Josef Bacik96303082009-07-13 21:29:25 -04001884 goto out_lock;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001885 }
Josef Bacik96303082009-07-13 21:29:25 -04001886
1887 info = next_info;
1888 }
1889
1890 if (info->bytes == bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001891 unlink_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001892 if (info->bitmap) {
1893 kfree(info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001894 ctl->total_bitmaps--;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001895 }
Josef Bacikdc89e982011-01-28 17:05:48 -05001896 kmem_cache_free(btrfs_free_space_cachep, info);
Chris Mason1eae31e2011-10-14 06:31:20 -04001897 ret = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001898 goto out_lock;
1899 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001900
Josef Bacik96303082009-07-13 21:29:25 -04001901 if (!info->bitmap && info->offset == offset) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001902 unlink_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001903 info->offset += bytes;
1904 info->bytes -= bytes;
Chris Mason1eae31e2011-10-14 06:31:20 -04001905 ret = link_free_space(ctl, info);
1906 WARN_ON(ret);
Josef Bacik96303082009-07-13 21:29:25 -04001907 goto out_lock;
1908 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001909
Josef Bacik96303082009-07-13 21:29:25 -04001910 if (!info->bitmap && info->offset <= offset &&
1911 info->offset + info->bytes >= offset + bytes) {
Chris Mason9b49c9b2008-09-24 11:23:25 -04001912 u64 old_start = info->offset;
1913 /*
1914 * we're freeing space in the middle of the info,
1915 * this can happen during tree log replay
1916 *
1917 * first unlink the old info and then
1918 * insert it again after the hole we're creating
1919 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001920 unlink_free_space(ctl, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001921 if (offset + bytes < info->offset + info->bytes) {
1922 u64 old_end = info->offset + info->bytes;
1923
1924 info->offset = offset + bytes;
1925 info->bytes = old_end - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001926 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001927 WARN_ON(ret);
1928 if (ret)
1929 goto out_lock;
Chris Mason9b49c9b2008-09-24 11:23:25 -04001930 } else {
1931 /* the hole we're creating ends at the end
1932 * of the info struct, just free the info
1933 */
Josef Bacikdc89e982011-01-28 17:05:48 -05001934 kmem_cache_free(btrfs_free_space_cachep, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001935 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001936 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001937
1938 /* step two, insert a new info struct to cover
1939 * anything before the hole
Chris Mason9b49c9b2008-09-24 11:23:25 -04001940 */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001941 ret = btrfs_add_free_space(block_group, old_start,
1942 offset - old_start);
Josef Bacik96303082009-07-13 21:29:25 -04001943 WARN_ON(ret);
1944 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001945 }
Josef Bacik96303082009-07-13 21:29:25 -04001946
Li Zefan34d52cb2011-03-29 13:46:06 +08001947 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001948 if (ret == -EAGAIN)
1949 goto again;
1950 BUG_ON(ret);
1951out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08001952 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001953out:
Josef Bacik25179202008-10-29 14:49:05 -04001954 return ret;
1955}
1956
Josef Bacik0f9dd462008-09-23 13:14:11 -04001957void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1958 u64 bytes)
1959{
Li Zefan34d52cb2011-03-29 13:46:06 +08001960 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001961 struct btrfs_free_space *info;
1962 struct rb_node *n;
1963 int count = 0;
1964
Li Zefan34d52cb2011-03-29 13:46:06 +08001965 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001966 info = rb_entry(n, struct btrfs_free_space, offset_index);
1967 if (info->bytes >= bytes)
1968 count++;
Josef Bacik96303082009-07-13 21:29:25 -04001969 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
Joel Becker21380932009-04-21 12:38:29 -07001970 (unsigned long long)info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001971 (unsigned long long)info->bytes,
1972 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001973 }
Josef Bacik96303082009-07-13 21:29:25 -04001974 printk(KERN_INFO "block group has cluster?: %s\n",
1975 list_empty(&block_group->cluster_list) ? "no" : "yes");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001976 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1977 "\n", count);
1978}
1979
Li Zefan34d52cb2011-03-29 13:46:06 +08001980void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001981{
Li Zefan34d52cb2011-03-29 13:46:06 +08001982 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001983
Li Zefan34d52cb2011-03-29 13:46:06 +08001984 spin_lock_init(&ctl->tree_lock);
1985 ctl->unit = block_group->sectorsize;
1986 ctl->start = block_group->key.objectid;
1987 ctl->private = block_group;
1988 ctl->op = &free_space_op;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001989
Li Zefan34d52cb2011-03-29 13:46:06 +08001990 /*
1991 * we only want to have 32k of ram per block group for keeping
1992 * track of free space, and if we pass 1/2 of that we want to
1993 * start converting things over to using bitmaps
1994 */
1995 ctl->extents_thresh = ((1024 * 32) / 2) /
1996 sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001997}
1998
Chris Masonfa9c0d792009-04-03 09:47:43 -04001999/*
2000 * for a given cluster, put all of its extents back into the free
2001 * space cache. If the block group passed doesn't match the block group
2002 * pointed to by the cluster, someone else raced in and freed the
2003 * cluster already. In that case, we just return without changing anything
2004 */
2005static int
2006__btrfs_return_cluster_to_free_space(
2007 struct btrfs_block_group_cache *block_group,
2008 struct btrfs_free_cluster *cluster)
2009{
Li Zefan34d52cb2011-03-29 13:46:06 +08002010 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002011 struct btrfs_free_space *entry;
2012 struct rb_node *node;
2013
2014 spin_lock(&cluster->lock);
2015 if (cluster->block_group != block_group)
2016 goto out;
2017
Josef Bacik96303082009-07-13 21:29:25 -04002018 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002019 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002020 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002021
Chris Masonfa9c0d792009-04-03 09:47:43 -04002022 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002023 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002024 bool bitmap;
2025
Chris Masonfa9c0d792009-04-03 09:47:43 -04002026 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2027 node = rb_next(&entry->offset_index);
2028 rb_erase(&entry->offset_index, &cluster->root);
Josef Bacik4e69b592011-03-21 10:11:24 -04002029
2030 bitmap = (entry->bitmap != NULL);
2031 if (!bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08002032 try_merge_free_space(ctl, entry, false);
2033 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002034 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002035 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002036 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002037
Chris Masonfa9c0d792009-04-03 09:47:43 -04002038out:
2039 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002040 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002041 return 0;
2042}
2043
Chris Mason09655372011-05-21 09:27:38 -04002044void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002045{
2046 struct btrfs_free_space *info;
2047 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002048
Li Zefan581bb052011-04-20 10:06:11 +08002049 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2050 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002051 if (!info->bitmap) {
2052 unlink_free_space(ctl, info);
2053 kmem_cache_free(btrfs_free_space_cachep, info);
2054 } else {
2055 free_bitmap(ctl, info);
2056 }
Li Zefan581bb052011-04-20 10:06:11 +08002057 if (need_resched()) {
2058 spin_unlock(&ctl->tree_lock);
2059 cond_resched();
2060 spin_lock(&ctl->tree_lock);
2061 }
2062 }
Chris Mason09655372011-05-21 09:27:38 -04002063}
2064
2065void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2066{
2067 spin_lock(&ctl->tree_lock);
2068 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002069 spin_unlock(&ctl->tree_lock);
2070}
2071
Josef Bacik0f9dd462008-09-23 13:14:11 -04002072void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2073{
Li Zefan34d52cb2011-03-29 13:46:06 +08002074 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002075 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002076 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002077
Li Zefan34d52cb2011-03-29 13:46:06 +08002078 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002079 while ((head = block_group->cluster_list.next) !=
2080 &block_group->cluster_list) {
2081 cluster = list_entry(head, struct btrfs_free_cluster,
2082 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002083
2084 WARN_ON(cluster->block_group != block_group);
2085 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -04002086 if (need_resched()) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002087 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002088 cond_resched();
Li Zefan34d52cb2011-03-29 13:46:06 +08002089 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002090 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002091 }
Chris Mason09655372011-05-21 09:27:38 -04002092 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002093 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002094
Josef Bacik0f9dd462008-09-23 13:14:11 -04002095}
2096
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002097u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
2098 u64 offset, u64 bytes, u64 empty_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002099{
Li Zefan34d52cb2011-03-29 13:46:06 +08002100 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002101 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002102 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002103 u64 ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002104
Li Zefan34d52cb2011-03-29 13:46:06 +08002105 spin_lock(&ctl->tree_lock);
2106 entry = find_free_space(ctl, &offset, &bytes_search);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002107 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002108 goto out;
2109
2110 ret = offset;
2111 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002112 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002113 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002114 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002115 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002116 unlink_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002117 entry->offset += bytes;
2118 entry->bytes -= bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002119 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002120 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002121 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002122 link_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002123 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002124
Josef Bacik96303082009-07-13 21:29:25 -04002125out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002126 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002127
Josef Bacik0f9dd462008-09-23 13:14:11 -04002128 return ret;
2129}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002130
2131/*
2132 * given a cluster, put all of its extents back into the free space
2133 * cache. If a block group is passed, this function will only free
2134 * a cluster that belongs to the passed block group.
2135 *
2136 * Otherwise, it'll get a reference on the block group pointed to by the
2137 * cluster and remove the cluster from it.
2138 */
2139int btrfs_return_cluster_to_free_space(
2140 struct btrfs_block_group_cache *block_group,
2141 struct btrfs_free_cluster *cluster)
2142{
Li Zefan34d52cb2011-03-29 13:46:06 +08002143 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002144 int ret;
2145
2146 /* first, get a safe pointer to the block group */
2147 spin_lock(&cluster->lock);
2148 if (!block_group) {
2149 block_group = cluster->block_group;
2150 if (!block_group) {
2151 spin_unlock(&cluster->lock);
2152 return 0;
2153 }
2154 } else if (cluster->block_group != block_group) {
2155 /* someone else has already freed it don't redo their work */
2156 spin_unlock(&cluster->lock);
2157 return 0;
2158 }
2159 atomic_inc(&block_group->count);
2160 spin_unlock(&cluster->lock);
2161
Li Zefan34d52cb2011-03-29 13:46:06 +08002162 ctl = block_group->free_space_ctl;
2163
Chris Masonfa9c0d792009-04-03 09:47:43 -04002164 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002165 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002166 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002167 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002168
2169 /* finally drop our ref */
2170 btrfs_put_block_group(block_group);
2171 return ret;
2172}
2173
Josef Bacik96303082009-07-13 21:29:25 -04002174static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2175 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002176 struct btrfs_free_space *entry,
Josef Bacik96303082009-07-13 21:29:25 -04002177 u64 bytes, u64 min_start)
2178{
Li Zefan34d52cb2011-03-29 13:46:06 +08002179 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002180 int err;
2181 u64 search_start = cluster->window_start;
2182 u64 search_bytes = bytes;
2183 u64 ret = 0;
2184
Josef Bacik96303082009-07-13 21:29:25 -04002185 search_start = min_start;
2186 search_bytes = bytes;
2187
Li Zefan34d52cb2011-03-29 13:46:06 +08002188 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002189 if (err)
Josef Bacik4e69b592011-03-21 10:11:24 -04002190 return 0;
Josef Bacik96303082009-07-13 21:29:25 -04002191
2192 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002193 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002194
2195 return ret;
2196}
2197
Chris Masonfa9c0d792009-04-03 09:47:43 -04002198/*
2199 * given a cluster, try to allocate 'bytes' from it, returns 0
2200 * if it couldn't find anything suitably large, or a logical disk offset
2201 * if things worked out
2202 */
2203u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2204 struct btrfs_free_cluster *cluster, u64 bytes,
2205 u64 min_start)
2206{
Li Zefan34d52cb2011-03-29 13:46:06 +08002207 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002208 struct btrfs_free_space *entry = NULL;
2209 struct rb_node *node;
2210 u64 ret = 0;
2211
2212 spin_lock(&cluster->lock);
2213 if (bytes > cluster->max_size)
2214 goto out;
2215
2216 if (cluster->block_group != block_group)
2217 goto out;
2218
2219 node = rb_first(&cluster->root);
2220 if (!node)
2221 goto out;
2222
2223 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002224 while(1) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002225 if (entry->bytes < bytes ||
2226 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002227 node = rb_next(&entry->offset_index);
2228 if (!node)
2229 break;
2230 entry = rb_entry(node, struct btrfs_free_space,
2231 offset_index);
2232 continue;
2233 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002234
Josef Bacik4e69b592011-03-21 10:11:24 -04002235 if (entry->bitmap) {
2236 ret = btrfs_alloc_from_bitmap(block_group,
2237 cluster, entry, bytes,
2238 min_start);
2239 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002240 node = rb_next(&entry->offset_index);
2241 if (!node)
2242 break;
2243 entry = rb_entry(node, struct btrfs_free_space,
2244 offset_index);
2245 continue;
2246 }
2247 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002248 ret = entry->offset;
2249
2250 entry->offset += bytes;
2251 entry->bytes -= bytes;
2252 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002253
Li Zefan5e71b5d2010-11-09 14:55:34 +08002254 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002255 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002256 break;
2257 }
2258out:
2259 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002260
Li Zefan5e71b5d2010-11-09 14:55:34 +08002261 if (!ret)
2262 return 0;
2263
Li Zefan34d52cb2011-03-29 13:46:06 +08002264 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002265
Li Zefan34d52cb2011-03-29 13:46:06 +08002266 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002267 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002268 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002269 if (entry->bitmap) {
2270 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002271 ctl->total_bitmaps--;
2272 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002273 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002274 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002275 }
2276
Li Zefan34d52cb2011-03-29 13:46:06 +08002277 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002278
Chris Masonfa9c0d792009-04-03 09:47:43 -04002279 return ret;
2280}
2281
Josef Bacik96303082009-07-13 21:29:25 -04002282static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2283 struct btrfs_free_space *entry,
2284 struct btrfs_free_cluster *cluster,
2285 u64 offset, u64 bytes, u64 min_bytes)
2286{
Li Zefan34d52cb2011-03-29 13:46:06 +08002287 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002288 unsigned long next_zero;
2289 unsigned long i;
2290 unsigned long search_bits;
2291 unsigned long total_bits;
2292 unsigned long found_bits;
2293 unsigned long start = 0;
2294 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002295 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002296 bool found = false;
2297
2298 i = offset_to_bit(entry->offset, block_group->sectorsize,
2299 max_t(u64, offset, entry->offset));
Josef Bacikd0a365e2011-03-18 15:27:43 -04002300 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2301 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04002302
2303again:
2304 found_bits = 0;
2305 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2306 i < BITS_PER_BITMAP;
2307 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2308 next_zero = find_next_zero_bit(entry->bitmap,
2309 BITS_PER_BITMAP, i);
2310 if (next_zero - i >= search_bits) {
2311 found_bits = next_zero - i;
2312 break;
2313 }
2314 i = next_zero;
2315 }
2316
2317 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002318 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002319
2320 if (!found) {
2321 start = i;
2322 found = true;
2323 }
2324
2325 total_found += found_bits;
2326
2327 if (cluster->max_size < found_bits * block_group->sectorsize)
2328 cluster->max_size = found_bits * block_group->sectorsize;
2329
2330 if (total_found < total_bits) {
2331 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2332 if (i - start > total_bits * 2) {
2333 total_found = 0;
2334 cluster->max_size = 0;
2335 found = false;
2336 }
2337 goto again;
2338 }
2339
2340 cluster->window_start = start * block_group->sectorsize +
2341 entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002342 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002343 ret = tree_insert_offset(&cluster->root, entry->offset,
2344 &entry->offset_index, 1);
2345 BUG_ON(ret);
Josef Bacik96303082009-07-13 21:29:25 -04002346
2347 return 0;
2348}
2349
Chris Masonfa9c0d792009-04-03 09:47:43 -04002350/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002351 * This searches the block group for just extents to fill the cluster with.
2352 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002353static noinline int
2354setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2355 struct btrfs_free_cluster *cluster,
2356 struct list_head *bitmaps, u64 offset, u64 bytes,
2357 u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002358{
Li Zefan34d52cb2011-03-29 13:46:06 +08002359 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002360 struct btrfs_free_space *first = NULL;
2361 struct btrfs_free_space *entry = NULL;
2362 struct btrfs_free_space *prev = NULL;
2363 struct btrfs_free_space *last;
2364 struct rb_node *node;
2365 u64 window_start;
2366 u64 window_free;
2367 u64 max_extent;
2368 u64 max_gap = 128 * 1024;
2369
Li Zefan34d52cb2011-03-29 13:46:06 +08002370 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002371 if (!entry)
2372 return -ENOSPC;
2373
2374 /*
2375 * We don't want bitmaps, so just move along until we find a normal
2376 * extent entry.
2377 */
2378 while (entry->bitmap) {
Josef Bacik86d4a772011-05-25 13:03:16 -04002379 if (list_empty(&entry->list))
2380 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002381 node = rb_next(&entry->offset_index);
2382 if (!node)
2383 return -ENOSPC;
2384 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2385 }
2386
2387 window_start = entry->offset;
2388 window_free = entry->bytes;
2389 max_extent = entry->bytes;
2390 first = entry;
2391 last = entry;
2392 prev = entry;
2393
2394 while (window_free <= min_bytes) {
2395 node = rb_next(&entry->offset_index);
2396 if (!node)
2397 return -ENOSPC;
2398 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2399
Josef Bacik86d4a772011-05-25 13:03:16 -04002400 if (entry->bitmap) {
2401 if (list_empty(&entry->list))
2402 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002403 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002404 }
2405
Josef Bacik4e69b592011-03-21 10:11:24 -04002406 /*
2407 * we haven't filled the empty size and the window is
2408 * very large. reset and try again
2409 */
2410 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2411 entry->offset - window_start > (min_bytes * 2)) {
2412 first = entry;
2413 window_start = entry->offset;
2414 window_free = entry->bytes;
2415 last = entry;
2416 max_extent = entry->bytes;
2417 } else {
2418 last = entry;
2419 window_free += entry->bytes;
2420 if (entry->bytes > max_extent)
2421 max_extent = entry->bytes;
2422 }
2423 prev = entry;
2424 }
2425
2426 cluster->window_start = first->offset;
2427
2428 node = &first->offset_index;
2429
2430 /*
2431 * now we've found our entries, pull them out of the free space
2432 * cache and put them into the cluster rbtree
2433 */
2434 do {
2435 int ret;
2436
2437 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2438 node = rb_next(&entry->offset_index);
2439 if (entry->bitmap)
2440 continue;
2441
Li Zefan34d52cb2011-03-29 13:46:06 +08002442 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002443 ret = tree_insert_offset(&cluster->root, entry->offset,
2444 &entry->offset_index, 0);
2445 BUG_ON(ret);
2446 } while (node && entry != last);
2447
2448 cluster->max_size = max_extent;
2449
2450 return 0;
2451}
2452
2453/*
2454 * This specifically looks for bitmaps that may work in the cluster, we assume
2455 * that we have already failed to find extents that will work.
2456 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002457static noinline int
2458setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2459 struct btrfs_free_cluster *cluster,
2460 struct list_head *bitmaps, u64 offset, u64 bytes,
2461 u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002462{
Li Zefan34d52cb2011-03-29 13:46:06 +08002463 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002464 struct btrfs_free_space *entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002465 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05002466 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002467
Li Zefan34d52cb2011-03-29 13:46:06 +08002468 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002469 return -ENOSPC;
2470
Josef Bacik86d4a772011-05-25 13:03:16 -04002471 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05002472 * The bitmap that covers offset won't be in the list unless offset
2473 * is just its start offset.
2474 */
2475 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
2476 if (entry->offset != bitmap_offset) {
2477 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
2478 if (entry && list_empty(&entry->list))
2479 list_add(&entry->list, bitmaps);
2480 }
2481
Josef Bacik86d4a772011-05-25 13:03:16 -04002482 list_for_each_entry(entry, bitmaps, list) {
2483 if (entry->bytes < min_bytes)
2484 continue;
2485 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2486 bytes, min_bytes);
2487 if (!ret)
2488 return 0;
2489 }
2490
2491 /*
Li Zefan52621cb2011-11-20 07:33:38 -05002492 * The bitmaps list has all the bitmaps that record free space
2493 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04002494 */
Li Zefan52621cb2011-11-20 07:33:38 -05002495 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04002496}
2497
2498/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002499 * here we try to find a cluster of blocks in a block group. The goal
2500 * is to find at least bytes free and up to empty_size + bytes free.
2501 * We might not find them all in one contiguous area.
2502 *
2503 * returns zero and sets up cluster if things worked out, otherwise
2504 * it returns -enospc
2505 */
2506int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
Chris Mason451d7582009-06-09 20:28:34 -04002507 struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002508 struct btrfs_block_group_cache *block_group,
2509 struct btrfs_free_cluster *cluster,
2510 u64 offset, u64 bytes, u64 empty_size)
2511{
Li Zefan34d52cb2011-03-29 13:46:06 +08002512 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04002513 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05002514 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002515 u64 min_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002516 int ret;
2517
2518 /* for metadata, allow allocates with more holes */
Chris Mason451d7582009-06-09 20:28:34 -04002519 if (btrfs_test_opt(root, SSD_SPREAD)) {
2520 min_bytes = bytes + empty_size;
2521 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002522 /*
2523 * we want to do larger allocations when we are
2524 * flushing out the delayed refs, it helps prevent
2525 * making more work as we go along.
2526 */
2527 if (trans->transaction->delayed_refs.flushing)
2528 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2529 else
2530 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2531 } else
2532 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2533
Li Zefan34d52cb2011-03-29 13:46:06 +08002534 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002535
2536 /*
2537 * If we know we don't have enough space to make a cluster don't even
2538 * bother doing all the work to try and find one.
2539 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002540 if (ctl->free_space < min_bytes) {
2541 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002542 return -ENOSPC;
2543 }
2544
Chris Masonfa9c0d792009-04-03 09:47:43 -04002545 spin_lock(&cluster->lock);
2546
2547 /* someone already found a cluster, hooray */
2548 if (cluster->block_group) {
2549 ret = 0;
2550 goto out;
2551 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002552
Josef Bacik86d4a772011-05-25 13:03:16 -04002553 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
2554 bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04002555 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04002556 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
2557 offset, bytes, min_bytes);
2558
2559 /* Clear our temporary list */
2560 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2561 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04002562
2563 if (!ret) {
2564 atomic_inc(&block_group->count);
2565 list_add_tail(&cluster->block_group_list,
2566 &block_group->cluster_list);
2567 cluster->block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002568 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002569out:
2570 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002571 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002572
2573 return ret;
2574}
2575
2576/*
2577 * simple code to zero out a cluster
2578 */
2579void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2580{
2581 spin_lock_init(&cluster->lock);
2582 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002583 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002584 cluster->max_size = 0;
2585 INIT_LIST_HEAD(&cluster->block_group_list);
2586 cluster->block_group = NULL;
2587}
2588
Li Dongyangf7039b12011-03-24 10:24:28 +00002589int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2590 u64 *trimmed, u64 start, u64 end, u64 minlen)
2591{
Li Zefan34d52cb2011-03-29 13:46:06 +08002592 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Dongyangf7039b12011-03-24 10:24:28 +00002593 struct btrfs_free_space *entry = NULL;
2594 struct btrfs_fs_info *fs_info = block_group->fs_info;
2595 u64 bytes = 0;
2596 u64 actually_trimmed;
2597 int ret = 0;
2598
2599 *trimmed = 0;
2600
2601 while (start < end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002602 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002603
Li Zefan34d52cb2011-03-29 13:46:06 +08002604 if (ctl->free_space < minlen) {
2605 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002606 break;
2607 }
2608
Li Zefan34d52cb2011-03-29 13:46:06 +08002609 entry = tree_search_offset(ctl, start, 0, 1);
Li Dongyangf7039b12011-03-24 10:24:28 +00002610 if (!entry)
Li Zefan34d52cb2011-03-29 13:46:06 +08002611 entry = tree_search_offset(ctl,
2612 offset_to_bitmap(ctl, start),
Li Dongyangf7039b12011-03-24 10:24:28 +00002613 1, 1);
2614
2615 if (!entry || entry->offset >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002616 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002617 break;
2618 }
2619
2620 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002621 ret = search_bitmap(ctl, entry, &start, &bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002622 if (!ret) {
2623 if (start >= 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 bytes = min(bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002628 bitmap_clear_bits(ctl, entry, start, bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002629 if (entry->bytes == 0)
Li Zefan34d52cb2011-03-29 13:46:06 +08002630 free_bitmap(ctl, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002631 } else {
2632 start = entry->offset + BITS_PER_BITMAP *
2633 block_group->sectorsize;
Li Zefan34d52cb2011-03-29 13:46:06 +08002634 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002635 ret = 0;
2636 continue;
2637 }
2638 } else {
2639 start = entry->offset;
2640 bytes = min(entry->bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002641 unlink_free_space(ctl, entry);
Li Zefanf789b682011-04-25 19:43:52 -04002642 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002643 }
2644
Li Zefan34d52cb2011-03-29 13:46:06 +08002645 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002646
2647 if (bytes >= minlen) {
Josef Bacikfb25e912011-07-26 17:00:46 -04002648 struct btrfs_space_info *space_info;
2649 int update = 0;
2650
2651 space_info = block_group->space_info;
2652 spin_lock(&space_info->lock);
2653 spin_lock(&block_group->lock);
2654 if (!block_group->ro) {
2655 block_group->reserved += bytes;
2656 space_info->bytes_reserved += bytes;
2657 update = 1;
2658 }
2659 spin_unlock(&block_group->lock);
2660 spin_unlock(&space_info->lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002661
2662 ret = btrfs_error_discard_extent(fs_info->extent_root,
2663 start,
2664 bytes,
2665 &actually_trimmed);
2666
Li Zefan34d52cb2011-03-29 13:46:06 +08002667 btrfs_add_free_space(block_group, start, bytes);
Josef Bacikfb25e912011-07-26 17:00:46 -04002668 if (update) {
2669 spin_lock(&space_info->lock);
2670 spin_lock(&block_group->lock);
2671 if (block_group->ro)
2672 space_info->bytes_readonly += bytes;
2673 block_group->reserved -= bytes;
2674 space_info->bytes_reserved -= bytes;
2675 spin_unlock(&space_info->lock);
2676 spin_unlock(&block_group->lock);
2677 }
Li Dongyangf7039b12011-03-24 10:24:28 +00002678
2679 if (ret)
2680 break;
2681 *trimmed += actually_trimmed;
2682 }
2683 start += bytes;
2684 bytes = 0;
2685
2686 if (fatal_signal_pending(current)) {
2687 ret = -ERESTARTSYS;
2688 break;
2689 }
2690
2691 cond_resched();
2692 }
2693
2694 return ret;
2695}
Li Zefan581bb052011-04-20 10:06:11 +08002696
2697/*
2698 * Find the left-most item in the cache tree, and then return the
2699 * smallest inode number in the item.
2700 *
2701 * Note: the returned inode number may not be the smallest one in
2702 * the tree, if the left-most item is a bitmap.
2703 */
2704u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2705{
2706 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2707 struct btrfs_free_space *entry = NULL;
2708 u64 ino = 0;
2709
2710 spin_lock(&ctl->tree_lock);
2711
2712 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2713 goto out;
2714
2715 entry = rb_entry(rb_first(&ctl->free_space_offset),
2716 struct btrfs_free_space, offset_index);
2717
2718 if (!entry->bitmap) {
2719 ino = entry->offset;
2720
2721 unlink_free_space(ctl, entry);
2722 entry->offset++;
2723 entry->bytes--;
2724 if (!entry->bytes)
2725 kmem_cache_free(btrfs_free_space_cachep, entry);
2726 else
2727 link_free_space(ctl, entry);
2728 } else {
2729 u64 offset = 0;
2730 u64 count = 1;
2731 int ret;
2732
2733 ret = search_bitmap(ctl, entry, &offset, &count);
2734 BUG_ON(ret);
2735
2736 ino = offset;
2737 bitmap_clear_bits(ctl, entry, offset, 1);
2738 if (entry->bytes == 0)
2739 free_bitmap(ctl, entry);
2740 }
2741out:
2742 spin_unlock(&ctl->tree_lock);
2743
2744 return ino;
2745}
Li Zefan82d59022011-04-20 10:33:24 +08002746
2747struct inode *lookup_free_ino_inode(struct btrfs_root *root,
2748 struct btrfs_path *path)
2749{
2750 struct inode *inode = NULL;
2751
2752 spin_lock(&root->cache_lock);
2753 if (root->cache_inode)
2754 inode = igrab(root->cache_inode);
2755 spin_unlock(&root->cache_lock);
2756 if (inode)
2757 return inode;
2758
2759 inode = __lookup_free_space_inode(root, path, 0);
2760 if (IS_ERR(inode))
2761 return inode;
2762
2763 spin_lock(&root->cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02002764 if (!btrfs_fs_closing(root->fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08002765 root->cache_inode = igrab(inode);
2766 spin_unlock(&root->cache_lock);
2767
2768 return inode;
2769}
2770
2771int create_free_ino_inode(struct btrfs_root *root,
2772 struct btrfs_trans_handle *trans,
2773 struct btrfs_path *path)
2774{
2775 return __create_free_space_inode(root, trans, path,
2776 BTRFS_FREE_INO_OBJECTID, 0);
2777}
2778
2779int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2780{
2781 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2782 struct btrfs_path *path;
2783 struct inode *inode;
2784 int ret = 0;
2785 u64 root_gen = btrfs_root_generation(&root->root_item);
2786
Chris Mason4b9465c2011-06-03 09:36:29 -04002787 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2788 return 0;
2789
Li Zefan82d59022011-04-20 10:33:24 +08002790 /*
2791 * If we're unmounting then just return, since this does a search on the
2792 * normal root and not the commit root and we could deadlock.
2793 */
David Sterba7841cb22011-05-31 18:07:27 +02002794 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08002795 return 0;
2796
2797 path = btrfs_alloc_path();
2798 if (!path)
2799 return 0;
2800
2801 inode = lookup_free_ino_inode(root, path);
2802 if (IS_ERR(inode))
2803 goto out;
2804
2805 if (root_gen != BTRFS_I(inode)->generation)
2806 goto out_put;
2807
2808 ret = __load_free_space_cache(root, inode, ctl, path, 0);
2809
2810 if (ret < 0)
2811 printk(KERN_ERR "btrfs: failed to load free ino cache for "
2812 "root %llu\n", root->root_key.objectid);
2813out_put:
2814 iput(inode);
2815out:
2816 btrfs_free_path(path);
2817 return ret;
2818}
2819
2820int btrfs_write_out_ino_cache(struct btrfs_root *root,
2821 struct btrfs_trans_handle *trans,
2822 struct btrfs_path *path)
2823{
2824 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2825 struct inode *inode;
2826 int ret;
2827
Chris Mason4b9465c2011-06-03 09:36:29 -04002828 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2829 return 0;
2830
Li Zefan82d59022011-04-20 10:33:24 +08002831 inode = lookup_free_ino_inode(root, path);
2832 if (IS_ERR(inode))
2833 return 0;
2834
2835 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
Josef Bacikc09544e2011-08-30 10:19:10 -04002836 if (ret) {
2837 btrfs_delalloc_release_metadata(inode, inode->i_size);
2838#ifdef DEBUG
Li Zefan82d59022011-04-20 10:33:24 +08002839 printk(KERN_ERR "btrfs: failed to write free ino cache "
2840 "for root %llu\n", root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04002841#endif
2842 }
Li Zefan82d59022011-04-20 10:33:24 +08002843
2844 iput(inode);
2845 return ret;
2846}