blob: 47c2adb3ddf339428c5e90281dfabd0b8f64067c [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"
Filipe Manana04216822014-11-27 21:14:15 +000030#include "volumes.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040031
Josef Bacik96303082009-07-13 21:29:25 -040032#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
33#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
34
Filipe Manana55507ce2014-12-01 17:04:09 +000035struct btrfs_trim_range {
36 u64 start;
37 u64 bytes;
38 struct list_head list;
39};
40
Li Zefan34d52cb2011-03-29 13:46:06 +080041static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040042 struct btrfs_free_space *info);
Josef Bacikcd023e72012-05-14 10:06:40 -040043static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
44 struct btrfs_free_space *info);
Josef Bacik0cb59c92010-07-02 12:14:14 -040045
Li Zefan0414efa2011-04-20 10:20:14 +080046static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
47 struct btrfs_path *path,
48 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040049{
50 struct btrfs_key key;
51 struct btrfs_key location;
52 struct btrfs_disk_key disk_key;
53 struct btrfs_free_space_header *header;
54 struct extent_buffer *leaf;
55 struct inode *inode = NULL;
56 int ret;
57
Josef Bacik0af3d002010-06-21 14:48:16 -040058 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080059 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040060 key.type = 0;
61
62 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
63 if (ret < 0)
64 return ERR_PTR(ret);
65 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020066 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040067 return ERR_PTR(-ENOENT);
68 }
69
70 leaf = path->nodes[0];
71 header = btrfs_item_ptr(leaf, path->slots[0],
72 struct btrfs_free_space_header);
73 btrfs_free_space_key(leaf, header, &disk_key);
74 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020075 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040076
77 inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
78 if (!inode)
79 return ERR_PTR(-ENOENT);
80 if (IS_ERR(inode))
81 return inode;
82 if (is_bad_inode(inode)) {
83 iput(inode);
84 return ERR_PTR(-ENOENT);
85 }
86
Al Viro528c0322012-04-13 11:03:55 -040087 mapping_set_gfp_mask(inode->i_mapping,
88 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
Miao Xieadae52b2011-03-31 09:43:23 +000089
Li Zefan0414efa2011-04-20 10:20:14 +080090 return inode;
91}
92
93struct inode *lookup_free_space_inode(struct btrfs_root *root,
94 struct btrfs_block_group_cache
95 *block_group, struct btrfs_path *path)
96{
97 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -040098 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +080099
100 spin_lock(&block_group->lock);
101 if (block_group->inode)
102 inode = igrab(block_group->inode);
103 spin_unlock(&block_group->lock);
104 if (inode)
105 return inode;
106
107 inode = __lookup_free_space_inode(root, path,
108 block_group->key.objectid);
109 if (IS_ERR(inode))
110 return inode;
111
Josef Bacik0af3d002010-06-21 14:48:16 -0400112 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400113 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000114 btrfs_info(root->fs_info,
115 "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400116 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
117 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400118 block_group->disk_cache_state = BTRFS_DC_CLEAR;
119 }
120
Josef Bacik300e4f82011-08-29 14:06:00 -0400121 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400122 block_group->inode = igrab(inode);
123 block_group->iref = 1;
124 }
125 spin_unlock(&block_group->lock);
126
127 return inode;
128}
129
Eric Sandeen48a3b632013-04-25 20:41:01 +0000130static int __create_free_space_inode(struct btrfs_root *root,
131 struct btrfs_trans_handle *trans,
132 struct btrfs_path *path,
133 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400134{
135 struct btrfs_key key;
136 struct btrfs_disk_key disk_key;
137 struct btrfs_free_space_header *header;
138 struct btrfs_inode_item *inode_item;
139 struct extent_buffer *leaf;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400140 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
Josef Bacik0af3d002010-06-21 14:48:16 -0400141 int ret;
142
Li Zefan0414efa2011-04-20 10:20:14 +0800143 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400144 if (ret)
145 return ret;
146
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400147 /* We inline crc's for the free disk space cache */
148 if (ino != BTRFS_FREE_INO_OBJECTID)
149 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
150
Josef Bacik0af3d002010-06-21 14:48:16 -0400151 leaf = path->nodes[0];
152 inode_item = btrfs_item_ptr(leaf, path->slots[0],
153 struct btrfs_inode_item);
154 btrfs_item_key(leaf, &disk_key, path->slots[0]);
155 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
156 sizeof(*inode_item));
157 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
158 btrfs_set_inode_size(leaf, inode_item, 0);
159 btrfs_set_inode_nbytes(leaf, inode_item, 0);
160 btrfs_set_inode_uid(leaf, inode_item, 0);
161 btrfs_set_inode_gid(leaf, inode_item, 0);
162 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400163 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400164 btrfs_set_inode_nlink(leaf, inode_item, 1);
165 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800166 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400167 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200168 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400169
170 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800171 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400172 key.type = 0;
173
174 ret = btrfs_insert_empty_item(trans, root, path, &key,
175 sizeof(struct btrfs_free_space_header));
176 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200177 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400178 return ret;
179 }
180 leaf = path->nodes[0];
181 header = btrfs_item_ptr(leaf, path->slots[0],
182 struct btrfs_free_space_header);
183 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
184 btrfs_set_free_space_key(leaf, header, &disk_key);
185 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200186 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400187
188 return 0;
189}
190
Li Zefan0414efa2011-04-20 10:20:14 +0800191int create_free_space_inode(struct btrfs_root *root,
192 struct btrfs_trans_handle *trans,
193 struct btrfs_block_group_cache *block_group,
194 struct btrfs_path *path)
195{
196 int ret;
197 u64 ino;
198
199 ret = btrfs_find_free_objectid(root, &ino);
200 if (ret < 0)
201 return ret;
202
203 return __create_free_space_inode(root, trans, path, ino,
204 block_group->key.objectid);
205}
206
Miao Xie7b61cd92013-05-13 13:55:09 +0000207int btrfs_check_trunc_cache_free_space(struct btrfs_root *root,
208 struct btrfs_block_rsv *rsv)
Josef Bacik0af3d002010-06-21 14:48:16 -0400209{
Josef Bacikc8174312011-11-02 09:29:35 -0400210 u64 needed_bytes;
Miao Xie7b61cd92013-05-13 13:55:09 +0000211 int ret;
Josef Bacikc8174312011-11-02 09:29:35 -0400212
213 /* 1 for slack space, 1 for updating the inode */
214 needed_bytes = btrfs_calc_trunc_metadata_size(root, 1) +
215 btrfs_calc_trans_metadata_size(root, 1);
216
Miao Xie7b61cd92013-05-13 13:55:09 +0000217 spin_lock(&rsv->lock);
218 if (rsv->reserved < needed_bytes)
219 ret = -ENOSPC;
220 else
221 ret = 0;
222 spin_unlock(&rsv->lock);
Wei Yongjun4b286cd2013-05-21 02:39:21 +0000223 return ret;
Miao Xie7b61cd92013-05-13 13:55:09 +0000224}
225
226int btrfs_truncate_free_space_cache(struct btrfs_root *root,
227 struct btrfs_trans_handle *trans,
Miao Xie7b61cd92013-05-13 13:55:09 +0000228 struct inode *inode)
229{
Miao Xie7b61cd92013-05-13 13:55:09 +0000230 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400231
Josef Bacik0af3d002010-06-21 14:48:16 -0400232 btrfs_i_size_write(inode, 0);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700233 truncate_pagecache(inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400234
235 /*
236 * We don't need an orphan item because truncating the free space cache
237 * will never be split across transactions.
Chris Mason28ed1342014-12-17 09:41:04 -0800238 * We don't need to check for -EAGAIN because we're a free space
239 * cache inode
Josef Bacik0af3d002010-06-21 14:48:16 -0400240 */
241 ret = btrfs_truncate_inode_items(trans, root, inode,
242 0, BTRFS_EXTENT_DATA_KEY);
243 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100244 btrfs_abort_transaction(trans, root, ret);
Josef Bacik0af3d002010-06-21 14:48:16 -0400245 return ret;
246 }
247
Li Zefan82d59022011-04-20 10:33:24 +0800248 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100249 if (ret)
250 btrfs_abort_transaction(trans, root, ret);
Josef Bacikc8174312011-11-02 09:29:35 -0400251
Li Zefan82d59022011-04-20 10:33:24 +0800252 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400253}
254
Josef Bacik9d66e232010-08-25 16:54:15 -0400255static int readahead_cache(struct inode *inode)
256{
257 struct file_ra_state *ra;
258 unsigned long last_index;
259
260 ra = kzalloc(sizeof(*ra), GFP_NOFS);
261 if (!ra)
262 return -ENOMEM;
263
264 file_ra_state_init(ra, inode->i_mapping);
265 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
266
267 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
268
269 kfree(ra);
270
271 return 0;
272}
273
Chris Mason4c6d1d82015-04-06 13:17:20 -0700274static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Miao Xie5349d6c2014-06-19 10:42:49 +0800275 struct btrfs_root *root, int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400276{
Miao Xie5349d6c2014-06-19 10:42:49 +0800277 int num_pages;
278 int check_crcs = 0;
279
David Sterbaed6078f2014-06-05 01:59:57 +0200280 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800281
282 if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID)
283 check_crcs = 1;
284
285 /* Make sure we can fit our crcs into the first page */
286 if (write && check_crcs &&
287 (num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE)
288 return -ENOSPC;
289
Chris Mason4c6d1d82015-04-06 13:17:20 -0700290 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800291
David Sterba31e818f2015-02-20 18:00:26 +0100292 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400293 if (!io_ctl->pages)
294 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800295
296 io_ctl->num_pages = num_pages;
Josef Bacika67509c2011-10-05 15:18:58 -0400297 io_ctl->root = root;
Miao Xie5349d6c2014-06-19 10:42:49 +0800298 io_ctl->check_crcs = check_crcs;
299
Josef Bacika67509c2011-10-05 15:18:58 -0400300 return 0;
301}
302
Chris Mason4c6d1d82015-04-06 13:17:20 -0700303static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400304{
305 kfree(io_ctl->pages);
306}
307
Chris Mason4c6d1d82015-04-06 13:17:20 -0700308static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400309{
310 if (io_ctl->cur) {
311 kunmap(io_ctl->page);
312 io_ctl->cur = NULL;
313 io_ctl->orig = NULL;
314 }
315}
316
Chris Mason4c6d1d82015-04-06 13:17:20 -0700317static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
Josef Bacika67509c2011-10-05 15:18:58 -0400318{
Josef Bacikb12d6862013-08-26 17:14:08 -0400319 ASSERT(io_ctl->index < io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400320 io_ctl->page = io_ctl->pages[io_ctl->index++];
321 io_ctl->cur = kmap(io_ctl->page);
322 io_ctl->orig = io_ctl->cur;
323 io_ctl->size = PAGE_CACHE_SIZE;
324 if (clear)
325 memset(io_ctl->cur, 0, PAGE_CACHE_SIZE);
326}
327
Chris Mason4c6d1d82015-04-06 13:17:20 -0700328static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400329{
330 int i;
331
332 io_ctl_unmap_page(io_ctl);
333
334 for (i = 0; i < io_ctl->num_pages; i++) {
Li Zefana1ee5a42012-01-09 14:27:42 +0800335 if (io_ctl->pages[i]) {
336 ClearPageChecked(io_ctl->pages[i]);
337 unlock_page(io_ctl->pages[i]);
338 page_cache_release(io_ctl->pages[i]);
339 }
Josef Bacika67509c2011-10-05 15:18:58 -0400340 }
341}
342
Chris Mason4c6d1d82015-04-06 13:17:20 -0700343static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Josef Bacika67509c2011-10-05 15:18:58 -0400344 int uptodate)
345{
346 struct page *page;
347 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
348 int i;
349
350 for (i = 0; i < io_ctl->num_pages; i++) {
351 page = find_or_create_page(inode->i_mapping, i, mask);
352 if (!page) {
353 io_ctl_drop_pages(io_ctl);
354 return -ENOMEM;
355 }
356 io_ctl->pages[i] = page;
357 if (uptodate && !PageUptodate(page)) {
358 btrfs_readpage(NULL, page);
359 lock_page(page);
360 if (!PageUptodate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500361 btrfs_err(BTRFS_I(inode)->root->fs_info,
362 "error reading free space cache");
Josef Bacika67509c2011-10-05 15:18:58 -0400363 io_ctl_drop_pages(io_ctl);
364 return -EIO;
365 }
366 }
367 }
368
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500369 for (i = 0; i < io_ctl->num_pages; i++) {
370 clear_page_dirty_for_io(io_ctl->pages[i]);
371 set_page_extent_mapped(io_ctl->pages[i]);
372 }
373
Josef Bacika67509c2011-10-05 15:18:58 -0400374 return 0;
375}
376
Chris Mason4c6d1d82015-04-06 13:17:20 -0700377static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400378{
Al Viro528c0322012-04-13 11:03:55 -0400379 __le64 *val;
Josef Bacika67509c2011-10-05 15:18:58 -0400380
381 io_ctl_map_page(io_ctl, 1);
382
383 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400384 * Skip the csum areas. If we don't check crcs then we just have a
385 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400386 */
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400387 if (io_ctl->check_crcs) {
388 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
389 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
390 } else {
391 io_ctl->cur += sizeof(u64);
392 io_ctl->size -= sizeof(u64) * 2;
393 }
Josef Bacika67509c2011-10-05 15:18:58 -0400394
395 val = io_ctl->cur;
396 *val = cpu_to_le64(generation);
397 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400398}
399
Chris Mason4c6d1d82015-04-06 13:17:20 -0700400static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400401{
Al Viro528c0322012-04-13 11:03:55 -0400402 __le64 *gen;
Josef Bacika67509c2011-10-05 15:18:58 -0400403
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400404 /*
405 * Skip the crc area. If we don't check crcs then we just have a 64bit
406 * chunk at the front of the first page.
407 */
408 if (io_ctl->check_crcs) {
409 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
410 io_ctl->size -= sizeof(u64) +
411 (sizeof(u32) * io_ctl->num_pages);
412 } else {
413 io_ctl->cur += sizeof(u64);
414 io_ctl->size -= sizeof(u64) * 2;
415 }
Josef Bacika67509c2011-10-05 15:18:58 -0400416
Josef Bacika67509c2011-10-05 15:18:58 -0400417 gen = io_ctl->cur;
418 if (le64_to_cpu(*gen) != generation) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500419 printk_ratelimited(KERN_ERR "BTRFS: space cache generation "
Josef Bacika67509c2011-10-05 15:18:58 -0400420 "(%Lu) does not match inode (%Lu)\n", *gen,
421 generation);
422 io_ctl_unmap_page(io_ctl);
423 return -EIO;
424 }
425 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400426 return 0;
427}
428
Chris Mason4c6d1d82015-04-06 13:17:20 -0700429static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400430{
431 u32 *tmp;
432 u32 crc = ~(u32)0;
433 unsigned offset = 0;
434
435 if (!io_ctl->check_crcs) {
436 io_ctl_unmap_page(io_ctl);
437 return;
438 }
439
440 if (index == 0)
Justin P. Mattockcb54f252011-11-21 08:43:28 -0800441 offset = sizeof(u32) * io_ctl->num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400442
Liu Bob0496682013-03-14 14:57:45 +0000443 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400444 PAGE_CACHE_SIZE - offset);
445 btrfs_csum_final(crc, (char *)&crc);
446 io_ctl_unmap_page(io_ctl);
447 tmp = kmap(io_ctl->pages[0]);
448 tmp += index;
449 *tmp = crc;
450 kunmap(io_ctl->pages[0]);
451}
452
Chris Mason4c6d1d82015-04-06 13:17:20 -0700453static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400454{
455 u32 *tmp, val;
456 u32 crc = ~(u32)0;
457 unsigned offset = 0;
458
459 if (!io_ctl->check_crcs) {
460 io_ctl_map_page(io_ctl, 0);
461 return 0;
462 }
463
464 if (index == 0)
465 offset = sizeof(u32) * io_ctl->num_pages;
466
467 tmp = kmap(io_ctl->pages[0]);
468 tmp += index;
469 val = *tmp;
470 kunmap(io_ctl->pages[0]);
471
472 io_ctl_map_page(io_ctl, 0);
Liu Bob0496682013-03-14 14:57:45 +0000473 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400474 PAGE_CACHE_SIZE - offset);
475 btrfs_csum_final(crc, (char *)&crc);
476 if (val != crc) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500477 printk_ratelimited(KERN_ERR "BTRFS: csum mismatch on free "
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400478 "space cache\n");
479 io_ctl_unmap_page(io_ctl);
480 return -EIO;
481 }
482
Josef Bacika67509c2011-10-05 15:18:58 -0400483 return 0;
484}
485
Chris Mason4c6d1d82015-04-06 13:17:20 -0700486static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400487 void *bitmap)
488{
489 struct btrfs_free_space_entry *entry;
490
491 if (!io_ctl->cur)
492 return -ENOSPC;
493
494 entry = io_ctl->cur;
495 entry->offset = cpu_to_le64(offset);
496 entry->bytes = cpu_to_le64(bytes);
497 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
498 BTRFS_FREE_SPACE_EXTENT;
499 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
500 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
501
502 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
503 return 0;
504
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400505 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400506
507 /* No more pages to map */
508 if (io_ctl->index >= io_ctl->num_pages)
509 return 0;
510
511 /* map the next page */
512 io_ctl_map_page(io_ctl, 1);
513 return 0;
514}
515
Chris Mason4c6d1d82015-04-06 13:17:20 -0700516static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
Josef Bacika67509c2011-10-05 15:18:58 -0400517{
518 if (!io_ctl->cur)
519 return -ENOSPC;
520
521 /*
522 * If we aren't at the start of the current page, unmap this one and
523 * map the next one if there is any left.
524 */
525 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400526 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400527 if (io_ctl->index >= io_ctl->num_pages)
528 return -ENOSPC;
529 io_ctl_map_page(io_ctl, 0);
530 }
531
532 memcpy(io_ctl->cur, bitmap, PAGE_CACHE_SIZE);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400533 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400534 if (io_ctl->index < io_ctl->num_pages)
535 io_ctl_map_page(io_ctl, 0);
536 return 0;
537}
538
Chris Mason4c6d1d82015-04-06 13:17:20 -0700539static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400540{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400541 /*
542 * If we're not on the boundary we know we've modified the page and we
543 * need to crc the page.
544 */
545 if (io_ctl->cur != io_ctl->orig)
546 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
547 else
548 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400549
550 while (io_ctl->index < io_ctl->num_pages) {
551 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400552 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400553 }
554}
555
Chris Mason4c6d1d82015-04-06 13:17:20 -0700556static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400557 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400558{
559 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500560 int ret;
561
562 if (!io_ctl->cur) {
563 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
564 if (ret)
565 return ret;
566 }
Josef Bacika67509c2011-10-05 15:18:58 -0400567
568 e = io_ctl->cur;
569 entry->offset = le64_to_cpu(e->offset);
570 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400571 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400572 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
573 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
574
575 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400576 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400577
578 io_ctl_unmap_page(io_ctl);
579
Josef Bacik2f120c02011-11-10 20:45:05 -0500580 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400581}
582
Chris Mason4c6d1d82015-04-06 13:17:20 -0700583static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400584 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400585{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400586 int ret;
587
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400588 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
589 if (ret)
590 return ret;
591
Josef Bacika67509c2011-10-05 15:18:58 -0400592 memcpy(entry->bitmap, io_ctl->cur, PAGE_CACHE_SIZE);
593 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400594
595 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400596}
597
Josef Bacikcd023e72012-05-14 10:06:40 -0400598/*
599 * Since we attach pinned extents after the fact we can have contiguous sections
600 * of free space that are split up in entries. This poses a problem with the
601 * tree logging stuff since it could have allocated across what appears to be 2
602 * entries since we would have merged the entries when adding the pinned extents
603 * back to the free space cache. So run through the space cache that we just
604 * loaded and merge contiguous entries. This will make the log replay stuff not
605 * blow up and it will make for nicer allocator behavior.
606 */
607static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
608{
609 struct btrfs_free_space *e, *prev = NULL;
610 struct rb_node *n;
611
612again:
613 spin_lock(&ctl->tree_lock);
614 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
615 e = rb_entry(n, struct btrfs_free_space, offset_index);
616 if (!prev)
617 goto next;
618 if (e->bitmap || prev->bitmap)
619 goto next;
620 if (prev->offset + prev->bytes == e->offset) {
621 unlink_free_space(ctl, prev);
622 unlink_free_space(ctl, e);
623 prev->bytes += e->bytes;
624 kmem_cache_free(btrfs_free_space_cachep, e);
625 link_free_space(ctl, prev);
626 prev = NULL;
627 spin_unlock(&ctl->tree_lock);
628 goto again;
629 }
630next:
631 prev = e;
632 }
633 spin_unlock(&ctl->tree_lock);
634}
635
Eric Sandeen48a3b632013-04-25 20:41:01 +0000636static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
637 struct btrfs_free_space_ctl *ctl,
638 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400639{
Josef Bacik9d66e232010-08-25 16:54:15 -0400640 struct btrfs_free_space_header *header;
641 struct extent_buffer *leaf;
Chris Mason4c6d1d82015-04-06 13:17:20 -0700642 struct btrfs_io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400643 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400644 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800645 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400646 u64 num_entries;
647 u64 num_bitmaps;
648 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400649 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400650 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400651
Josef Bacik9d66e232010-08-25 16:54:15 -0400652 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800653 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400654 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400655
656 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800657 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400658 key.type = 0;
659
660 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800661 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400662 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800663 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400664 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400665 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400666 }
667
Li Zefan0414efa2011-04-20 10:20:14 +0800668 ret = -1;
669
Josef Bacik9d66e232010-08-25 16:54:15 -0400670 leaf = path->nodes[0];
671 header = btrfs_item_ptr(leaf, path->slots[0],
672 struct btrfs_free_space_header);
673 num_entries = btrfs_free_space_entries(leaf, header);
674 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
675 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400676 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400677
Miao Xiee570fd22014-06-19 10:42:50 +0800678 if (!BTRFS_I(inode)->generation) {
679 btrfs_info(root->fs_info,
680 "The free space cache file (%llu) is invalid. skip it\n",
681 offset);
682 return 0;
683 }
684
Josef Bacik9d66e232010-08-25 16:54:15 -0400685 if (BTRFS_I(inode)->generation != generation) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000686 btrfs_err(root->fs_info,
687 "free space inode generation (%llu) "
688 "did not match free space cache generation (%llu)",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200689 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400690 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400691 }
692
693 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400694 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400695
Miao Xie5349d6c2014-06-19 10:42:49 +0800696 ret = io_ctl_init(&io_ctl, inode, root, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800697 if (ret)
698 return ret;
699
Josef Bacik9d66e232010-08-25 16:54:15 -0400700 ret = readahead_cache(inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800701 if (ret)
Josef Bacik9d66e232010-08-25 16:54:15 -0400702 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400703
Josef Bacika67509c2011-10-05 15:18:58 -0400704 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
705 if (ret)
706 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400707
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400708 ret = io_ctl_check_crc(&io_ctl, 0);
709 if (ret)
710 goto free_cache;
711
Josef Bacika67509c2011-10-05 15:18:58 -0400712 ret = io_ctl_check_generation(&io_ctl, generation);
713 if (ret)
714 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400715
Josef Bacika67509c2011-10-05 15:18:58 -0400716 while (num_entries) {
717 e = kmem_cache_zalloc(btrfs_free_space_cachep,
718 GFP_NOFS);
719 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400720 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400721
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400722 ret = io_ctl_read_entry(&io_ctl, e, &type);
723 if (ret) {
724 kmem_cache_free(btrfs_free_space_cachep, e);
725 goto free_cache;
726 }
727
Josef Bacika67509c2011-10-05 15:18:58 -0400728 if (!e->bytes) {
729 kmem_cache_free(btrfs_free_space_cachep, e);
730 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400731 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400732
Josef Bacika67509c2011-10-05 15:18:58 -0400733 if (type == BTRFS_FREE_SPACE_EXTENT) {
734 spin_lock(&ctl->tree_lock);
735 ret = link_free_space(ctl, e);
736 spin_unlock(&ctl->tree_lock);
737 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000738 btrfs_err(root->fs_info,
739 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500740 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400741 goto free_cache;
742 }
Josef Bacika67509c2011-10-05 15:18:58 -0400743 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400744 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400745 num_bitmaps--;
746 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
747 if (!e->bitmap) {
748 kmem_cache_free(
749 btrfs_free_space_cachep, e);
750 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400751 }
Josef Bacika67509c2011-10-05 15:18:58 -0400752 spin_lock(&ctl->tree_lock);
753 ret = link_free_space(ctl, e);
754 ctl->total_bitmaps++;
755 ctl->op->recalc_thresholds(ctl);
756 spin_unlock(&ctl->tree_lock);
757 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000758 btrfs_err(root->fs_info,
759 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400760 kmem_cache_free(btrfs_free_space_cachep, e);
761 goto free_cache;
762 }
763 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400764 }
765
Josef Bacika67509c2011-10-05 15:18:58 -0400766 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400767 }
768
Josef Bacik2f120c02011-11-10 20:45:05 -0500769 io_ctl_unmap_page(&io_ctl);
770
Josef Bacika67509c2011-10-05 15:18:58 -0400771 /*
772 * We add the bitmaps at the end of the entries in order that
773 * the bitmap entries are added to the cache.
774 */
775 list_for_each_entry_safe(e, n, &bitmaps, list) {
776 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400777 ret = io_ctl_read_bitmap(&io_ctl, e);
778 if (ret)
779 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400780 }
781
782 io_ctl_drop_pages(&io_ctl);
Josef Bacikcd023e72012-05-14 10:06:40 -0400783 merge_space_tree(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400784 ret = 1;
785out:
Josef Bacika67509c2011-10-05 15:18:58 -0400786 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400787 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400788free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400789 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800790 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400791 goto out;
792}
793
Li Zefan0414efa2011-04-20 10:20:14 +0800794int load_free_space_cache(struct btrfs_fs_info *fs_info,
795 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400796{
Li Zefan34d52cb2011-03-29 13:46:06 +0800797 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800798 struct btrfs_root *root = fs_info->tree_root;
799 struct inode *inode;
800 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400801 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800802 bool matched;
803 u64 used = btrfs_block_group_used(&block_group->item);
804
805 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800806 * If this block group has been marked to be cleared for one reason or
807 * another then we can't trust the on disk cache, so just return.
808 */
809 spin_lock(&block_group->lock);
810 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
811 spin_unlock(&block_group->lock);
812 return 0;
813 }
814 spin_unlock(&block_group->lock);
815
816 path = btrfs_alloc_path();
817 if (!path)
818 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400819 path->search_commit_root = 1;
820 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800821
822 inode = lookup_free_space_inode(root, block_group, path);
823 if (IS_ERR(inode)) {
824 btrfs_free_path(path);
825 return 0;
826 }
827
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400828 /* We may have converted the inode and made the cache invalid. */
829 spin_lock(&block_group->lock);
830 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
831 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900832 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400833 goto out;
834 }
835 spin_unlock(&block_group->lock);
836
Li Zefan0414efa2011-04-20 10:20:14 +0800837 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
838 path, block_group->key.objectid);
839 btrfs_free_path(path);
840 if (ret <= 0)
841 goto out;
842
843 spin_lock(&ctl->tree_lock);
844 matched = (ctl->free_space == (block_group->key.offset - used -
845 block_group->bytes_super));
846 spin_unlock(&ctl->tree_lock);
847
848 if (!matched) {
849 __btrfs_remove_free_space_cache(ctl);
Miao Xie32d6b472014-04-24 13:31:55 +0800850 btrfs_warn(fs_info, "block group %llu has wrong amount of free space",
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000851 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800852 ret = -1;
853 }
854out:
855 if (ret < 0) {
856 /* This cache is bogus, make sure it gets cleared */
857 spin_lock(&block_group->lock);
858 block_group->disk_cache_state = BTRFS_DC_CLEAR;
859 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800860 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800861
Miao Xie32d6b472014-04-24 13:31:55 +0800862 btrfs_warn(fs_info, "failed to load free space cache for block group %llu, rebuild it now",
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000863 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800864 }
865
866 iput(inode);
867 return ret;
868}
869
Chris Masond4452bc2014-05-19 20:47:56 -0700870static noinline_for_stack
Chris Mason4c6d1d82015-04-06 13:17:20 -0700871int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -0700872 struct btrfs_free_space_ctl *ctl,
873 struct btrfs_block_group_cache *block_group,
874 int *entries, int *bitmaps,
875 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400876{
Josef Bacikc09544e2011-08-30 10:19:10 -0400877 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -0700878 struct btrfs_free_cluster *cluster = NULL;
879 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +0000880 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400881
Josef Bacik43be2142011-04-01 14:55:00 +0000882 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -0700883 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000884 cluster = list_entry(block_group->cluster_list.next,
885 struct btrfs_free_cluster,
886 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -0700887 }
Josef Bacik43be2142011-04-01 14:55:00 +0000888
Josef Bacikf75b1302011-10-05 10:00:18 -0400889 if (!node && cluster) {
890 node = rb_first(&cluster->root);
891 cluster = NULL;
892 }
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);
Chris Masond4452bc2014-05-19 20:47:56 -0700899 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +0000900
Chris Masond4452bc2014-05-19 20:47:56 -0700901 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400902 e->bitmap);
903 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -0700904 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -0400905
906 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -0700907 list_add_tail(&e->list, bitmap_list);
908 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400909 }
910 node = rb_next(node);
911 if (!node && cluster) {
912 node = rb_first(&cluster->root);
913 cluster = NULL;
914 }
915 }
Filipe Manana55507ce2014-12-01 17:04:09 +0000916
917 /*
918 * Make sure we don't miss any range that was removed from our rbtree
919 * because trimming is running. Otherwise after a umount+mount (or crash
920 * after committing the transaction) we would leak free space and get
921 * an inconsistent free space cache report from fsck.
922 */
923 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
924 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
925 trim_entry->bytes, NULL);
926 if (ret)
927 goto fail;
928 *entries += 1;
929 }
930
Chris Masond4452bc2014-05-19 20:47:56 -0700931 return 0;
932fail:
933 return -ENOSPC;
934}
935
936static noinline_for_stack int
937update_cache_item(struct btrfs_trans_handle *trans,
938 struct btrfs_root *root,
939 struct inode *inode,
940 struct btrfs_path *path, u64 offset,
941 int entries, int bitmaps)
942{
943 struct btrfs_key key;
944 struct btrfs_free_space_header *header;
945 struct extent_buffer *leaf;
946 int ret;
947
948 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
949 key.offset = offset;
950 key.type = 0;
951
952 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
953 if (ret < 0) {
954 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
955 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
956 GFP_NOFS);
957 goto fail;
958 }
959 leaf = path->nodes[0];
960 if (ret > 0) {
961 struct btrfs_key found_key;
962 ASSERT(path->slots[0]);
963 path->slots[0]--;
964 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
965 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
966 found_key.offset != offset) {
967 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
968 inode->i_size - 1,
969 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
970 NULL, GFP_NOFS);
971 btrfs_release_path(path);
972 goto fail;
973 }
974 }
975
976 BTRFS_I(inode)->generation = trans->transid;
977 header = btrfs_item_ptr(leaf, path->slots[0],
978 struct btrfs_free_space_header);
979 btrfs_set_free_space_entries(leaf, header, entries);
980 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
981 btrfs_set_free_space_generation(leaf, header, trans->transid);
982 btrfs_mark_buffer_dirty(leaf);
983 btrfs_release_path(path);
984
985 return 0;
986
987fail:
988 return -1;
989}
990
991static noinline_for_stack int
Miao Xie5349d6c2014-06-19 10:42:49 +0800992write_pinned_extent_entries(struct btrfs_root *root,
993 struct btrfs_block_group_cache *block_group,
Chris Mason4c6d1d82015-04-06 13:17:20 -0700994 struct btrfs_io_ctl *io_ctl,
Miao Xie5349d6c2014-06-19 10:42:49 +0800995 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -0700996{
997 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -0700998 struct extent_io_tree *unpin = NULL;
999 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001000
Miao Xie5349d6c2014-06-19 10:42:49 +08001001 if (!block_group)
1002 return 0;
1003
Josef Bacika67509c2011-10-05 15:18:58 -04001004 /*
1005 * We want to add any pinned extents to our free space cache
1006 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001007 *
Li Zefandb804f22012-01-10 16:41:01 +08001008 * We shouldn't have switched the pinned extents yet so this is the
1009 * right one
1010 */
1011 unpin = root->fs_info->pinned_extents;
1012
Miao Xie5349d6c2014-06-19 10:42:49 +08001013 start = block_group->key.objectid;
Li Zefandb804f22012-01-10 16:41:01 +08001014
Miao Xie5349d6c2014-06-19 10:42:49 +08001015 while (start < block_group->key.objectid + block_group->key.offset) {
Li Zefandb804f22012-01-10 16:41:01 +08001016 ret = find_first_extent_bit(unpin, start,
1017 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001018 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001019 if (ret)
1020 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001021
Josef Bacika67509c2011-10-05 15:18:58 -04001022 /* This pinned extent is out of our range */
Li Zefandb804f22012-01-10 16:41:01 +08001023 if (extent_start >= block_group->key.objectid +
Josef Bacika67509c2011-10-05 15:18:58 -04001024 block_group->key.offset)
Miao Xie5349d6c2014-06-19 10:42:49 +08001025 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001026
Li Zefandb804f22012-01-10 16:41:01 +08001027 extent_start = max(extent_start, start);
1028 extent_end = min(block_group->key.objectid +
1029 block_group->key.offset, extent_end + 1);
1030 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001031
Chris Masond4452bc2014-05-19 20:47:56 -07001032 *entries += 1;
1033 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001034 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001035 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001036
Li Zefandb804f22012-01-10 16:41:01 +08001037 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001038 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001039
Miao Xie5349d6c2014-06-19 10:42:49 +08001040 return 0;
1041}
1042
1043static noinline_for_stack int
Chris Mason4c6d1d82015-04-06 13:17:20 -07001044write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
Miao Xie5349d6c2014-06-19 10:42:49 +08001045{
1046 struct list_head *pos, *n;
1047 int ret;
1048
Josef Bacik0cb59c92010-07-02 12:14:14 -04001049 /* Write out the bitmaps */
Chris Masond4452bc2014-05-19 20:47:56 -07001050 list_for_each_safe(pos, n, bitmap_list) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001051 struct btrfs_free_space *entry =
1052 list_entry(pos, struct btrfs_free_space, list);
1053
Chris Masond4452bc2014-05-19 20:47:56 -07001054 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001055 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001056 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001057 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001058 }
1059
Miao Xie5349d6c2014-06-19 10:42:49 +08001060 return 0;
1061}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001062
Miao Xie5349d6c2014-06-19 10:42:49 +08001063static int flush_dirty_cache(struct inode *inode)
1064{
1065 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001066
Josef Bacik0ef8b722013-10-25 16:13:35 -04001067 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001068 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001069 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1070 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
1071 GFP_NOFS);
Chris Masond4452bc2014-05-19 20:47:56 -07001072
Miao Xie5349d6c2014-06-19 10:42:49 +08001073 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001074}
1075
1076static void noinline_for_stack
1077cleanup_write_cache_enospc(struct inode *inode,
Chris Mason4c6d1d82015-04-06 13:17:20 -07001078 struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001079 struct extent_state **cached_state,
1080 struct list_head *bitmap_list)
1081{
1082 struct list_head *pos, *n;
Miao Xie5349d6c2014-06-19 10:42:49 +08001083
Chris Masond4452bc2014-05-19 20:47:56 -07001084 list_for_each_safe(pos, n, bitmap_list) {
1085 struct btrfs_free_space *entry =
1086 list_entry(pos, struct btrfs_free_space, list);
1087 list_del_init(&entry->list);
1088 }
1089 io_ctl_drop_pages(io_ctl);
1090 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1091 i_size_read(inode) - 1, cached_state,
1092 GFP_NOFS);
1093}
1094
1095/**
1096 * __btrfs_write_out_cache - write out cached info to an inode
1097 * @root - the root the inode belongs to
1098 * @ctl - the free space cache we are going to write out
1099 * @block_group - the block_group for this cache if it belongs to a block_group
1100 * @trans - the trans handle
1101 * @path - the path to use
1102 * @offset - the offset for the key we'll insert
1103 *
1104 * This function writes out a free space cache struct to disk for quick recovery
1105 * on mount. This will return 0 if it was successfull in writing the cache out,
1106 * and -1 if it was not.
1107 */
1108static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1109 struct btrfs_free_space_ctl *ctl,
1110 struct btrfs_block_group_cache *block_group,
1111 struct btrfs_trans_handle *trans,
1112 struct btrfs_path *path, u64 offset)
1113{
1114 struct extent_state *cached_state = NULL;
Chris Mason4c6d1d82015-04-06 13:17:20 -07001115 struct btrfs_io_ctl io_ctl;
Miao Xie5349d6c2014-06-19 10:42:49 +08001116 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001117 int entries = 0;
1118 int bitmaps = 0;
1119 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001120
1121 if (!i_size_read(inode))
1122 return -1;
1123
Miao Xie5349d6c2014-06-19 10:42:49 +08001124 ret = io_ctl_init(&io_ctl, inode, root, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001125 if (ret)
1126 return -1;
1127
Miao Xiee570fd22014-06-19 10:42:50 +08001128 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1129 down_write(&block_group->data_rwsem);
1130 spin_lock(&block_group->lock);
1131 if (block_group->delalloc_bytes) {
1132 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1133 spin_unlock(&block_group->lock);
1134 up_write(&block_group->data_rwsem);
1135 BTRFS_I(inode)->generation = 0;
1136 ret = 0;
1137 goto out;
1138 }
1139 spin_unlock(&block_group->lock);
1140 }
1141
Chris Masond4452bc2014-05-19 20:47:56 -07001142 /* Lock all pages first so we can lock the extent safely. */
1143 io_ctl_prepare_pages(&io_ctl, inode, 0);
1144
1145 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
1146 0, &cached_state);
1147
Chris Masond4452bc2014-05-19 20:47:56 -07001148 io_ctl_set_generation(&io_ctl, trans->transid);
1149
Filipe Manana55507ce2014-12-01 17:04:09 +00001150 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001151 /* Write out the extent entries in the free space cache */
Chris Masond4452bc2014-05-19 20:47:56 -07001152 ret = write_cache_extent_entries(&io_ctl, ctl,
1153 block_group, &entries, &bitmaps,
1154 &bitmap_list);
Filipe Manana55507ce2014-12-01 17:04:09 +00001155 if (ret) {
1156 mutex_unlock(&ctl->cache_writeout_mutex);
Chris Masond4452bc2014-05-19 20:47:56 -07001157 goto out_nospc;
Filipe Manana55507ce2014-12-01 17:04:09 +00001158 }
Chris Masond4452bc2014-05-19 20:47:56 -07001159
Miao Xie5349d6c2014-06-19 10:42:49 +08001160 /*
1161 * Some spaces that are freed in the current transaction are pinned,
1162 * they will be added into free space cache after the transaction is
1163 * committed, we shouldn't lose them.
1164 */
1165 ret = write_pinned_extent_entries(root, block_group, &io_ctl, &entries);
Filipe Manana55507ce2014-12-01 17:04:09 +00001166 if (ret) {
1167 mutex_unlock(&ctl->cache_writeout_mutex);
Chris Masond4452bc2014-05-19 20:47:56 -07001168 goto out_nospc;
Filipe Manana55507ce2014-12-01 17:04:09 +00001169 }
Miao Xie5349d6c2014-06-19 10:42:49 +08001170
Filipe Manana55507ce2014-12-01 17:04:09 +00001171 /*
1172 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1173 * locked while doing it because a concurrent trim can be manipulating
1174 * or freeing the bitmap.
1175 */
Miao Xie5349d6c2014-06-19 10:42:49 +08001176 ret = write_bitmap_entries(&io_ctl, &bitmap_list);
Filipe Manana55507ce2014-12-01 17:04:09 +00001177 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001178 if (ret)
1179 goto out_nospc;
1180
1181 /* Zero out the rest of the pages just to make sure */
1182 io_ctl_zero_remaining_pages(&io_ctl);
1183
1184 /* Everything is written out, now we dirty the pages in the file. */
1185 ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages,
1186 0, i_size_read(inode), &cached_state);
1187 if (ret)
1188 goto out_nospc;
1189
Miao Xiee570fd22014-06-19 10:42:50 +08001190 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1191 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001192 /*
1193 * Release the pages and unlock the extent, we will flush
1194 * them out later
1195 */
1196 io_ctl_drop_pages(&io_ctl);
1197
1198 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1199 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
1200
1201 /* Flush the dirty pages in the cache file. */
1202 ret = flush_dirty_cache(inode);
1203 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001204 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001205
Miao Xie5349d6c2014-06-19 10:42:49 +08001206 /* Update the cache item to tell everyone this cache file is valid. */
1207 ret = update_cache_item(trans, root, inode, path, offset,
Chris Masond4452bc2014-05-19 20:47:56 -07001208 entries, bitmaps);
Josef Bacik2f356122011-06-10 15:31:13 -04001209out:
Josef Bacika67509c2011-10-05 15:18:58 -04001210 io_ctl_free(&io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001211 if (ret) {
Josef Bacika67509c2011-10-05 15:18:58 -04001212 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001213 BTRFS_I(inode)->generation = 0;
1214 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001215 btrfs_update_inode(trans, root, inode);
Miao Xie5349d6c2014-06-19 10:42:49 +08001216 return ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001217
1218out_nospc:
Chris Masond4452bc2014-05-19 20:47:56 -07001219 cleanup_write_cache_enospc(inode, &io_ctl, &cached_state, &bitmap_list);
Miao Xiee570fd22014-06-19 10:42:50 +08001220
1221 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1222 up_write(&block_group->data_rwsem);
1223
Josef Bacika67509c2011-10-05 15:18:58 -04001224 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001225}
1226
1227int btrfs_write_out_cache(struct btrfs_root *root,
1228 struct btrfs_trans_handle *trans,
1229 struct btrfs_block_group_cache *block_group,
1230 struct btrfs_path *path)
1231{
1232 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1233 struct inode *inode;
1234 int ret = 0;
Josef Bacikce93ec52014-11-17 15:45:48 -05001235 enum btrfs_disk_cache_state dcs = BTRFS_DC_WRITTEN;
Li Zefan0414efa2011-04-20 10:20:14 +08001236
1237 root = root->fs_info->tree_root;
1238
1239 spin_lock(&block_group->lock);
1240 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1241 spin_unlock(&block_group->lock);
1242 return 0;
1243 }
Miao Xiee570fd22014-06-19 10:42:50 +08001244
1245 if (block_group->delalloc_bytes) {
1246 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1247 spin_unlock(&block_group->lock);
1248 return 0;
1249 }
Li Zefan0414efa2011-04-20 10:20:14 +08001250 spin_unlock(&block_group->lock);
1251
1252 inode = lookup_free_space_inode(root, block_group, path);
1253 if (IS_ERR(inode))
1254 return 0;
1255
1256 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
1257 path, block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001258 if (ret) {
Josef Bacikce93ec52014-11-17 15:45:48 -05001259 dcs = BTRFS_DC_ERROR;
Li Zefan82d59022011-04-20 10:33:24 +08001260 ret = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -04001261#ifdef DEBUG
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00001262 btrfs_err(root->fs_info,
1263 "failed to write free space cache for block group %llu",
1264 block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001265#endif
Li Zefan0414efa2011-04-20 10:20:14 +08001266 }
1267
Josef Bacikce93ec52014-11-17 15:45:48 -05001268 spin_lock(&block_group->lock);
1269 block_group->disk_cache_state = dcs;
1270 spin_unlock(&block_group->lock);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001271 iput(inode);
1272 return ret;
1273}
1274
Li Zefan34d52cb2011-03-29 13:46:06 +08001275static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001276 u64 offset)
1277{
Josef Bacikb12d6862013-08-26 17:14:08 -04001278 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001279 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001280 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001281}
1282
Li Zefan34d52cb2011-03-29 13:46:06 +08001283static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001284{
Li Zefan34d52cb2011-03-29 13:46:06 +08001285 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001286}
1287
Li Zefan34d52cb2011-03-29 13:46:06 +08001288static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001289 u64 offset)
1290{
1291 u64 bitmap_start;
David Sterbab8b93ad2015-01-16 17:26:13 +01001292 u32 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001293
Li Zefan34d52cb2011-03-29 13:46:06 +08001294 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1295 bitmap_start = offset - ctl->start;
David Sterbab8b93ad2015-01-16 17:26:13 +01001296 bitmap_start = div_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001297 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001298 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001299
1300 return bitmap_start;
1301}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001302
1303static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001304 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001305{
1306 struct rb_node **p = &root->rb_node;
1307 struct rb_node *parent = NULL;
1308 struct btrfs_free_space *info;
1309
1310 while (*p) {
1311 parent = *p;
1312 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1313
Josef Bacik96303082009-07-13 21:29:25 -04001314 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001315 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001316 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001317 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001318 } else {
1319 /*
1320 * we could have a bitmap entry and an extent entry
1321 * share the same offset. If this is the case, we want
1322 * the extent entry to always be found first if we do a
1323 * linear search through the tree, since we want to have
1324 * the quickest allocation time, and allocating from an
1325 * extent is faster than allocating from a bitmap. So
1326 * if we're inserting a bitmap and we find an entry at
1327 * this offset, we want to go right, or after this entry
1328 * logically. If we are inserting an extent and we've
1329 * found a bitmap, we want to go left, or before
1330 * logically.
1331 */
1332 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001333 if (info->bitmap) {
1334 WARN_ON_ONCE(1);
1335 return -EEXIST;
1336 }
Josef Bacik96303082009-07-13 21:29:25 -04001337 p = &(*p)->rb_right;
1338 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001339 if (!info->bitmap) {
1340 WARN_ON_ONCE(1);
1341 return -EEXIST;
1342 }
Josef Bacik96303082009-07-13 21:29:25 -04001343 p = &(*p)->rb_left;
1344 }
1345 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001346 }
1347
1348 rb_link_node(node, parent, p);
1349 rb_insert_color(node, root);
1350
1351 return 0;
1352}
1353
1354/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001355 * searches the tree for the given offset.
1356 *
Josef Bacik96303082009-07-13 21:29:25 -04001357 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1358 * want a section that has at least bytes size and comes at or after the given
1359 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001360 */
Josef Bacik96303082009-07-13 21:29:25 -04001361static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001362tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001363 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001364{
Li Zefan34d52cb2011-03-29 13:46:06 +08001365 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001366 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001367
Josef Bacik96303082009-07-13 21:29:25 -04001368 /* find entry that is closest to the 'offset' */
1369 while (1) {
1370 if (!n) {
1371 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001372 break;
1373 }
Josef Bacik96303082009-07-13 21:29:25 -04001374
1375 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1376 prev = entry;
1377
1378 if (offset < entry->offset)
1379 n = n->rb_left;
1380 else if (offset > entry->offset)
1381 n = n->rb_right;
1382 else
1383 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001384 }
1385
Josef Bacik96303082009-07-13 21:29:25 -04001386 if (bitmap_only) {
1387 if (!entry)
1388 return NULL;
1389 if (entry->bitmap)
1390 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001391
Josef Bacik96303082009-07-13 21:29:25 -04001392 /*
1393 * bitmap entry and extent entry may share same offset,
1394 * in that case, bitmap entry comes after extent entry.
1395 */
1396 n = rb_next(n);
1397 if (!n)
1398 return NULL;
1399 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1400 if (entry->offset != offset)
1401 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001402
Josef Bacik96303082009-07-13 21:29:25 -04001403 WARN_ON(!entry->bitmap);
1404 return entry;
1405 } else if (entry) {
1406 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001407 /*
Josef Bacik96303082009-07-13 21:29:25 -04001408 * if previous extent entry covers the offset,
1409 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001410 */
Miao Xiede6c4112012-10-18 08:18:01 +00001411 n = rb_prev(&entry->offset_index);
1412 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001413 prev = rb_entry(n, struct btrfs_free_space,
1414 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001415 if (!prev->bitmap &&
1416 prev->offset + prev->bytes > offset)
1417 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001418 }
Josef Bacik96303082009-07-13 21:29:25 -04001419 }
1420 return entry;
1421 }
1422
1423 if (!prev)
1424 return NULL;
1425
1426 /* find last entry before the 'offset' */
1427 entry = prev;
1428 if (entry->offset > offset) {
1429 n = rb_prev(&entry->offset_index);
1430 if (n) {
1431 entry = rb_entry(n, struct btrfs_free_space,
1432 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001433 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001434 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001435 if (fuzzy)
1436 return entry;
1437 else
1438 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001439 }
1440 }
1441
Josef Bacik96303082009-07-13 21:29:25 -04001442 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001443 n = rb_prev(&entry->offset_index);
1444 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001445 prev = rb_entry(n, struct btrfs_free_space,
1446 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001447 if (!prev->bitmap &&
1448 prev->offset + prev->bytes > offset)
1449 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001450 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001451 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001452 return entry;
1453 } else if (entry->offset + entry->bytes > offset)
1454 return entry;
1455
1456 if (!fuzzy)
1457 return NULL;
1458
1459 while (1) {
1460 if (entry->bitmap) {
1461 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001462 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001463 break;
1464 } else {
1465 if (entry->offset + entry->bytes > offset)
1466 break;
1467 }
1468
1469 n = rb_next(&entry->offset_index);
1470 if (!n)
1471 return NULL;
1472 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1473 }
1474 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001475}
1476
Li Zefanf333adb2010-11-09 14:57:39 +08001477static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001478__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001479 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001480{
Li Zefan34d52cb2011-03-29 13:46:06 +08001481 rb_erase(&info->offset_index, &ctl->free_space_offset);
1482 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001483}
1484
Li Zefan34d52cb2011-03-29 13:46:06 +08001485static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001486 struct btrfs_free_space *info)
1487{
Li Zefan34d52cb2011-03-29 13:46:06 +08001488 __unlink_free_space(ctl, info);
1489 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001490}
1491
Li Zefan34d52cb2011-03-29 13:46:06 +08001492static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001493 struct btrfs_free_space *info)
1494{
1495 int ret = 0;
1496
Josef Bacikb12d6862013-08-26 17:14:08 -04001497 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001498 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001499 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001500 if (ret)
1501 return ret;
1502
Li Zefan34d52cb2011-03-29 13:46:06 +08001503 ctl->free_space += info->bytes;
1504 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001505 return ret;
1506}
1507
Li Zefan34d52cb2011-03-29 13:46:06 +08001508static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001509{
Li Zefan34d52cb2011-03-29 13:46:06 +08001510 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001511 u64 max_bytes;
1512 u64 bitmap_bytes;
1513 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001514 u64 size = block_group->key.offset;
David Sterbab8b93ad2015-01-16 17:26:13 +01001515 u32 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1516 u32 max_bitmaps = div_u64(size + bytes_per_bg - 1, bytes_per_bg);
Li Zefan34d52cb2011-03-29 13:46:06 +08001517
David Sterbab8b93ad2015-01-16 17:26:13 +01001518 max_bitmaps = max_t(u32, max_bitmaps, 1);
Josef Bacikdde57402013-02-12 14:07:51 -05001519
Josef Bacikb12d6862013-08-26 17:14:08 -04001520 ASSERT(ctl->total_bitmaps <= max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001521
1522 /*
1523 * The goal is to keep the total amount of memory used per 1gb of space
1524 * at or below 32k, so we need to adjust how much memory we allow to be
1525 * used by extent based free space tracking
1526 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001527 if (size < 1024 * 1024 * 1024)
1528 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1529 else
1530 max_bytes = MAX_CACHE_BYTES_PER_GIG *
David Sterbaf8c269d2015-01-16 17:21:12 +01001531 div_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001532
Josef Bacik25891f72009-09-11 16:11:20 -04001533 /*
1534 * we want to account for 1 more bitmap than what we have so we can make
1535 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1536 * we add more bitmaps.
1537 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001538 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001539
Josef Bacik25891f72009-09-11 16:11:20 -04001540 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001541 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001542 return;
Josef Bacik96303082009-07-13 21:29:25 -04001543 }
Josef Bacik25891f72009-09-11 16:11:20 -04001544
1545 /*
David Sterbaf8c269d2015-01-16 17:21:12 +01001546 * we want the extent entry threshold to always be at most 1/2 the max
Josef Bacik25891f72009-09-11 16:11:20 -04001547 * bytes we can have, or whatever is less than that.
1548 */
1549 extent_bytes = max_bytes - bitmap_bytes;
David Sterbaf8c269d2015-01-16 17:21:12 +01001550 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
Josef Bacik25891f72009-09-11 16:11:20 -04001551
Li Zefan34d52cb2011-03-29 13:46:06 +08001552 ctl->extents_thresh =
David Sterbaf8c269d2015-01-16 17:21:12 +01001553 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
Josef Bacik96303082009-07-13 21:29:25 -04001554}
1555
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001556static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1557 struct btrfs_free_space *info,
1558 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001559{
Li Zefanf38b6e72011-03-14 13:40:51 +08001560 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001561
Li Zefan34d52cb2011-03-29 13:46:06 +08001562 start = offset_to_bit(info->offset, ctl->unit, offset);
1563 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001564 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001565
Li Zefanf38b6e72011-03-14 13:40:51 +08001566 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001567
1568 info->bytes -= bytes;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001569}
1570
1571static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1572 struct btrfs_free_space *info, u64 offset,
1573 u64 bytes)
1574{
1575 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001576 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001577}
1578
Li Zefan34d52cb2011-03-29 13:46:06 +08001579static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001580 struct btrfs_free_space *info, u64 offset,
1581 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001582{
Li Zefanf38b6e72011-03-14 13:40:51 +08001583 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001584
Li Zefan34d52cb2011-03-29 13:46:06 +08001585 start = offset_to_bit(info->offset, ctl->unit, offset);
1586 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001587 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001588
Li Zefanf38b6e72011-03-14 13:40:51 +08001589 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001590
1591 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001592 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001593}
1594
Miao Xiea4820392013-09-09 13:19:42 +08001595/*
1596 * If we can not find suitable extent, we will use bytes to record
1597 * the size of the max extent.
1598 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001599static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001600 struct btrfs_free_space *bitmap_info, u64 *offset,
1601 u64 *bytes)
1602{
1603 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001604 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001605 unsigned long bits, i;
1606 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001607 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001608
Li Zefan34d52cb2011-03-29 13:46:06 +08001609 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001610 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001611 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001612
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001613 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04001614 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1615 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001616 extent_bits = next_zero - i;
1617 if (extent_bits >= bits) {
1618 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001619 break;
Miao Xiea4820392013-09-09 13:19:42 +08001620 } else if (extent_bits > max_bits) {
1621 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001622 }
1623 i = next_zero;
1624 }
1625
1626 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001627 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1628 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001629 return 0;
1630 }
1631
Miao Xiea4820392013-09-09 13:19:42 +08001632 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001633 return -1;
1634}
1635
Miao Xiea4820392013-09-09 13:19:42 +08001636/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001637static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001638find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Miao Xiea4820392013-09-09 13:19:42 +08001639 unsigned long align, u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04001640{
1641 struct btrfs_free_space *entry;
1642 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001643 u64 tmp;
1644 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001645 int ret;
1646
Li Zefan34d52cb2011-03-29 13:46:06 +08001647 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001648 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001649
Li Zefan34d52cb2011-03-29 13:46:06 +08001650 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001651 if (!entry)
Miao Xiea4820392013-09-09 13:19:42 +08001652 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001653
1654 for (node = &entry->offset_index; node; node = rb_next(node)) {
1655 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Miao Xiea4820392013-09-09 13:19:42 +08001656 if (entry->bytes < *bytes) {
1657 if (entry->bytes > *max_extent_size)
1658 *max_extent_size = entry->bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001659 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001660 }
Josef Bacik96303082009-07-13 21:29:25 -04001661
David Woodhouse53b381b2013-01-29 18:40:14 -05001662 /* make sure the space returned is big enough
1663 * to match our requested alignment
1664 */
1665 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001666 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01001667 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05001668 tmp = tmp * align + ctl->start;
1669 align_off = tmp - entry->offset;
1670 } else {
1671 align_off = 0;
1672 tmp = entry->offset;
1673 }
1674
Miao Xiea4820392013-09-09 13:19:42 +08001675 if (entry->bytes < *bytes + align_off) {
1676 if (entry->bytes > *max_extent_size)
1677 *max_extent_size = entry->bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05001678 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001679 }
David Woodhouse53b381b2013-01-29 18:40:14 -05001680
Josef Bacik96303082009-07-13 21:29:25 -04001681 if (entry->bitmap) {
Miao Xiea4820392013-09-09 13:19:42 +08001682 u64 size = *bytes;
1683
1684 ret = search_bitmap(ctl, entry, &tmp, &size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001685 if (!ret) {
1686 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08001687 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04001688 return entry;
Miao Xiea4820392013-09-09 13:19:42 +08001689 } else if (size > *max_extent_size) {
1690 *max_extent_size = size;
David Woodhouse53b381b2013-01-29 18:40:14 -05001691 }
Josef Bacik96303082009-07-13 21:29:25 -04001692 continue;
1693 }
1694
David Woodhouse53b381b2013-01-29 18:40:14 -05001695 *offset = tmp;
1696 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001697 return entry;
1698 }
Miao Xiea4820392013-09-09 13:19:42 +08001699out:
Josef Bacik96303082009-07-13 21:29:25 -04001700 return NULL;
1701}
1702
Li Zefan34d52cb2011-03-29 13:46:06 +08001703static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001704 struct btrfs_free_space *info, u64 offset)
1705{
Li Zefan34d52cb2011-03-29 13:46:06 +08001706 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001707 info->bytes = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02001708 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08001709 link_free_space(ctl, info);
1710 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001711
Li Zefan34d52cb2011-03-29 13:46:06 +08001712 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001713}
1714
Li Zefan34d52cb2011-03-29 13:46:06 +08001715static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001716 struct btrfs_free_space *bitmap_info)
1717{
Li Zefan34d52cb2011-03-29 13:46:06 +08001718 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001719 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001720 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001721 ctl->total_bitmaps--;
1722 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001723}
1724
Li Zefan34d52cb2011-03-29 13:46:06 +08001725static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001726 struct btrfs_free_space *bitmap_info,
1727 u64 *offset, u64 *bytes)
1728{
1729 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001730 u64 search_start, search_bytes;
1731 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001732
1733again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001734 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001735
Josef Bacik6606bb92009-07-31 11:03:58 -04001736 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04001737 * We need to search for bits in this bitmap. We could only cover some
1738 * of the extent in this bitmap thanks to how we add space, so we need
1739 * to search for as much as it as we can and clear that amount, and then
1740 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04001741 */
1742 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001743 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00001744 search_bytes = min(search_bytes, end - search_start + 1);
Li Zefan34d52cb2011-03-29 13:46:06 +08001745 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
Josef Bacikb50c6e22013-04-25 15:55:30 -04001746 if (ret < 0 || search_start != *offset)
1747 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04001748
Josef Bacikbdb7d302012-06-27 15:10:56 -04001749 /* We may have found more bits than what we need */
1750 search_bytes = min(search_bytes, *bytes);
1751
1752 /* Cannot clear past the end of the bitmap */
1753 search_bytes = min(search_bytes, end - search_start + 1);
1754
1755 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1756 *offset += search_bytes;
1757 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001758
1759 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001760 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001761 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001762 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001763
Josef Bacik6606bb92009-07-31 11:03:58 -04001764 /*
1765 * no entry after this bitmap, but we still have bytes to
1766 * remove, so something has gone wrong.
1767 */
1768 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001769 return -EINVAL;
1770
Josef Bacik6606bb92009-07-31 11:03:58 -04001771 bitmap_info = rb_entry(next, struct btrfs_free_space,
1772 offset_index);
1773
1774 /*
1775 * if the next entry isn't a bitmap we need to return to let the
1776 * extent stuff do its work.
1777 */
Josef Bacik96303082009-07-13 21:29:25 -04001778 if (!bitmap_info->bitmap)
1779 return -EAGAIN;
1780
Josef Bacik6606bb92009-07-31 11:03:58 -04001781 /*
1782 * Ok the next item is a bitmap, but it may not actually hold
1783 * the information for the rest of this free space stuff, so
1784 * look for it, and if we don't find it return so we can try
1785 * everything over again.
1786 */
1787 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001788 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08001789 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik6606bb92009-07-31 11:03:58 -04001790 &search_bytes);
1791 if (ret < 0 || search_start != *offset)
1792 return -EAGAIN;
1793
Josef Bacik96303082009-07-13 21:29:25 -04001794 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001795 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001796 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001797
1798 return 0;
1799}
1800
Josef Bacik2cdc3422011-05-27 14:07:49 -04001801static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1802 struct btrfs_free_space *info, u64 offset,
1803 u64 bytes)
1804{
1805 u64 bytes_to_set = 0;
1806 u64 end;
1807
1808 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1809
1810 bytes_to_set = min(end - offset, bytes);
1811
1812 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1813
1814 return bytes_to_set;
1815
1816}
1817
Li Zefan34d52cb2011-03-29 13:46:06 +08001818static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1819 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001820{
Li Zefan34d52cb2011-03-29 13:46:06 +08001821 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik96303082009-07-13 21:29:25 -04001822
1823 /*
1824 * If we are below the extents threshold then we can add this as an
1825 * extent, and don't have to deal with the bitmap
1826 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001827 if (ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001828 /*
1829 * If this block group has some small extents we don't want to
1830 * use up all of our free slots in the cache with them, we want
1831 * to reserve them to larger extents, however if we have plent
1832 * of cache left then go ahead an dadd them, no sense in adding
1833 * the overhead of a bitmap if we don't have to.
1834 */
1835 if (info->bytes <= block_group->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001836 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1837 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001838 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001839 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001840 }
1841 }
Josef Bacik96303082009-07-13 21:29:25 -04001842
1843 /*
Josef Bacikdde57402013-02-12 14:07:51 -05001844 * The original block groups from mkfs can be really small, like 8
1845 * megabytes, so don't bother with a bitmap for those entries. However
1846 * some block groups can be smaller than what a bitmap would cover but
1847 * are still large enough that they could overflow the 32k memory limit,
1848 * so allow those block groups to still be allowed to have a bitmap
1849 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04001850 */
Josef Bacikdde57402013-02-12 14:07:51 -05001851 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08001852 return false;
1853
1854 return true;
1855}
1856
Josef Bacik2cdc3422011-05-27 14:07:49 -04001857static struct btrfs_free_space_op free_space_op = {
1858 .recalc_thresholds = recalculate_thresholds,
1859 .use_bitmap = use_bitmap,
1860};
1861
Li Zefan34d52cb2011-03-29 13:46:06 +08001862static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1863 struct btrfs_free_space *info)
1864{
1865 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001866 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08001867 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001868 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08001869 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001870
1871 bytes = info->bytes;
1872 offset = info->offset;
1873
Li Zefan34d52cb2011-03-29 13:46:06 +08001874 if (!ctl->op->use_bitmap(ctl, info))
1875 return 0;
1876
Josef Bacik2cdc3422011-05-27 14:07:49 -04001877 if (ctl->op == &free_space_op)
1878 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04001879again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04001880 /*
1881 * Since we link bitmaps right into the cluster we need to see if we
1882 * have a cluster here, and if so and it has our bitmap we need to add
1883 * the free space to that bitmap.
1884 */
1885 if (block_group && !list_empty(&block_group->cluster_list)) {
1886 struct btrfs_free_cluster *cluster;
1887 struct rb_node *node;
1888 struct btrfs_free_space *entry;
1889
1890 cluster = list_entry(block_group->cluster_list.next,
1891 struct btrfs_free_cluster,
1892 block_group_list);
1893 spin_lock(&cluster->lock);
1894 node = rb_first(&cluster->root);
1895 if (!node) {
1896 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001897 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001898 }
1899
1900 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1901 if (!entry->bitmap) {
1902 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001903 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001904 }
1905
1906 if (entry->offset == offset_to_bitmap(ctl, offset)) {
1907 bytes_added = add_bytes_to_bitmap(ctl, entry,
1908 offset, bytes);
1909 bytes -= bytes_added;
1910 offset += bytes_added;
1911 }
1912 spin_unlock(&cluster->lock);
1913 if (!bytes) {
1914 ret = 1;
1915 goto out;
1916 }
1917 }
Chris Mason38e87882011-06-10 16:36:57 -04001918
1919no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08001920 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04001921 1, 0);
1922 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04001923 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04001924 goto new_bitmap;
1925 }
1926
Josef Bacik2cdc3422011-05-27 14:07:49 -04001927 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
1928 bytes -= bytes_added;
1929 offset += bytes_added;
1930 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001931
1932 if (!bytes) {
1933 ret = 1;
1934 goto out;
1935 } else
1936 goto again;
1937
1938new_bitmap:
1939 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001940 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04001941 added = 1;
1942 info = NULL;
1943 goto again;
1944 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001945 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001946
1947 /* no pre-allocated info, allocate a new one */
1948 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05001949 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1950 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04001951 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001952 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001953 ret = -ENOMEM;
1954 goto out;
1955 }
1956 }
1957
1958 /* allocate the bitmap */
1959 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08001960 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001961 if (!info->bitmap) {
1962 ret = -ENOMEM;
1963 goto out;
1964 }
1965 goto again;
1966 }
1967
1968out:
1969 if (info) {
1970 if (info->bitmap)
1971 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001972 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001973 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001974
1975 return ret;
1976}
1977
Chris Mason945d8962011-05-22 12:33:42 -04001978static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001979 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001980{
Li Zefan120d66e2010-11-09 14:56:50 +08001981 struct btrfs_free_space *left_info;
1982 struct btrfs_free_space *right_info;
1983 bool merged = false;
1984 u64 offset = info->offset;
1985 u64 bytes = info->bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001986
Josef Bacik0f9dd462008-09-23 13:14:11 -04001987 /*
1988 * first we want to see if there is free space adjacent to the range we
1989 * are adding, if there is remove that struct and add a new one to
1990 * cover the entire range
1991 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001992 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001993 if (right_info && rb_prev(&right_info->offset_index))
1994 left_info = rb_entry(rb_prev(&right_info->offset_index),
1995 struct btrfs_free_space, offset_index);
1996 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001997 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001998
Josef Bacik96303082009-07-13 21:29:25 -04001999 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08002000 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002001 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002002 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002003 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002004 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002005 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002006 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002007 }
2008
Josef Bacik96303082009-07-13 21:29:25 -04002009 if (left_info && !left_info->bitmap &&
2010 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08002011 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002012 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002013 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002014 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002015 info->offset = left_info->offset;
2016 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002017 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002018 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002019 }
2020
Li Zefan120d66e2010-11-09 14:56:50 +08002021 return merged;
2022}
2023
Filipe Manana20005522014-08-29 13:35:13 +01002024static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2025 struct btrfs_free_space *info,
2026 bool update_stat)
2027{
2028 struct btrfs_free_space *bitmap;
2029 unsigned long i;
2030 unsigned long j;
2031 const u64 end = info->offset + info->bytes;
2032 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2033 u64 bytes;
2034
2035 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2036 if (!bitmap)
2037 return false;
2038
2039 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2040 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2041 if (j == i)
2042 return false;
2043 bytes = (j - i) * ctl->unit;
2044 info->bytes += bytes;
2045
2046 if (update_stat)
2047 bitmap_clear_bits(ctl, bitmap, end, bytes);
2048 else
2049 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2050
2051 if (!bitmap->bytes)
2052 free_bitmap(ctl, bitmap);
2053
2054 return true;
2055}
2056
2057static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2058 struct btrfs_free_space *info,
2059 bool update_stat)
2060{
2061 struct btrfs_free_space *bitmap;
2062 u64 bitmap_offset;
2063 unsigned long i;
2064 unsigned long j;
2065 unsigned long prev_j;
2066 u64 bytes;
2067
2068 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2069 /* If we're on a boundary, try the previous logical bitmap. */
2070 if (bitmap_offset == info->offset) {
2071 if (info->offset == 0)
2072 return false;
2073 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2074 }
2075
2076 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2077 if (!bitmap)
2078 return false;
2079
2080 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2081 j = 0;
2082 prev_j = (unsigned long)-1;
2083 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2084 if (j > i)
2085 break;
2086 prev_j = j;
2087 }
2088 if (prev_j == i)
2089 return false;
2090
2091 if (prev_j == (unsigned long)-1)
2092 bytes = (i + 1) * ctl->unit;
2093 else
2094 bytes = (i - prev_j) * ctl->unit;
2095
2096 info->offset -= bytes;
2097 info->bytes += bytes;
2098
2099 if (update_stat)
2100 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2101 else
2102 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2103
2104 if (!bitmap->bytes)
2105 free_bitmap(ctl, bitmap);
2106
2107 return true;
2108}
2109
2110/*
2111 * We prefer always to allocate from extent entries, both for clustered and
2112 * non-clustered allocation requests. So when attempting to add a new extent
2113 * entry, try to see if there's adjacent free space in bitmap entries, and if
2114 * there is, migrate that space from the bitmaps to the extent.
2115 * Like this we get better chances of satisfying space allocation requests
2116 * because we attempt to satisfy them based on a single cache entry, and never
2117 * on 2 or more entries - even if the entries represent a contiguous free space
2118 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2119 * ends).
2120 */
2121static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2122 struct btrfs_free_space *info,
2123 bool update_stat)
2124{
2125 /*
2126 * Only work with disconnected entries, as we can change their offset,
2127 * and must be extent entries.
2128 */
2129 ASSERT(!info->bitmap);
2130 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2131
2132 if (ctl->total_bitmaps > 0) {
2133 bool stole_end;
2134 bool stole_front = false;
2135
2136 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2137 if (ctl->total_bitmaps > 0)
2138 stole_front = steal_from_bitmap_to_front(ctl, info,
2139 update_stat);
2140
2141 if (stole_end || stole_front)
2142 try_merge_free_space(ctl, info, update_stat);
2143 }
2144}
2145
Li Zefan581bb052011-04-20 10:06:11 +08002146int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
2147 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08002148{
2149 struct btrfs_free_space *info;
2150 int ret = 0;
2151
Josef Bacikdc89e982011-01-28 17:05:48 -05002152 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002153 if (!info)
2154 return -ENOMEM;
2155
2156 info->offset = offset;
2157 info->bytes = bytes;
Filipe Manana20005522014-08-29 13:35:13 +01002158 RB_CLEAR_NODE(&info->offset_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002159
Li Zefan34d52cb2011-03-29 13:46:06 +08002160 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002161
Li Zefan34d52cb2011-03-29 13:46:06 +08002162 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002163 goto link;
2164
2165 /*
2166 * There was no extent directly to the left or right of this new
2167 * extent then we know we're going to have to allocate a new extent, so
2168 * before we do that see if we need to drop this into a bitmap
2169 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002170 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002171 if (ret < 0) {
2172 goto out;
2173 } else if (ret) {
2174 ret = 0;
2175 goto out;
2176 }
2177link:
Filipe Manana20005522014-08-29 13:35:13 +01002178 /*
2179 * Only steal free space from adjacent bitmaps if we're sure we're not
2180 * going to add the new free space to existing bitmap entries - because
2181 * that would mean unnecessary work that would be reverted. Therefore
2182 * attempt to steal space from bitmaps if we're adding an extent entry.
2183 */
2184 steal_from_bitmap(ctl, info, true);
2185
Li Zefan34d52cb2011-03-29 13:46:06 +08002186 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002187 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002188 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002189out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002190 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002191
Josef Bacik0f9dd462008-09-23 13:14:11 -04002192 if (ret) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002193 printk(KERN_CRIT "BTRFS: unable to add free space :%d\n", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002194 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002195 }
2196
Josef Bacik0f9dd462008-09-23 13:14:11 -04002197 return ret;
2198}
2199
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002200int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2201 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002202{
Li Zefan34d52cb2011-03-29 13:46:06 +08002203 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002204 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002205 int ret;
2206 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002207
Li Zefan34d52cb2011-03-29 13:46:06 +08002208 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002209
Josef Bacik96303082009-07-13 21:29:25 -04002210again:
Josef Bacikb0175112012-12-18 11:39:19 -05002211 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002212 if (!bytes)
2213 goto out_lock;
2214
Li Zefan34d52cb2011-03-29 13:46:06 +08002215 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002216 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002217 /*
2218 * oops didn't find an extent that matched the space we wanted
2219 * to remove, look for a bitmap instead
2220 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002221 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002222 1, 0);
2223 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002224 /*
2225 * If we found a partial bit of our free space in a
2226 * bitmap but then couldn't find the other part this may
2227 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002228 */
Josef Bacikb0175112012-12-18 11:39:19 -05002229 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002230 goto out_lock;
2231 }
Josef Bacik96303082009-07-13 21:29:25 -04002232 }
2233
Josef Bacikb0175112012-12-18 11:39:19 -05002234 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002235 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002236 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002237 if (offset == info->offset) {
2238 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002239
Josef Bacikbdb7d302012-06-27 15:10:56 -04002240 info->bytes -= to_free;
2241 info->offset += to_free;
2242 if (info->bytes) {
2243 ret = link_free_space(ctl, info);
2244 WARN_ON(ret);
2245 } else {
2246 kmem_cache_free(btrfs_free_space_cachep, info);
2247 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002248
Josef Bacikbdb7d302012-06-27 15:10:56 -04002249 offset += to_free;
2250 bytes -= to_free;
2251 goto again;
2252 } else {
2253 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002254
Josef Bacikbdb7d302012-06-27 15:10:56 -04002255 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002256 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002257 WARN_ON(ret);
2258 if (ret)
2259 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002260
Josef Bacikbdb7d302012-06-27 15:10:56 -04002261 /* Not enough bytes in this entry to satisfy us */
2262 if (old_end < offset + bytes) {
2263 bytes -= old_end - offset;
2264 offset = old_end;
2265 goto again;
2266 } else if (old_end == offset + bytes) {
2267 /* all done */
2268 goto out_lock;
2269 }
2270 spin_unlock(&ctl->tree_lock);
2271
2272 ret = btrfs_add_free_space(block_group, offset + bytes,
2273 old_end - (offset + bytes));
2274 WARN_ON(ret);
2275 goto out;
2276 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002277 }
Josef Bacik96303082009-07-13 21:29:25 -04002278
Li Zefan34d52cb2011-03-29 13:46:06 +08002279 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002280 if (ret == -EAGAIN) {
2281 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002282 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002283 }
Josef Bacik96303082009-07-13 21:29:25 -04002284out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08002285 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002286out:
Josef Bacik25179202008-10-29 14:49:05 -04002287 return ret;
2288}
2289
Josef Bacik0f9dd462008-09-23 13:14:11 -04002290void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2291 u64 bytes)
2292{
Li Zefan34d52cb2011-03-29 13:46:06 +08002293 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002294 struct btrfs_free_space *info;
2295 struct rb_node *n;
2296 int count = 0;
2297
Li Zefan34d52cb2011-03-29 13:46:06 +08002298 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002299 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002300 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002301 count++;
Frank Holtonefe120a2013-12-20 11:37:06 -05002302 btrfs_crit(block_group->fs_info,
2303 "entry offset %llu, bytes %llu, bitmap %s",
2304 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002305 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002306 }
Frank Holtonefe120a2013-12-20 11:37:06 -05002307 btrfs_info(block_group->fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002308 list_empty(&block_group->cluster_list) ? "no" : "yes");
Frank Holtonefe120a2013-12-20 11:37:06 -05002309 btrfs_info(block_group->fs_info,
2310 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002311}
2312
Li Zefan34d52cb2011-03-29 13:46:06 +08002313void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002314{
Li Zefan34d52cb2011-03-29 13:46:06 +08002315 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002316
Li Zefan34d52cb2011-03-29 13:46:06 +08002317 spin_lock_init(&ctl->tree_lock);
2318 ctl->unit = block_group->sectorsize;
2319 ctl->start = block_group->key.objectid;
2320 ctl->private = block_group;
2321 ctl->op = &free_space_op;
Filipe Manana55507ce2014-12-01 17:04:09 +00002322 INIT_LIST_HEAD(&ctl->trimming_ranges);
2323 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002324
Li Zefan34d52cb2011-03-29 13:46:06 +08002325 /*
2326 * we only want to have 32k of ram per block group for keeping
2327 * track of free space, and if we pass 1/2 of that we want to
2328 * start converting things over to using bitmaps
2329 */
2330 ctl->extents_thresh = ((1024 * 32) / 2) /
2331 sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002332}
2333
Chris Masonfa9c0d792009-04-03 09:47:43 -04002334/*
2335 * for a given cluster, put all of its extents back into the free
2336 * space cache. If the block group passed doesn't match the block group
2337 * pointed to by the cluster, someone else raced in and freed the
2338 * cluster already. In that case, we just return without changing anything
2339 */
2340static int
2341__btrfs_return_cluster_to_free_space(
2342 struct btrfs_block_group_cache *block_group,
2343 struct btrfs_free_cluster *cluster)
2344{
Li Zefan34d52cb2011-03-29 13:46:06 +08002345 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002346 struct btrfs_free_space *entry;
2347 struct rb_node *node;
2348
2349 spin_lock(&cluster->lock);
2350 if (cluster->block_group != block_group)
2351 goto out;
2352
Josef Bacik96303082009-07-13 21:29:25 -04002353 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002354 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002355 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002356
Chris Masonfa9c0d792009-04-03 09:47:43 -04002357 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002358 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002359 bool bitmap;
2360
Chris Masonfa9c0d792009-04-03 09:47:43 -04002361 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2362 node = rb_next(&entry->offset_index);
2363 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002364 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002365
2366 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002367 if (!bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002368 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002369 steal_from_bitmap(ctl, entry, false);
2370 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002371 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002372 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002373 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002374 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002375
Chris Masonfa9c0d792009-04-03 09:47:43 -04002376out:
2377 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002378 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002379 return 0;
2380}
2381
Eric Sandeen48a3b632013-04-25 20:41:01 +00002382static void __btrfs_remove_free_space_cache_locked(
2383 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002384{
2385 struct btrfs_free_space *info;
2386 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002387
Li Zefan581bb052011-04-20 10:06:11 +08002388 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2389 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002390 if (!info->bitmap) {
2391 unlink_free_space(ctl, info);
2392 kmem_cache_free(btrfs_free_space_cachep, info);
2393 } else {
2394 free_bitmap(ctl, info);
2395 }
David Sterba351810c2015-01-08 15:20:54 +01002396
2397 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002398 }
Chris Mason09655372011-05-21 09:27:38 -04002399}
2400
2401void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2402{
2403 spin_lock(&ctl->tree_lock);
2404 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002405 spin_unlock(&ctl->tree_lock);
2406}
2407
Josef Bacik0f9dd462008-09-23 13:14:11 -04002408void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2409{
Li Zefan34d52cb2011-03-29 13:46:06 +08002410 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002411 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002412 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002413
Li Zefan34d52cb2011-03-29 13:46:06 +08002414 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002415 while ((head = block_group->cluster_list.next) !=
2416 &block_group->cluster_list) {
2417 cluster = list_entry(head, struct btrfs_free_cluster,
2418 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002419
2420 WARN_ON(cluster->block_group != block_group);
2421 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01002422
2423 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002424 }
Chris Mason09655372011-05-21 09:27:38 -04002425 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002426 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002427
Josef Bacik0f9dd462008-09-23 13:14:11 -04002428}
2429
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002430u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002431 u64 offset, u64 bytes, u64 empty_size,
2432 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002433{
Li Zefan34d52cb2011-03-29 13:46:06 +08002434 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002435 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002436 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002437 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002438 u64 align_gap = 0;
2439 u64 align_gap_len = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002440
Li Zefan34d52cb2011-03-29 13:46:06 +08002441 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002442 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002443 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002444 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002445 goto out;
2446
2447 ret = offset;
2448 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002449 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002450 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002451 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002452 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002453 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002454 align_gap_len = offset - entry->offset;
2455 align_gap = entry->offset;
2456
2457 entry->offset = offset + bytes;
2458 WARN_ON(entry->bytes < bytes + align_gap_len);
2459
2460 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002461 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002462 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002463 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002464 link_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002465 }
Josef Bacik96303082009-07-13 21:29:25 -04002466out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002467 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002468
David Woodhouse53b381b2013-01-29 18:40:14 -05002469 if (align_gap_len)
2470 __btrfs_add_free_space(ctl, align_gap, align_gap_len);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002471 return ret;
2472}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002473
2474/*
2475 * given a cluster, put all of its extents back into the free space
2476 * cache. If a block group is passed, this function will only free
2477 * a cluster that belongs to the passed block group.
2478 *
2479 * Otherwise, it'll get a reference on the block group pointed to by the
2480 * cluster and remove the cluster from it.
2481 */
2482int btrfs_return_cluster_to_free_space(
2483 struct btrfs_block_group_cache *block_group,
2484 struct btrfs_free_cluster *cluster)
2485{
Li Zefan34d52cb2011-03-29 13:46:06 +08002486 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002487 int ret;
2488
2489 /* first, get a safe pointer to the block group */
2490 spin_lock(&cluster->lock);
2491 if (!block_group) {
2492 block_group = cluster->block_group;
2493 if (!block_group) {
2494 spin_unlock(&cluster->lock);
2495 return 0;
2496 }
2497 } else if (cluster->block_group != block_group) {
2498 /* someone else has already freed it don't redo their work */
2499 spin_unlock(&cluster->lock);
2500 return 0;
2501 }
2502 atomic_inc(&block_group->count);
2503 spin_unlock(&cluster->lock);
2504
Li Zefan34d52cb2011-03-29 13:46:06 +08002505 ctl = block_group->free_space_ctl;
2506
Chris Masonfa9c0d792009-04-03 09:47:43 -04002507 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002508 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002509 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002510 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002511
2512 /* finally drop our ref */
2513 btrfs_put_block_group(block_group);
2514 return ret;
2515}
2516
Josef Bacik96303082009-07-13 21:29:25 -04002517static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2518 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002519 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002520 u64 bytes, u64 min_start,
2521 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002522{
Li Zefan34d52cb2011-03-29 13:46:06 +08002523 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002524 int err;
2525 u64 search_start = cluster->window_start;
2526 u64 search_bytes = bytes;
2527 u64 ret = 0;
2528
Josef Bacik96303082009-07-13 21:29:25 -04002529 search_start = min_start;
2530 search_bytes = bytes;
2531
Li Zefan34d52cb2011-03-29 13:46:06 +08002532 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Miao Xiea4820392013-09-09 13:19:42 +08002533 if (err) {
2534 if (search_bytes > *max_extent_size)
2535 *max_extent_size = search_bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002536 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002537 }
Josef Bacik96303082009-07-13 21:29:25 -04002538
2539 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002540 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002541
2542 return ret;
2543}
2544
Chris Masonfa9c0d792009-04-03 09:47:43 -04002545/*
2546 * given a cluster, try to allocate 'bytes' from it, returns 0
2547 * if it couldn't find anything suitably large, or a logical disk offset
2548 * if things worked out
2549 */
2550u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2551 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002552 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002553{
Li Zefan34d52cb2011-03-29 13:46:06 +08002554 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002555 struct btrfs_free_space *entry = NULL;
2556 struct rb_node *node;
2557 u64 ret = 0;
2558
2559 spin_lock(&cluster->lock);
2560 if (bytes > cluster->max_size)
2561 goto out;
2562
2563 if (cluster->block_group != block_group)
2564 goto out;
2565
2566 node = rb_first(&cluster->root);
2567 if (!node)
2568 goto out;
2569
2570 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302571 while (1) {
Miao Xiea4820392013-09-09 13:19:42 +08002572 if (entry->bytes < bytes && entry->bytes > *max_extent_size)
2573 *max_extent_size = entry->bytes;
2574
Josef Bacik4e69b592011-03-21 10:11:24 -04002575 if (entry->bytes < bytes ||
2576 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002577 node = rb_next(&entry->offset_index);
2578 if (!node)
2579 break;
2580 entry = rb_entry(node, struct btrfs_free_space,
2581 offset_index);
2582 continue;
2583 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002584
Josef Bacik4e69b592011-03-21 10:11:24 -04002585 if (entry->bitmap) {
2586 ret = btrfs_alloc_from_bitmap(block_group,
2587 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002588 cluster->window_start,
2589 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002590 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002591 node = rb_next(&entry->offset_index);
2592 if (!node)
2593 break;
2594 entry = rb_entry(node, struct btrfs_free_space,
2595 offset_index);
2596 continue;
2597 }
Josef Bacik9b230622012-01-26 15:01:12 -05002598 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002599 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002600 ret = entry->offset;
2601
2602 entry->offset += bytes;
2603 entry->bytes -= bytes;
2604 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002605
Li Zefan5e71b5d2010-11-09 14:55:34 +08002606 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002607 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002608 break;
2609 }
2610out:
2611 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002612
Li Zefan5e71b5d2010-11-09 14:55:34 +08002613 if (!ret)
2614 return 0;
2615
Li Zefan34d52cb2011-03-29 13:46:06 +08002616 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002617
Li Zefan34d52cb2011-03-29 13:46:06 +08002618 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002619 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002620 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002621 if (entry->bitmap) {
2622 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002623 ctl->total_bitmaps--;
2624 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002625 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002626 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002627 }
2628
Li Zefan34d52cb2011-03-29 13:46:06 +08002629 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002630
Chris Masonfa9c0d792009-04-03 09:47:43 -04002631 return ret;
2632}
2633
Josef Bacik96303082009-07-13 21:29:25 -04002634static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2635 struct btrfs_free_space *entry,
2636 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002637 u64 offset, u64 bytes,
2638 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04002639{
Li Zefan34d52cb2011-03-29 13:46:06 +08002640 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002641 unsigned long next_zero;
2642 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002643 unsigned long want_bits;
2644 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002645 unsigned long found_bits;
2646 unsigned long start = 0;
2647 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002648 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002649
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002650 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04002651 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002652 want_bits = bytes_to_bits(bytes, ctl->unit);
2653 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04002654
2655again:
2656 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06002657 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04002658 next_zero = find_next_zero_bit(entry->bitmap,
2659 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002660 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04002661 found_bits = next_zero - i;
2662 break;
2663 }
2664 i = next_zero;
2665 }
2666
2667 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002668 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002669
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002670 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04002671 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05002672 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002673 }
2674
2675 total_found += found_bits;
2676
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002677 if (cluster->max_size < found_bits * ctl->unit)
2678 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04002679
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002680 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2681 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04002682 goto again;
2683 }
2684
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002685 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002686 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002687 ret = tree_insert_offset(&cluster->root, entry->offset,
2688 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04002689 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04002690
Josef Bacik3f7de032011-11-10 08:29:20 -05002691 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002692 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04002693 return 0;
2694}
2695
Chris Masonfa9c0d792009-04-03 09:47:43 -04002696/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002697 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002698 * Try to find a cluster with at least bytes total bytes, at least one
2699 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04002700 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002701static noinline int
2702setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2703 struct btrfs_free_cluster *cluster,
2704 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002705 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002706{
Li Zefan34d52cb2011-03-29 13:46:06 +08002707 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002708 struct btrfs_free_space *first = NULL;
2709 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002710 struct btrfs_free_space *last;
2711 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04002712 u64 window_free;
2713 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002714 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002715
Li Zefan34d52cb2011-03-29 13:46:06 +08002716 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002717 if (!entry)
2718 return -ENOSPC;
2719
2720 /*
2721 * We don't want bitmaps, so just move along until we find a normal
2722 * extent entry.
2723 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002724 while (entry->bitmap || entry->bytes < min_bytes) {
2725 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04002726 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002727 node = rb_next(&entry->offset_index);
2728 if (!node)
2729 return -ENOSPC;
2730 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2731 }
2732
Josef Bacik4e69b592011-03-21 10:11:24 -04002733 window_free = entry->bytes;
2734 max_extent = entry->bytes;
2735 first = entry;
2736 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002737
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002738 for (node = rb_next(&entry->offset_index); node;
2739 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002740 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2741
Josef Bacik86d4a772011-05-25 13:03:16 -04002742 if (entry->bitmap) {
2743 if (list_empty(&entry->list))
2744 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002745 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002746 }
2747
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002748 if (entry->bytes < min_bytes)
2749 continue;
2750
2751 last = entry;
2752 window_free += entry->bytes;
2753 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04002754 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002755 }
2756
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002757 if (window_free < bytes || max_extent < cont1_bytes)
2758 return -ENOSPC;
2759
Josef Bacik4e69b592011-03-21 10:11:24 -04002760 cluster->window_start = first->offset;
2761
2762 node = &first->offset_index;
2763
2764 /*
2765 * now we've found our entries, pull them out of the free space
2766 * cache and put them into the cluster rbtree
2767 */
2768 do {
2769 int ret;
2770
2771 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2772 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002773 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002774 continue;
2775
Li Zefan34d52cb2011-03-29 13:46:06 +08002776 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002777 ret = tree_insert_offset(&cluster->root, entry->offset,
2778 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05002779 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04002780 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04002781 } while (node && entry != last);
2782
2783 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002784 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04002785 return 0;
2786}
2787
2788/*
2789 * This specifically looks for bitmaps that may work in the cluster, we assume
2790 * that we have already failed to find extents that will work.
2791 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002792static noinline int
2793setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2794 struct btrfs_free_cluster *cluster,
2795 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002796 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002797{
Li Zefan34d52cb2011-03-29 13:46:06 +08002798 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002799 struct btrfs_free_space *entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002800 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05002801 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002802
Li Zefan34d52cb2011-03-29 13:46:06 +08002803 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002804 return -ENOSPC;
2805
Josef Bacik86d4a772011-05-25 13:03:16 -04002806 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05002807 * The bitmap that covers offset won't be in the list unless offset
2808 * is just its start offset.
2809 */
2810 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
2811 if (entry->offset != bitmap_offset) {
2812 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
2813 if (entry && list_empty(&entry->list))
2814 list_add(&entry->list, bitmaps);
2815 }
2816
Josef Bacik86d4a772011-05-25 13:03:16 -04002817 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05002818 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04002819 continue;
2820 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002821 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04002822 if (!ret)
2823 return 0;
2824 }
2825
2826 /*
Li Zefan52621cb2011-11-20 07:33:38 -05002827 * The bitmaps list has all the bitmaps that record free space
2828 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04002829 */
Li Zefan52621cb2011-11-20 07:33:38 -05002830 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04002831}
2832
2833/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002834 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002835 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04002836 * We might not find them all in one contiguous area.
2837 *
2838 * returns zero and sets up cluster if things worked out, otherwise
2839 * it returns -enospc
2840 */
Josef Bacik00361582013-08-14 14:02:47 -04002841int btrfs_find_space_cluster(struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002842 struct btrfs_block_group_cache *block_group,
2843 struct btrfs_free_cluster *cluster,
2844 u64 offset, u64 bytes, u64 empty_size)
2845{
Li Zefan34d52cb2011-03-29 13:46:06 +08002846 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04002847 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05002848 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002849 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002850 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002851 int ret;
2852
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002853 /*
2854 * Choose the minimum extent size we'll require for this
2855 * cluster. For SSD_SPREAD, don't allow any fragmentation.
2856 * For metadata, allow allocates with smaller extents. For
2857 * data, keep it dense.
2858 */
Chris Mason451d7582009-06-09 20:28:34 -04002859 if (btrfs_test_opt(root, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002860 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04002861 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002862 cont1_bytes = bytes;
2863 min_bytes = block_group->sectorsize;
2864 } else {
2865 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
2866 min_bytes = block_group->sectorsize;
2867 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002868
Li Zefan34d52cb2011-03-29 13:46:06 +08002869 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002870
2871 /*
2872 * If we know we don't have enough space to make a cluster don't even
2873 * bother doing all the work to try and find one.
2874 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002875 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002876 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002877 return -ENOSPC;
2878 }
2879
Chris Masonfa9c0d792009-04-03 09:47:43 -04002880 spin_lock(&cluster->lock);
2881
2882 /* someone already found a cluster, hooray */
2883 if (cluster->block_group) {
2884 ret = 0;
2885 goto out;
2886 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002887
Josef Bacik3f7de032011-11-10 08:29:20 -05002888 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
2889 min_bytes);
2890
Josef Bacik86d4a772011-05-25 13:03:16 -04002891 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002892 bytes + empty_size,
2893 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04002894 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04002895 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002896 offset, bytes + empty_size,
2897 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04002898
2899 /* Clear our temporary list */
2900 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2901 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04002902
2903 if (!ret) {
2904 atomic_inc(&block_group->count);
2905 list_add_tail(&cluster->block_group_list,
2906 &block_group->cluster_list);
2907 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05002908 } else {
2909 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002910 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002911out:
2912 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002913 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002914
2915 return ret;
2916}
2917
2918/*
2919 * simple code to zero out a cluster
2920 */
2921void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2922{
2923 spin_lock_init(&cluster->lock);
2924 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002925 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002926 cluster->max_size = 0;
2927 INIT_LIST_HEAD(&cluster->block_group_list);
2928 cluster->block_group = NULL;
2929}
2930
Li Zefan7fe1e642011-12-29 14:47:27 +08002931static int do_trimming(struct btrfs_block_group_cache *block_group,
2932 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00002933 u64 reserved_start, u64 reserved_bytes,
2934 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08002935{
2936 struct btrfs_space_info *space_info = block_group->space_info;
2937 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00002938 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08002939 int ret;
2940 int update = 0;
2941 u64 trimmed = 0;
2942
2943 spin_lock(&space_info->lock);
2944 spin_lock(&block_group->lock);
2945 if (!block_group->ro) {
2946 block_group->reserved += reserved_bytes;
2947 space_info->bytes_reserved += reserved_bytes;
2948 update = 1;
2949 }
2950 spin_unlock(&block_group->lock);
2951 spin_unlock(&space_info->lock);
2952
Filipe Manana1edb647b2014-12-08 14:01:12 +00002953 ret = btrfs_discard_extent(fs_info->extent_root,
2954 start, bytes, &trimmed);
Li Zefan7fe1e642011-12-29 14:47:27 +08002955 if (!ret)
2956 *total_trimmed += trimmed;
2957
Filipe Manana55507ce2014-12-01 17:04:09 +00002958 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08002959 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
Filipe Manana55507ce2014-12-01 17:04:09 +00002960 list_del(&trim_entry->list);
2961 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08002962
2963 if (update) {
2964 spin_lock(&space_info->lock);
2965 spin_lock(&block_group->lock);
2966 if (block_group->ro)
2967 space_info->bytes_readonly += reserved_bytes;
2968 block_group->reserved -= reserved_bytes;
2969 space_info->bytes_reserved -= reserved_bytes;
2970 spin_unlock(&space_info->lock);
2971 spin_unlock(&block_group->lock);
2972 }
2973
2974 return ret;
2975}
2976
2977static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
2978 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
Li Dongyangf7039b12011-03-24 10:24:28 +00002979{
Li Zefan34d52cb2011-03-29 13:46:06 +08002980 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08002981 struct btrfs_free_space *entry;
2982 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00002983 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08002984 u64 extent_start;
2985 u64 extent_bytes;
2986 u64 bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00002987
2988 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00002989 struct btrfs_trim_range trim_entry;
2990
2991 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08002992 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002993
Li Zefan34d52cb2011-03-29 13:46:06 +08002994 if (ctl->free_space < minlen) {
2995 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00002996 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00002997 break;
2998 }
2999
Li Zefan34d52cb2011-03-29 13:46:06 +08003000 entry = tree_search_offset(ctl, start, 0, 1);
Li Zefan7fe1e642011-12-29 14:47:27 +08003001 if (!entry) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003002 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003003 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003004 break;
3005 }
3006
Li Zefan7fe1e642011-12-29 14:47:27 +08003007 /* skip bitmaps */
3008 while (entry->bitmap) {
3009 node = rb_next(&entry->offset_index);
3010 if (!node) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003011 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003012 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003013 goto out;
Li Dongyangf7039b12011-03-24 10:24:28 +00003014 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003015 entry = rb_entry(node, struct btrfs_free_space,
3016 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003017 }
3018
Li Zefan7fe1e642011-12-29 14:47:27 +08003019 if (entry->offset >= end) {
3020 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003021 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003022 break;
3023 }
3024
3025 extent_start = entry->offset;
3026 extent_bytes = entry->bytes;
3027 start = max(start, extent_start);
3028 bytes = min(extent_start + extent_bytes, end) - start;
3029 if (bytes < minlen) {
3030 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003031 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003032 goto next;
3033 }
3034
3035 unlink_free_space(ctl, entry);
3036 kmem_cache_free(btrfs_free_space_cachep, entry);
3037
Li Zefan34d52cb2011-03-29 13:46:06 +08003038 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003039 trim_entry.start = extent_start;
3040 trim_entry.bytes = extent_bytes;
3041 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3042 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003043
Li Zefan7fe1e642011-12-29 14:47:27 +08003044 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003045 extent_start, extent_bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003046 if (ret)
3047 break;
3048next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003049 start += bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003050
3051 if (fatal_signal_pending(current)) {
3052 ret = -ERESTARTSYS;
3053 break;
3054 }
3055
3056 cond_resched();
3057 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003058out:
3059 return ret;
3060}
3061
3062static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3063 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3064{
3065 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3066 struct btrfs_free_space *entry;
3067 int ret = 0;
3068 int ret2;
3069 u64 bytes;
3070 u64 offset = offset_to_bitmap(ctl, start);
3071
3072 while (offset < end) {
3073 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003074 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003075
Filipe Manana55507ce2014-12-01 17:04:09 +00003076 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003077 spin_lock(&ctl->tree_lock);
3078
3079 if (ctl->free_space < minlen) {
3080 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003081 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003082 break;
3083 }
3084
3085 entry = tree_search_offset(ctl, offset, 1, 0);
3086 if (!entry) {
3087 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003088 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003089 next_bitmap = true;
3090 goto next;
3091 }
3092
3093 bytes = minlen;
3094 ret2 = search_bitmap(ctl, entry, &start, &bytes);
3095 if (ret2 || start >= end) {
3096 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003097 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003098 next_bitmap = true;
3099 goto next;
3100 }
3101
3102 bytes = min(bytes, end - start);
3103 if (bytes < minlen) {
3104 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003105 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003106 goto next;
3107 }
3108
3109 bitmap_clear_bits(ctl, entry, start, bytes);
3110 if (entry->bytes == 0)
3111 free_bitmap(ctl, entry);
3112
3113 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003114 trim_entry.start = start;
3115 trim_entry.bytes = bytes;
3116 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3117 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003118
3119 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003120 start, bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003121 if (ret)
3122 break;
3123next:
3124 if (next_bitmap) {
3125 offset += BITS_PER_BITMAP * ctl->unit;
3126 } else {
3127 start += bytes;
3128 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3129 offset += BITS_PER_BITMAP * ctl->unit;
3130 }
3131
3132 if (fatal_signal_pending(current)) {
3133 ret = -ERESTARTSYS;
3134 break;
3135 }
3136
3137 cond_resched();
3138 }
3139
3140 return ret;
3141}
3142
3143int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
3144 u64 *trimmed, u64 start, u64 end, u64 minlen)
3145{
3146 int ret;
3147
3148 *trimmed = 0;
3149
Filipe Manana04216822014-11-27 21:14:15 +00003150 spin_lock(&block_group->lock);
3151 if (block_group->removed) {
3152 spin_unlock(&block_group->lock);
3153 return 0;
3154 }
3155 atomic_inc(&block_group->trimming);
3156 spin_unlock(&block_group->lock);
3157
Li Zefan7fe1e642011-12-29 14:47:27 +08003158 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3159 if (ret)
Filipe Manana04216822014-11-27 21:14:15 +00003160 goto out;
Li Zefan7fe1e642011-12-29 14:47:27 +08003161
3162 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
Filipe Manana04216822014-11-27 21:14:15 +00003163out:
3164 spin_lock(&block_group->lock);
3165 if (atomic_dec_and_test(&block_group->trimming) &&
3166 block_group->removed) {
3167 struct extent_map_tree *em_tree;
3168 struct extent_map *em;
3169
3170 spin_unlock(&block_group->lock);
3171
Filipe Mananaa1e7e162014-12-04 15:31:01 +00003172 lock_chunks(block_group->fs_info->chunk_root);
Filipe Manana04216822014-11-27 21:14:15 +00003173 em_tree = &block_group->fs_info->mapping_tree.map_tree;
3174 write_lock(&em_tree->lock);
3175 em = lookup_extent_mapping(em_tree, block_group->key.objectid,
3176 1);
3177 BUG_ON(!em); /* logic error, can't happen */
Filipe Mananaa1e7e162014-12-04 15:31:01 +00003178 /*
3179 * remove_extent_mapping() will delete us from the pinned_chunks
3180 * list, which is protected by the chunk mutex.
3181 */
Filipe Manana04216822014-11-27 21:14:15 +00003182 remove_extent_mapping(em_tree, em);
3183 write_unlock(&em_tree->lock);
Filipe Manana04216822014-11-27 21:14:15 +00003184 unlock_chunks(block_group->fs_info->chunk_root);
3185
3186 /* once for us and once for the tree */
3187 free_extent_map(em);
3188 free_extent_map(em);
Filipe Manana946ddbe2014-12-01 17:04:40 +00003189
3190 /*
3191 * We've left one free space entry and other tasks trimming
3192 * this block group have left 1 entry each one. Free them.
3193 */
3194 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
Filipe Manana04216822014-11-27 21:14:15 +00003195 } else {
3196 spin_unlock(&block_group->lock);
3197 }
Li Dongyangf7039b12011-03-24 10:24:28 +00003198
3199 return ret;
3200}
Li Zefan581bb052011-04-20 10:06:11 +08003201
3202/*
3203 * Find the left-most item in the cache tree, and then return the
3204 * smallest inode number in the item.
3205 *
3206 * Note: the returned inode number may not be the smallest one in
3207 * the tree, if the left-most item is a bitmap.
3208 */
3209u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3210{
3211 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3212 struct btrfs_free_space *entry = NULL;
3213 u64 ino = 0;
3214
3215 spin_lock(&ctl->tree_lock);
3216
3217 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3218 goto out;
3219
3220 entry = rb_entry(rb_first(&ctl->free_space_offset),
3221 struct btrfs_free_space, offset_index);
3222
3223 if (!entry->bitmap) {
3224 ino = entry->offset;
3225
3226 unlink_free_space(ctl, entry);
3227 entry->offset++;
3228 entry->bytes--;
3229 if (!entry->bytes)
3230 kmem_cache_free(btrfs_free_space_cachep, entry);
3231 else
3232 link_free_space(ctl, entry);
3233 } else {
3234 u64 offset = 0;
3235 u64 count = 1;
3236 int ret;
3237
3238 ret = search_bitmap(ctl, entry, &offset, &count);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003239 /* Logic error; Should be empty if it can't find anything */
Josef Bacikb12d6862013-08-26 17:14:08 -04003240 ASSERT(!ret);
Li Zefan581bb052011-04-20 10:06:11 +08003241
3242 ino = offset;
3243 bitmap_clear_bits(ctl, entry, offset, 1);
3244 if (entry->bytes == 0)
3245 free_bitmap(ctl, entry);
3246 }
3247out:
3248 spin_unlock(&ctl->tree_lock);
3249
3250 return ino;
3251}
Li Zefan82d59022011-04-20 10:33:24 +08003252
3253struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3254 struct btrfs_path *path)
3255{
3256 struct inode *inode = NULL;
3257
David Sterba57cdc8d2014-02-05 02:37:48 +01003258 spin_lock(&root->ino_cache_lock);
3259 if (root->ino_cache_inode)
3260 inode = igrab(root->ino_cache_inode);
3261 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003262 if (inode)
3263 return inode;
3264
3265 inode = __lookup_free_space_inode(root, path, 0);
3266 if (IS_ERR(inode))
3267 return inode;
3268
David Sterba57cdc8d2014-02-05 02:37:48 +01003269 spin_lock(&root->ino_cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02003270 if (!btrfs_fs_closing(root->fs_info))
David Sterba57cdc8d2014-02-05 02:37:48 +01003271 root->ino_cache_inode = igrab(inode);
3272 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003273
3274 return inode;
3275}
3276
3277int create_free_ino_inode(struct btrfs_root *root,
3278 struct btrfs_trans_handle *trans,
3279 struct btrfs_path *path)
3280{
3281 return __create_free_space_inode(root, trans, path,
3282 BTRFS_FREE_INO_OBJECTID, 0);
3283}
3284
3285int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3286{
3287 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3288 struct btrfs_path *path;
3289 struct inode *inode;
3290 int ret = 0;
3291 u64 root_gen = btrfs_root_generation(&root->root_item);
3292
Chris Mason4b9465c2011-06-03 09:36:29 -04003293 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
3294 return 0;
3295
Li Zefan82d59022011-04-20 10:33:24 +08003296 /*
3297 * If we're unmounting then just return, since this does a search on the
3298 * normal root and not the commit root and we could deadlock.
3299 */
David Sterba7841cb22011-05-31 18:07:27 +02003300 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08003301 return 0;
3302
3303 path = btrfs_alloc_path();
3304 if (!path)
3305 return 0;
3306
3307 inode = lookup_free_ino_inode(root, path);
3308 if (IS_ERR(inode))
3309 goto out;
3310
3311 if (root_gen != BTRFS_I(inode)->generation)
3312 goto out_put;
3313
3314 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3315
3316 if (ret < 0)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003317 btrfs_err(fs_info,
3318 "failed to load free ino cache for root %llu",
3319 root->root_key.objectid);
Li Zefan82d59022011-04-20 10:33:24 +08003320out_put:
3321 iput(inode);
3322out:
3323 btrfs_free_path(path);
3324 return ret;
3325}
3326
3327int btrfs_write_out_ino_cache(struct btrfs_root *root,
3328 struct btrfs_trans_handle *trans,
Filipe David Borba Manana53645a92013-09-20 14:43:28 +01003329 struct btrfs_path *path,
3330 struct inode *inode)
Li Zefan82d59022011-04-20 10:33:24 +08003331{
3332 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
Li Zefan82d59022011-04-20 10:33:24 +08003333 int ret;
3334
Chris Mason4b9465c2011-06-03 09:36:29 -04003335 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
3336 return 0;
3337
Li Zefan82d59022011-04-20 10:33:24 +08003338 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
Josef Bacikc09544e2011-08-30 10:19:10 -04003339 if (ret) {
3340 btrfs_delalloc_release_metadata(inode, inode->i_size);
3341#ifdef DEBUG
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003342 btrfs_err(root->fs_info,
3343 "failed to write free ino cache for root %llu",
3344 root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04003345#endif
3346 }
Li Zefan82d59022011-04-20 10:33:24 +08003347
Li Zefan82d59022011-04-20 10:33:24 +08003348 return ret;
3349}
Josef Bacik74255aa2013-03-15 09:47:08 -04003350
3351#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003352/*
3353 * Use this if you need to make a bitmap or extent entry specifically, it
3354 * doesn't do any of the merging that add_free_space does, this acts a lot like
3355 * how the free space cache loading stuff works, so you can get really weird
3356 * configurations.
3357 */
3358int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3359 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04003360{
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003361 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3362 struct btrfs_free_space *info = NULL, *bitmap_info;
3363 void *map = NULL;
3364 u64 bytes_added;
3365 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04003366
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003367again:
3368 if (!info) {
3369 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3370 if (!info)
3371 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04003372 }
3373
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003374 if (!bitmap) {
3375 spin_lock(&ctl->tree_lock);
3376 info->offset = offset;
3377 info->bytes = bytes;
3378 ret = link_free_space(ctl, info);
3379 spin_unlock(&ctl->tree_lock);
3380 if (ret)
3381 kmem_cache_free(btrfs_free_space_cachep, info);
3382 return ret;
3383 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003384
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003385 if (!map) {
3386 map = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
3387 if (!map) {
3388 kmem_cache_free(btrfs_free_space_cachep, info);
3389 return -ENOMEM;
3390 }
3391 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003392
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003393 spin_lock(&ctl->tree_lock);
3394 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3395 1, 0);
3396 if (!bitmap_info) {
3397 info->bitmap = map;
3398 map = NULL;
3399 add_new_bitmap(ctl, info, offset);
3400 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01003401 info = NULL;
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003402 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003403
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003404 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
3405 bytes -= bytes_added;
3406 offset += bytes_added;
3407 spin_unlock(&ctl->tree_lock);
3408
3409 if (bytes)
3410 goto again;
3411
Filipe Manana20005522014-08-29 13:35:13 +01003412 if (info)
3413 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003414 if (map)
3415 kfree(map);
3416 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003417}
3418
3419/*
3420 * Checks to see if the given range is in the free space cache. This is really
3421 * just used to check the absence of space, so if there is free space in the
3422 * range at all we will return 1.
3423 */
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003424int test_check_exists(struct btrfs_block_group_cache *cache,
3425 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04003426{
3427 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3428 struct btrfs_free_space *info;
3429 int ret = 0;
3430
3431 spin_lock(&ctl->tree_lock);
3432 info = tree_search_offset(ctl, offset, 0, 0);
3433 if (!info) {
3434 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3435 1, 0);
3436 if (!info)
3437 goto out;
3438 }
3439
3440have_info:
3441 if (info->bitmap) {
3442 u64 bit_off, bit_bytes;
3443 struct rb_node *n;
3444 struct btrfs_free_space *tmp;
3445
3446 bit_off = offset;
3447 bit_bytes = ctl->unit;
3448 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes);
3449 if (!ret) {
3450 if (bit_off == offset) {
3451 ret = 1;
3452 goto out;
3453 } else if (bit_off > offset &&
3454 offset + bytes > bit_off) {
3455 ret = 1;
3456 goto out;
3457 }
3458 }
3459
3460 n = rb_prev(&info->offset_index);
3461 while (n) {
3462 tmp = rb_entry(n, struct btrfs_free_space,
3463 offset_index);
3464 if (tmp->offset + tmp->bytes < offset)
3465 break;
3466 if (offset + bytes < tmp->offset) {
3467 n = rb_prev(&info->offset_index);
3468 continue;
3469 }
3470 info = tmp;
3471 goto have_info;
3472 }
3473
3474 n = rb_next(&info->offset_index);
3475 while (n) {
3476 tmp = rb_entry(n, struct btrfs_free_space,
3477 offset_index);
3478 if (offset + bytes < tmp->offset)
3479 break;
3480 if (tmp->offset + tmp->bytes < offset) {
3481 n = rb_next(&info->offset_index);
3482 continue;
3483 }
3484 info = tmp;
3485 goto have_info;
3486 }
3487
Filipe Manana20005522014-08-29 13:35:13 +01003488 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003489 goto out;
3490 }
3491
3492 if (info->offset == offset) {
3493 ret = 1;
3494 goto out;
3495 }
3496
3497 if (offset > info->offset && offset < info->offset + info->bytes)
3498 ret = 1;
3499out:
3500 spin_unlock(&ctl->tree_lock);
3501 return ret;
3502}
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003503#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */