blob: c51482031edfd34c9d582b7b5868eb4bf1269eb8 [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
Josef Bacika67509c2011-10-05 15:18:58 -0400274struct io_ctl {
275 void *cur, *orig;
276 struct page *page;
277 struct page **pages;
278 struct btrfs_root *root;
279 unsigned long size;
280 int index;
281 int num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400282 unsigned check_crcs:1;
Josef Bacika67509c2011-10-05 15:18:58 -0400283};
284
285static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode,
Miao Xie5349d6c2014-06-19 10:42:49 +0800286 struct btrfs_root *root, int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400287{
Miao Xie5349d6c2014-06-19 10:42:49 +0800288 int num_pages;
289 int check_crcs = 0;
290
David Sterbaed6078f2014-06-05 01:59:57 +0200291 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800292
293 if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID)
294 check_crcs = 1;
295
296 /* Make sure we can fit our crcs into the first page */
297 if (write && check_crcs &&
298 (num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE)
299 return -ENOSPC;
300
Josef Bacika67509c2011-10-05 15:18:58 -0400301 memset(io_ctl, 0, sizeof(struct io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800302
David Sterba31e818f2015-02-20 18:00:26 +0100303 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400304 if (!io_ctl->pages)
305 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800306
307 io_ctl->num_pages = num_pages;
Josef Bacika67509c2011-10-05 15:18:58 -0400308 io_ctl->root = root;
Miao Xie5349d6c2014-06-19 10:42:49 +0800309 io_ctl->check_crcs = check_crcs;
310
Josef Bacika67509c2011-10-05 15:18:58 -0400311 return 0;
312}
313
314static void io_ctl_free(struct io_ctl *io_ctl)
315{
316 kfree(io_ctl->pages);
317}
318
319static void io_ctl_unmap_page(struct io_ctl *io_ctl)
320{
321 if (io_ctl->cur) {
322 kunmap(io_ctl->page);
323 io_ctl->cur = NULL;
324 io_ctl->orig = NULL;
325 }
326}
327
328static void io_ctl_map_page(struct io_ctl *io_ctl, int clear)
329{
Josef Bacikb12d6862013-08-26 17:14:08 -0400330 ASSERT(io_ctl->index < io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400331 io_ctl->page = io_ctl->pages[io_ctl->index++];
332 io_ctl->cur = kmap(io_ctl->page);
333 io_ctl->orig = io_ctl->cur;
334 io_ctl->size = PAGE_CACHE_SIZE;
335 if (clear)
336 memset(io_ctl->cur, 0, PAGE_CACHE_SIZE);
337}
338
339static void io_ctl_drop_pages(struct io_ctl *io_ctl)
340{
341 int i;
342
343 io_ctl_unmap_page(io_ctl);
344
345 for (i = 0; i < io_ctl->num_pages; i++) {
Li Zefana1ee5a42012-01-09 14:27:42 +0800346 if (io_ctl->pages[i]) {
347 ClearPageChecked(io_ctl->pages[i]);
348 unlock_page(io_ctl->pages[i]);
349 page_cache_release(io_ctl->pages[i]);
350 }
Josef Bacika67509c2011-10-05 15:18:58 -0400351 }
352}
353
354static int io_ctl_prepare_pages(struct io_ctl *io_ctl, struct inode *inode,
355 int uptodate)
356{
357 struct page *page;
358 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
359 int i;
360
361 for (i = 0; i < io_ctl->num_pages; i++) {
362 page = find_or_create_page(inode->i_mapping, i, mask);
363 if (!page) {
364 io_ctl_drop_pages(io_ctl);
365 return -ENOMEM;
366 }
367 io_ctl->pages[i] = page;
368 if (uptodate && !PageUptodate(page)) {
369 btrfs_readpage(NULL, page);
370 lock_page(page);
371 if (!PageUptodate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500372 btrfs_err(BTRFS_I(inode)->root->fs_info,
373 "error reading free space cache");
Josef Bacika67509c2011-10-05 15:18:58 -0400374 io_ctl_drop_pages(io_ctl);
375 return -EIO;
376 }
377 }
378 }
379
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500380 for (i = 0; i < io_ctl->num_pages; i++) {
381 clear_page_dirty_for_io(io_ctl->pages[i]);
382 set_page_extent_mapped(io_ctl->pages[i]);
383 }
384
Josef Bacika67509c2011-10-05 15:18:58 -0400385 return 0;
386}
387
388static void io_ctl_set_generation(struct io_ctl *io_ctl, u64 generation)
389{
Al Viro528c0322012-04-13 11:03:55 -0400390 __le64 *val;
Josef Bacika67509c2011-10-05 15:18:58 -0400391
392 io_ctl_map_page(io_ctl, 1);
393
394 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400395 * Skip the csum areas. If we don't check crcs then we just have a
396 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400397 */
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400398 if (io_ctl->check_crcs) {
399 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
400 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
401 } else {
402 io_ctl->cur += sizeof(u64);
403 io_ctl->size -= sizeof(u64) * 2;
404 }
Josef Bacika67509c2011-10-05 15:18:58 -0400405
406 val = io_ctl->cur;
407 *val = cpu_to_le64(generation);
408 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400409}
410
411static int io_ctl_check_generation(struct io_ctl *io_ctl, u64 generation)
412{
Al Viro528c0322012-04-13 11:03:55 -0400413 __le64 *gen;
Josef Bacika67509c2011-10-05 15:18:58 -0400414
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400415 /*
416 * Skip the crc area. If we don't check crcs then we just have a 64bit
417 * chunk at the front of the first page.
418 */
419 if (io_ctl->check_crcs) {
420 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
421 io_ctl->size -= sizeof(u64) +
422 (sizeof(u32) * io_ctl->num_pages);
423 } else {
424 io_ctl->cur += sizeof(u64);
425 io_ctl->size -= sizeof(u64) * 2;
426 }
Josef Bacika67509c2011-10-05 15:18:58 -0400427
Josef Bacika67509c2011-10-05 15:18:58 -0400428 gen = io_ctl->cur;
429 if (le64_to_cpu(*gen) != generation) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500430 printk_ratelimited(KERN_ERR "BTRFS: space cache generation "
Josef Bacika67509c2011-10-05 15:18:58 -0400431 "(%Lu) does not match inode (%Lu)\n", *gen,
432 generation);
433 io_ctl_unmap_page(io_ctl);
434 return -EIO;
435 }
436 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400437 return 0;
438}
439
440static void io_ctl_set_crc(struct io_ctl *io_ctl, int index)
441{
442 u32 *tmp;
443 u32 crc = ~(u32)0;
444 unsigned offset = 0;
445
446 if (!io_ctl->check_crcs) {
447 io_ctl_unmap_page(io_ctl);
448 return;
449 }
450
451 if (index == 0)
Justin P. Mattockcb54f252011-11-21 08:43:28 -0800452 offset = sizeof(u32) * io_ctl->num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400453
Liu Bob0496682013-03-14 14:57:45 +0000454 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400455 PAGE_CACHE_SIZE - offset);
456 btrfs_csum_final(crc, (char *)&crc);
457 io_ctl_unmap_page(io_ctl);
458 tmp = kmap(io_ctl->pages[0]);
459 tmp += index;
460 *tmp = crc;
461 kunmap(io_ctl->pages[0]);
462}
463
464static int io_ctl_check_crc(struct io_ctl *io_ctl, int index)
465{
466 u32 *tmp, val;
467 u32 crc = ~(u32)0;
468 unsigned offset = 0;
469
470 if (!io_ctl->check_crcs) {
471 io_ctl_map_page(io_ctl, 0);
472 return 0;
473 }
474
475 if (index == 0)
476 offset = sizeof(u32) * io_ctl->num_pages;
477
478 tmp = kmap(io_ctl->pages[0]);
479 tmp += index;
480 val = *tmp;
481 kunmap(io_ctl->pages[0]);
482
483 io_ctl_map_page(io_ctl, 0);
Liu Bob0496682013-03-14 14:57:45 +0000484 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400485 PAGE_CACHE_SIZE - offset);
486 btrfs_csum_final(crc, (char *)&crc);
487 if (val != crc) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500488 printk_ratelimited(KERN_ERR "BTRFS: csum mismatch on free "
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400489 "space cache\n");
490 io_ctl_unmap_page(io_ctl);
491 return -EIO;
492 }
493
Josef Bacika67509c2011-10-05 15:18:58 -0400494 return 0;
495}
496
497static int io_ctl_add_entry(struct io_ctl *io_ctl, u64 offset, u64 bytes,
498 void *bitmap)
499{
500 struct btrfs_free_space_entry *entry;
501
502 if (!io_ctl->cur)
503 return -ENOSPC;
504
505 entry = io_ctl->cur;
506 entry->offset = cpu_to_le64(offset);
507 entry->bytes = cpu_to_le64(bytes);
508 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
509 BTRFS_FREE_SPACE_EXTENT;
510 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
511 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
512
513 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
514 return 0;
515
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400516 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400517
518 /* No more pages to map */
519 if (io_ctl->index >= io_ctl->num_pages)
520 return 0;
521
522 /* map the next page */
523 io_ctl_map_page(io_ctl, 1);
524 return 0;
525}
526
527static int io_ctl_add_bitmap(struct io_ctl *io_ctl, void *bitmap)
528{
529 if (!io_ctl->cur)
530 return -ENOSPC;
531
532 /*
533 * If we aren't at the start of the current page, unmap this one and
534 * map the next one if there is any left.
535 */
536 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400537 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400538 if (io_ctl->index >= io_ctl->num_pages)
539 return -ENOSPC;
540 io_ctl_map_page(io_ctl, 0);
541 }
542
543 memcpy(io_ctl->cur, bitmap, PAGE_CACHE_SIZE);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400544 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400545 if (io_ctl->index < io_ctl->num_pages)
546 io_ctl_map_page(io_ctl, 0);
547 return 0;
548}
549
550static void io_ctl_zero_remaining_pages(struct io_ctl *io_ctl)
551{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400552 /*
553 * If we're not on the boundary we know we've modified the page and we
554 * need to crc the page.
555 */
556 if (io_ctl->cur != io_ctl->orig)
557 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
558 else
559 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400560
561 while (io_ctl->index < io_ctl->num_pages) {
562 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400563 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400564 }
565}
566
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400567static int io_ctl_read_entry(struct io_ctl *io_ctl,
568 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400569{
570 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500571 int ret;
572
573 if (!io_ctl->cur) {
574 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
575 if (ret)
576 return ret;
577 }
Josef Bacika67509c2011-10-05 15:18:58 -0400578
579 e = io_ctl->cur;
580 entry->offset = le64_to_cpu(e->offset);
581 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400582 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400583 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
584 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
585
586 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400587 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400588
589 io_ctl_unmap_page(io_ctl);
590
Josef Bacik2f120c02011-11-10 20:45:05 -0500591 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400592}
593
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400594static int io_ctl_read_bitmap(struct io_ctl *io_ctl,
595 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400596{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400597 int ret;
598
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400599 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
600 if (ret)
601 return ret;
602
Josef Bacika67509c2011-10-05 15:18:58 -0400603 memcpy(entry->bitmap, io_ctl->cur, PAGE_CACHE_SIZE);
604 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400605
606 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400607}
608
Josef Bacikcd023e72012-05-14 10:06:40 -0400609/*
610 * Since we attach pinned extents after the fact we can have contiguous sections
611 * of free space that are split up in entries. This poses a problem with the
612 * tree logging stuff since it could have allocated across what appears to be 2
613 * entries since we would have merged the entries when adding the pinned extents
614 * back to the free space cache. So run through the space cache that we just
615 * loaded and merge contiguous entries. This will make the log replay stuff not
616 * blow up and it will make for nicer allocator behavior.
617 */
618static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
619{
620 struct btrfs_free_space *e, *prev = NULL;
621 struct rb_node *n;
622
623again:
624 spin_lock(&ctl->tree_lock);
625 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
626 e = rb_entry(n, struct btrfs_free_space, offset_index);
627 if (!prev)
628 goto next;
629 if (e->bitmap || prev->bitmap)
630 goto next;
631 if (prev->offset + prev->bytes == e->offset) {
632 unlink_free_space(ctl, prev);
633 unlink_free_space(ctl, e);
634 prev->bytes += e->bytes;
635 kmem_cache_free(btrfs_free_space_cachep, e);
636 link_free_space(ctl, prev);
637 prev = NULL;
638 spin_unlock(&ctl->tree_lock);
639 goto again;
640 }
641next:
642 prev = e;
643 }
644 spin_unlock(&ctl->tree_lock);
645}
646
Eric Sandeen48a3b632013-04-25 20:41:01 +0000647static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
648 struct btrfs_free_space_ctl *ctl,
649 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400650{
Josef Bacik9d66e232010-08-25 16:54:15 -0400651 struct btrfs_free_space_header *header;
652 struct extent_buffer *leaf;
Josef Bacika67509c2011-10-05 15:18:58 -0400653 struct io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400654 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400655 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800656 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400657 u64 num_entries;
658 u64 num_bitmaps;
659 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400660 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400661 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400662
Josef Bacik9d66e232010-08-25 16:54:15 -0400663 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800664 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400665 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400666
667 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800668 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400669 key.type = 0;
670
671 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800672 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400673 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800674 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400675 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400676 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400677 }
678
Li Zefan0414efa2011-04-20 10:20:14 +0800679 ret = -1;
680
Josef Bacik9d66e232010-08-25 16:54:15 -0400681 leaf = path->nodes[0];
682 header = btrfs_item_ptr(leaf, path->slots[0],
683 struct btrfs_free_space_header);
684 num_entries = btrfs_free_space_entries(leaf, header);
685 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
686 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400687 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400688
Miao Xiee570fd22014-06-19 10:42:50 +0800689 if (!BTRFS_I(inode)->generation) {
690 btrfs_info(root->fs_info,
691 "The free space cache file (%llu) is invalid. skip it\n",
692 offset);
693 return 0;
694 }
695
Josef Bacik9d66e232010-08-25 16:54:15 -0400696 if (BTRFS_I(inode)->generation != generation) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000697 btrfs_err(root->fs_info,
698 "free space inode generation (%llu) "
699 "did not match free space cache generation (%llu)",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200700 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400701 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400702 }
703
704 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400705 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400706
Miao Xie5349d6c2014-06-19 10:42:49 +0800707 ret = io_ctl_init(&io_ctl, inode, root, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800708 if (ret)
709 return ret;
710
Josef Bacik9d66e232010-08-25 16:54:15 -0400711 ret = readahead_cache(inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800712 if (ret)
Josef Bacik9d66e232010-08-25 16:54:15 -0400713 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400714
Josef Bacika67509c2011-10-05 15:18:58 -0400715 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
716 if (ret)
717 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400718
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400719 ret = io_ctl_check_crc(&io_ctl, 0);
720 if (ret)
721 goto free_cache;
722
Josef Bacika67509c2011-10-05 15:18:58 -0400723 ret = io_ctl_check_generation(&io_ctl, generation);
724 if (ret)
725 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400726
Josef Bacika67509c2011-10-05 15:18:58 -0400727 while (num_entries) {
728 e = kmem_cache_zalloc(btrfs_free_space_cachep,
729 GFP_NOFS);
730 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400731 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400732
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400733 ret = io_ctl_read_entry(&io_ctl, e, &type);
734 if (ret) {
735 kmem_cache_free(btrfs_free_space_cachep, e);
736 goto free_cache;
737 }
738
Josef Bacika67509c2011-10-05 15:18:58 -0400739 if (!e->bytes) {
740 kmem_cache_free(btrfs_free_space_cachep, e);
741 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400742 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400743
Josef Bacika67509c2011-10-05 15:18:58 -0400744 if (type == BTRFS_FREE_SPACE_EXTENT) {
745 spin_lock(&ctl->tree_lock);
746 ret = link_free_space(ctl, e);
747 spin_unlock(&ctl->tree_lock);
748 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000749 btrfs_err(root->fs_info,
750 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500751 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400752 goto free_cache;
753 }
Josef Bacika67509c2011-10-05 15:18:58 -0400754 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400755 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400756 num_bitmaps--;
757 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
758 if (!e->bitmap) {
759 kmem_cache_free(
760 btrfs_free_space_cachep, e);
761 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400762 }
Josef Bacika67509c2011-10-05 15:18:58 -0400763 spin_lock(&ctl->tree_lock);
764 ret = link_free_space(ctl, e);
765 ctl->total_bitmaps++;
766 ctl->op->recalc_thresholds(ctl);
767 spin_unlock(&ctl->tree_lock);
768 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000769 btrfs_err(root->fs_info,
770 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400771 kmem_cache_free(btrfs_free_space_cachep, e);
772 goto free_cache;
773 }
774 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400775 }
776
Josef Bacika67509c2011-10-05 15:18:58 -0400777 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400778 }
779
Josef Bacik2f120c02011-11-10 20:45:05 -0500780 io_ctl_unmap_page(&io_ctl);
781
Josef Bacika67509c2011-10-05 15:18:58 -0400782 /*
783 * We add the bitmaps at the end of the entries in order that
784 * the bitmap entries are added to the cache.
785 */
786 list_for_each_entry_safe(e, n, &bitmaps, list) {
787 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400788 ret = io_ctl_read_bitmap(&io_ctl, e);
789 if (ret)
790 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400791 }
792
793 io_ctl_drop_pages(&io_ctl);
Josef Bacikcd023e72012-05-14 10:06:40 -0400794 merge_space_tree(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400795 ret = 1;
796out:
Josef Bacika67509c2011-10-05 15:18:58 -0400797 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400798 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400799free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400800 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800801 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400802 goto out;
803}
804
Li Zefan0414efa2011-04-20 10:20:14 +0800805int load_free_space_cache(struct btrfs_fs_info *fs_info,
806 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400807{
Li Zefan34d52cb2011-03-29 13:46:06 +0800808 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800809 struct btrfs_root *root = fs_info->tree_root;
810 struct inode *inode;
811 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400812 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800813 bool matched;
814 u64 used = btrfs_block_group_used(&block_group->item);
815
816 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800817 * If this block group has been marked to be cleared for one reason or
818 * another then we can't trust the on disk cache, so just return.
819 */
820 spin_lock(&block_group->lock);
821 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
822 spin_unlock(&block_group->lock);
823 return 0;
824 }
825 spin_unlock(&block_group->lock);
826
827 path = btrfs_alloc_path();
828 if (!path)
829 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400830 path->search_commit_root = 1;
831 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800832
833 inode = lookup_free_space_inode(root, block_group, path);
834 if (IS_ERR(inode)) {
835 btrfs_free_path(path);
836 return 0;
837 }
838
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400839 /* We may have converted the inode and made the cache invalid. */
840 spin_lock(&block_group->lock);
841 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
842 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900843 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400844 goto out;
845 }
846 spin_unlock(&block_group->lock);
847
Li Zefan0414efa2011-04-20 10:20:14 +0800848 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
849 path, block_group->key.objectid);
850 btrfs_free_path(path);
851 if (ret <= 0)
852 goto out;
853
854 spin_lock(&ctl->tree_lock);
855 matched = (ctl->free_space == (block_group->key.offset - used -
856 block_group->bytes_super));
857 spin_unlock(&ctl->tree_lock);
858
859 if (!matched) {
860 __btrfs_remove_free_space_cache(ctl);
Miao Xie32d6b472014-04-24 13:31:55 +0800861 btrfs_warn(fs_info, "block group %llu has wrong amount of free space",
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000862 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800863 ret = -1;
864 }
865out:
866 if (ret < 0) {
867 /* This cache is bogus, make sure it gets cleared */
868 spin_lock(&block_group->lock);
869 block_group->disk_cache_state = BTRFS_DC_CLEAR;
870 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800871 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800872
Miao Xie32d6b472014-04-24 13:31:55 +0800873 btrfs_warn(fs_info, "failed to load free space cache for block group %llu, rebuild it now",
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000874 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800875 }
876
877 iput(inode);
878 return ret;
879}
880
Chris Masond4452bc2014-05-19 20:47:56 -0700881static noinline_for_stack
882int write_cache_extent_entries(struct io_ctl *io_ctl,
883 struct btrfs_free_space_ctl *ctl,
884 struct btrfs_block_group_cache *block_group,
885 int *entries, int *bitmaps,
886 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400887{
Josef Bacikc09544e2011-08-30 10:19:10 -0400888 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -0700889 struct btrfs_free_cluster *cluster = NULL;
890 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +0000891 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400892
Josef Bacik43be2142011-04-01 14:55:00 +0000893 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -0700894 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000895 cluster = list_entry(block_group->cluster_list.next,
896 struct btrfs_free_cluster,
897 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -0700898 }
Josef Bacik43be2142011-04-01 14:55:00 +0000899
Josef Bacikf75b1302011-10-05 10:00:18 -0400900 if (!node && cluster) {
901 node = rb_first(&cluster->root);
902 cluster = NULL;
903 }
904
Josef Bacik0cb59c92010-07-02 12:14:14 -0400905 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400906 while (node) {
907 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400908
Josef Bacika67509c2011-10-05 15:18:58 -0400909 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -0700910 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +0000911
Chris Masond4452bc2014-05-19 20:47:56 -0700912 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400913 e->bitmap);
914 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -0700915 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -0400916
917 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -0700918 list_add_tail(&e->list, bitmap_list);
919 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400920 }
921 node = rb_next(node);
922 if (!node && cluster) {
923 node = rb_first(&cluster->root);
924 cluster = NULL;
925 }
926 }
Filipe Manana55507ce2014-12-01 17:04:09 +0000927
928 /*
929 * Make sure we don't miss any range that was removed from our rbtree
930 * because trimming is running. Otherwise after a umount+mount (or crash
931 * after committing the transaction) we would leak free space and get
932 * an inconsistent free space cache report from fsck.
933 */
934 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
935 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
936 trim_entry->bytes, NULL);
937 if (ret)
938 goto fail;
939 *entries += 1;
940 }
941
Chris Masond4452bc2014-05-19 20:47:56 -0700942 return 0;
943fail:
944 return -ENOSPC;
945}
946
947static noinline_for_stack int
948update_cache_item(struct btrfs_trans_handle *trans,
949 struct btrfs_root *root,
950 struct inode *inode,
951 struct btrfs_path *path, u64 offset,
952 int entries, int bitmaps)
953{
954 struct btrfs_key key;
955 struct btrfs_free_space_header *header;
956 struct extent_buffer *leaf;
957 int ret;
958
959 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
960 key.offset = offset;
961 key.type = 0;
962
963 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
964 if (ret < 0) {
965 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
966 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
967 GFP_NOFS);
968 goto fail;
969 }
970 leaf = path->nodes[0];
971 if (ret > 0) {
972 struct btrfs_key found_key;
973 ASSERT(path->slots[0]);
974 path->slots[0]--;
975 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
976 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
977 found_key.offset != offset) {
978 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
979 inode->i_size - 1,
980 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
981 NULL, GFP_NOFS);
982 btrfs_release_path(path);
983 goto fail;
984 }
985 }
986
987 BTRFS_I(inode)->generation = trans->transid;
988 header = btrfs_item_ptr(leaf, path->slots[0],
989 struct btrfs_free_space_header);
990 btrfs_set_free_space_entries(leaf, header, entries);
991 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
992 btrfs_set_free_space_generation(leaf, header, trans->transid);
993 btrfs_mark_buffer_dirty(leaf);
994 btrfs_release_path(path);
995
996 return 0;
997
998fail:
999 return -1;
1000}
1001
1002static noinline_for_stack int
Miao Xie5349d6c2014-06-19 10:42:49 +08001003write_pinned_extent_entries(struct btrfs_root *root,
1004 struct btrfs_block_group_cache *block_group,
1005 struct io_ctl *io_ctl,
1006 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -07001007{
1008 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -07001009 struct extent_io_tree *unpin = NULL;
1010 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001011
Miao Xie5349d6c2014-06-19 10:42:49 +08001012 if (!block_group)
1013 return 0;
1014
Josef Bacika67509c2011-10-05 15:18:58 -04001015 /*
1016 * We want to add any pinned extents to our free space cache
1017 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001018 *
Li Zefandb804f22012-01-10 16:41:01 +08001019 * We shouldn't have switched the pinned extents yet so this is the
1020 * right one
1021 */
1022 unpin = root->fs_info->pinned_extents;
1023
Miao Xie5349d6c2014-06-19 10:42:49 +08001024 start = block_group->key.objectid;
Li Zefandb804f22012-01-10 16:41:01 +08001025
Miao Xie5349d6c2014-06-19 10:42:49 +08001026 while (start < block_group->key.objectid + block_group->key.offset) {
Li Zefandb804f22012-01-10 16:41:01 +08001027 ret = find_first_extent_bit(unpin, start,
1028 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001029 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001030 if (ret)
1031 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001032
Josef Bacika67509c2011-10-05 15:18:58 -04001033 /* This pinned extent is out of our range */
Li Zefandb804f22012-01-10 16:41:01 +08001034 if (extent_start >= block_group->key.objectid +
Josef Bacika67509c2011-10-05 15:18:58 -04001035 block_group->key.offset)
Miao Xie5349d6c2014-06-19 10:42:49 +08001036 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001037
Li Zefandb804f22012-01-10 16:41:01 +08001038 extent_start = max(extent_start, start);
1039 extent_end = min(block_group->key.objectid +
1040 block_group->key.offset, extent_end + 1);
1041 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001042
Chris Masond4452bc2014-05-19 20:47:56 -07001043 *entries += 1;
1044 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001045 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001046 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001047
Li Zefandb804f22012-01-10 16:41:01 +08001048 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001049 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001050
Miao Xie5349d6c2014-06-19 10:42:49 +08001051 return 0;
1052}
1053
1054static noinline_for_stack int
1055write_bitmap_entries(struct io_ctl *io_ctl, struct list_head *bitmap_list)
1056{
1057 struct list_head *pos, *n;
1058 int ret;
1059
Josef Bacik0cb59c92010-07-02 12:14:14 -04001060 /* Write out the bitmaps */
Chris Masond4452bc2014-05-19 20:47:56 -07001061 list_for_each_safe(pos, n, bitmap_list) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001062 struct btrfs_free_space *entry =
1063 list_entry(pos, struct btrfs_free_space, list);
1064
Chris Masond4452bc2014-05-19 20:47:56 -07001065 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001066 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001067 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001068 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001069 }
1070
Miao Xie5349d6c2014-06-19 10:42:49 +08001071 return 0;
1072}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001073
Miao Xie5349d6c2014-06-19 10:42:49 +08001074static int flush_dirty_cache(struct inode *inode)
1075{
1076 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001077
Josef Bacik0ef8b722013-10-25 16:13:35 -04001078 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001079 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001080 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1081 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
1082 GFP_NOFS);
Chris Masond4452bc2014-05-19 20:47:56 -07001083
Miao Xie5349d6c2014-06-19 10:42:49 +08001084 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001085}
1086
1087static void noinline_for_stack
1088cleanup_write_cache_enospc(struct inode *inode,
1089 struct io_ctl *io_ctl,
1090 struct extent_state **cached_state,
1091 struct list_head *bitmap_list)
1092{
1093 struct list_head *pos, *n;
Miao Xie5349d6c2014-06-19 10:42:49 +08001094
Chris Masond4452bc2014-05-19 20:47:56 -07001095 list_for_each_safe(pos, n, bitmap_list) {
1096 struct btrfs_free_space *entry =
1097 list_entry(pos, struct btrfs_free_space, list);
1098 list_del_init(&entry->list);
1099 }
1100 io_ctl_drop_pages(io_ctl);
1101 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1102 i_size_read(inode) - 1, cached_state,
1103 GFP_NOFS);
1104}
1105
1106/**
1107 * __btrfs_write_out_cache - write out cached info to an inode
1108 * @root - the root the inode belongs to
1109 * @ctl - the free space cache we are going to write out
1110 * @block_group - the block_group for this cache if it belongs to a block_group
1111 * @trans - the trans handle
1112 * @path - the path to use
1113 * @offset - the offset for the key we'll insert
1114 *
1115 * This function writes out a free space cache struct to disk for quick recovery
1116 * on mount. This will return 0 if it was successfull in writing the cache out,
1117 * and -1 if it was not.
1118 */
1119static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1120 struct btrfs_free_space_ctl *ctl,
1121 struct btrfs_block_group_cache *block_group,
1122 struct btrfs_trans_handle *trans,
1123 struct btrfs_path *path, u64 offset)
1124{
1125 struct extent_state *cached_state = NULL;
1126 struct io_ctl io_ctl;
Miao Xie5349d6c2014-06-19 10:42:49 +08001127 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001128 int entries = 0;
1129 int bitmaps = 0;
1130 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001131
1132 if (!i_size_read(inode))
1133 return -1;
1134
Miao Xie5349d6c2014-06-19 10:42:49 +08001135 ret = io_ctl_init(&io_ctl, inode, root, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001136 if (ret)
1137 return -1;
1138
Miao Xiee570fd22014-06-19 10:42:50 +08001139 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1140 down_write(&block_group->data_rwsem);
1141 spin_lock(&block_group->lock);
1142 if (block_group->delalloc_bytes) {
1143 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1144 spin_unlock(&block_group->lock);
1145 up_write(&block_group->data_rwsem);
1146 BTRFS_I(inode)->generation = 0;
1147 ret = 0;
1148 goto out;
1149 }
1150 spin_unlock(&block_group->lock);
1151 }
1152
Chris Masond4452bc2014-05-19 20:47:56 -07001153 /* Lock all pages first so we can lock the extent safely. */
1154 io_ctl_prepare_pages(&io_ctl, inode, 0);
1155
1156 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
1157 0, &cached_state);
1158
Chris Masond4452bc2014-05-19 20:47:56 -07001159 io_ctl_set_generation(&io_ctl, trans->transid);
1160
Filipe Manana55507ce2014-12-01 17:04:09 +00001161 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001162 /* Write out the extent entries in the free space cache */
Chris Masond4452bc2014-05-19 20:47:56 -07001163 ret = write_cache_extent_entries(&io_ctl, ctl,
1164 block_group, &entries, &bitmaps,
1165 &bitmap_list);
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 }
Chris Masond4452bc2014-05-19 20:47:56 -07001170
Miao Xie5349d6c2014-06-19 10:42:49 +08001171 /*
1172 * Some spaces that are freed in the current transaction are pinned,
1173 * they will be added into free space cache after the transaction is
1174 * committed, we shouldn't lose them.
1175 */
1176 ret = write_pinned_extent_entries(root, block_group, &io_ctl, &entries);
Filipe Manana55507ce2014-12-01 17:04:09 +00001177 if (ret) {
1178 mutex_unlock(&ctl->cache_writeout_mutex);
Chris Masond4452bc2014-05-19 20:47:56 -07001179 goto out_nospc;
Filipe Manana55507ce2014-12-01 17:04:09 +00001180 }
Miao Xie5349d6c2014-06-19 10:42:49 +08001181
Filipe Manana55507ce2014-12-01 17:04:09 +00001182 /*
1183 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1184 * locked while doing it because a concurrent trim can be manipulating
1185 * or freeing the bitmap.
1186 */
Miao Xie5349d6c2014-06-19 10:42:49 +08001187 ret = write_bitmap_entries(&io_ctl, &bitmap_list);
Filipe Manana55507ce2014-12-01 17:04:09 +00001188 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001189 if (ret)
1190 goto out_nospc;
1191
1192 /* Zero out the rest of the pages just to make sure */
1193 io_ctl_zero_remaining_pages(&io_ctl);
1194
1195 /* Everything is written out, now we dirty the pages in the file. */
1196 ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages,
1197 0, i_size_read(inode), &cached_state);
1198 if (ret)
1199 goto out_nospc;
1200
Miao Xiee570fd22014-06-19 10:42:50 +08001201 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1202 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001203 /*
1204 * Release the pages and unlock the extent, we will flush
1205 * them out later
1206 */
1207 io_ctl_drop_pages(&io_ctl);
1208
1209 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1210 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
1211
1212 /* Flush the dirty pages in the cache file. */
1213 ret = flush_dirty_cache(inode);
1214 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001215 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001216
Miao Xie5349d6c2014-06-19 10:42:49 +08001217 /* Update the cache item to tell everyone this cache file is valid. */
1218 ret = update_cache_item(trans, root, inode, path, offset,
Chris Masond4452bc2014-05-19 20:47:56 -07001219 entries, bitmaps);
Josef Bacik2f356122011-06-10 15:31:13 -04001220out:
Josef Bacika67509c2011-10-05 15:18:58 -04001221 io_ctl_free(&io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001222 if (ret) {
Josef Bacika67509c2011-10-05 15:18:58 -04001223 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001224 BTRFS_I(inode)->generation = 0;
1225 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001226 btrfs_update_inode(trans, root, inode);
Miao Xie5349d6c2014-06-19 10:42:49 +08001227 return ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001228
1229out_nospc:
Chris Masond4452bc2014-05-19 20:47:56 -07001230 cleanup_write_cache_enospc(inode, &io_ctl, &cached_state, &bitmap_list);
Miao Xiee570fd22014-06-19 10:42:50 +08001231
1232 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1233 up_write(&block_group->data_rwsem);
1234
Josef Bacika67509c2011-10-05 15:18:58 -04001235 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001236}
1237
1238int btrfs_write_out_cache(struct btrfs_root *root,
1239 struct btrfs_trans_handle *trans,
1240 struct btrfs_block_group_cache *block_group,
1241 struct btrfs_path *path)
1242{
1243 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1244 struct inode *inode;
1245 int ret = 0;
Josef Bacikce93ec52014-11-17 15:45:48 -05001246 enum btrfs_disk_cache_state dcs = BTRFS_DC_WRITTEN;
Li Zefan0414efa2011-04-20 10:20:14 +08001247
1248 root = root->fs_info->tree_root;
1249
1250 spin_lock(&block_group->lock);
1251 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1252 spin_unlock(&block_group->lock);
1253 return 0;
1254 }
Miao Xiee570fd22014-06-19 10:42:50 +08001255
1256 if (block_group->delalloc_bytes) {
1257 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1258 spin_unlock(&block_group->lock);
1259 return 0;
1260 }
Li Zefan0414efa2011-04-20 10:20:14 +08001261 spin_unlock(&block_group->lock);
1262
1263 inode = lookup_free_space_inode(root, block_group, path);
1264 if (IS_ERR(inode))
1265 return 0;
1266
1267 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
1268 path, block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001269 if (ret) {
Josef Bacikce93ec52014-11-17 15:45:48 -05001270 dcs = BTRFS_DC_ERROR;
Li Zefan82d59022011-04-20 10:33:24 +08001271 ret = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -04001272#ifdef DEBUG
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00001273 btrfs_err(root->fs_info,
1274 "failed to write free space cache for block group %llu",
1275 block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001276#endif
Li Zefan0414efa2011-04-20 10:20:14 +08001277 }
1278
Josef Bacikce93ec52014-11-17 15:45:48 -05001279 spin_lock(&block_group->lock);
1280 block_group->disk_cache_state = dcs;
1281 spin_unlock(&block_group->lock);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001282 iput(inode);
1283 return ret;
1284}
1285
Li Zefan34d52cb2011-03-29 13:46:06 +08001286static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001287 u64 offset)
1288{
Josef Bacikb12d6862013-08-26 17:14:08 -04001289 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001290 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001291 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001292}
1293
Li Zefan34d52cb2011-03-29 13:46:06 +08001294static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001295{
Li Zefan34d52cb2011-03-29 13:46:06 +08001296 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001297}
1298
Li Zefan34d52cb2011-03-29 13:46:06 +08001299static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001300 u64 offset)
1301{
1302 u64 bitmap_start;
David Sterbab8b93ad2015-01-16 17:26:13 +01001303 u32 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001304
Li Zefan34d52cb2011-03-29 13:46:06 +08001305 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1306 bitmap_start = offset - ctl->start;
David Sterbab8b93ad2015-01-16 17:26:13 +01001307 bitmap_start = div_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001308 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001309 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001310
1311 return bitmap_start;
1312}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001313
1314static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001315 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001316{
1317 struct rb_node **p = &root->rb_node;
1318 struct rb_node *parent = NULL;
1319 struct btrfs_free_space *info;
1320
1321 while (*p) {
1322 parent = *p;
1323 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1324
Josef Bacik96303082009-07-13 21:29:25 -04001325 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001326 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001327 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001328 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001329 } else {
1330 /*
1331 * we could have a bitmap entry and an extent entry
1332 * share the same offset. If this is the case, we want
1333 * the extent entry to always be found first if we do a
1334 * linear search through the tree, since we want to have
1335 * the quickest allocation time, and allocating from an
1336 * extent is faster than allocating from a bitmap. So
1337 * if we're inserting a bitmap and we find an entry at
1338 * this offset, we want to go right, or after this entry
1339 * logically. If we are inserting an extent and we've
1340 * found a bitmap, we want to go left, or before
1341 * logically.
1342 */
1343 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001344 if (info->bitmap) {
1345 WARN_ON_ONCE(1);
1346 return -EEXIST;
1347 }
Josef Bacik96303082009-07-13 21:29:25 -04001348 p = &(*p)->rb_right;
1349 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001350 if (!info->bitmap) {
1351 WARN_ON_ONCE(1);
1352 return -EEXIST;
1353 }
Josef Bacik96303082009-07-13 21:29:25 -04001354 p = &(*p)->rb_left;
1355 }
1356 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001357 }
1358
1359 rb_link_node(node, parent, p);
1360 rb_insert_color(node, root);
1361
1362 return 0;
1363}
1364
1365/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001366 * searches the tree for the given offset.
1367 *
Josef Bacik96303082009-07-13 21:29:25 -04001368 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1369 * want a section that has at least bytes size and comes at or after the given
1370 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001371 */
Josef Bacik96303082009-07-13 21:29:25 -04001372static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001373tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001374 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001375{
Li Zefan34d52cb2011-03-29 13:46:06 +08001376 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001377 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001378
Josef Bacik96303082009-07-13 21:29:25 -04001379 /* find entry that is closest to the 'offset' */
1380 while (1) {
1381 if (!n) {
1382 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001383 break;
1384 }
Josef Bacik96303082009-07-13 21:29:25 -04001385
1386 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1387 prev = entry;
1388
1389 if (offset < entry->offset)
1390 n = n->rb_left;
1391 else if (offset > entry->offset)
1392 n = n->rb_right;
1393 else
1394 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001395 }
1396
Josef Bacik96303082009-07-13 21:29:25 -04001397 if (bitmap_only) {
1398 if (!entry)
1399 return NULL;
1400 if (entry->bitmap)
1401 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001402
Josef Bacik96303082009-07-13 21:29:25 -04001403 /*
1404 * bitmap entry and extent entry may share same offset,
1405 * in that case, bitmap entry comes after extent entry.
1406 */
1407 n = rb_next(n);
1408 if (!n)
1409 return NULL;
1410 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1411 if (entry->offset != offset)
1412 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001413
Josef Bacik96303082009-07-13 21:29:25 -04001414 WARN_ON(!entry->bitmap);
1415 return entry;
1416 } else if (entry) {
1417 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001418 /*
Josef Bacik96303082009-07-13 21:29:25 -04001419 * if previous extent entry covers the offset,
1420 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001421 */
Miao Xiede6c4112012-10-18 08:18:01 +00001422 n = rb_prev(&entry->offset_index);
1423 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001424 prev = rb_entry(n, struct btrfs_free_space,
1425 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001426 if (!prev->bitmap &&
1427 prev->offset + prev->bytes > offset)
1428 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001429 }
Josef Bacik96303082009-07-13 21:29:25 -04001430 }
1431 return entry;
1432 }
1433
1434 if (!prev)
1435 return NULL;
1436
1437 /* find last entry before the 'offset' */
1438 entry = prev;
1439 if (entry->offset > offset) {
1440 n = rb_prev(&entry->offset_index);
1441 if (n) {
1442 entry = rb_entry(n, struct btrfs_free_space,
1443 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001444 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001445 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001446 if (fuzzy)
1447 return entry;
1448 else
1449 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001450 }
1451 }
1452
Josef Bacik96303082009-07-13 21:29:25 -04001453 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001454 n = rb_prev(&entry->offset_index);
1455 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001456 prev = rb_entry(n, struct btrfs_free_space,
1457 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001458 if (!prev->bitmap &&
1459 prev->offset + prev->bytes > offset)
1460 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001461 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001462 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001463 return entry;
1464 } else if (entry->offset + entry->bytes > offset)
1465 return entry;
1466
1467 if (!fuzzy)
1468 return NULL;
1469
1470 while (1) {
1471 if (entry->bitmap) {
1472 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001473 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001474 break;
1475 } else {
1476 if (entry->offset + entry->bytes > offset)
1477 break;
1478 }
1479
1480 n = rb_next(&entry->offset_index);
1481 if (!n)
1482 return NULL;
1483 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1484 }
1485 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001486}
1487
Li Zefanf333adb2010-11-09 14:57:39 +08001488static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001489__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001490 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001491{
Li Zefan34d52cb2011-03-29 13:46:06 +08001492 rb_erase(&info->offset_index, &ctl->free_space_offset);
1493 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001494}
1495
Li Zefan34d52cb2011-03-29 13:46:06 +08001496static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001497 struct btrfs_free_space *info)
1498{
Li Zefan34d52cb2011-03-29 13:46:06 +08001499 __unlink_free_space(ctl, info);
1500 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001501}
1502
Li Zefan34d52cb2011-03-29 13:46:06 +08001503static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001504 struct btrfs_free_space *info)
1505{
1506 int ret = 0;
1507
Josef Bacikb12d6862013-08-26 17:14:08 -04001508 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001509 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001510 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001511 if (ret)
1512 return ret;
1513
Li Zefan34d52cb2011-03-29 13:46:06 +08001514 ctl->free_space += info->bytes;
1515 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001516 return ret;
1517}
1518
Li Zefan34d52cb2011-03-29 13:46:06 +08001519static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001520{
Li Zefan34d52cb2011-03-29 13:46:06 +08001521 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001522 u64 max_bytes;
1523 u64 bitmap_bytes;
1524 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001525 u64 size = block_group->key.offset;
David Sterbab8b93ad2015-01-16 17:26:13 +01001526 u32 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1527 u32 max_bitmaps = div_u64(size + bytes_per_bg - 1, bytes_per_bg);
Li Zefan34d52cb2011-03-29 13:46:06 +08001528
David Sterbab8b93ad2015-01-16 17:26:13 +01001529 max_bitmaps = max_t(u32, max_bitmaps, 1);
Josef Bacikdde57402013-02-12 14:07:51 -05001530
Josef Bacikb12d6862013-08-26 17:14:08 -04001531 ASSERT(ctl->total_bitmaps <= max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001532
1533 /*
1534 * The goal is to keep the total amount of memory used per 1gb of space
1535 * at or below 32k, so we need to adjust how much memory we allow to be
1536 * used by extent based free space tracking
1537 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001538 if (size < 1024 * 1024 * 1024)
1539 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1540 else
1541 max_bytes = MAX_CACHE_BYTES_PER_GIG *
David Sterbaf8c269d2015-01-16 17:21:12 +01001542 div_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001543
Josef Bacik25891f72009-09-11 16:11:20 -04001544 /*
1545 * we want to account for 1 more bitmap than what we have so we can make
1546 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1547 * we add more bitmaps.
1548 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001549 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001550
Josef Bacik25891f72009-09-11 16:11:20 -04001551 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001552 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001553 return;
Josef Bacik96303082009-07-13 21:29:25 -04001554 }
Josef Bacik25891f72009-09-11 16:11:20 -04001555
1556 /*
David Sterbaf8c269d2015-01-16 17:21:12 +01001557 * we want the extent entry threshold to always be at most 1/2 the max
Josef Bacik25891f72009-09-11 16:11:20 -04001558 * bytes we can have, or whatever is less than that.
1559 */
1560 extent_bytes = max_bytes - bitmap_bytes;
David Sterbaf8c269d2015-01-16 17:21:12 +01001561 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
Josef Bacik25891f72009-09-11 16:11:20 -04001562
Li Zefan34d52cb2011-03-29 13:46:06 +08001563 ctl->extents_thresh =
David Sterbaf8c269d2015-01-16 17:21:12 +01001564 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
Josef Bacik96303082009-07-13 21:29:25 -04001565}
1566
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001567static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1568 struct btrfs_free_space *info,
1569 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001570{
Li Zefanf38b6e72011-03-14 13:40:51 +08001571 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001572
Li Zefan34d52cb2011-03-29 13:46:06 +08001573 start = offset_to_bit(info->offset, ctl->unit, offset);
1574 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001575 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001576
Li Zefanf38b6e72011-03-14 13:40:51 +08001577 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001578
1579 info->bytes -= bytes;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001580}
1581
1582static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1583 struct btrfs_free_space *info, u64 offset,
1584 u64 bytes)
1585{
1586 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001587 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001588}
1589
Li Zefan34d52cb2011-03-29 13:46:06 +08001590static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001591 struct btrfs_free_space *info, u64 offset,
1592 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001593{
Li Zefanf38b6e72011-03-14 13:40:51 +08001594 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001595
Li Zefan34d52cb2011-03-29 13:46:06 +08001596 start = offset_to_bit(info->offset, ctl->unit, offset);
1597 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001598 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001599
Li Zefanf38b6e72011-03-14 13:40:51 +08001600 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001601
1602 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001603 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001604}
1605
Miao Xiea4820392013-09-09 13:19:42 +08001606/*
1607 * If we can not find suitable extent, we will use bytes to record
1608 * the size of the max extent.
1609 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001610static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001611 struct btrfs_free_space *bitmap_info, u64 *offset,
1612 u64 *bytes)
1613{
1614 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001615 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001616 unsigned long bits, i;
1617 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001618 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001619
Li Zefan34d52cb2011-03-29 13:46:06 +08001620 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001621 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001622 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001623
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001624 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04001625 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1626 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001627 extent_bits = next_zero - i;
1628 if (extent_bits >= bits) {
1629 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001630 break;
Miao Xiea4820392013-09-09 13:19:42 +08001631 } else if (extent_bits > max_bits) {
1632 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001633 }
1634 i = next_zero;
1635 }
1636
1637 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001638 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1639 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001640 return 0;
1641 }
1642
Miao Xiea4820392013-09-09 13:19:42 +08001643 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001644 return -1;
1645}
1646
Miao Xiea4820392013-09-09 13:19:42 +08001647/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001648static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001649find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Miao Xiea4820392013-09-09 13:19:42 +08001650 unsigned long align, u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04001651{
1652 struct btrfs_free_space *entry;
1653 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001654 u64 tmp;
1655 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001656 int ret;
1657
Li Zefan34d52cb2011-03-29 13:46:06 +08001658 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001659 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001660
Li Zefan34d52cb2011-03-29 13:46:06 +08001661 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001662 if (!entry)
Miao Xiea4820392013-09-09 13:19:42 +08001663 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001664
1665 for (node = &entry->offset_index; node; node = rb_next(node)) {
1666 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Miao Xiea4820392013-09-09 13:19:42 +08001667 if (entry->bytes < *bytes) {
1668 if (entry->bytes > *max_extent_size)
1669 *max_extent_size = entry->bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001670 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001671 }
Josef Bacik96303082009-07-13 21:29:25 -04001672
David Woodhouse53b381b2013-01-29 18:40:14 -05001673 /* make sure the space returned is big enough
1674 * to match our requested alignment
1675 */
1676 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001677 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01001678 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05001679 tmp = tmp * align + ctl->start;
1680 align_off = tmp - entry->offset;
1681 } else {
1682 align_off = 0;
1683 tmp = entry->offset;
1684 }
1685
Miao Xiea4820392013-09-09 13:19:42 +08001686 if (entry->bytes < *bytes + align_off) {
1687 if (entry->bytes > *max_extent_size)
1688 *max_extent_size = entry->bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05001689 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001690 }
David Woodhouse53b381b2013-01-29 18:40:14 -05001691
Josef Bacik96303082009-07-13 21:29:25 -04001692 if (entry->bitmap) {
Miao Xiea4820392013-09-09 13:19:42 +08001693 u64 size = *bytes;
1694
1695 ret = search_bitmap(ctl, entry, &tmp, &size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001696 if (!ret) {
1697 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08001698 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04001699 return entry;
Miao Xiea4820392013-09-09 13:19:42 +08001700 } else if (size > *max_extent_size) {
1701 *max_extent_size = size;
David Woodhouse53b381b2013-01-29 18:40:14 -05001702 }
Josef Bacik96303082009-07-13 21:29:25 -04001703 continue;
1704 }
1705
David Woodhouse53b381b2013-01-29 18:40:14 -05001706 *offset = tmp;
1707 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001708 return entry;
1709 }
Miao Xiea4820392013-09-09 13:19:42 +08001710out:
Josef Bacik96303082009-07-13 21:29:25 -04001711 return NULL;
1712}
1713
Li Zefan34d52cb2011-03-29 13:46:06 +08001714static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001715 struct btrfs_free_space *info, u64 offset)
1716{
Li Zefan34d52cb2011-03-29 13:46:06 +08001717 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001718 info->bytes = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02001719 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08001720 link_free_space(ctl, info);
1721 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001722
Li Zefan34d52cb2011-03-29 13:46:06 +08001723 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001724}
1725
Li Zefan34d52cb2011-03-29 13:46:06 +08001726static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001727 struct btrfs_free_space *bitmap_info)
1728{
Li Zefan34d52cb2011-03-29 13:46:06 +08001729 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001730 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001731 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001732 ctl->total_bitmaps--;
1733 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001734}
1735
Li Zefan34d52cb2011-03-29 13:46:06 +08001736static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001737 struct btrfs_free_space *bitmap_info,
1738 u64 *offset, u64 *bytes)
1739{
1740 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001741 u64 search_start, search_bytes;
1742 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001743
1744again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001745 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001746
Josef Bacik6606bb92009-07-31 11:03:58 -04001747 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04001748 * We need to search for bits in this bitmap. We could only cover some
1749 * of the extent in this bitmap thanks to how we add space, so we need
1750 * to search for as much as it as we can and clear that amount, and then
1751 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04001752 */
1753 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001754 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00001755 search_bytes = min(search_bytes, end - search_start + 1);
Li Zefan34d52cb2011-03-29 13:46:06 +08001756 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
Josef Bacikb50c6e22013-04-25 15:55:30 -04001757 if (ret < 0 || search_start != *offset)
1758 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04001759
Josef Bacikbdb7d302012-06-27 15:10:56 -04001760 /* We may have found more bits than what we need */
1761 search_bytes = min(search_bytes, *bytes);
1762
1763 /* Cannot clear past the end of the bitmap */
1764 search_bytes = min(search_bytes, end - search_start + 1);
1765
1766 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1767 *offset += search_bytes;
1768 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001769
1770 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001771 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001772 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001773 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001774
Josef Bacik6606bb92009-07-31 11:03:58 -04001775 /*
1776 * no entry after this bitmap, but we still have bytes to
1777 * remove, so something has gone wrong.
1778 */
1779 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001780 return -EINVAL;
1781
Josef Bacik6606bb92009-07-31 11:03:58 -04001782 bitmap_info = rb_entry(next, struct btrfs_free_space,
1783 offset_index);
1784
1785 /*
1786 * if the next entry isn't a bitmap we need to return to let the
1787 * extent stuff do its work.
1788 */
Josef Bacik96303082009-07-13 21:29:25 -04001789 if (!bitmap_info->bitmap)
1790 return -EAGAIN;
1791
Josef Bacik6606bb92009-07-31 11:03:58 -04001792 /*
1793 * Ok the next item is a bitmap, but it may not actually hold
1794 * the information for the rest of this free space stuff, so
1795 * look for it, and if we don't find it return so we can try
1796 * everything over again.
1797 */
1798 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001799 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08001800 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik6606bb92009-07-31 11:03:58 -04001801 &search_bytes);
1802 if (ret < 0 || search_start != *offset)
1803 return -EAGAIN;
1804
Josef Bacik96303082009-07-13 21:29:25 -04001805 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001806 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001807 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001808
1809 return 0;
1810}
1811
Josef Bacik2cdc3422011-05-27 14:07:49 -04001812static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1813 struct btrfs_free_space *info, u64 offset,
1814 u64 bytes)
1815{
1816 u64 bytes_to_set = 0;
1817 u64 end;
1818
1819 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1820
1821 bytes_to_set = min(end - offset, bytes);
1822
1823 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1824
1825 return bytes_to_set;
1826
1827}
1828
Li Zefan34d52cb2011-03-29 13:46:06 +08001829static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1830 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001831{
Li Zefan34d52cb2011-03-29 13:46:06 +08001832 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik96303082009-07-13 21:29:25 -04001833
1834 /*
1835 * If we are below the extents threshold then we can add this as an
1836 * extent, and don't have to deal with the bitmap
1837 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001838 if (ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001839 /*
1840 * If this block group has some small extents we don't want to
1841 * use up all of our free slots in the cache with them, we want
1842 * to reserve them to larger extents, however if we have plent
1843 * of cache left then go ahead an dadd them, no sense in adding
1844 * the overhead of a bitmap if we don't have to.
1845 */
1846 if (info->bytes <= block_group->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001847 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1848 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001849 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001850 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001851 }
1852 }
Josef Bacik96303082009-07-13 21:29:25 -04001853
1854 /*
Josef Bacikdde57402013-02-12 14:07:51 -05001855 * The original block groups from mkfs can be really small, like 8
1856 * megabytes, so don't bother with a bitmap for those entries. However
1857 * some block groups can be smaller than what a bitmap would cover but
1858 * are still large enough that they could overflow the 32k memory limit,
1859 * so allow those block groups to still be allowed to have a bitmap
1860 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04001861 */
Josef Bacikdde57402013-02-12 14:07:51 -05001862 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08001863 return false;
1864
1865 return true;
1866}
1867
Josef Bacik2cdc3422011-05-27 14:07:49 -04001868static struct btrfs_free_space_op free_space_op = {
1869 .recalc_thresholds = recalculate_thresholds,
1870 .use_bitmap = use_bitmap,
1871};
1872
Li Zefan34d52cb2011-03-29 13:46:06 +08001873static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1874 struct btrfs_free_space *info)
1875{
1876 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001877 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08001878 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001879 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08001880 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001881
1882 bytes = info->bytes;
1883 offset = info->offset;
1884
Li Zefan34d52cb2011-03-29 13:46:06 +08001885 if (!ctl->op->use_bitmap(ctl, info))
1886 return 0;
1887
Josef Bacik2cdc3422011-05-27 14:07:49 -04001888 if (ctl->op == &free_space_op)
1889 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04001890again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04001891 /*
1892 * Since we link bitmaps right into the cluster we need to see if we
1893 * have a cluster here, and if so and it has our bitmap we need to add
1894 * the free space to that bitmap.
1895 */
1896 if (block_group && !list_empty(&block_group->cluster_list)) {
1897 struct btrfs_free_cluster *cluster;
1898 struct rb_node *node;
1899 struct btrfs_free_space *entry;
1900
1901 cluster = list_entry(block_group->cluster_list.next,
1902 struct btrfs_free_cluster,
1903 block_group_list);
1904 spin_lock(&cluster->lock);
1905 node = rb_first(&cluster->root);
1906 if (!node) {
1907 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001908 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001909 }
1910
1911 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1912 if (!entry->bitmap) {
1913 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001914 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001915 }
1916
1917 if (entry->offset == offset_to_bitmap(ctl, offset)) {
1918 bytes_added = add_bytes_to_bitmap(ctl, entry,
1919 offset, bytes);
1920 bytes -= bytes_added;
1921 offset += bytes_added;
1922 }
1923 spin_unlock(&cluster->lock);
1924 if (!bytes) {
1925 ret = 1;
1926 goto out;
1927 }
1928 }
Chris Mason38e87882011-06-10 16:36:57 -04001929
1930no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08001931 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04001932 1, 0);
1933 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04001934 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04001935 goto new_bitmap;
1936 }
1937
Josef Bacik2cdc3422011-05-27 14:07:49 -04001938 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
1939 bytes -= bytes_added;
1940 offset += bytes_added;
1941 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001942
1943 if (!bytes) {
1944 ret = 1;
1945 goto out;
1946 } else
1947 goto again;
1948
1949new_bitmap:
1950 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001951 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04001952 added = 1;
1953 info = NULL;
1954 goto again;
1955 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001956 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001957
1958 /* no pre-allocated info, allocate a new one */
1959 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05001960 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1961 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04001962 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001963 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001964 ret = -ENOMEM;
1965 goto out;
1966 }
1967 }
1968
1969 /* allocate the bitmap */
1970 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08001971 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001972 if (!info->bitmap) {
1973 ret = -ENOMEM;
1974 goto out;
1975 }
1976 goto again;
1977 }
1978
1979out:
1980 if (info) {
1981 if (info->bitmap)
1982 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001983 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001984 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001985
1986 return ret;
1987}
1988
Chris Mason945d8962011-05-22 12:33:42 -04001989static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001990 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001991{
Li Zefan120d66e2010-11-09 14:56:50 +08001992 struct btrfs_free_space *left_info;
1993 struct btrfs_free_space *right_info;
1994 bool merged = false;
1995 u64 offset = info->offset;
1996 u64 bytes = info->bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001997
Josef Bacik0f9dd462008-09-23 13:14:11 -04001998 /*
1999 * first we want to see if there is free space adjacent to the range we
2000 * are adding, if there is remove that struct and add a new one to
2001 * cover the entire range
2002 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002003 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002004 if (right_info && rb_prev(&right_info->offset_index))
2005 left_info = rb_entry(rb_prev(&right_info->offset_index),
2006 struct btrfs_free_space, offset_index);
2007 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002008 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002009
Josef Bacik96303082009-07-13 21:29:25 -04002010 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08002011 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002012 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002013 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002014 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002015 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002016 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002017 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002018 }
2019
Josef Bacik96303082009-07-13 21:29:25 -04002020 if (left_info && !left_info->bitmap &&
2021 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08002022 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002023 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002024 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002025 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002026 info->offset = left_info->offset;
2027 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002028 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002029 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002030 }
2031
Li Zefan120d66e2010-11-09 14:56:50 +08002032 return merged;
2033}
2034
Filipe Manana20005522014-08-29 13:35:13 +01002035static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2036 struct btrfs_free_space *info,
2037 bool update_stat)
2038{
2039 struct btrfs_free_space *bitmap;
2040 unsigned long i;
2041 unsigned long j;
2042 const u64 end = info->offset + info->bytes;
2043 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2044 u64 bytes;
2045
2046 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2047 if (!bitmap)
2048 return false;
2049
2050 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2051 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2052 if (j == i)
2053 return false;
2054 bytes = (j - i) * ctl->unit;
2055 info->bytes += bytes;
2056
2057 if (update_stat)
2058 bitmap_clear_bits(ctl, bitmap, end, bytes);
2059 else
2060 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2061
2062 if (!bitmap->bytes)
2063 free_bitmap(ctl, bitmap);
2064
2065 return true;
2066}
2067
2068static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2069 struct btrfs_free_space *info,
2070 bool update_stat)
2071{
2072 struct btrfs_free_space *bitmap;
2073 u64 bitmap_offset;
2074 unsigned long i;
2075 unsigned long j;
2076 unsigned long prev_j;
2077 u64 bytes;
2078
2079 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2080 /* If we're on a boundary, try the previous logical bitmap. */
2081 if (bitmap_offset == info->offset) {
2082 if (info->offset == 0)
2083 return false;
2084 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2085 }
2086
2087 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2088 if (!bitmap)
2089 return false;
2090
2091 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2092 j = 0;
2093 prev_j = (unsigned long)-1;
2094 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2095 if (j > i)
2096 break;
2097 prev_j = j;
2098 }
2099 if (prev_j == i)
2100 return false;
2101
2102 if (prev_j == (unsigned long)-1)
2103 bytes = (i + 1) * ctl->unit;
2104 else
2105 bytes = (i - prev_j) * ctl->unit;
2106
2107 info->offset -= bytes;
2108 info->bytes += bytes;
2109
2110 if (update_stat)
2111 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2112 else
2113 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2114
2115 if (!bitmap->bytes)
2116 free_bitmap(ctl, bitmap);
2117
2118 return true;
2119}
2120
2121/*
2122 * We prefer always to allocate from extent entries, both for clustered and
2123 * non-clustered allocation requests. So when attempting to add a new extent
2124 * entry, try to see if there's adjacent free space in bitmap entries, and if
2125 * there is, migrate that space from the bitmaps to the extent.
2126 * Like this we get better chances of satisfying space allocation requests
2127 * because we attempt to satisfy them based on a single cache entry, and never
2128 * on 2 or more entries - even if the entries represent a contiguous free space
2129 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2130 * ends).
2131 */
2132static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2133 struct btrfs_free_space *info,
2134 bool update_stat)
2135{
2136 /*
2137 * Only work with disconnected entries, as we can change their offset,
2138 * and must be extent entries.
2139 */
2140 ASSERT(!info->bitmap);
2141 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2142
2143 if (ctl->total_bitmaps > 0) {
2144 bool stole_end;
2145 bool stole_front = false;
2146
2147 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2148 if (ctl->total_bitmaps > 0)
2149 stole_front = steal_from_bitmap_to_front(ctl, info,
2150 update_stat);
2151
2152 if (stole_end || stole_front)
2153 try_merge_free_space(ctl, info, update_stat);
2154 }
2155}
2156
Li Zefan581bb052011-04-20 10:06:11 +08002157int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
2158 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08002159{
2160 struct btrfs_free_space *info;
2161 int ret = 0;
2162
Josef Bacikdc89e982011-01-28 17:05:48 -05002163 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002164 if (!info)
2165 return -ENOMEM;
2166
2167 info->offset = offset;
2168 info->bytes = bytes;
Filipe Manana20005522014-08-29 13:35:13 +01002169 RB_CLEAR_NODE(&info->offset_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002170
Li Zefan34d52cb2011-03-29 13:46:06 +08002171 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002172
Li Zefan34d52cb2011-03-29 13:46:06 +08002173 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002174 goto link;
2175
2176 /*
2177 * There was no extent directly to the left or right of this new
2178 * extent then we know we're going to have to allocate a new extent, so
2179 * before we do that see if we need to drop this into a bitmap
2180 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002181 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002182 if (ret < 0) {
2183 goto out;
2184 } else if (ret) {
2185 ret = 0;
2186 goto out;
2187 }
2188link:
Filipe Manana20005522014-08-29 13:35:13 +01002189 /*
2190 * Only steal free space from adjacent bitmaps if we're sure we're not
2191 * going to add the new free space to existing bitmap entries - because
2192 * that would mean unnecessary work that would be reverted. Therefore
2193 * attempt to steal space from bitmaps if we're adding an extent entry.
2194 */
2195 steal_from_bitmap(ctl, info, true);
2196
Li Zefan34d52cb2011-03-29 13:46:06 +08002197 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002198 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002199 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002200out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002201 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002202
Josef Bacik0f9dd462008-09-23 13:14:11 -04002203 if (ret) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002204 printk(KERN_CRIT "BTRFS: unable to add free space :%d\n", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002205 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002206 }
2207
Josef Bacik0f9dd462008-09-23 13:14:11 -04002208 return ret;
2209}
2210
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002211int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2212 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002213{
Li Zefan34d52cb2011-03-29 13:46:06 +08002214 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002215 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002216 int ret;
2217 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002218
Li Zefan34d52cb2011-03-29 13:46:06 +08002219 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002220
Josef Bacik96303082009-07-13 21:29:25 -04002221again:
Josef Bacikb0175112012-12-18 11:39:19 -05002222 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002223 if (!bytes)
2224 goto out_lock;
2225
Li Zefan34d52cb2011-03-29 13:46:06 +08002226 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002227 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002228 /*
2229 * oops didn't find an extent that matched the space we wanted
2230 * to remove, look for a bitmap instead
2231 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002232 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002233 1, 0);
2234 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002235 /*
2236 * If we found a partial bit of our free space in a
2237 * bitmap but then couldn't find the other part this may
2238 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002239 */
Josef Bacikb0175112012-12-18 11:39:19 -05002240 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002241 goto out_lock;
2242 }
Josef Bacik96303082009-07-13 21:29:25 -04002243 }
2244
Josef Bacikb0175112012-12-18 11:39:19 -05002245 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002246 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002247 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002248 if (offset == info->offset) {
2249 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002250
Josef Bacikbdb7d302012-06-27 15:10:56 -04002251 info->bytes -= to_free;
2252 info->offset += to_free;
2253 if (info->bytes) {
2254 ret = link_free_space(ctl, info);
2255 WARN_ON(ret);
2256 } else {
2257 kmem_cache_free(btrfs_free_space_cachep, info);
2258 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002259
Josef Bacikbdb7d302012-06-27 15:10:56 -04002260 offset += to_free;
2261 bytes -= to_free;
2262 goto again;
2263 } else {
2264 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002265
Josef Bacikbdb7d302012-06-27 15:10:56 -04002266 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002267 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002268 WARN_ON(ret);
2269 if (ret)
2270 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002271
Josef Bacikbdb7d302012-06-27 15:10:56 -04002272 /* Not enough bytes in this entry to satisfy us */
2273 if (old_end < offset + bytes) {
2274 bytes -= old_end - offset;
2275 offset = old_end;
2276 goto again;
2277 } else if (old_end == offset + bytes) {
2278 /* all done */
2279 goto out_lock;
2280 }
2281 spin_unlock(&ctl->tree_lock);
2282
2283 ret = btrfs_add_free_space(block_group, offset + bytes,
2284 old_end - (offset + bytes));
2285 WARN_ON(ret);
2286 goto out;
2287 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002288 }
Josef Bacik96303082009-07-13 21:29:25 -04002289
Li Zefan34d52cb2011-03-29 13:46:06 +08002290 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002291 if (ret == -EAGAIN) {
2292 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002293 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002294 }
Josef Bacik96303082009-07-13 21:29:25 -04002295out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08002296 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002297out:
Josef Bacik25179202008-10-29 14:49:05 -04002298 return ret;
2299}
2300
Josef Bacik0f9dd462008-09-23 13:14:11 -04002301void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2302 u64 bytes)
2303{
Li Zefan34d52cb2011-03-29 13:46:06 +08002304 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002305 struct btrfs_free_space *info;
2306 struct rb_node *n;
2307 int count = 0;
2308
Li Zefan34d52cb2011-03-29 13:46:06 +08002309 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002310 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002311 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002312 count++;
Frank Holtonefe120a2013-12-20 11:37:06 -05002313 btrfs_crit(block_group->fs_info,
2314 "entry offset %llu, bytes %llu, bitmap %s",
2315 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002316 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002317 }
Frank Holtonefe120a2013-12-20 11:37:06 -05002318 btrfs_info(block_group->fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002319 list_empty(&block_group->cluster_list) ? "no" : "yes");
Frank Holtonefe120a2013-12-20 11:37:06 -05002320 btrfs_info(block_group->fs_info,
2321 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002322}
2323
Li Zefan34d52cb2011-03-29 13:46:06 +08002324void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002325{
Li Zefan34d52cb2011-03-29 13:46:06 +08002326 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002327
Li Zefan34d52cb2011-03-29 13:46:06 +08002328 spin_lock_init(&ctl->tree_lock);
2329 ctl->unit = block_group->sectorsize;
2330 ctl->start = block_group->key.objectid;
2331 ctl->private = block_group;
2332 ctl->op = &free_space_op;
Filipe Manana55507ce2014-12-01 17:04:09 +00002333 INIT_LIST_HEAD(&ctl->trimming_ranges);
2334 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002335
Li Zefan34d52cb2011-03-29 13:46:06 +08002336 /*
2337 * we only want to have 32k of ram per block group for keeping
2338 * track of free space, and if we pass 1/2 of that we want to
2339 * start converting things over to using bitmaps
2340 */
2341 ctl->extents_thresh = ((1024 * 32) / 2) /
2342 sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002343}
2344
Chris Masonfa9c0d792009-04-03 09:47:43 -04002345/*
2346 * for a given cluster, put all of its extents back into the free
2347 * space cache. If the block group passed doesn't match the block group
2348 * pointed to by the cluster, someone else raced in and freed the
2349 * cluster already. In that case, we just return without changing anything
2350 */
2351static int
2352__btrfs_return_cluster_to_free_space(
2353 struct btrfs_block_group_cache *block_group,
2354 struct btrfs_free_cluster *cluster)
2355{
Li Zefan34d52cb2011-03-29 13:46:06 +08002356 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002357 struct btrfs_free_space *entry;
2358 struct rb_node *node;
2359
2360 spin_lock(&cluster->lock);
2361 if (cluster->block_group != block_group)
2362 goto out;
2363
Josef Bacik96303082009-07-13 21:29:25 -04002364 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002365 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002366 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002367
Chris Masonfa9c0d792009-04-03 09:47:43 -04002368 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002369 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002370 bool bitmap;
2371
Chris Masonfa9c0d792009-04-03 09:47:43 -04002372 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2373 node = rb_next(&entry->offset_index);
2374 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002375 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002376
2377 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002378 if (!bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002379 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002380 steal_from_bitmap(ctl, entry, false);
2381 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002382 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002383 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002384 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002385 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002386
Chris Masonfa9c0d792009-04-03 09:47:43 -04002387out:
2388 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002389 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002390 return 0;
2391}
2392
Eric Sandeen48a3b632013-04-25 20:41:01 +00002393static void __btrfs_remove_free_space_cache_locked(
2394 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002395{
2396 struct btrfs_free_space *info;
2397 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002398
Li Zefan581bb052011-04-20 10:06:11 +08002399 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2400 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002401 if (!info->bitmap) {
2402 unlink_free_space(ctl, info);
2403 kmem_cache_free(btrfs_free_space_cachep, info);
2404 } else {
2405 free_bitmap(ctl, info);
2406 }
David Sterba351810c2015-01-08 15:20:54 +01002407
2408 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002409 }
Chris Mason09655372011-05-21 09:27:38 -04002410}
2411
2412void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2413{
2414 spin_lock(&ctl->tree_lock);
2415 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002416 spin_unlock(&ctl->tree_lock);
2417}
2418
Josef Bacik0f9dd462008-09-23 13:14:11 -04002419void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2420{
Li Zefan34d52cb2011-03-29 13:46:06 +08002421 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002422 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002423 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002424
Li Zefan34d52cb2011-03-29 13:46:06 +08002425 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002426 while ((head = block_group->cluster_list.next) !=
2427 &block_group->cluster_list) {
2428 cluster = list_entry(head, struct btrfs_free_cluster,
2429 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002430
2431 WARN_ON(cluster->block_group != block_group);
2432 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01002433
2434 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002435 }
Chris Mason09655372011-05-21 09:27:38 -04002436 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002437 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002438
Josef Bacik0f9dd462008-09-23 13:14:11 -04002439}
2440
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002441u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002442 u64 offset, u64 bytes, u64 empty_size,
2443 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002444{
Li Zefan34d52cb2011-03-29 13:46:06 +08002445 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002446 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002447 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002448 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002449 u64 align_gap = 0;
2450 u64 align_gap_len = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002451
Li Zefan34d52cb2011-03-29 13:46:06 +08002452 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002453 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002454 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002455 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002456 goto out;
2457
2458 ret = offset;
2459 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002460 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002461 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002462 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002463 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002464 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002465 align_gap_len = offset - entry->offset;
2466 align_gap = entry->offset;
2467
2468 entry->offset = offset + bytes;
2469 WARN_ON(entry->bytes < bytes + align_gap_len);
2470
2471 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002472 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002473 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002474 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002475 link_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002476 }
Josef Bacik96303082009-07-13 21:29:25 -04002477out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002478 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002479
David Woodhouse53b381b2013-01-29 18:40:14 -05002480 if (align_gap_len)
2481 __btrfs_add_free_space(ctl, align_gap, align_gap_len);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002482 return ret;
2483}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002484
2485/*
2486 * given a cluster, put all of its extents back into the free space
2487 * cache. If a block group is passed, this function will only free
2488 * a cluster that belongs to the passed block group.
2489 *
2490 * Otherwise, it'll get a reference on the block group pointed to by the
2491 * cluster and remove the cluster from it.
2492 */
2493int btrfs_return_cluster_to_free_space(
2494 struct btrfs_block_group_cache *block_group,
2495 struct btrfs_free_cluster *cluster)
2496{
Li Zefan34d52cb2011-03-29 13:46:06 +08002497 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002498 int ret;
2499
2500 /* first, get a safe pointer to the block group */
2501 spin_lock(&cluster->lock);
2502 if (!block_group) {
2503 block_group = cluster->block_group;
2504 if (!block_group) {
2505 spin_unlock(&cluster->lock);
2506 return 0;
2507 }
2508 } else if (cluster->block_group != block_group) {
2509 /* someone else has already freed it don't redo their work */
2510 spin_unlock(&cluster->lock);
2511 return 0;
2512 }
2513 atomic_inc(&block_group->count);
2514 spin_unlock(&cluster->lock);
2515
Li Zefan34d52cb2011-03-29 13:46:06 +08002516 ctl = block_group->free_space_ctl;
2517
Chris Masonfa9c0d792009-04-03 09:47:43 -04002518 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002519 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002520 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002521 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002522
2523 /* finally drop our ref */
2524 btrfs_put_block_group(block_group);
2525 return ret;
2526}
2527
Josef Bacik96303082009-07-13 21:29:25 -04002528static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2529 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002530 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002531 u64 bytes, u64 min_start,
2532 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002533{
Li Zefan34d52cb2011-03-29 13:46:06 +08002534 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002535 int err;
2536 u64 search_start = cluster->window_start;
2537 u64 search_bytes = bytes;
2538 u64 ret = 0;
2539
Josef Bacik96303082009-07-13 21:29:25 -04002540 search_start = min_start;
2541 search_bytes = bytes;
2542
Li Zefan34d52cb2011-03-29 13:46:06 +08002543 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Miao Xiea4820392013-09-09 13:19:42 +08002544 if (err) {
2545 if (search_bytes > *max_extent_size)
2546 *max_extent_size = search_bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002547 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002548 }
Josef Bacik96303082009-07-13 21:29:25 -04002549
2550 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002551 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002552
2553 return ret;
2554}
2555
Chris Masonfa9c0d792009-04-03 09:47:43 -04002556/*
2557 * given a cluster, try to allocate 'bytes' from it, returns 0
2558 * if it couldn't find anything suitably large, or a logical disk offset
2559 * if things worked out
2560 */
2561u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2562 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002563 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002564{
Li Zefan34d52cb2011-03-29 13:46:06 +08002565 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002566 struct btrfs_free_space *entry = NULL;
2567 struct rb_node *node;
2568 u64 ret = 0;
2569
2570 spin_lock(&cluster->lock);
2571 if (bytes > cluster->max_size)
2572 goto out;
2573
2574 if (cluster->block_group != block_group)
2575 goto out;
2576
2577 node = rb_first(&cluster->root);
2578 if (!node)
2579 goto out;
2580
2581 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302582 while (1) {
Miao Xiea4820392013-09-09 13:19:42 +08002583 if (entry->bytes < bytes && entry->bytes > *max_extent_size)
2584 *max_extent_size = entry->bytes;
2585
Josef Bacik4e69b592011-03-21 10:11:24 -04002586 if (entry->bytes < bytes ||
2587 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002588 node = rb_next(&entry->offset_index);
2589 if (!node)
2590 break;
2591 entry = rb_entry(node, struct btrfs_free_space,
2592 offset_index);
2593 continue;
2594 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002595
Josef Bacik4e69b592011-03-21 10:11:24 -04002596 if (entry->bitmap) {
2597 ret = btrfs_alloc_from_bitmap(block_group,
2598 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002599 cluster->window_start,
2600 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002601 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002602 node = rb_next(&entry->offset_index);
2603 if (!node)
2604 break;
2605 entry = rb_entry(node, struct btrfs_free_space,
2606 offset_index);
2607 continue;
2608 }
Josef Bacik9b230622012-01-26 15:01:12 -05002609 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002610 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002611 ret = entry->offset;
2612
2613 entry->offset += bytes;
2614 entry->bytes -= bytes;
2615 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002616
Li Zefan5e71b5d2010-11-09 14:55:34 +08002617 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002618 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002619 break;
2620 }
2621out:
2622 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002623
Li Zefan5e71b5d2010-11-09 14:55:34 +08002624 if (!ret)
2625 return 0;
2626
Li Zefan34d52cb2011-03-29 13:46:06 +08002627 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002628
Li Zefan34d52cb2011-03-29 13:46:06 +08002629 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002630 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002631 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002632 if (entry->bitmap) {
2633 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002634 ctl->total_bitmaps--;
2635 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002636 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002637 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002638 }
2639
Li Zefan34d52cb2011-03-29 13:46:06 +08002640 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002641
Chris Masonfa9c0d792009-04-03 09:47:43 -04002642 return ret;
2643}
2644
Josef Bacik96303082009-07-13 21:29:25 -04002645static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2646 struct btrfs_free_space *entry,
2647 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002648 u64 offset, u64 bytes,
2649 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04002650{
Li Zefan34d52cb2011-03-29 13:46:06 +08002651 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002652 unsigned long next_zero;
2653 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002654 unsigned long want_bits;
2655 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002656 unsigned long found_bits;
2657 unsigned long start = 0;
2658 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002659 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002660
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002661 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04002662 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002663 want_bits = bytes_to_bits(bytes, ctl->unit);
2664 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04002665
2666again:
2667 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06002668 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04002669 next_zero = find_next_zero_bit(entry->bitmap,
2670 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002671 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04002672 found_bits = next_zero - i;
2673 break;
2674 }
2675 i = next_zero;
2676 }
2677
2678 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002679 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002680
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002681 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04002682 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05002683 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002684 }
2685
2686 total_found += found_bits;
2687
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002688 if (cluster->max_size < found_bits * ctl->unit)
2689 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04002690
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002691 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2692 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04002693 goto again;
2694 }
2695
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002696 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002697 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002698 ret = tree_insert_offset(&cluster->root, entry->offset,
2699 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04002700 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04002701
Josef Bacik3f7de032011-11-10 08:29:20 -05002702 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002703 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04002704 return 0;
2705}
2706
Chris Masonfa9c0d792009-04-03 09:47:43 -04002707/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002708 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002709 * Try to find a cluster with at least bytes total bytes, at least one
2710 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04002711 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002712static noinline int
2713setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2714 struct btrfs_free_cluster *cluster,
2715 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002716 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002717{
Li Zefan34d52cb2011-03-29 13:46:06 +08002718 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002719 struct btrfs_free_space *first = NULL;
2720 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002721 struct btrfs_free_space *last;
2722 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04002723 u64 window_free;
2724 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002725 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002726
Li Zefan34d52cb2011-03-29 13:46:06 +08002727 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002728 if (!entry)
2729 return -ENOSPC;
2730
2731 /*
2732 * We don't want bitmaps, so just move along until we find a normal
2733 * extent entry.
2734 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002735 while (entry->bitmap || entry->bytes < min_bytes) {
2736 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04002737 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002738 node = rb_next(&entry->offset_index);
2739 if (!node)
2740 return -ENOSPC;
2741 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2742 }
2743
Josef Bacik4e69b592011-03-21 10:11:24 -04002744 window_free = entry->bytes;
2745 max_extent = entry->bytes;
2746 first = entry;
2747 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002748
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002749 for (node = rb_next(&entry->offset_index); node;
2750 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002751 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2752
Josef Bacik86d4a772011-05-25 13:03:16 -04002753 if (entry->bitmap) {
2754 if (list_empty(&entry->list))
2755 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002756 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002757 }
2758
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002759 if (entry->bytes < min_bytes)
2760 continue;
2761
2762 last = entry;
2763 window_free += entry->bytes;
2764 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04002765 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002766 }
2767
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002768 if (window_free < bytes || max_extent < cont1_bytes)
2769 return -ENOSPC;
2770
Josef Bacik4e69b592011-03-21 10:11:24 -04002771 cluster->window_start = first->offset;
2772
2773 node = &first->offset_index;
2774
2775 /*
2776 * now we've found our entries, pull them out of the free space
2777 * cache and put them into the cluster rbtree
2778 */
2779 do {
2780 int ret;
2781
2782 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2783 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002784 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002785 continue;
2786
Li Zefan34d52cb2011-03-29 13:46:06 +08002787 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002788 ret = tree_insert_offset(&cluster->root, entry->offset,
2789 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05002790 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04002791 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04002792 } while (node && entry != last);
2793
2794 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002795 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04002796 return 0;
2797}
2798
2799/*
2800 * This specifically looks for bitmaps that may work in the cluster, we assume
2801 * that we have already failed to find extents that will work.
2802 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002803static noinline int
2804setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2805 struct btrfs_free_cluster *cluster,
2806 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002807 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002808{
Li Zefan34d52cb2011-03-29 13:46:06 +08002809 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002810 struct btrfs_free_space *entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002811 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05002812 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002813
Li Zefan34d52cb2011-03-29 13:46:06 +08002814 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002815 return -ENOSPC;
2816
Josef Bacik86d4a772011-05-25 13:03:16 -04002817 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05002818 * The bitmap that covers offset won't be in the list unless offset
2819 * is just its start offset.
2820 */
2821 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
2822 if (entry->offset != bitmap_offset) {
2823 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
2824 if (entry && list_empty(&entry->list))
2825 list_add(&entry->list, bitmaps);
2826 }
2827
Josef Bacik86d4a772011-05-25 13:03:16 -04002828 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05002829 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04002830 continue;
2831 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002832 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04002833 if (!ret)
2834 return 0;
2835 }
2836
2837 /*
Li Zefan52621cb2011-11-20 07:33:38 -05002838 * The bitmaps list has all the bitmaps that record free space
2839 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04002840 */
Li Zefan52621cb2011-11-20 07:33:38 -05002841 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04002842}
2843
2844/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002845 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002846 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04002847 * We might not find them all in one contiguous area.
2848 *
2849 * returns zero and sets up cluster if things worked out, otherwise
2850 * it returns -enospc
2851 */
Josef Bacik00361582013-08-14 14:02:47 -04002852int btrfs_find_space_cluster(struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002853 struct btrfs_block_group_cache *block_group,
2854 struct btrfs_free_cluster *cluster,
2855 u64 offset, u64 bytes, u64 empty_size)
2856{
Li Zefan34d52cb2011-03-29 13:46:06 +08002857 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04002858 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05002859 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002860 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002861 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002862 int ret;
2863
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002864 /*
2865 * Choose the minimum extent size we'll require for this
2866 * cluster. For SSD_SPREAD, don't allow any fragmentation.
2867 * For metadata, allow allocates with smaller extents. For
2868 * data, keep it dense.
2869 */
Chris Mason451d7582009-06-09 20:28:34 -04002870 if (btrfs_test_opt(root, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002871 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04002872 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002873 cont1_bytes = bytes;
2874 min_bytes = block_group->sectorsize;
2875 } else {
2876 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
2877 min_bytes = block_group->sectorsize;
2878 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002879
Li Zefan34d52cb2011-03-29 13:46:06 +08002880 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002881
2882 /*
2883 * If we know we don't have enough space to make a cluster don't even
2884 * bother doing all the work to try and find one.
2885 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002886 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002887 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002888 return -ENOSPC;
2889 }
2890
Chris Masonfa9c0d792009-04-03 09:47:43 -04002891 spin_lock(&cluster->lock);
2892
2893 /* someone already found a cluster, hooray */
2894 if (cluster->block_group) {
2895 ret = 0;
2896 goto out;
2897 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002898
Josef Bacik3f7de032011-11-10 08:29:20 -05002899 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
2900 min_bytes);
2901
Josef Bacik86d4a772011-05-25 13:03:16 -04002902 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002903 bytes + empty_size,
2904 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04002905 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04002906 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002907 offset, bytes + empty_size,
2908 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04002909
2910 /* Clear our temporary list */
2911 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2912 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04002913
2914 if (!ret) {
2915 atomic_inc(&block_group->count);
2916 list_add_tail(&cluster->block_group_list,
2917 &block_group->cluster_list);
2918 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05002919 } else {
2920 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002921 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002922out:
2923 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002924 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002925
2926 return ret;
2927}
2928
2929/*
2930 * simple code to zero out a cluster
2931 */
2932void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2933{
2934 spin_lock_init(&cluster->lock);
2935 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002936 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002937 cluster->max_size = 0;
2938 INIT_LIST_HEAD(&cluster->block_group_list);
2939 cluster->block_group = NULL;
2940}
2941
Li Zefan7fe1e642011-12-29 14:47:27 +08002942static int do_trimming(struct btrfs_block_group_cache *block_group,
2943 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00002944 u64 reserved_start, u64 reserved_bytes,
2945 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08002946{
2947 struct btrfs_space_info *space_info = block_group->space_info;
2948 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00002949 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08002950 int ret;
2951 int update = 0;
2952 u64 trimmed = 0;
2953
2954 spin_lock(&space_info->lock);
2955 spin_lock(&block_group->lock);
2956 if (!block_group->ro) {
2957 block_group->reserved += reserved_bytes;
2958 space_info->bytes_reserved += reserved_bytes;
2959 update = 1;
2960 }
2961 spin_unlock(&block_group->lock);
2962 spin_unlock(&space_info->lock);
2963
Filipe Manana1edb647b2014-12-08 14:01:12 +00002964 ret = btrfs_discard_extent(fs_info->extent_root,
2965 start, bytes, &trimmed);
Li Zefan7fe1e642011-12-29 14:47:27 +08002966 if (!ret)
2967 *total_trimmed += trimmed;
2968
Filipe Manana55507ce2014-12-01 17:04:09 +00002969 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08002970 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
Filipe Manana55507ce2014-12-01 17:04:09 +00002971 list_del(&trim_entry->list);
2972 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08002973
2974 if (update) {
2975 spin_lock(&space_info->lock);
2976 spin_lock(&block_group->lock);
2977 if (block_group->ro)
2978 space_info->bytes_readonly += reserved_bytes;
2979 block_group->reserved -= reserved_bytes;
2980 space_info->bytes_reserved -= reserved_bytes;
2981 spin_unlock(&space_info->lock);
2982 spin_unlock(&block_group->lock);
2983 }
2984
2985 return ret;
2986}
2987
2988static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
2989 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
Li Dongyangf7039b12011-03-24 10:24:28 +00002990{
Li Zefan34d52cb2011-03-29 13:46:06 +08002991 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08002992 struct btrfs_free_space *entry;
2993 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00002994 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08002995 u64 extent_start;
2996 u64 extent_bytes;
2997 u64 bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00002998
2999 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00003000 struct btrfs_trim_range trim_entry;
3001
3002 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08003003 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003004
Li Zefan34d52cb2011-03-29 13:46:06 +08003005 if (ctl->free_space < minlen) {
3006 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003007 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003008 break;
3009 }
3010
Li Zefan34d52cb2011-03-29 13:46:06 +08003011 entry = tree_search_offset(ctl, start, 0, 1);
Li Zefan7fe1e642011-12-29 14:47:27 +08003012 if (!entry) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003013 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003014 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003015 break;
3016 }
3017
Li Zefan7fe1e642011-12-29 14:47:27 +08003018 /* skip bitmaps */
3019 while (entry->bitmap) {
3020 node = rb_next(&entry->offset_index);
3021 if (!node) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003022 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003023 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003024 goto out;
Li Dongyangf7039b12011-03-24 10:24:28 +00003025 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003026 entry = rb_entry(node, struct btrfs_free_space,
3027 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003028 }
3029
Li Zefan7fe1e642011-12-29 14:47:27 +08003030 if (entry->offset >= end) {
3031 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003032 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003033 break;
3034 }
3035
3036 extent_start = entry->offset;
3037 extent_bytes = entry->bytes;
3038 start = max(start, extent_start);
3039 bytes = min(extent_start + extent_bytes, end) - start;
3040 if (bytes < minlen) {
3041 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003042 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003043 goto next;
3044 }
3045
3046 unlink_free_space(ctl, entry);
3047 kmem_cache_free(btrfs_free_space_cachep, entry);
3048
Li Zefan34d52cb2011-03-29 13:46:06 +08003049 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003050 trim_entry.start = extent_start;
3051 trim_entry.bytes = extent_bytes;
3052 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3053 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003054
Li Zefan7fe1e642011-12-29 14:47:27 +08003055 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003056 extent_start, extent_bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003057 if (ret)
3058 break;
3059next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003060 start += bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003061
3062 if (fatal_signal_pending(current)) {
3063 ret = -ERESTARTSYS;
3064 break;
3065 }
3066
3067 cond_resched();
3068 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003069out:
3070 return ret;
3071}
3072
3073static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3074 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3075{
3076 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3077 struct btrfs_free_space *entry;
3078 int ret = 0;
3079 int ret2;
3080 u64 bytes;
3081 u64 offset = offset_to_bitmap(ctl, start);
3082
3083 while (offset < end) {
3084 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003085 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003086
Filipe Manana55507ce2014-12-01 17:04:09 +00003087 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003088 spin_lock(&ctl->tree_lock);
3089
3090 if (ctl->free_space < minlen) {
3091 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003092 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003093 break;
3094 }
3095
3096 entry = tree_search_offset(ctl, offset, 1, 0);
3097 if (!entry) {
3098 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003099 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003100 next_bitmap = true;
3101 goto next;
3102 }
3103
3104 bytes = minlen;
3105 ret2 = search_bitmap(ctl, entry, &start, &bytes);
3106 if (ret2 || start >= end) {
3107 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003108 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003109 next_bitmap = true;
3110 goto next;
3111 }
3112
3113 bytes = min(bytes, end - start);
3114 if (bytes < minlen) {
3115 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003116 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003117 goto next;
3118 }
3119
3120 bitmap_clear_bits(ctl, entry, start, bytes);
3121 if (entry->bytes == 0)
3122 free_bitmap(ctl, entry);
3123
3124 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003125 trim_entry.start = start;
3126 trim_entry.bytes = bytes;
3127 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3128 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003129
3130 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003131 start, bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003132 if (ret)
3133 break;
3134next:
3135 if (next_bitmap) {
3136 offset += BITS_PER_BITMAP * ctl->unit;
3137 } else {
3138 start += bytes;
3139 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3140 offset += BITS_PER_BITMAP * ctl->unit;
3141 }
3142
3143 if (fatal_signal_pending(current)) {
3144 ret = -ERESTARTSYS;
3145 break;
3146 }
3147
3148 cond_resched();
3149 }
3150
3151 return ret;
3152}
3153
3154int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
3155 u64 *trimmed, u64 start, u64 end, u64 minlen)
3156{
3157 int ret;
3158
3159 *trimmed = 0;
3160
Filipe Manana04216822014-11-27 21:14:15 +00003161 spin_lock(&block_group->lock);
3162 if (block_group->removed) {
3163 spin_unlock(&block_group->lock);
3164 return 0;
3165 }
3166 atomic_inc(&block_group->trimming);
3167 spin_unlock(&block_group->lock);
3168
Li Zefan7fe1e642011-12-29 14:47:27 +08003169 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3170 if (ret)
Filipe Manana04216822014-11-27 21:14:15 +00003171 goto out;
Li Zefan7fe1e642011-12-29 14:47:27 +08003172
3173 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
Filipe Manana04216822014-11-27 21:14:15 +00003174out:
3175 spin_lock(&block_group->lock);
3176 if (atomic_dec_and_test(&block_group->trimming) &&
3177 block_group->removed) {
3178 struct extent_map_tree *em_tree;
3179 struct extent_map *em;
3180
3181 spin_unlock(&block_group->lock);
3182
Filipe Mananaa1e7e162014-12-04 15:31:01 +00003183 lock_chunks(block_group->fs_info->chunk_root);
Filipe Manana04216822014-11-27 21:14:15 +00003184 em_tree = &block_group->fs_info->mapping_tree.map_tree;
3185 write_lock(&em_tree->lock);
3186 em = lookup_extent_mapping(em_tree, block_group->key.objectid,
3187 1);
3188 BUG_ON(!em); /* logic error, can't happen */
Filipe Mananaa1e7e162014-12-04 15:31:01 +00003189 /*
3190 * remove_extent_mapping() will delete us from the pinned_chunks
3191 * list, which is protected by the chunk mutex.
3192 */
Filipe Manana04216822014-11-27 21:14:15 +00003193 remove_extent_mapping(em_tree, em);
3194 write_unlock(&em_tree->lock);
Filipe Manana04216822014-11-27 21:14:15 +00003195 unlock_chunks(block_group->fs_info->chunk_root);
3196
3197 /* once for us and once for the tree */
3198 free_extent_map(em);
3199 free_extent_map(em);
Filipe Manana946ddbe2014-12-01 17:04:40 +00003200
3201 /*
3202 * We've left one free space entry and other tasks trimming
3203 * this block group have left 1 entry each one. Free them.
3204 */
3205 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
Filipe Manana04216822014-11-27 21:14:15 +00003206 } else {
3207 spin_unlock(&block_group->lock);
3208 }
Li Dongyangf7039b12011-03-24 10:24:28 +00003209
3210 return ret;
3211}
Li Zefan581bb052011-04-20 10:06:11 +08003212
3213/*
3214 * Find the left-most item in the cache tree, and then return the
3215 * smallest inode number in the item.
3216 *
3217 * Note: the returned inode number may not be the smallest one in
3218 * the tree, if the left-most item is a bitmap.
3219 */
3220u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3221{
3222 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3223 struct btrfs_free_space *entry = NULL;
3224 u64 ino = 0;
3225
3226 spin_lock(&ctl->tree_lock);
3227
3228 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3229 goto out;
3230
3231 entry = rb_entry(rb_first(&ctl->free_space_offset),
3232 struct btrfs_free_space, offset_index);
3233
3234 if (!entry->bitmap) {
3235 ino = entry->offset;
3236
3237 unlink_free_space(ctl, entry);
3238 entry->offset++;
3239 entry->bytes--;
3240 if (!entry->bytes)
3241 kmem_cache_free(btrfs_free_space_cachep, entry);
3242 else
3243 link_free_space(ctl, entry);
3244 } else {
3245 u64 offset = 0;
3246 u64 count = 1;
3247 int ret;
3248
3249 ret = search_bitmap(ctl, entry, &offset, &count);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003250 /* Logic error; Should be empty if it can't find anything */
Josef Bacikb12d6862013-08-26 17:14:08 -04003251 ASSERT(!ret);
Li Zefan581bb052011-04-20 10:06:11 +08003252
3253 ino = offset;
3254 bitmap_clear_bits(ctl, entry, offset, 1);
3255 if (entry->bytes == 0)
3256 free_bitmap(ctl, entry);
3257 }
3258out:
3259 spin_unlock(&ctl->tree_lock);
3260
3261 return ino;
3262}
Li Zefan82d59022011-04-20 10:33:24 +08003263
3264struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3265 struct btrfs_path *path)
3266{
3267 struct inode *inode = NULL;
3268
David Sterba57cdc8d2014-02-05 02:37:48 +01003269 spin_lock(&root->ino_cache_lock);
3270 if (root->ino_cache_inode)
3271 inode = igrab(root->ino_cache_inode);
3272 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003273 if (inode)
3274 return inode;
3275
3276 inode = __lookup_free_space_inode(root, path, 0);
3277 if (IS_ERR(inode))
3278 return inode;
3279
David Sterba57cdc8d2014-02-05 02:37:48 +01003280 spin_lock(&root->ino_cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02003281 if (!btrfs_fs_closing(root->fs_info))
David Sterba57cdc8d2014-02-05 02:37:48 +01003282 root->ino_cache_inode = igrab(inode);
3283 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003284
3285 return inode;
3286}
3287
3288int create_free_ino_inode(struct btrfs_root *root,
3289 struct btrfs_trans_handle *trans,
3290 struct btrfs_path *path)
3291{
3292 return __create_free_space_inode(root, trans, path,
3293 BTRFS_FREE_INO_OBJECTID, 0);
3294}
3295
3296int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3297{
3298 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3299 struct btrfs_path *path;
3300 struct inode *inode;
3301 int ret = 0;
3302 u64 root_gen = btrfs_root_generation(&root->root_item);
3303
Chris Mason4b9465c2011-06-03 09:36:29 -04003304 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
3305 return 0;
3306
Li Zefan82d59022011-04-20 10:33:24 +08003307 /*
3308 * If we're unmounting then just return, since this does a search on the
3309 * normal root and not the commit root and we could deadlock.
3310 */
David Sterba7841cb22011-05-31 18:07:27 +02003311 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08003312 return 0;
3313
3314 path = btrfs_alloc_path();
3315 if (!path)
3316 return 0;
3317
3318 inode = lookup_free_ino_inode(root, path);
3319 if (IS_ERR(inode))
3320 goto out;
3321
3322 if (root_gen != BTRFS_I(inode)->generation)
3323 goto out_put;
3324
3325 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3326
3327 if (ret < 0)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003328 btrfs_err(fs_info,
3329 "failed to load free ino cache for root %llu",
3330 root->root_key.objectid);
Li Zefan82d59022011-04-20 10:33:24 +08003331out_put:
3332 iput(inode);
3333out:
3334 btrfs_free_path(path);
3335 return ret;
3336}
3337
3338int btrfs_write_out_ino_cache(struct btrfs_root *root,
3339 struct btrfs_trans_handle *trans,
Filipe David Borba Manana53645a92013-09-20 14:43:28 +01003340 struct btrfs_path *path,
3341 struct inode *inode)
Li Zefan82d59022011-04-20 10:33:24 +08003342{
3343 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
Li Zefan82d59022011-04-20 10:33:24 +08003344 int ret;
3345
Chris Mason4b9465c2011-06-03 09:36:29 -04003346 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
3347 return 0;
3348
Li Zefan82d59022011-04-20 10:33:24 +08003349 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
Josef Bacikc09544e2011-08-30 10:19:10 -04003350 if (ret) {
3351 btrfs_delalloc_release_metadata(inode, inode->i_size);
3352#ifdef DEBUG
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003353 btrfs_err(root->fs_info,
3354 "failed to write free ino cache for root %llu",
3355 root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04003356#endif
3357 }
Li Zefan82d59022011-04-20 10:33:24 +08003358
Li Zefan82d59022011-04-20 10:33:24 +08003359 return ret;
3360}
Josef Bacik74255aa2013-03-15 09:47:08 -04003361
3362#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003363/*
3364 * Use this if you need to make a bitmap or extent entry specifically, it
3365 * doesn't do any of the merging that add_free_space does, this acts a lot like
3366 * how the free space cache loading stuff works, so you can get really weird
3367 * configurations.
3368 */
3369int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3370 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04003371{
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003372 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3373 struct btrfs_free_space *info = NULL, *bitmap_info;
3374 void *map = NULL;
3375 u64 bytes_added;
3376 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04003377
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003378again:
3379 if (!info) {
3380 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3381 if (!info)
3382 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04003383 }
3384
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003385 if (!bitmap) {
3386 spin_lock(&ctl->tree_lock);
3387 info->offset = offset;
3388 info->bytes = bytes;
3389 ret = link_free_space(ctl, info);
3390 spin_unlock(&ctl->tree_lock);
3391 if (ret)
3392 kmem_cache_free(btrfs_free_space_cachep, info);
3393 return ret;
3394 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003395
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003396 if (!map) {
3397 map = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
3398 if (!map) {
3399 kmem_cache_free(btrfs_free_space_cachep, info);
3400 return -ENOMEM;
3401 }
3402 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003403
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003404 spin_lock(&ctl->tree_lock);
3405 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3406 1, 0);
3407 if (!bitmap_info) {
3408 info->bitmap = map;
3409 map = NULL;
3410 add_new_bitmap(ctl, info, offset);
3411 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01003412 info = NULL;
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003413 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003414
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003415 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
3416 bytes -= bytes_added;
3417 offset += bytes_added;
3418 spin_unlock(&ctl->tree_lock);
3419
3420 if (bytes)
3421 goto again;
3422
Filipe Manana20005522014-08-29 13:35:13 +01003423 if (info)
3424 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003425 if (map)
3426 kfree(map);
3427 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003428}
3429
3430/*
3431 * Checks to see if the given range is in the free space cache. This is really
3432 * just used to check the absence of space, so if there is free space in the
3433 * range at all we will return 1.
3434 */
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003435int test_check_exists(struct btrfs_block_group_cache *cache,
3436 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04003437{
3438 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3439 struct btrfs_free_space *info;
3440 int ret = 0;
3441
3442 spin_lock(&ctl->tree_lock);
3443 info = tree_search_offset(ctl, offset, 0, 0);
3444 if (!info) {
3445 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3446 1, 0);
3447 if (!info)
3448 goto out;
3449 }
3450
3451have_info:
3452 if (info->bitmap) {
3453 u64 bit_off, bit_bytes;
3454 struct rb_node *n;
3455 struct btrfs_free_space *tmp;
3456
3457 bit_off = offset;
3458 bit_bytes = ctl->unit;
3459 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes);
3460 if (!ret) {
3461 if (bit_off == offset) {
3462 ret = 1;
3463 goto out;
3464 } else if (bit_off > offset &&
3465 offset + bytes > bit_off) {
3466 ret = 1;
3467 goto out;
3468 }
3469 }
3470
3471 n = rb_prev(&info->offset_index);
3472 while (n) {
3473 tmp = rb_entry(n, struct btrfs_free_space,
3474 offset_index);
3475 if (tmp->offset + tmp->bytes < offset)
3476 break;
3477 if (offset + bytes < tmp->offset) {
3478 n = rb_prev(&info->offset_index);
3479 continue;
3480 }
3481 info = tmp;
3482 goto have_info;
3483 }
3484
3485 n = rb_next(&info->offset_index);
3486 while (n) {
3487 tmp = rb_entry(n, struct btrfs_free_space,
3488 offset_index);
3489 if (offset + bytes < tmp->offset)
3490 break;
3491 if (tmp->offset + tmp->bytes < offset) {
3492 n = rb_next(&info->offset_index);
3493 continue;
3494 }
3495 info = tmp;
3496 goto have_info;
3497 }
3498
Filipe Manana20005522014-08-29 13:35:13 +01003499 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003500 goto out;
3501 }
3502
3503 if (info->offset == offset) {
3504 ret = 1;
3505 goto out;
3506 }
3507
3508 if (offset > info->offset && offset < info->offset + info->bytes)
3509 ret = 1;
3510out:
3511 spin_unlock(&ctl->tree_lock);
3512 return ret;
3513}
Josef Bacikdc11dd5d2013-08-14 15:05:12 -04003514#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */