blob: fb1382893bfc253b9f392613aaba3a7c621ab5f0 [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>
Ingo Molnarf361bf42017-02-03 23:47:37 +010021#include <linux/sched/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Josef Bacik96303082009-07-13 21:29:25 -040023#include <linux/math64.h>
Josef Bacik6ab60602011-08-08 08:24:46 -040024#include <linux/ratelimit.h>
Josef Bacik023f46c2017-12-11 11:36:50 -050025#include <linux/bpf.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -040026#include "ctree.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040027#include "free-space-cache.h"
28#include "transaction.h"
Josef Bacik0af3d002010-06-21 14:48:16 -040029#include "disk-io.h"
Josef Bacik43be2142011-04-01 14:55:00 +000030#include "extent_io.h"
Li Zefan581bb052011-04-20 10:06:11 +080031#include "inode-map.h"
Filipe Manana04216822014-11-27 21:14:15 +000032#include "volumes.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040033
Feifei Xu0ef64472016-06-01 19:18:24 +080034#define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
Byongho Leeee221842015-12-15 01:42:10 +090035#define MAX_CACHE_BYTES_PER_GIG SZ_32K
Josef Bacik96303082009-07-13 21:29:25 -040036
Filipe Manana55507ce2014-12-01 17:04:09 +000037struct btrfs_trim_range {
38 u64 start;
39 u64 bytes;
40 struct list_head list;
41};
42
Li Zefan34d52cb2011-03-29 13:46:06 +080043static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040044 struct btrfs_free_space *info);
Josef Bacikcd023e72012-05-14 10:06:40 -040045static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
46 struct btrfs_free_space *info);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040047static int btrfs_wait_cache_io_root(struct btrfs_root *root,
48 struct btrfs_trans_handle *trans,
49 struct btrfs_io_ctl *io_ctl,
50 struct btrfs_path *path);
Josef Bacik0cb59c92010-07-02 12:14:14 -040051
Li Zefan0414efa2011-04-20 10:20:14 +080052static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
53 struct btrfs_path *path,
54 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040055{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040056 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0af3d002010-06-21 14:48:16 -040057 struct btrfs_key key;
58 struct btrfs_key location;
59 struct btrfs_disk_key disk_key;
60 struct btrfs_free_space_header *header;
61 struct extent_buffer *leaf;
62 struct inode *inode = NULL;
63 int ret;
64
Josef Bacik0af3d002010-06-21 14:48:16 -040065 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080066 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040067 key.type = 0;
68
69 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
70 if (ret < 0)
71 return ERR_PTR(ret);
72 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020073 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040074 return ERR_PTR(-ENOENT);
75 }
76
77 leaf = path->nodes[0];
78 header = btrfs_item_ptr(leaf, path->slots[0],
79 struct btrfs_free_space_header);
80 btrfs_free_space_key(leaf, header, &disk_key);
81 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020082 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040083
Jeff Mahoney0b246af2016-06-22 18:54:23 -040084 inode = btrfs_iget(fs_info->sb, &location, root, NULL);
Josef Bacik0af3d002010-06-21 14:48:16 -040085 if (IS_ERR(inode))
86 return inode;
87 if (is_bad_inode(inode)) {
88 iput(inode);
89 return ERR_PTR(-ENOENT);
90 }
91
Al Viro528c0322012-04-13 11:03:55 -040092 mapping_set_gfp_mask(inode->i_mapping,
Michal Hockoc62d2552015-11-06 16:28:49 -080093 mapping_gfp_constraint(inode->i_mapping,
94 ~(__GFP_FS | __GFP_HIGHMEM)));
Miao Xieadae52b2011-03-31 09:43:23 +000095
Li Zefan0414efa2011-04-20 10:20:14 +080096 return inode;
97}
98
Jeff Mahoney77ab86b2017-02-15 16:28:30 -050099struct inode *lookup_free_space_inode(struct btrfs_fs_info *fs_info,
Li Zefan0414efa2011-04-20 10:20:14 +0800100 struct btrfs_block_group_cache
101 *block_group, struct btrfs_path *path)
102{
103 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400104 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +0800105
106 spin_lock(&block_group->lock);
107 if (block_group->inode)
108 inode = igrab(block_group->inode);
109 spin_unlock(&block_group->lock);
110 if (inode)
111 return inode;
112
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500113 inode = __lookup_free_space_inode(fs_info->tree_root, path,
Li Zefan0414efa2011-04-20 10:20:14 +0800114 block_group->key.objectid);
115 if (IS_ERR(inode))
116 return inode;
117
Josef Bacik0af3d002010-06-21 14:48:16 -0400118 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400119 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400120 btrfs_info(fs_info, "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400121 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
122 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400123 block_group->disk_cache_state = BTRFS_DC_CLEAR;
124 }
125
Josef Bacik300e4f82011-08-29 14:06:00 -0400126 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400127 block_group->inode = igrab(inode);
128 block_group->iref = 1;
129 }
130 spin_unlock(&block_group->lock);
131
132 return inode;
133}
134
Eric Sandeen48a3b632013-04-25 20:41:01 +0000135static int __create_free_space_inode(struct btrfs_root *root,
136 struct btrfs_trans_handle *trans,
137 struct btrfs_path *path,
138 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400139{
140 struct btrfs_key key;
141 struct btrfs_disk_key disk_key;
142 struct btrfs_free_space_header *header;
143 struct btrfs_inode_item *inode_item;
144 struct extent_buffer *leaf;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400145 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
Josef Bacik0af3d002010-06-21 14:48:16 -0400146 int ret;
147
Li Zefan0414efa2011-04-20 10:20:14 +0800148 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400149 if (ret)
150 return ret;
151
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400152 /* We inline crc's for the free disk space cache */
153 if (ino != BTRFS_FREE_INO_OBJECTID)
154 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
155
Josef Bacik0af3d002010-06-21 14:48:16 -0400156 leaf = path->nodes[0];
157 inode_item = btrfs_item_ptr(leaf, path->slots[0],
158 struct btrfs_inode_item);
159 btrfs_item_key(leaf, &disk_key, path->slots[0]);
David Sterbab159fa22016-11-08 18:09:03 +0100160 memzero_extent_buffer(leaf, (unsigned long)inode_item,
Josef Bacik0af3d002010-06-21 14:48:16 -0400161 sizeof(*inode_item));
162 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
163 btrfs_set_inode_size(leaf, inode_item, 0);
164 btrfs_set_inode_nbytes(leaf, inode_item, 0);
165 btrfs_set_inode_uid(leaf, inode_item, 0);
166 btrfs_set_inode_gid(leaf, inode_item, 0);
167 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400168 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400169 btrfs_set_inode_nlink(leaf, inode_item, 1);
170 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800171 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400172 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200173 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400174
175 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800176 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400177 key.type = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400178 ret = btrfs_insert_empty_item(trans, root, path, &key,
179 sizeof(struct btrfs_free_space_header));
180 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200181 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400182 return ret;
183 }
Chris Masonc9dc4c62015-04-04 17:14:42 -0700184
Josef Bacik0af3d002010-06-21 14:48:16 -0400185 leaf = path->nodes[0];
186 header = btrfs_item_ptr(leaf, path->slots[0],
187 struct btrfs_free_space_header);
David Sterbab159fa22016-11-08 18:09:03 +0100188 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
Josef Bacik0af3d002010-06-21 14:48:16 -0400189 btrfs_set_free_space_key(leaf, header, &disk_key);
190 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200191 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400192
193 return 0;
194}
195
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500196int create_free_space_inode(struct btrfs_fs_info *fs_info,
Li Zefan0414efa2011-04-20 10:20:14 +0800197 struct btrfs_trans_handle *trans,
198 struct btrfs_block_group_cache *block_group,
199 struct btrfs_path *path)
200{
201 int ret;
202 u64 ino;
203
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500204 ret = btrfs_find_free_objectid(fs_info->tree_root, &ino);
Li Zefan0414efa2011-04-20 10:20:14 +0800205 if (ret < 0)
206 return ret;
207
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500208 return __create_free_space_inode(fs_info->tree_root, trans, path, ino,
Li Zefan0414efa2011-04-20 10:20:14 +0800209 block_group->key.objectid);
210}
211
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400212int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
Miao Xie7b61cd92013-05-13 13:55:09 +0000213 struct btrfs_block_rsv *rsv)
Josef Bacik0af3d002010-06-21 14:48:16 -0400214{
Josef Bacikc8174312011-11-02 09:29:35 -0400215 u64 needed_bytes;
Miao Xie7b61cd92013-05-13 13:55:09 +0000216 int ret;
Josef Bacikc8174312011-11-02 09:29:35 -0400217
218 /* 1 for slack space, 1 for updating the inode */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400219 needed_bytes = btrfs_calc_trunc_metadata_size(fs_info, 1) +
220 btrfs_calc_trans_metadata_size(fs_info, 1);
Josef Bacikc8174312011-11-02 09:29:35 -0400221
Miao Xie7b61cd92013-05-13 13:55:09 +0000222 spin_lock(&rsv->lock);
223 if (rsv->reserved < needed_bytes)
224 ret = -ENOSPC;
225 else
226 ret = 0;
227 spin_unlock(&rsv->lock);
Wei Yongjun4b286cd2013-05-21 02:39:21 +0000228 return ret;
Miao Xie7b61cd92013-05-13 13:55:09 +0000229}
230
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500231int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
Chris Mason1bbc6212015-04-06 12:46:08 -0700232 struct btrfs_block_group_cache *block_group,
Miao Xie7b61cd92013-05-13 13:55:09 +0000233 struct inode *inode)
234{
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500235 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b61cd92013-05-13 13:55:09 +0000236 int ret = 0;
Filipe Manana35c76642015-04-30 17:47:05 +0100237 bool locked = false;
Chris Mason1bbc6212015-04-06 12:46:08 -0700238
Chris Mason1bbc6212015-04-06 12:46:08 -0700239 if (block_group) {
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500240 struct btrfs_path *path = btrfs_alloc_path();
241
242 if (!path) {
243 ret = -ENOMEM;
244 goto fail;
245 }
Filipe Manana35c76642015-04-30 17:47:05 +0100246 locked = true;
Chris Mason1bbc6212015-04-06 12:46:08 -0700247 mutex_lock(&trans->transaction->cache_write_mutex);
248 if (!list_empty(&block_group->io_list)) {
249 list_del_init(&block_group->io_list);
250
Jeff Mahoneyafdb5712016-09-09 12:09:35 -0400251 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700252 btrfs_put_block_group(block_group);
253 }
254
255 /*
256 * now that we've truncated the cache away, its no longer
257 * setup or written
258 */
259 spin_lock(&block_group->lock);
260 block_group->disk_cache_state = BTRFS_DC_CLEAR;
261 spin_unlock(&block_group->lock);
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500262 btrfs_free_path(path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700263 }
Josef Bacik0af3d002010-06-21 14:48:16 -0400264
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200265 btrfs_i_size_write(BTRFS_I(inode), 0);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700266 truncate_pagecache(inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400267
268 /*
269 * We don't need an orphan item because truncating the free space cache
270 * will never be split across transactions.
Chris Mason28ed1342014-12-17 09:41:04 -0800271 * We don't need to check for -EAGAIN because we're a free space
272 * cache inode
Josef Bacik0af3d002010-06-21 14:48:16 -0400273 */
274 ret = btrfs_truncate_inode_items(trans, root, inode,
275 0, BTRFS_EXTENT_DATA_KEY);
Filipe Manana35c76642015-04-30 17:47:05 +0100276 if (ret)
277 goto fail;
Josef Bacik0af3d002010-06-21 14:48:16 -0400278
Li Zefan82d59022011-04-20 10:33:24 +0800279 ret = btrfs_update_inode(trans, root, inode);
Chris Mason1bbc6212015-04-06 12:46:08 -0700280
Chris Mason1bbc6212015-04-06 12:46:08 -0700281fail:
Filipe Manana35c76642015-04-30 17:47:05 +0100282 if (locked)
283 mutex_unlock(&trans->transaction->cache_write_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100284 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400285 btrfs_abort_transaction(trans, ret);
Josef Bacikc8174312011-11-02 09:29:35 -0400286
Li Zefan82d59022011-04-20 10:33:24 +0800287 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400288}
289
David Sterba1d480532017-01-23 17:28:19 +0100290static void readahead_cache(struct inode *inode)
Josef Bacik9d66e232010-08-25 16:54:15 -0400291{
292 struct file_ra_state *ra;
293 unsigned long last_index;
294
295 ra = kzalloc(sizeof(*ra), GFP_NOFS);
296 if (!ra)
David Sterba1d480532017-01-23 17:28:19 +0100297 return;
Josef Bacik9d66e232010-08-25 16:54:15 -0400298
299 file_ra_state_init(ra, inode->i_mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300300 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
Josef Bacik9d66e232010-08-25 16:54:15 -0400301
302 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
303
304 kfree(ra);
Josef Bacik9d66e232010-08-25 16:54:15 -0400305}
306
Chris Mason4c6d1d82015-04-06 13:17:20 -0700307static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400308 int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400309{
Miao Xie5349d6c2014-06-19 10:42:49 +0800310 int num_pages;
311 int check_crcs = 0;
312
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300313 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800314
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200315 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FREE_INO_OBJECTID)
Miao Xie5349d6c2014-06-19 10:42:49 +0800316 check_crcs = 1;
317
318 /* Make sure we can fit our crcs into the first page */
319 if (write && check_crcs &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300320 (num_pages * sizeof(u32)) >= PAGE_SIZE)
Miao Xie5349d6c2014-06-19 10:42:49 +0800321 return -ENOSPC;
322
Chris Mason4c6d1d82015-04-06 13:17:20 -0700323 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800324
David Sterba31e818f2015-02-20 18:00:26 +0100325 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400326 if (!io_ctl->pages)
327 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800328
329 io_ctl->num_pages = num_pages;
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400330 io_ctl->fs_info = btrfs_sb(inode->i_sb);
Miao Xie5349d6c2014-06-19 10:42:49 +0800331 io_ctl->check_crcs = check_crcs;
Chris Masonc9dc4c62015-04-04 17:14:42 -0700332 io_ctl->inode = inode;
Miao Xie5349d6c2014-06-19 10:42:49 +0800333
Josef Bacika67509c2011-10-05 15:18:58 -0400334 return 0;
335}
Josef Bacik023f46c2017-12-11 11:36:50 -0500336BPF_ALLOW_ERROR_INJECTION(io_ctl_init);
Josef Bacika67509c2011-10-05 15:18:58 -0400337
Chris Mason4c6d1d82015-04-06 13:17:20 -0700338static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400339{
340 kfree(io_ctl->pages);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700341 io_ctl->pages = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400342}
343
Chris Mason4c6d1d82015-04-06 13:17:20 -0700344static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400345{
346 if (io_ctl->cur) {
Josef Bacika67509c2011-10-05 15:18:58 -0400347 io_ctl->cur = NULL;
348 io_ctl->orig = NULL;
349 }
350}
351
Chris Mason4c6d1d82015-04-06 13:17:20 -0700352static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
Josef Bacika67509c2011-10-05 15:18:58 -0400353{
Josef Bacikb12d6862013-08-26 17:14:08 -0400354 ASSERT(io_ctl->index < io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400355 io_ctl->page = io_ctl->pages[io_ctl->index++];
Chris Mason2b108262015-04-06 07:48:20 -0700356 io_ctl->cur = page_address(io_ctl->page);
Josef Bacika67509c2011-10-05 15:18:58 -0400357 io_ctl->orig = io_ctl->cur;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300358 io_ctl->size = PAGE_SIZE;
Josef Bacika67509c2011-10-05 15:18:58 -0400359 if (clear)
David Sterba619a9742017-03-29 20:48:44 +0200360 clear_page(io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400361}
362
Chris Mason4c6d1d82015-04-06 13:17:20 -0700363static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400364{
365 int i;
366
367 io_ctl_unmap_page(io_ctl);
368
369 for (i = 0; i < io_ctl->num_pages; i++) {
Li Zefana1ee5a42012-01-09 14:27:42 +0800370 if (io_ctl->pages[i]) {
371 ClearPageChecked(io_ctl->pages[i]);
372 unlock_page(io_ctl->pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300373 put_page(io_ctl->pages[i]);
Li Zefana1ee5a42012-01-09 14:27:42 +0800374 }
Josef Bacika67509c2011-10-05 15:18:58 -0400375 }
376}
377
Chris Mason4c6d1d82015-04-06 13:17:20 -0700378static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Josef Bacika67509c2011-10-05 15:18:58 -0400379 int uptodate)
380{
381 struct page *page;
382 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
383 int i;
384
385 for (i = 0; i < io_ctl->num_pages; i++) {
386 page = find_or_create_page(inode->i_mapping, i, mask);
387 if (!page) {
388 io_ctl_drop_pages(io_ctl);
389 return -ENOMEM;
390 }
391 io_ctl->pages[i] = page;
392 if (uptodate && !PageUptodate(page)) {
393 btrfs_readpage(NULL, page);
394 lock_page(page);
395 if (!PageUptodate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500396 btrfs_err(BTRFS_I(inode)->root->fs_info,
397 "error reading free space cache");
Josef Bacika67509c2011-10-05 15:18:58 -0400398 io_ctl_drop_pages(io_ctl);
399 return -EIO;
400 }
401 }
402 }
403
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500404 for (i = 0; i < io_ctl->num_pages; i++) {
405 clear_page_dirty_for_io(io_ctl->pages[i]);
406 set_page_extent_mapped(io_ctl->pages[i]);
407 }
408
Josef Bacika67509c2011-10-05 15:18:58 -0400409 return 0;
410}
411
Chris Mason4c6d1d82015-04-06 13:17:20 -0700412static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400413{
Al Viro528c0322012-04-13 11:03:55 -0400414 __le64 *val;
Josef Bacika67509c2011-10-05 15:18:58 -0400415
416 io_ctl_map_page(io_ctl, 1);
417
418 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400419 * Skip the csum areas. If we don't check crcs then we just have a
420 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400421 */
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400422 if (io_ctl->check_crcs) {
423 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
424 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
425 } else {
426 io_ctl->cur += sizeof(u64);
427 io_ctl->size -= sizeof(u64) * 2;
428 }
Josef Bacika67509c2011-10-05 15:18:58 -0400429
430 val = io_ctl->cur;
431 *val = cpu_to_le64(generation);
432 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400433}
434
Chris Mason4c6d1d82015-04-06 13:17:20 -0700435static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400436{
Al Viro528c0322012-04-13 11:03:55 -0400437 __le64 *gen;
Josef Bacika67509c2011-10-05 15:18:58 -0400438
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400439 /*
440 * Skip the crc area. If we don't check crcs then we just have a 64bit
441 * chunk at the front of the first page.
442 */
443 if (io_ctl->check_crcs) {
444 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
445 io_ctl->size -= sizeof(u64) +
446 (sizeof(u32) * io_ctl->num_pages);
447 } else {
448 io_ctl->cur += sizeof(u64);
449 io_ctl->size -= sizeof(u64) * 2;
450 }
Josef Bacika67509c2011-10-05 15:18:58 -0400451
Josef Bacika67509c2011-10-05 15:18:58 -0400452 gen = io_ctl->cur;
453 if (le64_to_cpu(*gen) != generation) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400454 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200455 "space cache generation (%llu) does not match inode (%llu)",
456 *gen, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400457 io_ctl_unmap_page(io_ctl);
458 return -EIO;
459 }
460 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400461 return 0;
462}
463
Chris Mason4c6d1d82015-04-06 13:17:20 -0700464static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400465{
466 u32 *tmp;
467 u32 crc = ~(u32)0;
468 unsigned offset = 0;
469
470 if (!io_ctl->check_crcs) {
471 io_ctl_unmap_page(io_ctl);
472 return;
473 }
474
475 if (index == 0)
Justin P. Mattockcb54f252011-11-21 08:43:28 -0800476 offset = sizeof(u32) * io_ctl->num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400477
Liu Bob0496682013-03-14 14:57:45 +0000478 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300479 PAGE_SIZE - offset);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +0100480 btrfs_csum_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400481 io_ctl_unmap_page(io_ctl);
Chris Mason2b108262015-04-06 07:48:20 -0700482 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400483 tmp += index;
484 *tmp = crc;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400485}
486
Chris Mason4c6d1d82015-04-06 13:17:20 -0700487static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400488{
489 u32 *tmp, val;
490 u32 crc = ~(u32)0;
491 unsigned offset = 0;
492
493 if (!io_ctl->check_crcs) {
494 io_ctl_map_page(io_ctl, 0);
495 return 0;
496 }
497
498 if (index == 0)
499 offset = sizeof(u32) * io_ctl->num_pages;
500
Chris Mason2b108262015-04-06 07:48:20 -0700501 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400502 tmp += index;
503 val = *tmp;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400504
505 io_ctl_map_page(io_ctl, 0);
Liu Bob0496682013-03-14 14:57:45 +0000506 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300507 PAGE_SIZE - offset);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +0100508 btrfs_csum_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400509 if (val != crc) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400510 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200511 "csum mismatch on free space cache");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400512 io_ctl_unmap_page(io_ctl);
513 return -EIO;
514 }
515
Josef Bacika67509c2011-10-05 15:18:58 -0400516 return 0;
517}
518
Chris Mason4c6d1d82015-04-06 13:17:20 -0700519static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400520 void *bitmap)
521{
522 struct btrfs_free_space_entry *entry;
523
524 if (!io_ctl->cur)
525 return -ENOSPC;
526
527 entry = io_ctl->cur;
528 entry->offset = cpu_to_le64(offset);
529 entry->bytes = cpu_to_le64(bytes);
530 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
531 BTRFS_FREE_SPACE_EXTENT;
532 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
533 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
534
535 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
536 return 0;
537
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400538 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400539
540 /* No more pages to map */
541 if (io_ctl->index >= io_ctl->num_pages)
542 return 0;
543
544 /* map the next page */
545 io_ctl_map_page(io_ctl, 1);
546 return 0;
547}
548
Chris Mason4c6d1d82015-04-06 13:17:20 -0700549static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
Josef Bacika67509c2011-10-05 15:18:58 -0400550{
551 if (!io_ctl->cur)
552 return -ENOSPC;
553
554 /*
555 * If we aren't at the start of the current page, unmap this one and
556 * map the next one if there is any left.
557 */
558 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400559 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400560 if (io_ctl->index >= io_ctl->num_pages)
561 return -ENOSPC;
562 io_ctl_map_page(io_ctl, 0);
563 }
564
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300565 memcpy(io_ctl->cur, bitmap, PAGE_SIZE);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400566 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400567 if (io_ctl->index < io_ctl->num_pages)
568 io_ctl_map_page(io_ctl, 0);
569 return 0;
570}
571
Chris Mason4c6d1d82015-04-06 13:17:20 -0700572static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400573{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400574 /*
575 * If we're not on the boundary we know we've modified the page and we
576 * need to crc the page.
577 */
578 if (io_ctl->cur != io_ctl->orig)
579 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
580 else
581 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400582
583 while (io_ctl->index < io_ctl->num_pages) {
584 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400585 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400586 }
587}
588
Chris Mason4c6d1d82015-04-06 13:17:20 -0700589static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400590 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400591{
592 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500593 int ret;
594
595 if (!io_ctl->cur) {
596 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
597 if (ret)
598 return ret;
599 }
Josef Bacika67509c2011-10-05 15:18:58 -0400600
601 e = io_ctl->cur;
602 entry->offset = le64_to_cpu(e->offset);
603 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400604 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400605 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
606 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
607
608 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400609 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400610
611 io_ctl_unmap_page(io_ctl);
612
Josef Bacik2f120c02011-11-10 20:45:05 -0500613 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400614}
615
Chris Mason4c6d1d82015-04-06 13:17:20 -0700616static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400617 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400618{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400619 int ret;
620
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400621 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
622 if (ret)
623 return ret;
624
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300625 memcpy(entry->bitmap, io_ctl->cur, PAGE_SIZE);
Josef Bacika67509c2011-10-05 15:18:58 -0400626 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400627
628 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400629}
630
Josef Bacikcd023e72012-05-14 10:06:40 -0400631/*
632 * Since we attach pinned extents after the fact we can have contiguous sections
633 * of free space that are split up in entries. This poses a problem with the
634 * tree logging stuff since it could have allocated across what appears to be 2
635 * entries since we would have merged the entries when adding the pinned extents
636 * back to the free space cache. So run through the space cache that we just
637 * loaded and merge contiguous entries. This will make the log replay stuff not
638 * blow up and it will make for nicer allocator behavior.
639 */
640static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
641{
642 struct btrfs_free_space *e, *prev = NULL;
643 struct rb_node *n;
644
645again:
646 spin_lock(&ctl->tree_lock);
647 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
648 e = rb_entry(n, struct btrfs_free_space, offset_index);
649 if (!prev)
650 goto next;
651 if (e->bitmap || prev->bitmap)
652 goto next;
653 if (prev->offset + prev->bytes == e->offset) {
654 unlink_free_space(ctl, prev);
655 unlink_free_space(ctl, e);
656 prev->bytes += e->bytes;
657 kmem_cache_free(btrfs_free_space_cachep, e);
658 link_free_space(ctl, prev);
659 prev = NULL;
660 spin_unlock(&ctl->tree_lock);
661 goto again;
662 }
663next:
664 prev = e;
665 }
666 spin_unlock(&ctl->tree_lock);
667}
668
Eric Sandeen48a3b632013-04-25 20:41:01 +0000669static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
670 struct btrfs_free_space_ctl *ctl,
671 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400672{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400673 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik9d66e232010-08-25 16:54:15 -0400674 struct btrfs_free_space_header *header;
675 struct extent_buffer *leaf;
Chris Mason4c6d1d82015-04-06 13:17:20 -0700676 struct btrfs_io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400677 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400678 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800679 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400680 u64 num_entries;
681 u64 num_bitmaps;
682 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400683 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400684 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400685
Josef Bacik9d66e232010-08-25 16:54:15 -0400686 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800687 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400688 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400689
690 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800691 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400692 key.type = 0;
693
694 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800695 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400696 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800697 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400698 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400699 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400700 }
701
Li Zefan0414efa2011-04-20 10:20:14 +0800702 ret = -1;
703
Josef Bacik9d66e232010-08-25 16:54:15 -0400704 leaf = path->nodes[0];
705 header = btrfs_item_ptr(leaf, path->slots[0],
706 struct btrfs_free_space_header);
707 num_entries = btrfs_free_space_entries(leaf, header);
708 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
709 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400710 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400711
Miao Xiee570fd22014-06-19 10:42:50 +0800712 if (!BTRFS_I(inode)->generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400713 btrfs_info(fs_info,
David Sterba913e1532017-07-13 15:32:18 +0200714 "the free space cache file (%llu) is invalid, skip it",
Miao Xiee570fd22014-06-19 10:42:50 +0800715 offset);
716 return 0;
717 }
718
Josef Bacik9d66e232010-08-25 16:54:15 -0400719 if (BTRFS_I(inode)->generation != generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400720 btrfs_err(fs_info,
721 "free space inode generation (%llu) did not match free space cache generation (%llu)",
722 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400723 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400724 }
725
726 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400727 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400728
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400729 ret = io_ctl_init(&io_ctl, inode, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800730 if (ret)
731 return ret;
732
David Sterba1d480532017-01-23 17:28:19 +0100733 readahead_cache(inode);
Josef Bacik9d66e232010-08-25 16:54:15 -0400734
Josef Bacika67509c2011-10-05 15:18:58 -0400735 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
736 if (ret)
737 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400738
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400739 ret = io_ctl_check_crc(&io_ctl, 0);
740 if (ret)
741 goto free_cache;
742
Josef Bacika67509c2011-10-05 15:18:58 -0400743 ret = io_ctl_check_generation(&io_ctl, generation);
744 if (ret)
745 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400746
Josef Bacika67509c2011-10-05 15:18:58 -0400747 while (num_entries) {
748 e = kmem_cache_zalloc(btrfs_free_space_cachep,
749 GFP_NOFS);
750 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400751 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400752
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400753 ret = io_ctl_read_entry(&io_ctl, e, &type);
754 if (ret) {
755 kmem_cache_free(btrfs_free_space_cachep, e);
756 goto free_cache;
757 }
758
Josef Bacika67509c2011-10-05 15:18:58 -0400759 if (!e->bytes) {
760 kmem_cache_free(btrfs_free_space_cachep, e);
761 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400762 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400763
Josef Bacika67509c2011-10-05 15:18:58 -0400764 if (type == BTRFS_FREE_SPACE_EXTENT) {
765 spin_lock(&ctl->tree_lock);
766 ret = link_free_space(ctl, e);
767 spin_unlock(&ctl->tree_lock);
768 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400769 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000770 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500771 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400772 goto free_cache;
773 }
Josef Bacika67509c2011-10-05 15:18:58 -0400774 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400775 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400776 num_bitmaps--;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300777 e->bitmap = kzalloc(PAGE_SIZE, GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400778 if (!e->bitmap) {
779 kmem_cache_free(
780 btrfs_free_space_cachep, e);
781 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400782 }
Josef Bacika67509c2011-10-05 15:18:58 -0400783 spin_lock(&ctl->tree_lock);
784 ret = link_free_space(ctl, e);
785 ctl->total_bitmaps++;
786 ctl->op->recalc_thresholds(ctl);
787 spin_unlock(&ctl->tree_lock);
788 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400789 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000790 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400791 kmem_cache_free(btrfs_free_space_cachep, e);
792 goto free_cache;
793 }
794 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400795 }
796
Josef Bacika67509c2011-10-05 15:18:58 -0400797 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400798 }
799
Josef Bacik2f120c02011-11-10 20:45:05 -0500800 io_ctl_unmap_page(&io_ctl);
801
Josef Bacika67509c2011-10-05 15:18:58 -0400802 /*
803 * We add the bitmaps at the end of the entries in order that
804 * the bitmap entries are added to the cache.
805 */
806 list_for_each_entry_safe(e, n, &bitmaps, list) {
807 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400808 ret = io_ctl_read_bitmap(&io_ctl, e);
809 if (ret)
810 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400811 }
812
813 io_ctl_drop_pages(&io_ctl);
Josef Bacikcd023e72012-05-14 10:06:40 -0400814 merge_space_tree(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400815 ret = 1;
816out:
Josef Bacika67509c2011-10-05 15:18:58 -0400817 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400818 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400819free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400820 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800821 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400822 goto out;
823}
824
Li Zefan0414efa2011-04-20 10:20:14 +0800825int load_free_space_cache(struct btrfs_fs_info *fs_info,
826 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400827{
Li Zefan34d52cb2011-03-29 13:46:06 +0800828 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800829 struct inode *inode;
830 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400831 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800832 bool matched;
833 u64 used = btrfs_block_group_used(&block_group->item);
834
835 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800836 * If this block group has been marked to be cleared for one reason or
837 * another then we can't trust the on disk cache, so just return.
838 */
839 spin_lock(&block_group->lock);
840 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
841 spin_unlock(&block_group->lock);
842 return 0;
843 }
844 spin_unlock(&block_group->lock);
845
846 path = btrfs_alloc_path();
847 if (!path)
848 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400849 path->search_commit_root = 1;
850 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800851
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500852 inode = lookup_free_space_inode(fs_info, block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +0800853 if (IS_ERR(inode)) {
854 btrfs_free_path(path);
855 return 0;
856 }
857
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400858 /* We may have converted the inode and made the cache invalid. */
859 spin_lock(&block_group->lock);
860 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
861 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900862 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400863 goto out;
864 }
865 spin_unlock(&block_group->lock);
866
Li Zefan0414efa2011-04-20 10:20:14 +0800867 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
868 path, block_group->key.objectid);
869 btrfs_free_path(path);
870 if (ret <= 0)
871 goto out;
872
873 spin_lock(&ctl->tree_lock);
874 matched = (ctl->free_space == (block_group->key.offset - used -
875 block_group->bytes_super));
876 spin_unlock(&ctl->tree_lock);
877
878 if (!matched) {
879 __btrfs_remove_free_space_cache(ctl);
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400880 btrfs_warn(fs_info,
881 "block group %llu has wrong amount of free space",
882 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800883 ret = -1;
884 }
885out:
886 if (ret < 0) {
887 /* This cache is bogus, make sure it gets cleared */
888 spin_lock(&block_group->lock);
889 block_group->disk_cache_state = BTRFS_DC_CLEAR;
890 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800891 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800892
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400893 btrfs_warn(fs_info,
894 "failed to load free space cache for block group %llu, rebuilding it now",
895 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800896 }
897
898 iput(inode);
899 return ret;
900}
901
Chris Masond4452bc2014-05-19 20:47:56 -0700902static noinline_for_stack
Chris Mason4c6d1d82015-04-06 13:17:20 -0700903int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -0700904 struct btrfs_free_space_ctl *ctl,
905 struct btrfs_block_group_cache *block_group,
906 int *entries, int *bitmaps,
907 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400908{
Josef Bacikc09544e2011-08-30 10:19:10 -0400909 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -0700910 struct btrfs_free_cluster *cluster = NULL;
Chris Mason1bbc6212015-04-06 12:46:08 -0700911 struct btrfs_free_cluster *cluster_locked = NULL;
Chris Masond4452bc2014-05-19 20:47:56 -0700912 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +0000913 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400914
Josef Bacik43be2142011-04-01 14:55:00 +0000915 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -0700916 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000917 cluster = list_entry(block_group->cluster_list.next,
918 struct btrfs_free_cluster,
919 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -0700920 }
Josef Bacik43be2142011-04-01 14:55:00 +0000921
Josef Bacikf75b1302011-10-05 10:00:18 -0400922 if (!node && cluster) {
Chris Mason1bbc6212015-04-06 12:46:08 -0700923 cluster_locked = cluster;
924 spin_lock(&cluster_locked->lock);
Josef Bacikf75b1302011-10-05 10:00:18 -0400925 node = rb_first(&cluster->root);
926 cluster = NULL;
927 }
928
Josef Bacik0cb59c92010-07-02 12:14:14 -0400929 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400930 while (node) {
931 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400932
Josef Bacika67509c2011-10-05 15:18:58 -0400933 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -0700934 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +0000935
Chris Masond4452bc2014-05-19 20:47:56 -0700936 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400937 e->bitmap);
938 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -0700939 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -0400940
941 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -0700942 list_add_tail(&e->list, bitmap_list);
943 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400944 }
945 node = rb_next(node);
946 if (!node && cluster) {
947 node = rb_first(&cluster->root);
Chris Mason1bbc6212015-04-06 12:46:08 -0700948 cluster_locked = cluster;
949 spin_lock(&cluster_locked->lock);
Josef Bacika67509c2011-10-05 15:18:58 -0400950 cluster = NULL;
951 }
952 }
Chris Mason1bbc6212015-04-06 12:46:08 -0700953 if (cluster_locked) {
954 spin_unlock(&cluster_locked->lock);
955 cluster_locked = NULL;
956 }
Filipe Manana55507ce2014-12-01 17:04:09 +0000957
958 /*
959 * Make sure we don't miss any range that was removed from our rbtree
960 * because trimming is running. Otherwise after a umount+mount (or crash
961 * after committing the transaction) we would leak free space and get
962 * an inconsistent free space cache report from fsck.
963 */
964 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
965 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
966 trim_entry->bytes, NULL);
967 if (ret)
968 goto fail;
969 *entries += 1;
970 }
971
Chris Masond4452bc2014-05-19 20:47:56 -0700972 return 0;
973fail:
Chris Mason1bbc6212015-04-06 12:46:08 -0700974 if (cluster_locked)
975 spin_unlock(&cluster_locked->lock);
Chris Masond4452bc2014-05-19 20:47:56 -0700976 return -ENOSPC;
977}
978
979static noinline_for_stack int
980update_cache_item(struct btrfs_trans_handle *trans,
981 struct btrfs_root *root,
982 struct inode *inode,
983 struct btrfs_path *path, u64 offset,
984 int entries, int bitmaps)
985{
986 struct btrfs_key key;
987 struct btrfs_free_space_header *header;
988 struct extent_buffer *leaf;
989 int ret;
990
991 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
992 key.offset = offset;
993 key.type = 0;
994
995 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
996 if (ret < 0) {
997 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
998 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
999 GFP_NOFS);
1000 goto fail;
1001 }
1002 leaf = path->nodes[0];
1003 if (ret > 0) {
1004 struct btrfs_key found_key;
1005 ASSERT(path->slots[0]);
1006 path->slots[0]--;
1007 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1008 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1009 found_key.offset != offset) {
1010 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
1011 inode->i_size - 1,
1012 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
1013 NULL, GFP_NOFS);
1014 btrfs_release_path(path);
1015 goto fail;
1016 }
1017 }
1018
1019 BTRFS_I(inode)->generation = trans->transid;
1020 header = btrfs_item_ptr(leaf, path->slots[0],
1021 struct btrfs_free_space_header);
1022 btrfs_set_free_space_entries(leaf, header, entries);
1023 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1024 btrfs_set_free_space_generation(leaf, header, trans->transid);
1025 btrfs_mark_buffer_dirty(leaf);
1026 btrfs_release_path(path);
1027
1028 return 0;
1029
1030fail:
1031 return -1;
1032}
1033
1034static noinline_for_stack int
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001035write_pinned_extent_entries(struct btrfs_fs_info *fs_info,
Miao Xie5349d6c2014-06-19 10:42:49 +08001036 struct btrfs_block_group_cache *block_group,
Chris Mason4c6d1d82015-04-06 13:17:20 -07001037 struct btrfs_io_ctl *io_ctl,
Miao Xie5349d6c2014-06-19 10:42:49 +08001038 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -07001039{
1040 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -07001041 struct extent_io_tree *unpin = NULL;
1042 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001043
Miao Xie5349d6c2014-06-19 10:42:49 +08001044 if (!block_group)
1045 return 0;
1046
Josef Bacika67509c2011-10-05 15:18:58 -04001047 /*
1048 * We want to add any pinned extents to our free space cache
1049 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001050 *
Li Zefandb804f22012-01-10 16:41:01 +08001051 * We shouldn't have switched the pinned extents yet so this is the
1052 * right one
1053 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001054 unpin = fs_info->pinned_extents;
Li Zefandb804f22012-01-10 16:41:01 +08001055
Miao Xie5349d6c2014-06-19 10:42:49 +08001056 start = block_group->key.objectid;
Li Zefandb804f22012-01-10 16:41:01 +08001057
Miao Xie5349d6c2014-06-19 10:42:49 +08001058 while (start < block_group->key.objectid + block_group->key.offset) {
Li Zefandb804f22012-01-10 16:41:01 +08001059 ret = find_first_extent_bit(unpin, start,
1060 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001061 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001062 if (ret)
1063 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001064
Josef Bacika67509c2011-10-05 15:18:58 -04001065 /* This pinned extent is out of our range */
Li Zefandb804f22012-01-10 16:41:01 +08001066 if (extent_start >= block_group->key.objectid +
Josef Bacika67509c2011-10-05 15:18:58 -04001067 block_group->key.offset)
Miao Xie5349d6c2014-06-19 10:42:49 +08001068 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001069
Li Zefandb804f22012-01-10 16:41:01 +08001070 extent_start = max(extent_start, start);
1071 extent_end = min(block_group->key.objectid +
1072 block_group->key.offset, extent_end + 1);
1073 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001074
Chris Masond4452bc2014-05-19 20:47:56 -07001075 *entries += 1;
1076 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001077 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001078 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001079
Li Zefandb804f22012-01-10 16:41:01 +08001080 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001081 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001082
Miao Xie5349d6c2014-06-19 10:42:49 +08001083 return 0;
1084}
1085
1086static noinline_for_stack int
Chris Mason4c6d1d82015-04-06 13:17:20 -07001087write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
Miao Xie5349d6c2014-06-19 10:42:49 +08001088{
Geliang Tang7ae16812015-12-18 22:17:00 +08001089 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001090 int ret;
1091
Josef Bacik0cb59c92010-07-02 12:14:14 -04001092 /* Write out the bitmaps */
Geliang Tang7ae16812015-12-18 22:17:00 +08001093 list_for_each_entry_safe(entry, next, bitmap_list, list) {
Chris Masond4452bc2014-05-19 20:47:56 -07001094 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001095 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001096 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001097 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001098 }
1099
Miao Xie5349d6c2014-06-19 10:42:49 +08001100 return 0;
1101}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001102
Miao Xie5349d6c2014-06-19 10:42:49 +08001103static int flush_dirty_cache(struct inode *inode)
1104{
1105 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001106
Josef Bacik0ef8b722013-10-25 16:13:35 -04001107 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001108 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001109 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1110 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
1111 GFP_NOFS);
Chris Masond4452bc2014-05-19 20:47:56 -07001112
Miao Xie5349d6c2014-06-19 10:42:49 +08001113 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001114}
1115
1116static void noinline_for_stack
Chris Masona3bdccc2015-04-24 11:00:00 -07001117cleanup_bitmap_list(struct list_head *bitmap_list)
Chris Masond4452bc2014-05-19 20:47:56 -07001118{
Geliang Tang7ae16812015-12-18 22:17:00 +08001119 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001120
Geliang Tang7ae16812015-12-18 22:17:00 +08001121 list_for_each_entry_safe(entry, next, bitmap_list, list)
Chris Masond4452bc2014-05-19 20:47:56 -07001122 list_del_init(&entry->list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001123}
1124
1125static void noinline_for_stack
1126cleanup_write_cache_enospc(struct inode *inode,
1127 struct btrfs_io_ctl *io_ctl,
David Sterba7bf1a152017-02-10 20:23:00 +01001128 struct extent_state **cached_state)
Chris Masona3bdccc2015-04-24 11:00:00 -07001129{
Chris Masond4452bc2014-05-19 20:47:56 -07001130 io_ctl_drop_pages(io_ctl);
1131 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1132 i_size_read(inode) - 1, cached_state,
1133 GFP_NOFS);
1134}
1135
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001136static int __btrfs_wait_cache_io(struct btrfs_root *root,
1137 struct btrfs_trans_handle *trans,
1138 struct btrfs_block_group_cache *block_group,
1139 struct btrfs_io_ctl *io_ctl,
1140 struct btrfs_path *path, u64 offset)
Chris Masonc9dc4c62015-04-04 17:14:42 -07001141{
1142 int ret;
1143 struct inode *inode = io_ctl->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001144 struct btrfs_fs_info *fs_info;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001145
Chris Mason1bbc6212015-04-06 12:46:08 -07001146 if (!inode)
1147 return 0;
1148
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001149 fs_info = btrfs_sb(inode->i_sb);
1150
Chris Masonc9dc4c62015-04-04 17:14:42 -07001151 /* Flush the dirty pages in the cache file. */
1152 ret = flush_dirty_cache(inode);
1153 if (ret)
1154 goto out;
1155
1156 /* Update the cache item to tell everyone this cache file is valid. */
1157 ret = update_cache_item(trans, root, inode, path, offset,
1158 io_ctl->entries, io_ctl->bitmaps);
1159out:
1160 io_ctl_free(io_ctl);
1161 if (ret) {
1162 invalidate_inode_pages2(inode->i_mapping);
1163 BTRFS_I(inode)->generation = 0;
1164 if (block_group) {
1165#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001166 btrfs_err(fs_info,
1167 "failed to write free space cache for block group %llu",
1168 block_group->key.objectid);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001169#endif
1170 }
1171 }
1172 btrfs_update_inode(trans, root, inode);
1173
1174 if (block_group) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001175 /* the dirty list is protected by the dirty_bgs_lock */
1176 spin_lock(&trans->transaction->dirty_bgs_lock);
1177
1178 /* the disk_cache_state is protected by the block group lock */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001179 spin_lock(&block_group->lock);
1180
1181 /*
1182 * only mark this as written if we didn't get put back on
Chris Mason1bbc6212015-04-06 12:46:08 -07001183 * the dirty list while waiting for IO. Otherwise our
1184 * cache state won't be right, and we won't get written again
Chris Masonc9dc4c62015-04-04 17:14:42 -07001185 */
1186 if (!ret && list_empty(&block_group->dirty_list))
1187 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1188 else if (ret)
1189 block_group->disk_cache_state = BTRFS_DC_ERROR;
1190
1191 spin_unlock(&block_group->lock);
Chris Mason1bbc6212015-04-06 12:46:08 -07001192 spin_unlock(&trans->transaction->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001193 io_ctl->inode = NULL;
1194 iput(inode);
1195 }
1196
1197 return ret;
1198
1199}
1200
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001201static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1202 struct btrfs_trans_handle *trans,
1203 struct btrfs_io_ctl *io_ctl,
1204 struct btrfs_path *path)
1205{
1206 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1207}
1208
1209int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
1210 struct btrfs_block_group_cache *block_group,
1211 struct btrfs_path *path)
1212{
1213 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1214 block_group, &block_group->io_ctl,
1215 path, block_group->key.objectid);
1216}
1217
Chris Masond4452bc2014-05-19 20:47:56 -07001218/**
1219 * __btrfs_write_out_cache - write out cached info to an inode
1220 * @root - the root the inode belongs to
1221 * @ctl - the free space cache we are going to write out
1222 * @block_group - the block_group for this cache if it belongs to a block_group
1223 * @trans - the trans handle
Chris Masond4452bc2014-05-19 20:47:56 -07001224 *
1225 * This function writes out a free space cache struct to disk for quick recovery
Geliang Tang8cd1e732015-10-04 17:05:32 +08001226 * on mount. This will return 0 if it was successful in writing the cache out,
Omar Sandovalb8605452015-02-24 02:47:06 -08001227 * or an errno if it was not.
Chris Masond4452bc2014-05-19 20:47:56 -07001228 */
1229static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1230 struct btrfs_free_space_ctl *ctl,
1231 struct btrfs_block_group_cache *block_group,
Chris Masonc9dc4c62015-04-04 17:14:42 -07001232 struct btrfs_io_ctl *io_ctl,
David Sterba0e8d9312017-02-10 20:26:24 +01001233 struct btrfs_trans_handle *trans)
Chris Masond4452bc2014-05-19 20:47:56 -07001234{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001235 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masond4452bc2014-05-19 20:47:56 -07001236 struct extent_state *cached_state = NULL;
Miao Xie5349d6c2014-06-19 10:42:49 +08001237 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001238 int entries = 0;
1239 int bitmaps = 0;
1240 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001241 int must_iput = 0;
Chris Masond4452bc2014-05-19 20:47:56 -07001242
1243 if (!i_size_read(inode))
Omar Sandovalb8605452015-02-24 02:47:06 -08001244 return -EIO;
Chris Masond4452bc2014-05-19 20:47:56 -07001245
Chris Masonc9dc4c62015-04-04 17:14:42 -07001246 WARN_ON(io_ctl->pages);
Jeff Mahoneyf15376d2016-06-22 18:56:18 -04001247 ret = io_ctl_init(io_ctl, inode, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001248 if (ret)
Omar Sandovalb8605452015-02-24 02:47:06 -08001249 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001250
Miao Xiee570fd22014-06-19 10:42:50 +08001251 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1252 down_write(&block_group->data_rwsem);
1253 spin_lock(&block_group->lock);
1254 if (block_group->delalloc_bytes) {
1255 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1256 spin_unlock(&block_group->lock);
1257 up_write(&block_group->data_rwsem);
1258 BTRFS_I(inode)->generation = 0;
1259 ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001260 must_iput = 1;
Miao Xiee570fd22014-06-19 10:42:50 +08001261 goto out;
1262 }
1263 spin_unlock(&block_group->lock);
1264 }
1265
Chris Masond4452bc2014-05-19 20:47:56 -07001266 /* Lock all pages first so we can lock the extent safely. */
Omar Sandovalb8605452015-02-24 02:47:06 -08001267 ret = io_ctl_prepare_pages(io_ctl, inode, 0);
1268 if (ret)
Josef Bacikb77000e2017-11-15 16:20:52 -05001269 goto out_unlock;
Chris Masond4452bc2014-05-19 20:47:56 -07001270
1271 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
David Sterbaff13db42015-12-03 14:30:40 +01001272 &cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001273
Chris Masonc9dc4c62015-04-04 17:14:42 -07001274 io_ctl_set_generation(io_ctl, trans->transid);
Chris Masond4452bc2014-05-19 20:47:56 -07001275
Filipe Manana55507ce2014-12-01 17:04:09 +00001276 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001277 /* Write out the extent entries in the free space cache */
Chris Mason1bbc6212015-04-06 12:46:08 -07001278 spin_lock(&ctl->tree_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001279 ret = write_cache_extent_entries(io_ctl, ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001280 block_group, &entries, &bitmaps,
1281 &bitmap_list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001282 if (ret)
1283 goto out_nospc_locked;
Chris Masond4452bc2014-05-19 20:47:56 -07001284
Miao Xie5349d6c2014-06-19 10:42:49 +08001285 /*
1286 * Some spaces that are freed in the current transaction are pinned,
1287 * they will be added into free space cache after the transaction is
1288 * committed, we shouldn't lose them.
Chris Mason1bbc6212015-04-06 12:46:08 -07001289 *
1290 * If this changes while we are working we'll get added back to
1291 * the dirty list and redo it. No locking needed
Miao Xie5349d6c2014-06-19 10:42:49 +08001292 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001293 ret = write_pinned_extent_entries(fs_info, block_group,
1294 io_ctl, &entries);
Chris Masona3bdccc2015-04-24 11:00:00 -07001295 if (ret)
1296 goto out_nospc_locked;
Miao Xie5349d6c2014-06-19 10:42:49 +08001297
Filipe Manana55507ce2014-12-01 17:04:09 +00001298 /*
1299 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1300 * locked while doing it because a concurrent trim can be manipulating
1301 * or freeing the bitmap.
1302 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001303 ret = write_bitmap_entries(io_ctl, &bitmap_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07001304 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00001305 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001306 if (ret)
1307 goto out_nospc;
1308
1309 /* Zero out the rest of the pages just to make sure */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001310 io_ctl_zero_remaining_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001311
1312 /* Everything is written out, now we dirty the pages in the file. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001313 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1314 i_size_read(inode), &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001315 if (ret)
1316 goto out_nospc;
1317
Miao Xiee570fd22014-06-19 10:42:50 +08001318 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1319 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001320 /*
1321 * Release the pages and unlock the extent, we will flush
1322 * them out later
1323 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001324 io_ctl_drop_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001325
1326 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1327 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
1328
Chris Masonc9dc4c62015-04-04 17:14:42 -07001329 /*
1330 * at this point the pages are under IO and we're happy,
1331 * The caller is responsible for waiting on them and updating the
1332 * the cache and the inode
1333 */
1334 io_ctl->entries = entries;
1335 io_ctl->bitmaps = bitmaps;
1336
1337 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001338 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001339 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001340
Chris Masonc9dc4c62015-04-04 17:14:42 -07001341 return 0;
1342
Josef Bacik2f356122011-06-10 15:31:13 -04001343out:
Chris Masonc9dc4c62015-04-04 17:14:42 -07001344 io_ctl->inode = NULL;
1345 io_ctl_free(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001346 if (ret) {
Josef Bacika67509c2011-10-05 15:18:58 -04001347 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001348 BTRFS_I(inode)->generation = 0;
1349 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001350 btrfs_update_inode(trans, root, inode);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001351 if (must_iput)
1352 iput(inode);
Miao Xie5349d6c2014-06-19 10:42:49 +08001353 return ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001354
Chris Masona3bdccc2015-04-24 11:00:00 -07001355out_nospc_locked:
1356 cleanup_bitmap_list(&bitmap_list);
1357 spin_unlock(&ctl->tree_lock);
1358 mutex_unlock(&ctl->cache_writeout_mutex);
1359
Josef Bacika67509c2011-10-05 15:18:58 -04001360out_nospc:
David Sterba7bf1a152017-02-10 20:23:00 +01001361 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
Miao Xiee570fd22014-06-19 10:42:50 +08001362
Josef Bacikb77000e2017-11-15 16:20:52 -05001363out_unlock:
Miao Xiee570fd22014-06-19 10:42:50 +08001364 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1365 up_write(&block_group->data_rwsem);
1366
Josef Bacika67509c2011-10-05 15:18:58 -04001367 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001368}
1369
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001370int btrfs_write_out_cache(struct btrfs_fs_info *fs_info,
Li Zefan0414efa2011-04-20 10:20:14 +08001371 struct btrfs_trans_handle *trans,
1372 struct btrfs_block_group_cache *block_group,
1373 struct btrfs_path *path)
1374{
1375 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1376 struct inode *inode;
1377 int ret = 0;
1378
Li Zefan0414efa2011-04-20 10:20:14 +08001379 spin_lock(&block_group->lock);
1380 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1381 spin_unlock(&block_group->lock);
1382 return 0;
1383 }
1384 spin_unlock(&block_group->lock);
1385
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001386 inode = lookup_free_space_inode(fs_info, block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +08001387 if (IS_ERR(inode))
1388 return 0;
1389
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001390 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1391 block_group, &block_group->io_ctl, trans);
Josef Bacikc09544e2011-08-30 10:19:10 -04001392 if (ret) {
Josef Bacikc09544e2011-08-30 10:19:10 -04001393#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001394 btrfs_err(fs_info,
1395 "failed to write free space cache for block group %llu",
1396 block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001397#endif
Chris Masonc9dc4c62015-04-04 17:14:42 -07001398 spin_lock(&block_group->lock);
1399 block_group->disk_cache_state = BTRFS_DC_ERROR;
1400 spin_unlock(&block_group->lock);
1401
1402 block_group->io_ctl.inode = NULL;
1403 iput(inode);
Li Zefan0414efa2011-04-20 10:20:14 +08001404 }
1405
Chris Masonc9dc4c62015-04-04 17:14:42 -07001406 /*
1407 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1408 * to wait for IO and put the inode
1409 */
1410
Josef Bacik0cb59c92010-07-02 12:14:14 -04001411 return ret;
1412}
1413
Li Zefan34d52cb2011-03-29 13:46:06 +08001414static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001415 u64 offset)
1416{
Josef Bacikb12d6862013-08-26 17:14:08 -04001417 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001418 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001419 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001420}
1421
Li Zefan34d52cb2011-03-29 13:46:06 +08001422static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001423{
Li Zefan34d52cb2011-03-29 13:46:06 +08001424 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001425}
1426
Li Zefan34d52cb2011-03-29 13:46:06 +08001427static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001428 u64 offset)
1429{
1430 u64 bitmap_start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001431 u64 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001432
Li Zefan34d52cb2011-03-29 13:46:06 +08001433 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1434 bitmap_start = offset - ctl->start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001435 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001436 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001437 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001438
1439 return bitmap_start;
1440}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001441
1442static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001443 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001444{
1445 struct rb_node **p = &root->rb_node;
1446 struct rb_node *parent = NULL;
1447 struct btrfs_free_space *info;
1448
1449 while (*p) {
1450 parent = *p;
1451 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1452
Josef Bacik96303082009-07-13 21:29:25 -04001453 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001454 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001455 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001456 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001457 } else {
1458 /*
1459 * we could have a bitmap entry and an extent entry
1460 * share the same offset. If this is the case, we want
1461 * the extent entry to always be found first if we do a
1462 * linear search through the tree, since we want to have
1463 * the quickest allocation time, and allocating from an
1464 * extent is faster than allocating from a bitmap. So
1465 * if we're inserting a bitmap and we find an entry at
1466 * this offset, we want to go right, or after this entry
1467 * logically. If we are inserting an extent and we've
1468 * found a bitmap, we want to go left, or before
1469 * logically.
1470 */
1471 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001472 if (info->bitmap) {
1473 WARN_ON_ONCE(1);
1474 return -EEXIST;
1475 }
Josef Bacik96303082009-07-13 21:29:25 -04001476 p = &(*p)->rb_right;
1477 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001478 if (!info->bitmap) {
1479 WARN_ON_ONCE(1);
1480 return -EEXIST;
1481 }
Josef Bacik96303082009-07-13 21:29:25 -04001482 p = &(*p)->rb_left;
1483 }
1484 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001485 }
1486
1487 rb_link_node(node, parent, p);
1488 rb_insert_color(node, root);
1489
1490 return 0;
1491}
1492
1493/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001494 * searches the tree for the given offset.
1495 *
Josef Bacik96303082009-07-13 21:29:25 -04001496 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1497 * want a section that has at least bytes size and comes at or after the given
1498 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001499 */
Josef Bacik96303082009-07-13 21:29:25 -04001500static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001501tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001502 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001503{
Li Zefan34d52cb2011-03-29 13:46:06 +08001504 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001505 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001506
Josef Bacik96303082009-07-13 21:29:25 -04001507 /* find entry that is closest to the 'offset' */
1508 while (1) {
1509 if (!n) {
1510 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001511 break;
1512 }
Josef Bacik96303082009-07-13 21:29:25 -04001513
1514 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1515 prev = entry;
1516
1517 if (offset < entry->offset)
1518 n = n->rb_left;
1519 else if (offset > entry->offset)
1520 n = n->rb_right;
1521 else
1522 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001523 }
1524
Josef Bacik96303082009-07-13 21:29:25 -04001525 if (bitmap_only) {
1526 if (!entry)
1527 return NULL;
1528 if (entry->bitmap)
1529 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001530
Josef Bacik96303082009-07-13 21:29:25 -04001531 /*
1532 * bitmap entry and extent entry may share same offset,
1533 * in that case, bitmap entry comes after extent entry.
1534 */
1535 n = rb_next(n);
1536 if (!n)
1537 return NULL;
1538 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1539 if (entry->offset != offset)
1540 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001541
Josef Bacik96303082009-07-13 21:29:25 -04001542 WARN_ON(!entry->bitmap);
1543 return entry;
1544 } else if (entry) {
1545 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001546 /*
Josef Bacik96303082009-07-13 21:29:25 -04001547 * if previous extent entry covers the offset,
1548 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001549 */
Miao Xiede6c4112012-10-18 08:18:01 +00001550 n = rb_prev(&entry->offset_index);
1551 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001552 prev = rb_entry(n, struct btrfs_free_space,
1553 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001554 if (!prev->bitmap &&
1555 prev->offset + prev->bytes > offset)
1556 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001557 }
Josef Bacik96303082009-07-13 21:29:25 -04001558 }
1559 return entry;
1560 }
1561
1562 if (!prev)
1563 return NULL;
1564
1565 /* find last entry before the 'offset' */
1566 entry = prev;
1567 if (entry->offset > offset) {
1568 n = rb_prev(&entry->offset_index);
1569 if (n) {
1570 entry = rb_entry(n, struct btrfs_free_space,
1571 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001572 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001573 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001574 if (fuzzy)
1575 return entry;
1576 else
1577 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001578 }
1579 }
1580
Josef Bacik96303082009-07-13 21:29:25 -04001581 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001582 n = rb_prev(&entry->offset_index);
1583 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001584 prev = rb_entry(n, struct btrfs_free_space,
1585 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001586 if (!prev->bitmap &&
1587 prev->offset + prev->bytes > offset)
1588 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001589 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001590 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001591 return entry;
1592 } else if (entry->offset + entry->bytes > offset)
1593 return entry;
1594
1595 if (!fuzzy)
1596 return NULL;
1597
1598 while (1) {
1599 if (entry->bitmap) {
1600 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001601 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001602 break;
1603 } else {
1604 if (entry->offset + entry->bytes > offset)
1605 break;
1606 }
1607
1608 n = rb_next(&entry->offset_index);
1609 if (!n)
1610 return NULL;
1611 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1612 }
1613 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001614}
1615
Li Zefanf333adb2010-11-09 14:57:39 +08001616static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001617__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001618 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001619{
Li Zefan34d52cb2011-03-29 13:46:06 +08001620 rb_erase(&info->offset_index, &ctl->free_space_offset);
1621 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001622}
1623
Li Zefan34d52cb2011-03-29 13:46:06 +08001624static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001625 struct btrfs_free_space *info)
1626{
Li Zefan34d52cb2011-03-29 13:46:06 +08001627 __unlink_free_space(ctl, info);
1628 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001629}
1630
Li Zefan34d52cb2011-03-29 13:46:06 +08001631static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001632 struct btrfs_free_space *info)
1633{
1634 int ret = 0;
1635
Josef Bacikb12d6862013-08-26 17:14:08 -04001636 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001637 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001638 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001639 if (ret)
1640 return ret;
1641
Li Zefan34d52cb2011-03-29 13:46:06 +08001642 ctl->free_space += info->bytes;
1643 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001644 return ret;
1645}
1646
Li Zefan34d52cb2011-03-29 13:46:06 +08001647static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001648{
Li Zefan34d52cb2011-03-29 13:46:06 +08001649 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001650 u64 max_bytes;
1651 u64 bitmap_bytes;
1652 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001653 u64 size = block_group->key.offset;
Feifei Xu0ef64472016-06-01 19:18:24 +08001654 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1655 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
Li Zefan34d52cb2011-03-29 13:46:06 +08001656
Feifei Xu0ef64472016-06-01 19:18:24 +08001657 max_bitmaps = max_t(u64, max_bitmaps, 1);
Josef Bacikdde57402013-02-12 14:07:51 -05001658
Josef Bacikb12d6862013-08-26 17:14:08 -04001659 ASSERT(ctl->total_bitmaps <= max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001660
1661 /*
1662 * The goal is to keep the total amount of memory used per 1gb of space
1663 * at or below 32k, so we need to adjust how much memory we allow to be
1664 * used by extent based free space tracking
1665 */
Byongho Leeee221842015-12-15 01:42:10 +09001666 if (size < SZ_1G)
Li Zefan8eb2d822010-11-09 14:48:01 +08001667 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1668 else
Byongho Leeee221842015-12-15 01:42:10 +09001669 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
Josef Bacik96303082009-07-13 21:29:25 -04001670
Josef Bacik25891f72009-09-11 16:11:20 -04001671 /*
1672 * we want to account for 1 more bitmap than what we have so we can make
1673 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1674 * we add more bitmaps.
1675 */
Feifei Xub9ef22d2016-06-01 19:18:25 +08001676 bitmap_bytes = (ctl->total_bitmaps + 1) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001677
Josef Bacik25891f72009-09-11 16:11:20 -04001678 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001679 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001680 return;
Josef Bacik96303082009-07-13 21:29:25 -04001681 }
Josef Bacik25891f72009-09-11 16:11:20 -04001682
1683 /*
David Sterbaf8c269d2015-01-16 17:21:12 +01001684 * we want the extent entry threshold to always be at most 1/2 the max
Josef Bacik25891f72009-09-11 16:11:20 -04001685 * bytes we can have, or whatever is less than that.
1686 */
1687 extent_bytes = max_bytes - bitmap_bytes;
David Sterbaf8c269d2015-01-16 17:21:12 +01001688 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
Josef Bacik25891f72009-09-11 16:11:20 -04001689
Li Zefan34d52cb2011-03-29 13:46:06 +08001690 ctl->extents_thresh =
David Sterbaf8c269d2015-01-16 17:21:12 +01001691 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
Josef Bacik96303082009-07-13 21:29:25 -04001692}
1693
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001694static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1695 struct btrfs_free_space *info,
1696 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001697{
Li Zefanf38b6e72011-03-14 13:40:51 +08001698 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001699
Li Zefan34d52cb2011-03-29 13:46:06 +08001700 start = offset_to_bit(info->offset, ctl->unit, offset);
1701 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001702 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001703
Li Zefanf38b6e72011-03-14 13:40:51 +08001704 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001705
1706 info->bytes -= bytes;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001707}
1708
1709static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1710 struct btrfs_free_space *info, u64 offset,
1711 u64 bytes)
1712{
1713 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001714 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001715}
1716
Li Zefan34d52cb2011-03-29 13:46:06 +08001717static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001718 struct btrfs_free_space *info, u64 offset,
1719 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001720{
Li Zefanf38b6e72011-03-14 13:40:51 +08001721 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001722
Li Zefan34d52cb2011-03-29 13:46:06 +08001723 start = offset_to_bit(info->offset, ctl->unit, offset);
1724 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001725 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001726
Li Zefanf38b6e72011-03-14 13:40:51 +08001727 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001728
1729 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001730 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001731}
1732
Miao Xiea4820392013-09-09 13:19:42 +08001733/*
1734 * If we can not find suitable extent, we will use bytes to record
1735 * the size of the max extent.
1736 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001737static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001738 struct btrfs_free_space *bitmap_info, u64 *offset,
Josef Bacik0584f712015-10-02 16:12:23 -04001739 u64 *bytes, bool for_alloc)
Josef Bacik96303082009-07-13 21:29:25 -04001740{
1741 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001742 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001743 unsigned long bits, i;
1744 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001745 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001746
Josef Bacikcef40482015-10-02 16:09:42 -04001747 /*
1748 * Skip searching the bitmap if we don't have a contiguous section that
1749 * is large enough for this allocation.
1750 */
Josef Bacik0584f712015-10-02 16:12:23 -04001751 if (for_alloc &&
1752 bitmap_info->max_extent_size &&
Josef Bacikcef40482015-10-02 16:09:42 -04001753 bitmap_info->max_extent_size < *bytes) {
1754 *bytes = bitmap_info->max_extent_size;
1755 return -1;
1756 }
1757
Li Zefan34d52cb2011-03-29 13:46:06 +08001758 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001759 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001760 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001761
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001762 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik0584f712015-10-02 16:12:23 -04001763 if (for_alloc && bits == 1) {
1764 found_bits = 1;
1765 break;
1766 }
Josef Bacik96303082009-07-13 21:29:25 -04001767 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1768 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001769 extent_bits = next_zero - i;
1770 if (extent_bits >= bits) {
1771 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001772 break;
Miao Xiea4820392013-09-09 13:19:42 +08001773 } else if (extent_bits > max_bits) {
1774 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001775 }
1776 i = next_zero;
1777 }
1778
1779 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001780 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1781 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001782 return 0;
1783 }
1784
Miao Xiea4820392013-09-09 13:19:42 +08001785 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacikcef40482015-10-02 16:09:42 -04001786 bitmap_info->max_extent_size = *bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001787 return -1;
1788}
1789
Miao Xiea4820392013-09-09 13:19:42 +08001790/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001791static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001792find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Miao Xiea4820392013-09-09 13:19:42 +08001793 unsigned long align, u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04001794{
1795 struct btrfs_free_space *entry;
1796 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001797 u64 tmp;
1798 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001799 int ret;
1800
Li Zefan34d52cb2011-03-29 13:46:06 +08001801 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001802 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001803
Li Zefan34d52cb2011-03-29 13:46:06 +08001804 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001805 if (!entry)
Miao Xiea4820392013-09-09 13:19:42 +08001806 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001807
1808 for (node = &entry->offset_index; node; node = rb_next(node)) {
1809 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Miao Xiea4820392013-09-09 13:19:42 +08001810 if (entry->bytes < *bytes) {
1811 if (entry->bytes > *max_extent_size)
1812 *max_extent_size = entry->bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001813 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001814 }
Josef Bacik96303082009-07-13 21:29:25 -04001815
David Woodhouse53b381b2013-01-29 18:40:14 -05001816 /* make sure the space returned is big enough
1817 * to match our requested alignment
1818 */
1819 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001820 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01001821 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05001822 tmp = tmp * align + ctl->start;
1823 align_off = tmp - entry->offset;
1824 } else {
1825 align_off = 0;
1826 tmp = entry->offset;
1827 }
1828
Miao Xiea4820392013-09-09 13:19:42 +08001829 if (entry->bytes < *bytes + align_off) {
1830 if (entry->bytes > *max_extent_size)
1831 *max_extent_size = entry->bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05001832 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001833 }
David Woodhouse53b381b2013-01-29 18:40:14 -05001834
Josef Bacik96303082009-07-13 21:29:25 -04001835 if (entry->bitmap) {
Miao Xiea4820392013-09-09 13:19:42 +08001836 u64 size = *bytes;
1837
Josef Bacik0584f712015-10-02 16:12:23 -04001838 ret = search_bitmap(ctl, entry, &tmp, &size, true);
David Woodhouse53b381b2013-01-29 18:40:14 -05001839 if (!ret) {
1840 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08001841 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04001842 return entry;
Miao Xiea4820392013-09-09 13:19:42 +08001843 } else if (size > *max_extent_size) {
1844 *max_extent_size = size;
David Woodhouse53b381b2013-01-29 18:40:14 -05001845 }
Josef Bacik96303082009-07-13 21:29:25 -04001846 continue;
1847 }
1848
David Woodhouse53b381b2013-01-29 18:40:14 -05001849 *offset = tmp;
1850 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001851 return entry;
1852 }
Miao Xiea4820392013-09-09 13:19:42 +08001853out:
Josef Bacik96303082009-07-13 21:29:25 -04001854 return NULL;
1855}
1856
Li Zefan34d52cb2011-03-29 13:46:06 +08001857static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001858 struct btrfs_free_space *info, u64 offset)
1859{
Li Zefan34d52cb2011-03-29 13:46:06 +08001860 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001861 info->bytes = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02001862 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08001863 link_free_space(ctl, info);
1864 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001865
Li Zefan34d52cb2011-03-29 13:46:06 +08001866 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001867}
1868
Li Zefan34d52cb2011-03-29 13:46:06 +08001869static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001870 struct btrfs_free_space *bitmap_info)
1871{
Li Zefan34d52cb2011-03-29 13:46:06 +08001872 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001873 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001874 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001875 ctl->total_bitmaps--;
1876 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001877}
1878
Li Zefan34d52cb2011-03-29 13:46:06 +08001879static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001880 struct btrfs_free_space *bitmap_info,
1881 u64 *offset, u64 *bytes)
1882{
1883 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001884 u64 search_start, search_bytes;
1885 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001886
1887again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001888 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001889
Josef Bacik6606bb92009-07-31 11:03:58 -04001890 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04001891 * We need to search for bits in this bitmap. We could only cover some
1892 * of the extent in this bitmap thanks to how we add space, so we need
1893 * to search for as much as it as we can and clear that amount, and then
1894 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04001895 */
1896 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001897 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00001898 search_bytes = min(search_bytes, end - search_start + 1);
Josef Bacik0584f712015-10-02 16:12:23 -04001899 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1900 false);
Josef Bacikb50c6e22013-04-25 15:55:30 -04001901 if (ret < 0 || search_start != *offset)
1902 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04001903
Josef Bacikbdb7d302012-06-27 15:10:56 -04001904 /* We may have found more bits than what we need */
1905 search_bytes = min(search_bytes, *bytes);
1906
1907 /* Cannot clear past the end of the bitmap */
1908 search_bytes = min(search_bytes, end - search_start + 1);
1909
1910 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1911 *offset += search_bytes;
1912 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001913
1914 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001915 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001916 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001917 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001918
Josef Bacik6606bb92009-07-31 11:03:58 -04001919 /*
1920 * no entry after this bitmap, but we still have bytes to
1921 * remove, so something has gone wrong.
1922 */
1923 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001924 return -EINVAL;
1925
Josef Bacik6606bb92009-07-31 11:03:58 -04001926 bitmap_info = rb_entry(next, struct btrfs_free_space,
1927 offset_index);
1928
1929 /*
1930 * if the next entry isn't a bitmap we need to return to let the
1931 * extent stuff do its work.
1932 */
Josef Bacik96303082009-07-13 21:29:25 -04001933 if (!bitmap_info->bitmap)
1934 return -EAGAIN;
1935
Josef Bacik6606bb92009-07-31 11:03:58 -04001936 /*
1937 * Ok the next item is a bitmap, but it may not actually hold
1938 * the information for the rest of this free space stuff, so
1939 * look for it, and if we don't find it return so we can try
1940 * everything over again.
1941 */
1942 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001943 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08001944 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik0584f712015-10-02 16:12:23 -04001945 &search_bytes, false);
Josef Bacik6606bb92009-07-31 11:03:58 -04001946 if (ret < 0 || search_start != *offset)
1947 return -EAGAIN;
1948
Josef Bacik96303082009-07-13 21:29:25 -04001949 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001950 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001951 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001952
1953 return 0;
1954}
1955
Josef Bacik2cdc3422011-05-27 14:07:49 -04001956static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1957 struct btrfs_free_space *info, u64 offset,
1958 u64 bytes)
1959{
1960 u64 bytes_to_set = 0;
1961 u64 end;
1962
1963 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1964
1965 bytes_to_set = min(end - offset, bytes);
1966
1967 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1968
Josef Bacikcef40482015-10-02 16:09:42 -04001969 /*
1970 * We set some bytes, we have no idea what the max extent size is
1971 * anymore.
1972 */
1973 info->max_extent_size = 0;
1974
Josef Bacik2cdc3422011-05-27 14:07:49 -04001975 return bytes_to_set;
1976
1977}
1978
Li Zefan34d52cb2011-03-29 13:46:06 +08001979static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1980 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001981{
Li Zefan34d52cb2011-03-29 13:46:06 +08001982 struct btrfs_block_group_cache *block_group = ctl->private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001983 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -04001984 bool forced = false;
1985
1986#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001987 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -04001988 forced = true;
1989#endif
Josef Bacik96303082009-07-13 21:29:25 -04001990
1991 /*
1992 * If we are below the extents threshold then we can add this as an
1993 * extent, and don't have to deal with the bitmap
1994 */
Josef Bacikd0bd4562015-09-23 14:54:14 -04001995 if (!forced && ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001996 /*
1997 * If this block group has some small extents we don't want to
1998 * use up all of our free slots in the cache with them, we want
Nicholas D Steeves01327612016-05-19 21:18:45 -04001999 * to reserve them to larger extents, however if we have plenty
Josef Bacik32cb0842011-03-18 16:16:21 -04002000 * of cache left then go ahead an dadd them, no sense in adding
2001 * the overhead of a bitmap if we don't have to.
2002 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002003 if (info->bytes <= fs_info->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002004 if (ctl->free_extents * 2 <= ctl->extents_thresh)
2005 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002006 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002007 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002008 }
2009 }
Josef Bacik96303082009-07-13 21:29:25 -04002010
2011 /*
Josef Bacikdde57402013-02-12 14:07:51 -05002012 * The original block groups from mkfs can be really small, like 8
2013 * megabytes, so don't bother with a bitmap for those entries. However
2014 * some block groups can be smaller than what a bitmap would cover but
2015 * are still large enough that they could overflow the 32k memory limit,
2016 * so allow those block groups to still be allowed to have a bitmap
2017 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04002018 */
Josef Bacikdde57402013-02-12 14:07:51 -05002019 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08002020 return false;
2021
2022 return true;
2023}
2024
David Sterba20e55062015-11-19 11:42:28 +01002025static const struct btrfs_free_space_op free_space_op = {
Josef Bacik2cdc3422011-05-27 14:07:49 -04002026 .recalc_thresholds = recalculate_thresholds,
2027 .use_bitmap = use_bitmap,
2028};
2029
Li Zefan34d52cb2011-03-29 13:46:06 +08002030static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2031 struct btrfs_free_space *info)
2032{
2033 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002034 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08002035 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002036 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08002037 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002038
2039 bytes = info->bytes;
2040 offset = info->offset;
2041
Li Zefan34d52cb2011-03-29 13:46:06 +08002042 if (!ctl->op->use_bitmap(ctl, info))
2043 return 0;
2044
Josef Bacik2cdc3422011-05-27 14:07:49 -04002045 if (ctl->op == &free_space_op)
2046 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04002047again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04002048 /*
2049 * Since we link bitmaps right into the cluster we need to see if we
2050 * have a cluster here, and if so and it has our bitmap we need to add
2051 * the free space to that bitmap.
2052 */
2053 if (block_group && !list_empty(&block_group->cluster_list)) {
2054 struct btrfs_free_cluster *cluster;
2055 struct rb_node *node;
2056 struct btrfs_free_space *entry;
2057
2058 cluster = list_entry(block_group->cluster_list.next,
2059 struct btrfs_free_cluster,
2060 block_group_list);
2061 spin_lock(&cluster->lock);
2062 node = rb_first(&cluster->root);
2063 if (!node) {
2064 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002065 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002066 }
2067
2068 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2069 if (!entry->bitmap) {
2070 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002071 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002072 }
2073
2074 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2075 bytes_added = add_bytes_to_bitmap(ctl, entry,
2076 offset, bytes);
2077 bytes -= bytes_added;
2078 offset += bytes_added;
2079 }
2080 spin_unlock(&cluster->lock);
2081 if (!bytes) {
2082 ret = 1;
2083 goto out;
2084 }
2085 }
Chris Mason38e87882011-06-10 16:36:57 -04002086
2087no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08002088 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04002089 1, 0);
2090 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04002091 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04002092 goto new_bitmap;
2093 }
2094
Josef Bacik2cdc3422011-05-27 14:07:49 -04002095 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2096 bytes -= bytes_added;
2097 offset += bytes_added;
2098 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002099
2100 if (!bytes) {
2101 ret = 1;
2102 goto out;
2103 } else
2104 goto again;
2105
2106new_bitmap:
2107 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002108 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04002109 added = 1;
2110 info = NULL;
2111 goto again;
2112 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002113 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002114
2115 /* no pre-allocated info, allocate a new one */
2116 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05002117 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2118 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04002119 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002120 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002121 ret = -ENOMEM;
2122 goto out;
2123 }
2124 }
2125
2126 /* allocate the bitmap */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002127 info->bitmap = kzalloc(PAGE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08002128 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002129 if (!info->bitmap) {
2130 ret = -ENOMEM;
2131 goto out;
2132 }
2133 goto again;
2134 }
2135
2136out:
2137 if (info) {
2138 if (info->bitmap)
2139 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002140 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002141 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002142
2143 return ret;
2144}
2145
Chris Mason945d8962011-05-22 12:33:42 -04002146static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08002147 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002148{
Li Zefan120d66e2010-11-09 14:56:50 +08002149 struct btrfs_free_space *left_info;
2150 struct btrfs_free_space *right_info;
2151 bool merged = false;
2152 u64 offset = info->offset;
2153 u64 bytes = info->bytes;
Josef Bacik6226cb02009-04-03 10:14:18 -04002154
Josef Bacik0f9dd462008-09-23 13:14:11 -04002155 /*
2156 * first we want to see if there is free space adjacent to the range we
2157 * are adding, if there is remove that struct and add a new one to
2158 * cover the entire range
2159 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002160 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002161 if (right_info && rb_prev(&right_info->offset_index))
2162 left_info = rb_entry(rb_prev(&right_info->offset_index),
2163 struct btrfs_free_space, offset_index);
2164 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002165 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002166
Josef Bacik96303082009-07-13 21:29:25 -04002167 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08002168 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002169 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002170 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002171 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002172 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002173 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002174 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002175 }
2176
Josef Bacik96303082009-07-13 21:29:25 -04002177 if (left_info && !left_info->bitmap &&
2178 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08002179 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002180 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002181 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002182 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002183 info->offset = left_info->offset;
2184 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002185 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002186 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002187 }
2188
Li Zefan120d66e2010-11-09 14:56:50 +08002189 return merged;
2190}
2191
Filipe Manana20005522014-08-29 13:35:13 +01002192static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2193 struct btrfs_free_space *info,
2194 bool update_stat)
2195{
2196 struct btrfs_free_space *bitmap;
2197 unsigned long i;
2198 unsigned long j;
2199 const u64 end = info->offset + info->bytes;
2200 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2201 u64 bytes;
2202
2203 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2204 if (!bitmap)
2205 return false;
2206
2207 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2208 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2209 if (j == i)
2210 return false;
2211 bytes = (j - i) * ctl->unit;
2212 info->bytes += bytes;
2213
2214 if (update_stat)
2215 bitmap_clear_bits(ctl, bitmap, end, bytes);
2216 else
2217 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2218
2219 if (!bitmap->bytes)
2220 free_bitmap(ctl, bitmap);
2221
2222 return true;
2223}
2224
2225static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2226 struct btrfs_free_space *info,
2227 bool update_stat)
2228{
2229 struct btrfs_free_space *bitmap;
2230 u64 bitmap_offset;
2231 unsigned long i;
2232 unsigned long j;
2233 unsigned long prev_j;
2234 u64 bytes;
2235
2236 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2237 /* If we're on a boundary, try the previous logical bitmap. */
2238 if (bitmap_offset == info->offset) {
2239 if (info->offset == 0)
2240 return false;
2241 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2242 }
2243
2244 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2245 if (!bitmap)
2246 return false;
2247
2248 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2249 j = 0;
2250 prev_j = (unsigned long)-1;
2251 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2252 if (j > i)
2253 break;
2254 prev_j = j;
2255 }
2256 if (prev_j == i)
2257 return false;
2258
2259 if (prev_j == (unsigned long)-1)
2260 bytes = (i + 1) * ctl->unit;
2261 else
2262 bytes = (i - prev_j) * ctl->unit;
2263
2264 info->offset -= bytes;
2265 info->bytes += bytes;
2266
2267 if (update_stat)
2268 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2269 else
2270 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2271
2272 if (!bitmap->bytes)
2273 free_bitmap(ctl, bitmap);
2274
2275 return true;
2276}
2277
2278/*
2279 * We prefer always to allocate from extent entries, both for clustered and
2280 * non-clustered allocation requests. So when attempting to add a new extent
2281 * entry, try to see if there's adjacent free space in bitmap entries, and if
2282 * there is, migrate that space from the bitmaps to the extent.
2283 * Like this we get better chances of satisfying space allocation requests
2284 * because we attempt to satisfy them based on a single cache entry, and never
2285 * on 2 or more entries - even if the entries represent a contiguous free space
2286 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2287 * ends).
2288 */
2289static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2290 struct btrfs_free_space *info,
2291 bool update_stat)
2292{
2293 /*
2294 * Only work with disconnected entries, as we can change their offset,
2295 * and must be extent entries.
2296 */
2297 ASSERT(!info->bitmap);
2298 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2299
2300 if (ctl->total_bitmaps > 0) {
2301 bool stole_end;
2302 bool stole_front = false;
2303
2304 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2305 if (ctl->total_bitmaps > 0)
2306 stole_front = steal_from_bitmap_to_front(ctl, info,
2307 update_stat);
2308
2309 if (stole_end || stole_front)
2310 try_merge_free_space(ctl, info, update_stat);
2311 }
2312}
2313
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002314int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2315 struct btrfs_free_space_ctl *ctl,
Li Zefan581bb052011-04-20 10:06:11 +08002316 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08002317{
2318 struct btrfs_free_space *info;
2319 int ret = 0;
2320
Josef Bacikdc89e982011-01-28 17:05:48 -05002321 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002322 if (!info)
2323 return -ENOMEM;
2324
2325 info->offset = offset;
2326 info->bytes = bytes;
Filipe Manana20005522014-08-29 13:35:13 +01002327 RB_CLEAR_NODE(&info->offset_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002328
Li Zefan34d52cb2011-03-29 13:46:06 +08002329 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002330
Li Zefan34d52cb2011-03-29 13:46:06 +08002331 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002332 goto link;
2333
2334 /*
2335 * There was no extent directly to the left or right of this new
2336 * extent then we know we're going to have to allocate a new extent, so
2337 * before we do that see if we need to drop this into a bitmap
2338 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002339 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002340 if (ret < 0) {
2341 goto out;
2342 } else if (ret) {
2343 ret = 0;
2344 goto out;
2345 }
2346link:
Filipe Manana20005522014-08-29 13:35:13 +01002347 /*
2348 * Only steal free space from adjacent bitmaps if we're sure we're not
2349 * going to add the new free space to existing bitmap entries - because
2350 * that would mean unnecessary work that would be reverted. Therefore
2351 * attempt to steal space from bitmaps if we're adding an extent entry.
2352 */
2353 steal_from_bitmap(ctl, info, true);
2354
Li Zefan34d52cb2011-03-29 13:46:06 +08002355 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002356 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002357 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002358out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002359 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002360
Josef Bacik0f9dd462008-09-23 13:14:11 -04002361 if (ret) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002362 btrfs_crit(fs_info, "unable to add free space :%d", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002363 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002364 }
2365
Josef Bacik0f9dd462008-09-23 13:14:11 -04002366 return ret;
2367}
2368
Josef Bacik6226cb02009-04-03 10:14:18 -04002369int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2370 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002371{
Li Zefan34d52cb2011-03-29 13:46:06 +08002372 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002373 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002374 int ret;
2375 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002376
Li Zefan34d52cb2011-03-29 13:46:06 +08002377 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002378
Josef Bacik96303082009-07-13 21:29:25 -04002379again:
Josef Bacikb0175112012-12-18 11:39:19 -05002380 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002381 if (!bytes)
2382 goto out_lock;
2383
Li Zefan34d52cb2011-03-29 13:46:06 +08002384 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002385 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002386 /*
2387 * oops didn't find an extent that matched the space we wanted
2388 * to remove, look for a bitmap instead
2389 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002390 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002391 1, 0);
2392 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002393 /*
2394 * If we found a partial bit of our free space in a
2395 * bitmap but then couldn't find the other part this may
2396 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002397 */
Josef Bacikb0175112012-12-18 11:39:19 -05002398 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002399 goto out_lock;
2400 }
Josef Bacik96303082009-07-13 21:29:25 -04002401 }
2402
Josef Bacikb0175112012-12-18 11:39:19 -05002403 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002404 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002405 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002406 if (offset == info->offset) {
2407 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002408
Josef Bacikbdb7d302012-06-27 15:10:56 -04002409 info->bytes -= to_free;
2410 info->offset += to_free;
2411 if (info->bytes) {
2412 ret = link_free_space(ctl, info);
2413 WARN_ON(ret);
2414 } else {
2415 kmem_cache_free(btrfs_free_space_cachep, info);
2416 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002417
Josef Bacikbdb7d302012-06-27 15:10:56 -04002418 offset += to_free;
2419 bytes -= to_free;
2420 goto again;
2421 } else {
2422 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002423
Josef Bacikbdb7d302012-06-27 15:10:56 -04002424 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002425 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002426 WARN_ON(ret);
2427 if (ret)
2428 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002429
Josef Bacikbdb7d302012-06-27 15:10:56 -04002430 /* Not enough bytes in this entry to satisfy us */
2431 if (old_end < offset + bytes) {
2432 bytes -= old_end - offset;
2433 offset = old_end;
2434 goto again;
2435 } else if (old_end == offset + bytes) {
2436 /* all done */
2437 goto out_lock;
2438 }
2439 spin_unlock(&ctl->tree_lock);
2440
2441 ret = btrfs_add_free_space(block_group, offset + bytes,
2442 old_end - (offset + bytes));
2443 WARN_ON(ret);
2444 goto out;
2445 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002446 }
Josef Bacik96303082009-07-13 21:29:25 -04002447
Li Zefan34d52cb2011-03-29 13:46:06 +08002448 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002449 if (ret == -EAGAIN) {
2450 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002451 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002452 }
Josef Bacik96303082009-07-13 21:29:25 -04002453out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08002454 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002455out:
Josef Bacik25179202008-10-29 14:49:05 -04002456 return ret;
2457}
2458
Josef Bacik0f9dd462008-09-23 13:14:11 -04002459void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2460 u64 bytes)
2461{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002462 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002463 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002464 struct btrfs_free_space *info;
2465 struct rb_node *n;
2466 int count = 0;
2467
Li Zefan34d52cb2011-03-29 13:46:06 +08002468 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002469 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002470 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002471 count++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002472 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
Frank Holtonefe120a2013-12-20 11:37:06 -05002473 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002474 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002475 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002476 btrfs_info(fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002477 list_empty(&block_group->cluster_list) ? "no" : "yes");
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002478 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002479 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002480}
2481
Li Zefan34d52cb2011-03-29 13:46:06 +08002482void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002483{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002484 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002485 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002486
Li Zefan34d52cb2011-03-29 13:46:06 +08002487 spin_lock_init(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002488 ctl->unit = fs_info->sectorsize;
Li Zefan34d52cb2011-03-29 13:46:06 +08002489 ctl->start = block_group->key.objectid;
2490 ctl->private = block_group;
2491 ctl->op = &free_space_op;
Filipe Manana55507ce2014-12-01 17:04:09 +00002492 INIT_LIST_HEAD(&ctl->trimming_ranges);
2493 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002494
Li Zefan34d52cb2011-03-29 13:46:06 +08002495 /*
2496 * we only want to have 32k of ram per block group for keeping
2497 * track of free space, and if we pass 1/2 of that we want to
2498 * start converting things over to using bitmaps
2499 */
Byongho Leeee221842015-12-15 01:42:10 +09002500 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002501}
2502
Chris Masonfa9c0d792009-04-03 09:47:43 -04002503/*
2504 * for a given cluster, put all of its extents back into the free
2505 * space cache. If the block group passed doesn't match the block group
2506 * pointed to by the cluster, someone else raced in and freed the
2507 * cluster already. In that case, we just return without changing anything
2508 */
2509static int
2510__btrfs_return_cluster_to_free_space(
2511 struct btrfs_block_group_cache *block_group,
2512 struct btrfs_free_cluster *cluster)
2513{
Li Zefan34d52cb2011-03-29 13:46:06 +08002514 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002515 struct btrfs_free_space *entry;
2516 struct rb_node *node;
2517
2518 spin_lock(&cluster->lock);
2519 if (cluster->block_group != block_group)
2520 goto out;
2521
Josef Bacik96303082009-07-13 21:29:25 -04002522 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002523 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002524 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002525
Chris Masonfa9c0d792009-04-03 09:47:43 -04002526 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002527 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002528 bool bitmap;
2529
Chris Masonfa9c0d792009-04-03 09:47:43 -04002530 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2531 node = rb_next(&entry->offset_index);
2532 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002533 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002534
2535 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002536 if (!bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002537 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002538 steal_from_bitmap(ctl, entry, false);
2539 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002540 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002541 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002542 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002543 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002544
Chris Masonfa9c0d792009-04-03 09:47:43 -04002545out:
2546 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002547 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002548 return 0;
2549}
2550
Eric Sandeen48a3b632013-04-25 20:41:01 +00002551static void __btrfs_remove_free_space_cache_locked(
2552 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002553{
2554 struct btrfs_free_space *info;
2555 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002556
Li Zefan581bb052011-04-20 10:06:11 +08002557 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2558 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002559 if (!info->bitmap) {
2560 unlink_free_space(ctl, info);
2561 kmem_cache_free(btrfs_free_space_cachep, info);
2562 } else {
2563 free_bitmap(ctl, info);
2564 }
David Sterba351810c2015-01-08 15:20:54 +01002565
2566 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002567 }
Chris Mason09655372011-05-21 09:27:38 -04002568}
2569
2570void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2571{
2572 spin_lock(&ctl->tree_lock);
2573 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002574 spin_unlock(&ctl->tree_lock);
2575}
2576
Josef Bacik0f9dd462008-09-23 13:14:11 -04002577void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2578{
Li Zefan34d52cb2011-03-29 13:46:06 +08002579 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002580 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002581 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002582
Li Zefan34d52cb2011-03-29 13:46:06 +08002583 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002584 while ((head = block_group->cluster_list.next) !=
2585 &block_group->cluster_list) {
2586 cluster = list_entry(head, struct btrfs_free_cluster,
2587 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002588
2589 WARN_ON(cluster->block_group != block_group);
2590 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01002591
2592 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002593 }
Chris Mason09655372011-05-21 09:27:38 -04002594 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002595 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002596
Josef Bacik0f9dd462008-09-23 13:14:11 -04002597}
2598
Josef Bacik6226cb02009-04-03 10:14:18 -04002599u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002600 u64 offset, u64 bytes, u64 empty_size,
2601 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002602{
Li Zefan34d52cb2011-03-29 13:46:06 +08002603 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb02009-04-03 10:14:18 -04002604 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002605 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb02009-04-03 10:14:18 -04002606 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002607 u64 align_gap = 0;
2608 u64 align_gap_len = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002609
Li Zefan34d52cb2011-03-29 13:46:06 +08002610 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002611 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002612 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb02009-04-03 10:14:18 -04002613 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002614 goto out;
2615
2616 ret = offset;
2617 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002618 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002619 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002620 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002621 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002622 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002623 align_gap_len = offset - entry->offset;
2624 align_gap = entry->offset;
2625
2626 entry->offset = offset + bytes;
2627 WARN_ON(entry->bytes < bytes + align_gap_len);
2628
2629 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb02009-04-03 10:14:18 -04002630 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002631 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002632 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002633 link_free_space(ctl, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002634 }
Josef Bacik96303082009-07-13 21:29:25 -04002635out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002636 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002637
David Woodhouse53b381b2013-01-29 18:40:14 -05002638 if (align_gap_len)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002639 __btrfs_add_free_space(block_group->fs_info, ctl,
2640 align_gap, align_gap_len);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002641 return ret;
2642}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002643
2644/*
2645 * given a cluster, put all of its extents back into the free space
2646 * cache. If a block group is passed, this function will only free
2647 * a cluster that belongs to the passed block group.
2648 *
2649 * Otherwise, it'll get a reference on the block group pointed to by the
2650 * cluster and remove the cluster from it.
2651 */
2652int btrfs_return_cluster_to_free_space(
2653 struct btrfs_block_group_cache *block_group,
2654 struct btrfs_free_cluster *cluster)
2655{
Li Zefan34d52cb2011-03-29 13:46:06 +08002656 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002657 int ret;
2658
2659 /* first, get a safe pointer to the block group */
2660 spin_lock(&cluster->lock);
2661 if (!block_group) {
2662 block_group = cluster->block_group;
2663 if (!block_group) {
2664 spin_unlock(&cluster->lock);
2665 return 0;
2666 }
2667 } else if (cluster->block_group != block_group) {
2668 /* someone else has already freed it don't redo their work */
2669 spin_unlock(&cluster->lock);
2670 return 0;
2671 }
2672 atomic_inc(&block_group->count);
2673 spin_unlock(&cluster->lock);
2674
Li Zefan34d52cb2011-03-29 13:46:06 +08002675 ctl = block_group->free_space_ctl;
2676
Chris Masonfa9c0d792009-04-03 09:47:43 -04002677 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002678 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002679 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002680 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002681
2682 /* finally drop our ref */
2683 btrfs_put_block_group(block_group);
2684 return ret;
2685}
2686
Josef Bacik96303082009-07-13 21:29:25 -04002687static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2688 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002689 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002690 u64 bytes, u64 min_start,
2691 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002692{
Li Zefan34d52cb2011-03-29 13:46:06 +08002693 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002694 int err;
2695 u64 search_start = cluster->window_start;
2696 u64 search_bytes = bytes;
2697 u64 ret = 0;
2698
Josef Bacik96303082009-07-13 21:29:25 -04002699 search_start = min_start;
2700 search_bytes = bytes;
2701
Josef Bacik0584f712015-10-02 16:12:23 -04002702 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
Miao Xiea4820392013-09-09 13:19:42 +08002703 if (err) {
2704 if (search_bytes > *max_extent_size)
2705 *max_extent_size = search_bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002706 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002707 }
Josef Bacik96303082009-07-13 21:29:25 -04002708
2709 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002710 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002711
2712 return ret;
2713}
2714
Chris Masonfa9c0d792009-04-03 09:47:43 -04002715/*
2716 * given a cluster, try to allocate 'bytes' from it, returns 0
2717 * if it couldn't find anything suitably large, or a logical disk offset
2718 * if things worked out
2719 */
2720u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2721 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002722 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002723{
Li Zefan34d52cb2011-03-29 13:46:06 +08002724 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002725 struct btrfs_free_space *entry = NULL;
2726 struct rb_node *node;
2727 u64 ret = 0;
2728
2729 spin_lock(&cluster->lock);
2730 if (bytes > cluster->max_size)
2731 goto out;
2732
2733 if (cluster->block_group != block_group)
2734 goto out;
2735
2736 node = rb_first(&cluster->root);
2737 if (!node)
2738 goto out;
2739
2740 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302741 while (1) {
Miao Xiea4820392013-09-09 13:19:42 +08002742 if (entry->bytes < bytes && entry->bytes > *max_extent_size)
2743 *max_extent_size = entry->bytes;
2744
Josef Bacik4e69b592011-03-21 10:11:24 -04002745 if (entry->bytes < bytes ||
2746 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002747 node = rb_next(&entry->offset_index);
2748 if (!node)
2749 break;
2750 entry = rb_entry(node, struct btrfs_free_space,
2751 offset_index);
2752 continue;
2753 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002754
Josef Bacik4e69b592011-03-21 10:11:24 -04002755 if (entry->bitmap) {
2756 ret = btrfs_alloc_from_bitmap(block_group,
2757 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002758 cluster->window_start,
2759 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002760 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002761 node = rb_next(&entry->offset_index);
2762 if (!node)
2763 break;
2764 entry = rb_entry(node, struct btrfs_free_space,
2765 offset_index);
2766 continue;
2767 }
Josef Bacik9b230622012-01-26 15:01:12 -05002768 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002769 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002770 ret = entry->offset;
2771
2772 entry->offset += bytes;
2773 entry->bytes -= bytes;
2774 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002775
Li Zefan5e71b5d2010-11-09 14:55:34 +08002776 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002777 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002778 break;
2779 }
2780out:
2781 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002782
Li Zefan5e71b5d2010-11-09 14:55:34 +08002783 if (!ret)
2784 return 0;
2785
Li Zefan34d52cb2011-03-29 13:46:06 +08002786 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002787
Li Zefan34d52cb2011-03-29 13:46:06 +08002788 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002789 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002790 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002791 if (entry->bitmap) {
2792 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002793 ctl->total_bitmaps--;
2794 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002795 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002796 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002797 }
2798
Li Zefan34d52cb2011-03-29 13:46:06 +08002799 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002800
Chris Masonfa9c0d792009-04-03 09:47:43 -04002801 return ret;
2802}
2803
Josef Bacik96303082009-07-13 21:29:25 -04002804static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2805 struct btrfs_free_space *entry,
2806 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002807 u64 offset, u64 bytes,
2808 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04002809{
Li Zefan34d52cb2011-03-29 13:46:06 +08002810 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002811 unsigned long next_zero;
2812 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002813 unsigned long want_bits;
2814 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002815 unsigned long found_bits;
Josef Bacikcef40482015-10-02 16:09:42 -04002816 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002817 unsigned long start = 0;
2818 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002819 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002820
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002821 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04002822 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002823 want_bits = bytes_to_bits(bytes, ctl->unit);
2824 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04002825
Josef Bacikcef40482015-10-02 16:09:42 -04002826 /*
2827 * Don't bother looking for a cluster in this bitmap if it's heavily
2828 * fragmented.
2829 */
2830 if (entry->max_extent_size &&
2831 entry->max_extent_size < cont1_bytes)
2832 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002833again:
2834 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06002835 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04002836 next_zero = find_next_zero_bit(entry->bitmap,
2837 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002838 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04002839 found_bits = next_zero - i;
Josef Bacikcef40482015-10-02 16:09:42 -04002840 if (found_bits > max_bits)
2841 max_bits = found_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002842 break;
2843 }
Josef Bacikcef40482015-10-02 16:09:42 -04002844 if (next_zero - i > max_bits)
2845 max_bits = next_zero - i;
Josef Bacik96303082009-07-13 21:29:25 -04002846 i = next_zero;
2847 }
2848
Josef Bacikcef40482015-10-02 16:09:42 -04002849 if (!found_bits) {
2850 entry->max_extent_size = (u64)max_bits * ctl->unit;
Josef Bacik4e69b592011-03-21 10:11:24 -04002851 return -ENOSPC;
Josef Bacikcef40482015-10-02 16:09:42 -04002852 }
Josef Bacik96303082009-07-13 21:29:25 -04002853
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002854 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04002855 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05002856 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002857 }
2858
2859 total_found += found_bits;
2860
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002861 if (cluster->max_size < found_bits * ctl->unit)
2862 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04002863
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002864 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2865 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04002866 goto again;
2867 }
2868
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002869 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002870 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002871 ret = tree_insert_offset(&cluster->root, entry->offset,
2872 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04002873 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04002874
Josef Bacik3f7de032011-11-10 08:29:20 -05002875 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002876 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04002877 return 0;
2878}
2879
Chris Masonfa9c0d792009-04-03 09:47:43 -04002880/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002881 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002882 * Try to find a cluster with at least bytes total bytes, at least one
2883 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04002884 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002885static noinline int
2886setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2887 struct btrfs_free_cluster *cluster,
2888 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002889 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002890{
Li Zefan34d52cb2011-03-29 13:46:06 +08002891 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002892 struct btrfs_free_space *first = NULL;
2893 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002894 struct btrfs_free_space *last;
2895 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04002896 u64 window_free;
2897 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002898 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002899
Li Zefan34d52cb2011-03-29 13:46:06 +08002900 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002901 if (!entry)
2902 return -ENOSPC;
2903
2904 /*
2905 * We don't want bitmaps, so just move along until we find a normal
2906 * extent entry.
2907 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002908 while (entry->bitmap || entry->bytes < min_bytes) {
2909 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04002910 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002911 node = rb_next(&entry->offset_index);
2912 if (!node)
2913 return -ENOSPC;
2914 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2915 }
2916
Josef Bacik4e69b592011-03-21 10:11:24 -04002917 window_free = entry->bytes;
2918 max_extent = entry->bytes;
2919 first = entry;
2920 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002921
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002922 for (node = rb_next(&entry->offset_index); node;
2923 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002924 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2925
Josef Bacik86d4a772011-05-25 13:03:16 -04002926 if (entry->bitmap) {
2927 if (list_empty(&entry->list))
2928 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002929 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002930 }
2931
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002932 if (entry->bytes < min_bytes)
2933 continue;
2934
2935 last = entry;
2936 window_free += entry->bytes;
2937 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04002938 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002939 }
2940
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002941 if (window_free < bytes || max_extent < cont1_bytes)
2942 return -ENOSPC;
2943
Josef Bacik4e69b592011-03-21 10:11:24 -04002944 cluster->window_start = first->offset;
2945
2946 node = &first->offset_index;
2947
2948 /*
2949 * now we've found our entries, pull them out of the free space
2950 * cache and put them into the cluster rbtree
2951 */
2952 do {
2953 int ret;
2954
2955 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2956 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002957 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002958 continue;
2959
Li Zefan34d52cb2011-03-29 13:46:06 +08002960 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002961 ret = tree_insert_offset(&cluster->root, entry->offset,
2962 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05002963 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04002964 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04002965 } while (node && entry != last);
2966
2967 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002968 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04002969 return 0;
2970}
2971
2972/*
2973 * This specifically looks for bitmaps that may work in the cluster, we assume
2974 * that we have already failed to find extents that will work.
2975 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002976static noinline int
2977setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2978 struct btrfs_free_cluster *cluster,
2979 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002980 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002981{
Li Zefan34d52cb2011-03-29 13:46:06 +08002982 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Mason1b9b9222015-12-15 07:15:32 -08002983 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002984 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05002985 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002986
Li Zefan34d52cb2011-03-29 13:46:06 +08002987 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002988 return -ENOSPC;
2989
Josef Bacik86d4a772011-05-25 13:03:16 -04002990 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05002991 * The bitmap that covers offset won't be in the list unless offset
2992 * is just its start offset.
2993 */
Chris Mason1b9b9222015-12-15 07:15:32 -08002994 if (!list_empty(bitmaps))
2995 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
2996
2997 if (!entry || entry->offset != bitmap_offset) {
Li Zefan0f0fbf12011-11-20 07:33:38 -05002998 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
2999 if (entry && list_empty(&entry->list))
3000 list_add(&entry->list, bitmaps);
3001 }
3002
Josef Bacik86d4a772011-05-25 13:03:16 -04003003 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05003004 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04003005 continue;
3006 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003007 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003008 if (!ret)
3009 return 0;
3010 }
3011
3012 /*
Li Zefan52621cb2011-11-20 07:33:38 -05003013 * The bitmaps list has all the bitmaps that record free space
3014 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04003015 */
Li Zefan52621cb2011-11-20 07:33:38 -05003016 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04003017}
3018
3019/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04003020 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003021 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04003022 * We might not find them all in one contiguous area.
3023 *
3024 * returns zero and sets up cluster if things worked out, otherwise
3025 * it returns -enospc
3026 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003027int btrfs_find_space_cluster(struct btrfs_fs_info *fs_info,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003028 struct btrfs_block_group_cache *block_group,
3029 struct btrfs_free_cluster *cluster,
3030 u64 offset, u64 bytes, u64 empty_size)
3031{
Li Zefan34d52cb2011-03-29 13:46:06 +08003032 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04003033 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05003034 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003035 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003036 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003037 int ret;
3038
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003039 /*
3040 * Choose the minimum extent size we'll require for this
3041 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3042 * For metadata, allow allocates with smaller extents. For
3043 * data, keep it dense.
3044 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003045 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003046 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04003047 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003048 cont1_bytes = bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003049 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003050 } else {
3051 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003052 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003053 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003054
Li Zefan34d52cb2011-03-29 13:46:06 +08003055 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003056
3057 /*
3058 * If we know we don't have enough space to make a cluster don't even
3059 * bother doing all the work to try and find one.
3060 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003061 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003062 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003063 return -ENOSPC;
3064 }
3065
Chris Masonfa9c0d792009-04-03 09:47:43 -04003066 spin_lock(&cluster->lock);
3067
3068 /* someone already found a cluster, hooray */
3069 if (cluster->block_group) {
3070 ret = 0;
3071 goto out;
3072 }
Josef Bacik4e69b592011-03-21 10:11:24 -04003073
Josef Bacik3f7de032011-11-10 08:29:20 -05003074 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3075 min_bytes);
3076
Josef Bacik86d4a772011-05-25 13:03:16 -04003077 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003078 bytes + empty_size,
3079 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04003080 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04003081 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003082 offset, bytes + empty_size,
3083 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003084
3085 /* Clear our temporary list */
3086 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3087 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04003088
3089 if (!ret) {
3090 atomic_inc(&block_group->count);
3091 list_add_tail(&cluster->block_group_list,
3092 &block_group->cluster_list);
3093 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05003094 } else {
3095 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003096 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003097out:
3098 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08003099 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003100
3101 return ret;
3102}
3103
3104/*
3105 * simple code to zero out a cluster
3106 */
3107void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3108{
3109 spin_lock_init(&cluster->lock);
3110 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00003111 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003112 cluster->max_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003113 cluster->fragmented = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003114 INIT_LIST_HEAD(&cluster->block_group_list);
3115 cluster->block_group = NULL;
3116}
3117
Li Zefan7fe1e642011-12-29 14:47:27 +08003118static int do_trimming(struct btrfs_block_group_cache *block_group,
3119 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003120 u64 reserved_start, u64 reserved_bytes,
3121 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08003122{
3123 struct btrfs_space_info *space_info = block_group->space_info;
3124 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00003125 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003126 int ret;
3127 int update = 0;
3128 u64 trimmed = 0;
3129
3130 spin_lock(&space_info->lock);
3131 spin_lock(&block_group->lock);
3132 if (!block_group->ro) {
3133 block_group->reserved += reserved_bytes;
3134 space_info->bytes_reserved += reserved_bytes;
3135 update = 1;
3136 }
3137 spin_unlock(&block_group->lock);
3138 spin_unlock(&space_info->lock);
3139
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003140 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
Li Zefan7fe1e642011-12-29 14:47:27 +08003141 if (!ret)
3142 *total_trimmed += trimmed;
3143
Filipe Manana55507ce2014-12-01 17:04:09 +00003144 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003145 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
Filipe Manana55507ce2014-12-01 17:04:09 +00003146 list_del(&trim_entry->list);
3147 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003148
3149 if (update) {
3150 spin_lock(&space_info->lock);
3151 spin_lock(&block_group->lock);
3152 if (block_group->ro)
3153 space_info->bytes_readonly += reserved_bytes;
3154 block_group->reserved -= reserved_bytes;
3155 space_info->bytes_reserved -= reserved_bytes;
3156 spin_unlock(&space_info->lock);
3157 spin_unlock(&block_group->lock);
3158 }
3159
3160 return ret;
3161}
3162
3163static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
3164 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
Li Dongyangf7039b12011-03-24 10:24:28 +00003165{
Li Zefan34d52cb2011-03-29 13:46:06 +08003166 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003167 struct btrfs_free_space *entry;
3168 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00003169 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08003170 u64 extent_start;
3171 u64 extent_bytes;
3172 u64 bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003173
3174 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00003175 struct btrfs_trim_range trim_entry;
3176
3177 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08003178 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003179
Li Zefan34d52cb2011-03-29 13:46:06 +08003180 if (ctl->free_space < minlen) {
3181 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003182 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003183 break;
3184 }
3185
Li Zefan34d52cb2011-03-29 13:46:06 +08003186 entry = tree_search_offset(ctl, start, 0, 1);
Li Zefan7fe1e642011-12-29 14:47:27 +08003187 if (!entry) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003188 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003189 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003190 break;
3191 }
3192
Li Zefan7fe1e642011-12-29 14:47:27 +08003193 /* skip bitmaps */
3194 while (entry->bitmap) {
3195 node = rb_next(&entry->offset_index);
3196 if (!node) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003197 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003198 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003199 goto out;
Li Dongyangf7039b12011-03-24 10:24:28 +00003200 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003201 entry = rb_entry(node, struct btrfs_free_space,
3202 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003203 }
3204
Li Zefan7fe1e642011-12-29 14:47:27 +08003205 if (entry->offset >= end) {
3206 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003207 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003208 break;
3209 }
3210
3211 extent_start = entry->offset;
3212 extent_bytes = entry->bytes;
3213 start = max(start, extent_start);
3214 bytes = min(extent_start + extent_bytes, end) - start;
3215 if (bytes < minlen) {
3216 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003217 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003218 goto next;
3219 }
3220
3221 unlink_free_space(ctl, entry);
3222 kmem_cache_free(btrfs_free_space_cachep, entry);
3223
Li Zefan34d52cb2011-03-29 13:46:06 +08003224 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003225 trim_entry.start = extent_start;
3226 trim_entry.bytes = extent_bytes;
3227 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3228 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003229
Li Zefan7fe1e642011-12-29 14:47:27 +08003230 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003231 extent_start, extent_bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003232 if (ret)
3233 break;
3234next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003235 start += bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003236
3237 if (fatal_signal_pending(current)) {
3238 ret = -ERESTARTSYS;
3239 break;
3240 }
3241
3242 cond_resched();
3243 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003244out:
3245 return ret;
3246}
3247
3248static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3249 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3250{
3251 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3252 struct btrfs_free_space *entry;
3253 int ret = 0;
3254 int ret2;
3255 u64 bytes;
3256 u64 offset = offset_to_bitmap(ctl, start);
3257
3258 while (offset < end) {
3259 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003260 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003261
Filipe Manana55507ce2014-12-01 17:04:09 +00003262 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003263 spin_lock(&ctl->tree_lock);
3264
3265 if (ctl->free_space < minlen) {
3266 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003267 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003268 break;
3269 }
3270
3271 entry = tree_search_offset(ctl, offset, 1, 0);
3272 if (!entry) {
3273 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003274 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003275 next_bitmap = true;
3276 goto next;
3277 }
3278
3279 bytes = minlen;
Josef Bacik0584f712015-10-02 16:12:23 -04003280 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
Li Zefan7fe1e642011-12-29 14:47:27 +08003281 if (ret2 || start >= end) {
3282 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003283 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003284 next_bitmap = true;
3285 goto next;
3286 }
3287
3288 bytes = min(bytes, end - start);
3289 if (bytes < minlen) {
3290 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003291 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003292 goto next;
3293 }
3294
3295 bitmap_clear_bits(ctl, entry, start, bytes);
3296 if (entry->bytes == 0)
3297 free_bitmap(ctl, entry);
3298
3299 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003300 trim_entry.start = start;
3301 trim_entry.bytes = bytes;
3302 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3303 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003304
3305 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003306 start, bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003307 if (ret)
3308 break;
3309next:
3310 if (next_bitmap) {
3311 offset += BITS_PER_BITMAP * ctl->unit;
3312 } else {
3313 start += bytes;
3314 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3315 offset += BITS_PER_BITMAP * ctl->unit;
3316 }
3317
3318 if (fatal_signal_pending(current)) {
3319 ret = -ERESTARTSYS;
3320 break;
3321 }
3322
3323 cond_resched();
3324 }
3325
3326 return ret;
3327}
3328
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003329void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache)
Li Zefan7fe1e642011-12-29 14:47:27 +08003330{
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003331 atomic_inc(&cache->trimming);
3332}
Li Zefan7fe1e642011-12-29 14:47:27 +08003333
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003334void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
3335{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003336 struct btrfs_fs_info *fs_info = block_group->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003337 struct extent_map_tree *em_tree;
3338 struct extent_map *em;
3339 bool cleanup;
Li Zefan7fe1e642011-12-29 14:47:27 +08003340
Filipe Manana04216822014-11-27 21:14:15 +00003341 spin_lock(&block_group->lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003342 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3343 block_group->removed);
Filipe Manana04216822014-11-27 21:14:15 +00003344 spin_unlock(&block_group->lock);
3345
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003346 if (cleanup) {
David Sterba34441362016-10-04 19:34:27 +02003347 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003348 em_tree = &fs_info->mapping_tree.map_tree;
Filipe Manana04216822014-11-27 21:14:15 +00003349 write_lock(&em_tree->lock);
3350 em = lookup_extent_mapping(em_tree, block_group->key.objectid,
3351 1);
3352 BUG_ON(!em); /* logic error, can't happen */
Filipe Mananaa1e7e162014-12-04 15:31:01 +00003353 /*
3354 * remove_extent_mapping() will delete us from the pinned_chunks
3355 * list, which is protected by the chunk mutex.
3356 */
Filipe Manana04216822014-11-27 21:14:15 +00003357 remove_extent_mapping(em_tree, em);
3358 write_unlock(&em_tree->lock);
David Sterba34441362016-10-04 19:34:27 +02003359 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana04216822014-11-27 21:14:15 +00003360
3361 /* once for us and once for the tree */
3362 free_extent_map(em);
3363 free_extent_map(em);
Filipe Manana946ddbe2014-12-01 17:04:40 +00003364
3365 /*
3366 * We've left one free space entry and other tasks trimming
3367 * this block group have left 1 entry each one. Free them.
3368 */
3369 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
Filipe Manana04216822014-11-27 21:14:15 +00003370 }
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003371}
Li Dongyangf7039b12011-03-24 10:24:28 +00003372
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003373int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
3374 u64 *trimmed, u64 start, u64 end, u64 minlen)
3375{
3376 int ret;
3377
3378 *trimmed = 0;
3379
3380 spin_lock(&block_group->lock);
3381 if (block_group->removed) {
3382 spin_unlock(&block_group->lock);
3383 return 0;
3384 }
3385 btrfs_get_block_group_trimming(block_group);
3386 spin_unlock(&block_group->lock);
3387
3388 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3389 if (ret)
3390 goto out;
3391
3392 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
3393out:
3394 btrfs_put_block_group_trimming(block_group);
Li Dongyangf7039b12011-03-24 10:24:28 +00003395 return ret;
3396}
Li Zefan581bb052011-04-20 10:06:11 +08003397
3398/*
3399 * Find the left-most item in the cache tree, and then return the
3400 * smallest inode number in the item.
3401 *
3402 * Note: the returned inode number may not be the smallest one in
3403 * the tree, if the left-most item is a bitmap.
3404 */
3405u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3406{
3407 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3408 struct btrfs_free_space *entry = NULL;
3409 u64 ino = 0;
3410
3411 spin_lock(&ctl->tree_lock);
3412
3413 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3414 goto out;
3415
3416 entry = rb_entry(rb_first(&ctl->free_space_offset),
3417 struct btrfs_free_space, offset_index);
3418
3419 if (!entry->bitmap) {
3420 ino = entry->offset;
3421
3422 unlink_free_space(ctl, entry);
3423 entry->offset++;
3424 entry->bytes--;
3425 if (!entry->bytes)
3426 kmem_cache_free(btrfs_free_space_cachep, entry);
3427 else
3428 link_free_space(ctl, entry);
3429 } else {
3430 u64 offset = 0;
3431 u64 count = 1;
3432 int ret;
3433
Josef Bacik0584f712015-10-02 16:12:23 -04003434 ret = search_bitmap(ctl, entry, &offset, &count, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003435 /* Logic error; Should be empty if it can't find anything */
Josef Bacikb12d6862013-08-26 17:14:08 -04003436 ASSERT(!ret);
Li Zefan581bb052011-04-20 10:06:11 +08003437
3438 ino = offset;
3439 bitmap_clear_bits(ctl, entry, offset, 1);
3440 if (entry->bytes == 0)
3441 free_bitmap(ctl, entry);
3442 }
3443out:
3444 spin_unlock(&ctl->tree_lock);
3445
3446 return ino;
3447}
Li Zefan82d59022011-04-20 10:33:24 +08003448
3449struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3450 struct btrfs_path *path)
3451{
3452 struct inode *inode = NULL;
3453
David Sterba57cdc8d2014-02-05 02:37:48 +01003454 spin_lock(&root->ino_cache_lock);
3455 if (root->ino_cache_inode)
3456 inode = igrab(root->ino_cache_inode);
3457 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003458 if (inode)
3459 return inode;
3460
3461 inode = __lookup_free_space_inode(root, path, 0);
3462 if (IS_ERR(inode))
3463 return inode;
3464
David Sterba57cdc8d2014-02-05 02:37:48 +01003465 spin_lock(&root->ino_cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02003466 if (!btrfs_fs_closing(root->fs_info))
David Sterba57cdc8d2014-02-05 02:37:48 +01003467 root->ino_cache_inode = igrab(inode);
3468 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003469
3470 return inode;
3471}
3472
3473int create_free_ino_inode(struct btrfs_root *root,
3474 struct btrfs_trans_handle *trans,
3475 struct btrfs_path *path)
3476{
3477 return __create_free_space_inode(root, trans, path,
3478 BTRFS_FREE_INO_OBJECTID, 0);
3479}
3480
3481int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3482{
3483 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3484 struct btrfs_path *path;
3485 struct inode *inode;
3486 int ret = 0;
3487 u64 root_gen = btrfs_root_generation(&root->root_item);
3488
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003489 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04003490 return 0;
3491
Li Zefan82d59022011-04-20 10:33:24 +08003492 /*
3493 * If we're unmounting then just return, since this does a search on the
3494 * normal root and not the commit root and we could deadlock.
3495 */
David Sterba7841cb22011-05-31 18:07:27 +02003496 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08003497 return 0;
3498
3499 path = btrfs_alloc_path();
3500 if (!path)
3501 return 0;
3502
3503 inode = lookup_free_ino_inode(root, path);
3504 if (IS_ERR(inode))
3505 goto out;
3506
3507 if (root_gen != BTRFS_I(inode)->generation)
3508 goto out_put;
3509
3510 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3511
3512 if (ret < 0)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003513 btrfs_err(fs_info,
3514 "failed to load free ino cache for root %llu",
3515 root->root_key.objectid);
Li Zefan82d59022011-04-20 10:33:24 +08003516out_put:
3517 iput(inode);
3518out:
3519 btrfs_free_path(path);
3520 return ret;
3521}
3522
3523int btrfs_write_out_ino_cache(struct btrfs_root *root,
3524 struct btrfs_trans_handle *trans,
Filipe David Borba Manana53645a92013-09-20 14:43:28 +01003525 struct btrfs_path *path,
3526 struct inode *inode)
Li Zefan82d59022011-04-20 10:33:24 +08003527{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003528 struct btrfs_fs_info *fs_info = root->fs_info;
Li Zefan82d59022011-04-20 10:33:24 +08003529 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
Li Zefan82d59022011-04-20 10:33:24 +08003530 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003531 struct btrfs_io_ctl io_ctl;
Filipe Mananae43699d2015-05-05 15:21:27 +01003532 bool release_metadata = true;
Li Zefan82d59022011-04-20 10:33:24 +08003533
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003534 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04003535 return 0;
3536
Chris Mason85db36c2015-04-23 08:02:49 -07003537 memset(&io_ctl, 0, sizeof(io_ctl));
David Sterba0e8d9312017-02-10 20:26:24 +01003538 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
Filipe Mananae43699d2015-05-05 15:21:27 +01003539 if (!ret) {
3540 /*
3541 * At this point writepages() didn't error out, so our metadata
3542 * reservation is released when the writeback finishes, at
3543 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3544 * with or without an error.
3545 */
3546 release_metadata = false;
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003547 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
Filipe Mananae43699d2015-05-05 15:21:27 +01003548 }
Chris Mason85db36c2015-04-23 08:02:49 -07003549
Josef Bacikc09544e2011-08-30 10:19:10 -04003550 if (ret) {
Filipe Mananae43699d2015-05-05 15:21:27 +01003551 if (release_metadata)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003552 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3553 inode->i_size);
Josef Bacikc09544e2011-08-30 10:19:10 -04003554#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003555 btrfs_err(fs_info,
3556 "failed to write free ino cache for root %llu",
3557 root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04003558#endif
3559 }
Li Zefan82d59022011-04-20 10:33:24 +08003560
Li Zefan82d59022011-04-20 10:33:24 +08003561 return ret;
3562}
Josef Bacik74255aa2013-03-15 09:47:08 -04003563
3564#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd52013-08-14 15:05:12 -04003565/*
3566 * Use this if you need to make a bitmap or extent entry specifically, it
3567 * doesn't do any of the merging that add_free_space does, this acts a lot like
3568 * how the free space cache loading stuff works, so you can get really weird
3569 * configurations.
3570 */
3571int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3572 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04003573{
Josef Bacikdc11dd52013-08-14 15:05:12 -04003574 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3575 struct btrfs_free_space *info = NULL, *bitmap_info;
3576 void *map = NULL;
3577 u64 bytes_added;
3578 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04003579
Josef Bacikdc11dd52013-08-14 15:05:12 -04003580again:
3581 if (!info) {
3582 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3583 if (!info)
3584 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04003585 }
3586
Josef Bacikdc11dd52013-08-14 15:05:12 -04003587 if (!bitmap) {
3588 spin_lock(&ctl->tree_lock);
3589 info->offset = offset;
3590 info->bytes = bytes;
Josef Bacikcef40482015-10-02 16:09:42 -04003591 info->max_extent_size = 0;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003592 ret = link_free_space(ctl, info);
3593 spin_unlock(&ctl->tree_lock);
3594 if (ret)
3595 kmem_cache_free(btrfs_free_space_cachep, info);
3596 return ret;
3597 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003598
Josef Bacikdc11dd52013-08-14 15:05:12 -04003599 if (!map) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003600 map = kzalloc(PAGE_SIZE, GFP_NOFS);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003601 if (!map) {
3602 kmem_cache_free(btrfs_free_space_cachep, info);
3603 return -ENOMEM;
3604 }
3605 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003606
Josef Bacikdc11dd52013-08-14 15:05:12 -04003607 spin_lock(&ctl->tree_lock);
3608 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3609 1, 0);
3610 if (!bitmap_info) {
3611 info->bitmap = map;
3612 map = NULL;
3613 add_new_bitmap(ctl, info, offset);
3614 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01003615 info = NULL;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003616 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003617
Josef Bacikdc11dd52013-08-14 15:05:12 -04003618 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
Josef Bacikcef40482015-10-02 16:09:42 -04003619
Josef Bacikdc11dd52013-08-14 15:05:12 -04003620 bytes -= bytes_added;
3621 offset += bytes_added;
3622 spin_unlock(&ctl->tree_lock);
3623
3624 if (bytes)
3625 goto again;
3626
Filipe Manana20005522014-08-29 13:35:13 +01003627 if (info)
3628 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003629 if (map)
3630 kfree(map);
3631 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003632}
3633
3634/*
3635 * Checks to see if the given range is in the free space cache. This is really
3636 * just used to check the absence of space, so if there is free space in the
3637 * range at all we will return 1.
3638 */
Josef Bacikdc11dd52013-08-14 15:05:12 -04003639int test_check_exists(struct btrfs_block_group_cache *cache,
3640 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04003641{
3642 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3643 struct btrfs_free_space *info;
3644 int ret = 0;
3645
3646 spin_lock(&ctl->tree_lock);
3647 info = tree_search_offset(ctl, offset, 0, 0);
3648 if (!info) {
3649 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3650 1, 0);
3651 if (!info)
3652 goto out;
3653 }
3654
3655have_info:
3656 if (info->bitmap) {
3657 u64 bit_off, bit_bytes;
3658 struct rb_node *n;
3659 struct btrfs_free_space *tmp;
3660
3661 bit_off = offset;
3662 bit_bytes = ctl->unit;
Josef Bacik0584f712015-10-02 16:12:23 -04003663 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
Josef Bacik74255aa2013-03-15 09:47:08 -04003664 if (!ret) {
3665 if (bit_off == offset) {
3666 ret = 1;
3667 goto out;
3668 } else if (bit_off > offset &&
3669 offset + bytes > bit_off) {
3670 ret = 1;
3671 goto out;
3672 }
3673 }
3674
3675 n = rb_prev(&info->offset_index);
3676 while (n) {
3677 tmp = rb_entry(n, struct btrfs_free_space,
3678 offset_index);
3679 if (tmp->offset + tmp->bytes < offset)
3680 break;
3681 if (offset + bytes < tmp->offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08003682 n = rb_prev(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04003683 continue;
3684 }
3685 info = tmp;
3686 goto have_info;
3687 }
3688
3689 n = rb_next(&info->offset_index);
3690 while (n) {
3691 tmp = rb_entry(n, struct btrfs_free_space,
3692 offset_index);
3693 if (offset + bytes < tmp->offset)
3694 break;
3695 if (tmp->offset + tmp->bytes < offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08003696 n = rb_next(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04003697 continue;
3698 }
3699 info = tmp;
3700 goto have_info;
3701 }
3702
Filipe Manana20005522014-08-29 13:35:13 +01003703 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003704 goto out;
3705 }
3706
3707 if (info->offset == offset) {
3708 ret = 1;
3709 goto out;
3710 }
3711
3712 if (offset > info->offset && offset < info->offset + info->bytes)
3713 ret = 1;
3714out:
3715 spin_unlock(&ctl->tree_lock);
3716 return ret;
3717}
Josef Bacikdc11dd52013-08-14 15:05:12 -04003718#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */