blob: 74aa552f47930699aa2a81134c3200bf2f5d5c5e [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Josef Bacik0f9dd462008-09-23 13:14:11 -04002/*
3 * Copyright (C) 2008 Red Hat. All rights reserved.
Josef Bacik0f9dd462008-09-23 13:14:11 -04004 */
5
Josef Bacik96303082009-07-13 21:29:25 -04006#include <linux/pagemap.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -04007#include <linux/sched.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +01008#include <linux/sched/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Josef Bacik96303082009-07-13 21:29:25 -040010#include <linux/math64.h>
Josef Bacik6ab60602011-08-08 08:24:46 -040011#include <linux/ratelimit.h>
Masami Hiramatsu540adea2018-01-13 02:55:03 +090012#include <linux/error-injection.h>
Josef Bacik84de76a2018-09-28 07:17:49 -040013#include <linux/sched/mm.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -040014#include "ctree.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040015#include "free-space-cache.h"
16#include "transaction.h"
Josef Bacik0af3d002010-06-21 14:48:16 -040017#include "disk-io.h"
Josef Bacik43be2142011-04-01 14:55:00 +000018#include "extent_io.h"
Li Zefan581bb052011-04-20 10:06:11 +080019#include "inode-map.h"
Filipe Manana04216822014-11-27 21:14:15 +000020#include "volumes.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040021
Feifei Xu0ef64472016-06-01 19:18:24 +080022#define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
Byongho Leeee221842015-12-15 01:42:10 +090023#define MAX_CACHE_BYTES_PER_GIG SZ_32K
Josef Bacik96303082009-07-13 21:29:25 -040024
Filipe Manana55507ce2014-12-01 17:04:09 +000025struct btrfs_trim_range {
26 u64 start;
27 u64 bytes;
28 struct list_head list;
29};
30
Li Zefan34d52cb2011-03-29 13:46:06 +080031static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040032 struct btrfs_free_space *info);
Josef Bacikcd023e72012-05-14 10:06:40 -040033static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
34 struct btrfs_free_space *info);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040035static int btrfs_wait_cache_io_root(struct btrfs_root *root,
36 struct btrfs_trans_handle *trans,
37 struct btrfs_io_ctl *io_ctl,
38 struct btrfs_path *path);
Josef Bacik0cb59c92010-07-02 12:14:14 -040039
Li Zefan0414efa2011-04-20 10:20:14 +080040static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
41 struct btrfs_path *path,
42 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040043{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040044 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0af3d002010-06-21 14:48:16 -040045 struct btrfs_key key;
46 struct btrfs_key location;
47 struct btrfs_disk_key disk_key;
48 struct btrfs_free_space_header *header;
49 struct extent_buffer *leaf;
50 struct inode *inode = NULL;
Josef Bacik84de76a2018-09-28 07:17:49 -040051 unsigned nofs_flag;
Josef Bacik0af3d002010-06-21 14:48:16 -040052 int ret;
53
Josef Bacik0af3d002010-06-21 14:48:16 -040054 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080055 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040056 key.type = 0;
57
58 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
59 if (ret < 0)
60 return ERR_PTR(ret);
61 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020062 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040063 return ERR_PTR(-ENOENT);
64 }
65
66 leaf = path->nodes[0];
67 header = btrfs_item_ptr(leaf, path->slots[0],
68 struct btrfs_free_space_header);
69 btrfs_free_space_key(leaf, header, &disk_key);
70 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020071 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040072
Josef Bacik84de76a2018-09-28 07:17:49 -040073 /*
74 * We are often under a trans handle at this point, so we need to make
75 * sure NOFS is set to keep us from deadlocking.
76 */
77 nofs_flag = memalloc_nofs_save();
Filipe Manana4222ea72018-10-24 10:13:03 +010078 inode = btrfs_iget_path(fs_info->sb, &location, root, NULL, path);
79 btrfs_release_path(path);
Josef Bacik84de76a2018-09-28 07:17:49 -040080 memalloc_nofs_restore(nofs_flag);
Josef Bacik0af3d002010-06-21 14:48:16 -040081 if (IS_ERR(inode))
82 return inode;
Josef Bacik0af3d002010-06-21 14:48:16 -040083
Al Viro528c0322012-04-13 11:03:55 -040084 mapping_set_gfp_mask(inode->i_mapping,
Michal Hockoc62d2552015-11-06 16:28:49 -080085 mapping_gfp_constraint(inode->i_mapping,
86 ~(__GFP_FS | __GFP_HIGHMEM)));
Miao Xieadae52b2011-03-31 09:43:23 +000087
Li Zefan0414efa2011-04-20 10:20:14 +080088 return inode;
89}
90
Jeff Mahoney77ab86b2017-02-15 16:28:30 -050091struct inode *lookup_free_space_inode(struct btrfs_fs_info *fs_info,
Li Zefan0414efa2011-04-20 10:20:14 +080092 struct btrfs_block_group_cache
93 *block_group, struct btrfs_path *path)
94{
95 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -040096 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +080097
98 spin_lock(&block_group->lock);
99 if (block_group->inode)
100 inode = igrab(block_group->inode);
101 spin_unlock(&block_group->lock);
102 if (inode)
103 return inode;
104
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500105 inode = __lookup_free_space_inode(fs_info->tree_root, path,
Li Zefan0414efa2011-04-20 10:20:14 +0800106 block_group->key.objectid);
107 if (IS_ERR(inode))
108 return inode;
109
Josef Bacik0af3d002010-06-21 14:48:16 -0400110 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400111 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400112 btrfs_info(fs_info, "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400113 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
114 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400115 block_group->disk_cache_state = BTRFS_DC_CLEAR;
116 }
117
Josef Bacik300e4f82011-08-29 14:06:00 -0400118 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400119 block_group->inode = igrab(inode);
120 block_group->iref = 1;
121 }
122 spin_unlock(&block_group->lock);
123
124 return inode;
125}
126
Eric Sandeen48a3b632013-04-25 20:41:01 +0000127static int __create_free_space_inode(struct btrfs_root *root,
128 struct btrfs_trans_handle *trans,
129 struct btrfs_path *path,
130 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400131{
132 struct btrfs_key key;
133 struct btrfs_disk_key disk_key;
134 struct btrfs_free_space_header *header;
135 struct btrfs_inode_item *inode_item;
136 struct extent_buffer *leaf;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400137 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
Josef Bacik0af3d002010-06-21 14:48:16 -0400138 int ret;
139
Li Zefan0414efa2011-04-20 10:20:14 +0800140 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400141 if (ret)
142 return ret;
143
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400144 /* We inline crc's for the free disk space cache */
145 if (ino != BTRFS_FREE_INO_OBJECTID)
146 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
147
Josef Bacik0af3d002010-06-21 14:48:16 -0400148 leaf = path->nodes[0];
149 inode_item = btrfs_item_ptr(leaf, path->slots[0],
150 struct btrfs_inode_item);
151 btrfs_item_key(leaf, &disk_key, path->slots[0]);
David Sterbab159fa22016-11-08 18:09:03 +0100152 memzero_extent_buffer(leaf, (unsigned long)inode_item,
Josef Bacik0af3d002010-06-21 14:48:16 -0400153 sizeof(*inode_item));
154 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
155 btrfs_set_inode_size(leaf, inode_item, 0);
156 btrfs_set_inode_nbytes(leaf, inode_item, 0);
157 btrfs_set_inode_uid(leaf, inode_item, 0);
158 btrfs_set_inode_gid(leaf, inode_item, 0);
159 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400160 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400161 btrfs_set_inode_nlink(leaf, inode_item, 1);
162 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800163 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400164 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200165 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400166
167 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800168 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400169 key.type = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400170 ret = btrfs_insert_empty_item(trans, root, path, &key,
171 sizeof(struct btrfs_free_space_header));
172 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200173 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400174 return ret;
175 }
Chris Masonc9dc4c62015-04-04 17:14:42 -0700176
Josef Bacik0af3d002010-06-21 14:48:16 -0400177 leaf = path->nodes[0];
178 header = btrfs_item_ptr(leaf, path->slots[0],
179 struct btrfs_free_space_header);
David Sterbab159fa22016-11-08 18:09:03 +0100180 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
Josef Bacik0af3d002010-06-21 14:48:16 -0400181 btrfs_set_free_space_key(leaf, header, &disk_key);
182 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200183 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400184
185 return 0;
186}
187
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500188int create_free_space_inode(struct btrfs_fs_info *fs_info,
Li Zefan0414efa2011-04-20 10:20:14 +0800189 struct btrfs_trans_handle *trans,
190 struct btrfs_block_group_cache *block_group,
191 struct btrfs_path *path)
192{
193 int ret;
194 u64 ino;
195
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500196 ret = btrfs_find_free_objectid(fs_info->tree_root, &ino);
Li Zefan0414efa2011-04-20 10:20:14 +0800197 if (ret < 0)
198 return ret;
199
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500200 return __create_free_space_inode(fs_info->tree_root, trans, path, ino,
Li Zefan0414efa2011-04-20 10:20:14 +0800201 block_group->key.objectid);
202}
203
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400204int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
Miao Xie7b61cd92013-05-13 13:55:09 +0000205 struct btrfs_block_rsv *rsv)
Josef Bacik0af3d002010-06-21 14:48:16 -0400206{
Josef Bacikc8174312011-11-02 09:29:35 -0400207 u64 needed_bytes;
Miao Xie7b61cd92013-05-13 13:55:09 +0000208 int ret;
Josef Bacikc8174312011-11-02 09:29:35 -0400209
210 /* 1 for slack space, 1 for updating the inode */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400211 needed_bytes = btrfs_calc_trunc_metadata_size(fs_info, 1) +
212 btrfs_calc_trans_metadata_size(fs_info, 1);
Josef Bacikc8174312011-11-02 09:29:35 -0400213
Miao Xie7b61cd92013-05-13 13:55:09 +0000214 spin_lock(&rsv->lock);
215 if (rsv->reserved < needed_bytes)
216 ret = -ENOSPC;
217 else
218 ret = 0;
219 spin_unlock(&rsv->lock);
Wei Yongjun4b286cd2013-05-21 02:39:21 +0000220 return ret;
Miao Xie7b61cd92013-05-13 13:55:09 +0000221}
222
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500223int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
Chris Mason1bbc6212015-04-06 12:46:08 -0700224 struct btrfs_block_group_cache *block_group,
Miao Xie7b61cd92013-05-13 13:55:09 +0000225 struct inode *inode)
226{
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500227 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b61cd92013-05-13 13:55:09 +0000228 int ret = 0;
Filipe Manana35c76642015-04-30 17:47:05 +0100229 bool locked = false;
Chris Mason1bbc6212015-04-06 12:46:08 -0700230
Chris Mason1bbc6212015-04-06 12:46:08 -0700231 if (block_group) {
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500232 struct btrfs_path *path = btrfs_alloc_path();
233
234 if (!path) {
235 ret = -ENOMEM;
236 goto fail;
237 }
Filipe Manana35c76642015-04-30 17:47:05 +0100238 locked = true;
Chris Mason1bbc6212015-04-06 12:46:08 -0700239 mutex_lock(&trans->transaction->cache_write_mutex);
240 if (!list_empty(&block_group->io_list)) {
241 list_del_init(&block_group->io_list);
242
Jeff Mahoneyafdb5712016-09-09 12:09:35 -0400243 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700244 btrfs_put_block_group(block_group);
245 }
246
247 /*
248 * now that we've truncated the cache away, its no longer
249 * setup or written
250 */
251 spin_lock(&block_group->lock);
252 block_group->disk_cache_state = BTRFS_DC_CLEAR;
253 spin_unlock(&block_group->lock);
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500254 btrfs_free_path(path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700255 }
Josef Bacik0af3d002010-06-21 14:48:16 -0400256
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200257 btrfs_i_size_write(BTRFS_I(inode), 0);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700258 truncate_pagecache(inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400259
260 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -0700261 * We skip the throttling logic for free space cache inodes, so we don't
262 * need to check for -EAGAIN.
Josef Bacik0af3d002010-06-21 14:48:16 -0400263 */
264 ret = btrfs_truncate_inode_items(trans, root, inode,
265 0, BTRFS_EXTENT_DATA_KEY);
Filipe Manana35c76642015-04-30 17:47:05 +0100266 if (ret)
267 goto fail;
Josef Bacik0af3d002010-06-21 14:48:16 -0400268
Li Zefan82d59022011-04-20 10:33:24 +0800269 ret = btrfs_update_inode(trans, root, inode);
Chris Mason1bbc6212015-04-06 12:46:08 -0700270
Chris Mason1bbc6212015-04-06 12:46:08 -0700271fail:
Filipe Manana35c76642015-04-30 17:47:05 +0100272 if (locked)
273 mutex_unlock(&trans->transaction->cache_write_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100274 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400275 btrfs_abort_transaction(trans, ret);
Josef Bacikc8174312011-11-02 09:29:35 -0400276
Li Zefan82d59022011-04-20 10:33:24 +0800277 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400278}
279
David Sterba1d480532017-01-23 17:28:19 +0100280static void readahead_cache(struct inode *inode)
Josef Bacik9d66e232010-08-25 16:54:15 -0400281{
282 struct file_ra_state *ra;
283 unsigned long last_index;
284
285 ra = kzalloc(sizeof(*ra), GFP_NOFS);
286 if (!ra)
David Sterba1d480532017-01-23 17:28:19 +0100287 return;
Josef Bacik9d66e232010-08-25 16:54:15 -0400288
289 file_ra_state_init(ra, inode->i_mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300290 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
Josef Bacik9d66e232010-08-25 16:54:15 -0400291
292 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
293
294 kfree(ra);
Josef Bacik9d66e232010-08-25 16:54:15 -0400295}
296
Chris Mason4c6d1d82015-04-06 13:17:20 -0700297static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400298 int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400299{
Miao Xie5349d6c2014-06-19 10:42:49 +0800300 int num_pages;
301 int check_crcs = 0;
302
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300303 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800304
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200305 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FREE_INO_OBJECTID)
Miao Xie5349d6c2014-06-19 10:42:49 +0800306 check_crcs = 1;
307
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400308 /* Make sure we can fit our crcs and generation into the first page */
Miao Xie5349d6c2014-06-19 10:42:49 +0800309 if (write && check_crcs &&
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400310 (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
Miao Xie5349d6c2014-06-19 10:42:49 +0800311 return -ENOSPC;
312
Chris Mason4c6d1d82015-04-06 13:17:20 -0700313 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800314
David Sterba31e818f2015-02-20 18:00:26 +0100315 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400316 if (!io_ctl->pages)
317 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800318
319 io_ctl->num_pages = num_pages;
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400320 io_ctl->fs_info = btrfs_sb(inode->i_sb);
Miao Xie5349d6c2014-06-19 10:42:49 +0800321 io_ctl->check_crcs = check_crcs;
Chris Masonc9dc4c62015-04-04 17:14:42 -0700322 io_ctl->inode = inode;
Miao Xie5349d6c2014-06-19 10:42:49 +0800323
Josef Bacika67509c2011-10-05 15:18:58 -0400324 return 0;
325}
Masami Hiramatsu663faf92018-01-13 02:55:33 +0900326ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
Josef Bacika67509c2011-10-05 15:18:58 -0400327
Chris Mason4c6d1d82015-04-06 13:17:20 -0700328static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400329{
330 kfree(io_ctl->pages);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700331 io_ctl->pages = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400332}
333
Chris Mason4c6d1d82015-04-06 13:17:20 -0700334static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400335{
336 if (io_ctl->cur) {
Josef Bacika67509c2011-10-05 15:18:58 -0400337 io_ctl->cur = NULL;
338 io_ctl->orig = NULL;
339 }
340}
341
Chris Mason4c6d1d82015-04-06 13:17:20 -0700342static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
Josef Bacika67509c2011-10-05 15:18:58 -0400343{
Josef Bacikb12d6862013-08-26 17:14:08 -0400344 ASSERT(io_ctl->index < io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400345 io_ctl->page = io_ctl->pages[io_ctl->index++];
Chris Mason2b108262015-04-06 07:48:20 -0700346 io_ctl->cur = page_address(io_ctl->page);
Josef Bacika67509c2011-10-05 15:18:58 -0400347 io_ctl->orig = io_ctl->cur;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300348 io_ctl->size = PAGE_SIZE;
Josef Bacika67509c2011-10-05 15:18:58 -0400349 if (clear)
David Sterba619a9742017-03-29 20:48:44 +0200350 clear_page(io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400351}
352
Chris Mason4c6d1d82015-04-06 13:17:20 -0700353static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400354{
355 int i;
356
357 io_ctl_unmap_page(io_ctl);
358
359 for (i = 0; i < io_ctl->num_pages; i++) {
Li Zefana1ee5a42012-01-09 14:27:42 +0800360 if (io_ctl->pages[i]) {
361 ClearPageChecked(io_ctl->pages[i]);
362 unlock_page(io_ctl->pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300363 put_page(io_ctl->pages[i]);
Li Zefana1ee5a42012-01-09 14:27:42 +0800364 }
Josef Bacika67509c2011-10-05 15:18:58 -0400365 }
366}
367
Chris Mason4c6d1d82015-04-06 13:17:20 -0700368static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Josef Bacika67509c2011-10-05 15:18:58 -0400369 int uptodate)
370{
371 struct page *page;
372 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
373 int i;
374
375 for (i = 0; i < io_ctl->num_pages; i++) {
376 page = find_or_create_page(inode->i_mapping, i, mask);
377 if (!page) {
378 io_ctl_drop_pages(io_ctl);
379 return -ENOMEM;
380 }
381 io_ctl->pages[i] = page;
382 if (uptodate && !PageUptodate(page)) {
383 btrfs_readpage(NULL, page);
384 lock_page(page);
385 if (!PageUptodate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500386 btrfs_err(BTRFS_I(inode)->root->fs_info,
387 "error reading free space cache");
Josef Bacika67509c2011-10-05 15:18:58 -0400388 io_ctl_drop_pages(io_ctl);
389 return -EIO;
390 }
391 }
392 }
393
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500394 for (i = 0; i < io_ctl->num_pages; i++) {
395 clear_page_dirty_for_io(io_ctl->pages[i]);
396 set_page_extent_mapped(io_ctl->pages[i]);
397 }
398
Josef Bacika67509c2011-10-05 15:18:58 -0400399 return 0;
400}
401
Chris Mason4c6d1d82015-04-06 13:17:20 -0700402static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400403{
Al Viro528c0322012-04-13 11:03:55 -0400404 __le64 *val;
Josef Bacika67509c2011-10-05 15:18:58 -0400405
406 io_ctl_map_page(io_ctl, 1);
407
408 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400409 * Skip the csum areas. If we don't check crcs then we just have a
410 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400411 */
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400412 if (io_ctl->check_crcs) {
413 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
414 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
415 } else {
416 io_ctl->cur += sizeof(u64);
417 io_ctl->size -= sizeof(u64) * 2;
418 }
Josef Bacika67509c2011-10-05 15:18:58 -0400419
420 val = io_ctl->cur;
421 *val = cpu_to_le64(generation);
422 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400423}
424
Chris Mason4c6d1d82015-04-06 13:17:20 -0700425static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400426{
Al Viro528c0322012-04-13 11:03:55 -0400427 __le64 *gen;
Josef Bacika67509c2011-10-05 15:18:58 -0400428
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400429 /*
430 * Skip the crc area. If we don't check crcs then we just have a 64bit
431 * chunk at the front of the first page.
432 */
433 if (io_ctl->check_crcs) {
434 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
435 io_ctl->size -= sizeof(u64) +
436 (sizeof(u32) * io_ctl->num_pages);
437 } else {
438 io_ctl->cur += sizeof(u64);
439 io_ctl->size -= sizeof(u64) * 2;
440 }
Josef Bacika67509c2011-10-05 15:18:58 -0400441
Josef Bacika67509c2011-10-05 15:18:58 -0400442 gen = io_ctl->cur;
443 if (le64_to_cpu(*gen) != generation) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400444 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200445 "space cache generation (%llu) does not match inode (%llu)",
446 *gen, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400447 io_ctl_unmap_page(io_ctl);
448 return -EIO;
449 }
450 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400451 return 0;
452}
453
Chris Mason4c6d1d82015-04-06 13:17:20 -0700454static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400455{
456 u32 *tmp;
457 u32 crc = ~(u32)0;
458 unsigned offset = 0;
459
460 if (!io_ctl->check_crcs) {
461 io_ctl_unmap_page(io_ctl);
462 return;
463 }
464
465 if (index == 0)
Justin P. Mattockcb54f252011-11-21 08:43:28 -0800466 offset = sizeof(u32) * io_ctl->num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400467
Liu Bob0496682013-03-14 14:57:45 +0000468 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300469 PAGE_SIZE - offset);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +0100470 btrfs_csum_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400471 io_ctl_unmap_page(io_ctl);
Chris Mason2b108262015-04-06 07:48:20 -0700472 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400473 tmp += index;
474 *tmp = crc;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400475}
476
Chris Mason4c6d1d82015-04-06 13:17:20 -0700477static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400478{
479 u32 *tmp, val;
480 u32 crc = ~(u32)0;
481 unsigned offset = 0;
482
483 if (!io_ctl->check_crcs) {
484 io_ctl_map_page(io_ctl, 0);
485 return 0;
486 }
487
488 if (index == 0)
489 offset = sizeof(u32) * io_ctl->num_pages;
490
Chris Mason2b108262015-04-06 07:48:20 -0700491 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400492 tmp += index;
493 val = *tmp;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400494
495 io_ctl_map_page(io_ctl, 0);
Liu Bob0496682013-03-14 14:57:45 +0000496 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300497 PAGE_SIZE - offset);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +0100498 btrfs_csum_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400499 if (val != crc) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400500 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200501 "csum mismatch on free space cache");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400502 io_ctl_unmap_page(io_ctl);
503 return -EIO;
504 }
505
Josef Bacika67509c2011-10-05 15:18:58 -0400506 return 0;
507}
508
Chris Mason4c6d1d82015-04-06 13:17:20 -0700509static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400510 void *bitmap)
511{
512 struct btrfs_free_space_entry *entry;
513
514 if (!io_ctl->cur)
515 return -ENOSPC;
516
517 entry = io_ctl->cur;
518 entry->offset = cpu_to_le64(offset);
519 entry->bytes = cpu_to_le64(bytes);
520 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
521 BTRFS_FREE_SPACE_EXTENT;
522 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
523 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
524
525 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
526 return 0;
527
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400528 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400529
530 /* No more pages to map */
531 if (io_ctl->index >= io_ctl->num_pages)
532 return 0;
533
534 /* map the next page */
535 io_ctl_map_page(io_ctl, 1);
536 return 0;
537}
538
Chris Mason4c6d1d82015-04-06 13:17:20 -0700539static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
Josef Bacika67509c2011-10-05 15:18:58 -0400540{
541 if (!io_ctl->cur)
542 return -ENOSPC;
543
544 /*
545 * If we aren't at the start of the current page, unmap this one and
546 * map the next one if there is any left.
547 */
548 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400549 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400550 if (io_ctl->index >= io_ctl->num_pages)
551 return -ENOSPC;
552 io_ctl_map_page(io_ctl, 0);
553 }
554
David Sterba69d24802018-06-29 10:56:44 +0200555 copy_page(io_ctl->cur, bitmap);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400556 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400557 if (io_ctl->index < io_ctl->num_pages)
558 io_ctl_map_page(io_ctl, 0);
559 return 0;
560}
561
Chris Mason4c6d1d82015-04-06 13:17:20 -0700562static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400563{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400564 /*
565 * If we're not on the boundary we know we've modified the page and we
566 * need to crc the page.
567 */
568 if (io_ctl->cur != io_ctl->orig)
569 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
570 else
571 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400572
573 while (io_ctl->index < io_ctl->num_pages) {
574 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400575 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400576 }
577}
578
Chris Mason4c6d1d82015-04-06 13:17:20 -0700579static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400580 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400581{
582 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500583 int ret;
584
585 if (!io_ctl->cur) {
586 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
587 if (ret)
588 return ret;
589 }
Josef Bacika67509c2011-10-05 15:18:58 -0400590
591 e = io_ctl->cur;
592 entry->offset = le64_to_cpu(e->offset);
593 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400594 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400595 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
596 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
597
598 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400599 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400600
601 io_ctl_unmap_page(io_ctl);
602
Josef Bacik2f120c02011-11-10 20:45:05 -0500603 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400604}
605
Chris Mason4c6d1d82015-04-06 13:17:20 -0700606static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400607 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400608{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400609 int ret;
610
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400611 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
612 if (ret)
613 return ret;
614
David Sterba69d24802018-06-29 10:56:44 +0200615 copy_page(entry->bitmap, io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400616 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400617
618 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400619}
620
Josef Bacikcd023e72012-05-14 10:06:40 -0400621/*
622 * Since we attach pinned extents after the fact we can have contiguous sections
623 * of free space that are split up in entries. This poses a problem with the
624 * tree logging stuff since it could have allocated across what appears to be 2
625 * entries since we would have merged the entries when adding the pinned extents
626 * back to the free space cache. So run through the space cache that we just
627 * loaded and merge contiguous entries. This will make the log replay stuff not
628 * blow up and it will make for nicer allocator behavior.
629 */
630static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
631{
632 struct btrfs_free_space *e, *prev = NULL;
633 struct rb_node *n;
634
635again:
636 spin_lock(&ctl->tree_lock);
637 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
638 e = rb_entry(n, struct btrfs_free_space, offset_index);
639 if (!prev)
640 goto next;
641 if (e->bitmap || prev->bitmap)
642 goto next;
643 if (prev->offset + prev->bytes == e->offset) {
644 unlink_free_space(ctl, prev);
645 unlink_free_space(ctl, e);
646 prev->bytes += e->bytes;
647 kmem_cache_free(btrfs_free_space_cachep, e);
648 link_free_space(ctl, prev);
649 prev = NULL;
650 spin_unlock(&ctl->tree_lock);
651 goto again;
652 }
653next:
654 prev = e;
655 }
656 spin_unlock(&ctl->tree_lock);
657}
658
Eric Sandeen48a3b632013-04-25 20:41:01 +0000659static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
660 struct btrfs_free_space_ctl *ctl,
661 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400662{
David Sterba3ffbd682018-06-29 10:56:42 +0200663 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik9d66e232010-08-25 16:54:15 -0400664 struct btrfs_free_space_header *header;
665 struct extent_buffer *leaf;
Chris Mason4c6d1d82015-04-06 13:17:20 -0700666 struct btrfs_io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400667 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400668 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800669 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400670 u64 num_entries;
671 u64 num_bitmaps;
672 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400673 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400674 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400675
Josef Bacik9d66e232010-08-25 16:54:15 -0400676 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800677 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400678 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400679
680 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800681 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400682 key.type = 0;
683
684 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800685 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400686 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800687 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400688 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400689 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400690 }
691
Li Zefan0414efa2011-04-20 10:20:14 +0800692 ret = -1;
693
Josef Bacik9d66e232010-08-25 16:54:15 -0400694 leaf = path->nodes[0];
695 header = btrfs_item_ptr(leaf, path->slots[0],
696 struct btrfs_free_space_header);
697 num_entries = btrfs_free_space_entries(leaf, header);
698 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
699 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400700 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400701
Miao Xiee570fd22014-06-19 10:42:50 +0800702 if (!BTRFS_I(inode)->generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400703 btrfs_info(fs_info,
David Sterba913e1532017-07-13 15:32:18 +0200704 "the free space cache file (%llu) is invalid, skip it",
Miao Xiee570fd22014-06-19 10:42:50 +0800705 offset);
706 return 0;
707 }
708
Josef Bacik9d66e232010-08-25 16:54:15 -0400709 if (BTRFS_I(inode)->generation != generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400710 btrfs_err(fs_info,
711 "free space inode generation (%llu) did not match free space cache generation (%llu)",
712 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400713 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400714 }
715
716 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400717 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400718
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400719 ret = io_ctl_init(&io_ctl, inode, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800720 if (ret)
721 return ret;
722
David Sterba1d480532017-01-23 17:28:19 +0100723 readahead_cache(inode);
Josef Bacik9d66e232010-08-25 16:54:15 -0400724
Josef Bacika67509c2011-10-05 15:18:58 -0400725 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
726 if (ret)
727 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400728
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400729 ret = io_ctl_check_crc(&io_ctl, 0);
730 if (ret)
731 goto free_cache;
732
Josef Bacika67509c2011-10-05 15:18:58 -0400733 ret = io_ctl_check_generation(&io_ctl, generation);
734 if (ret)
735 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400736
Josef Bacika67509c2011-10-05 15:18:58 -0400737 while (num_entries) {
738 e = kmem_cache_zalloc(btrfs_free_space_cachep,
739 GFP_NOFS);
740 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400741 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400742
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400743 ret = io_ctl_read_entry(&io_ctl, e, &type);
744 if (ret) {
745 kmem_cache_free(btrfs_free_space_cachep, e);
746 goto free_cache;
747 }
748
Josef Bacika67509c2011-10-05 15:18:58 -0400749 if (!e->bytes) {
750 kmem_cache_free(btrfs_free_space_cachep, e);
751 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400752 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400753
Josef Bacika67509c2011-10-05 15:18:58 -0400754 if (type == BTRFS_FREE_SPACE_EXTENT) {
755 spin_lock(&ctl->tree_lock);
756 ret = link_free_space(ctl, e);
757 spin_unlock(&ctl->tree_lock);
758 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400759 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000760 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500761 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400762 goto free_cache;
763 }
Josef Bacika67509c2011-10-05 15:18:58 -0400764 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400765 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400766 num_bitmaps--;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300767 e->bitmap = kzalloc(PAGE_SIZE, GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400768 if (!e->bitmap) {
769 kmem_cache_free(
770 btrfs_free_space_cachep, e);
771 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400772 }
Josef Bacika67509c2011-10-05 15:18:58 -0400773 spin_lock(&ctl->tree_lock);
774 ret = link_free_space(ctl, e);
775 ctl->total_bitmaps++;
776 ctl->op->recalc_thresholds(ctl);
777 spin_unlock(&ctl->tree_lock);
778 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400779 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000780 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400781 kmem_cache_free(btrfs_free_space_cachep, e);
782 goto free_cache;
783 }
784 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400785 }
786
Josef Bacika67509c2011-10-05 15:18:58 -0400787 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400788 }
789
Josef Bacik2f120c02011-11-10 20:45:05 -0500790 io_ctl_unmap_page(&io_ctl);
791
Josef Bacika67509c2011-10-05 15:18:58 -0400792 /*
793 * We add the bitmaps at the end of the entries in order that
794 * the bitmap entries are added to the cache.
795 */
796 list_for_each_entry_safe(e, n, &bitmaps, list) {
797 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400798 ret = io_ctl_read_bitmap(&io_ctl, e);
799 if (ret)
800 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400801 }
802
803 io_ctl_drop_pages(&io_ctl);
Josef Bacikcd023e72012-05-14 10:06:40 -0400804 merge_space_tree(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400805 ret = 1;
806out:
Josef Bacika67509c2011-10-05 15:18:58 -0400807 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400808 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400809free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400810 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800811 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400812 goto out;
813}
814
Li Zefan0414efa2011-04-20 10:20:14 +0800815int load_free_space_cache(struct btrfs_fs_info *fs_info,
816 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400817{
Li Zefan34d52cb2011-03-29 13:46:06 +0800818 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800819 struct inode *inode;
820 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400821 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800822 bool matched;
823 u64 used = btrfs_block_group_used(&block_group->item);
824
825 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800826 * If this block group has been marked to be cleared for one reason or
827 * another then we can't trust the on disk cache, so just return.
828 */
829 spin_lock(&block_group->lock);
830 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
831 spin_unlock(&block_group->lock);
832 return 0;
833 }
834 spin_unlock(&block_group->lock);
835
836 path = btrfs_alloc_path();
837 if (!path)
838 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400839 path->search_commit_root = 1;
840 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800841
Filipe Manana4222ea72018-10-24 10:13:03 +0100842 /*
843 * We must pass a path with search_commit_root set to btrfs_iget in
844 * order to avoid a deadlock when allocating extents for the tree root.
845 *
846 * When we are COWing an extent buffer from the tree root, when looking
847 * for a free extent, at extent-tree.c:find_free_extent(), we can find
848 * block group without its free space cache loaded. When we find one
849 * we must load its space cache which requires reading its free space
850 * cache's inode item from the root tree. If this inode item is located
851 * in the same leaf that we started COWing before, then we end up in
852 * deadlock on the extent buffer (trying to read lock it when we
853 * previously write locked it).
854 *
855 * It's safe to read the inode item using the commit root because
856 * block groups, once loaded, stay in memory forever (until they are
857 * removed) as well as their space caches once loaded. New block groups
858 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
859 * we will never try to read their inode item while the fs is mounted.
860 */
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500861 inode = lookup_free_space_inode(fs_info, block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +0800862 if (IS_ERR(inode)) {
863 btrfs_free_path(path);
864 return 0;
865 }
866
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400867 /* We may have converted the inode and made the cache invalid. */
868 spin_lock(&block_group->lock);
869 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
870 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900871 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400872 goto out;
873 }
874 spin_unlock(&block_group->lock);
875
Li Zefan0414efa2011-04-20 10:20:14 +0800876 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
877 path, block_group->key.objectid);
878 btrfs_free_path(path);
879 if (ret <= 0)
880 goto out;
881
882 spin_lock(&ctl->tree_lock);
883 matched = (ctl->free_space == (block_group->key.offset - used -
884 block_group->bytes_super));
885 spin_unlock(&ctl->tree_lock);
886
887 if (!matched) {
888 __btrfs_remove_free_space_cache(ctl);
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400889 btrfs_warn(fs_info,
890 "block group %llu has wrong amount of free space",
891 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800892 ret = -1;
893 }
894out:
895 if (ret < 0) {
896 /* This cache is bogus, make sure it gets cleared */
897 spin_lock(&block_group->lock);
898 block_group->disk_cache_state = BTRFS_DC_CLEAR;
899 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800900 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800901
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400902 btrfs_warn(fs_info,
903 "failed to load free space cache for block group %llu, rebuilding it now",
904 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800905 }
906
907 iput(inode);
908 return ret;
909}
910
Chris Masond4452bc2014-05-19 20:47:56 -0700911static noinline_for_stack
Chris Mason4c6d1d82015-04-06 13:17:20 -0700912int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -0700913 struct btrfs_free_space_ctl *ctl,
914 struct btrfs_block_group_cache *block_group,
915 int *entries, int *bitmaps,
916 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400917{
Josef Bacikc09544e2011-08-30 10:19:10 -0400918 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -0700919 struct btrfs_free_cluster *cluster = NULL;
Chris Mason1bbc6212015-04-06 12:46:08 -0700920 struct btrfs_free_cluster *cluster_locked = NULL;
Chris Masond4452bc2014-05-19 20:47:56 -0700921 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +0000922 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400923
Josef Bacik43be2142011-04-01 14:55:00 +0000924 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -0700925 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000926 cluster = list_entry(block_group->cluster_list.next,
927 struct btrfs_free_cluster,
928 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -0700929 }
Josef Bacik43be2142011-04-01 14:55:00 +0000930
Josef Bacikf75b1302011-10-05 10:00:18 -0400931 if (!node && cluster) {
Chris Mason1bbc6212015-04-06 12:46:08 -0700932 cluster_locked = cluster;
933 spin_lock(&cluster_locked->lock);
Josef Bacikf75b1302011-10-05 10:00:18 -0400934 node = rb_first(&cluster->root);
935 cluster = NULL;
936 }
937
Josef Bacik0cb59c92010-07-02 12:14:14 -0400938 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400939 while (node) {
940 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400941
Josef Bacika67509c2011-10-05 15:18:58 -0400942 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -0700943 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +0000944
Chris Masond4452bc2014-05-19 20:47:56 -0700945 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400946 e->bitmap);
947 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -0700948 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -0400949
950 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -0700951 list_add_tail(&e->list, bitmap_list);
952 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400953 }
954 node = rb_next(node);
955 if (!node && cluster) {
956 node = rb_first(&cluster->root);
Chris Mason1bbc6212015-04-06 12:46:08 -0700957 cluster_locked = cluster;
958 spin_lock(&cluster_locked->lock);
Josef Bacika67509c2011-10-05 15:18:58 -0400959 cluster = NULL;
960 }
961 }
Chris Mason1bbc6212015-04-06 12:46:08 -0700962 if (cluster_locked) {
963 spin_unlock(&cluster_locked->lock);
964 cluster_locked = NULL;
965 }
Filipe Manana55507ce2014-12-01 17:04:09 +0000966
967 /*
968 * Make sure we don't miss any range that was removed from our rbtree
969 * because trimming is running. Otherwise after a umount+mount (or crash
970 * after committing the transaction) we would leak free space and get
971 * an inconsistent free space cache report from fsck.
972 */
973 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
974 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
975 trim_entry->bytes, NULL);
976 if (ret)
977 goto fail;
978 *entries += 1;
979 }
980
Chris Masond4452bc2014-05-19 20:47:56 -0700981 return 0;
982fail:
Chris Mason1bbc6212015-04-06 12:46:08 -0700983 if (cluster_locked)
984 spin_unlock(&cluster_locked->lock);
Chris Masond4452bc2014-05-19 20:47:56 -0700985 return -ENOSPC;
986}
987
988static noinline_for_stack int
989update_cache_item(struct btrfs_trans_handle *trans,
990 struct btrfs_root *root,
991 struct inode *inode,
992 struct btrfs_path *path, u64 offset,
993 int entries, int bitmaps)
994{
995 struct btrfs_key key;
996 struct btrfs_free_space_header *header;
997 struct extent_buffer *leaf;
998 int ret;
999
1000 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1001 key.offset = offset;
1002 key.type = 0;
1003
1004 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1005 if (ret < 0) {
1006 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001007 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001008 goto fail;
1009 }
1010 leaf = path->nodes[0];
1011 if (ret > 0) {
1012 struct btrfs_key found_key;
1013 ASSERT(path->slots[0]);
1014 path->slots[0]--;
1015 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1016 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1017 found_key.offset != offset) {
1018 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
1019 inode->i_size - 1,
1020 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001021 NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001022 btrfs_release_path(path);
1023 goto fail;
1024 }
1025 }
1026
1027 BTRFS_I(inode)->generation = trans->transid;
1028 header = btrfs_item_ptr(leaf, path->slots[0],
1029 struct btrfs_free_space_header);
1030 btrfs_set_free_space_entries(leaf, header, entries);
1031 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1032 btrfs_set_free_space_generation(leaf, header, trans->transid);
1033 btrfs_mark_buffer_dirty(leaf);
1034 btrfs_release_path(path);
1035
1036 return 0;
1037
1038fail:
1039 return -1;
1040}
1041
1042static noinline_for_stack int
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001043write_pinned_extent_entries(struct btrfs_fs_info *fs_info,
Miao Xie5349d6c2014-06-19 10:42:49 +08001044 struct btrfs_block_group_cache *block_group,
Chris Mason4c6d1d82015-04-06 13:17:20 -07001045 struct btrfs_io_ctl *io_ctl,
Miao Xie5349d6c2014-06-19 10:42:49 +08001046 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -07001047{
1048 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -07001049 struct extent_io_tree *unpin = NULL;
1050 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001051
Miao Xie5349d6c2014-06-19 10:42:49 +08001052 if (!block_group)
1053 return 0;
1054
Josef Bacika67509c2011-10-05 15:18:58 -04001055 /*
1056 * We want to add any pinned extents to our free space cache
1057 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001058 *
Li Zefandb804f22012-01-10 16:41:01 +08001059 * We shouldn't have switched the pinned extents yet so this is the
1060 * right one
1061 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001062 unpin = fs_info->pinned_extents;
Li Zefandb804f22012-01-10 16:41:01 +08001063
Miao Xie5349d6c2014-06-19 10:42:49 +08001064 start = block_group->key.objectid;
Li Zefandb804f22012-01-10 16:41:01 +08001065
Miao Xie5349d6c2014-06-19 10:42:49 +08001066 while (start < block_group->key.objectid + block_group->key.offset) {
Li Zefandb804f22012-01-10 16:41:01 +08001067 ret = find_first_extent_bit(unpin, start,
1068 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001069 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001070 if (ret)
1071 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001072
Josef Bacika67509c2011-10-05 15:18:58 -04001073 /* This pinned extent is out of our range */
Li Zefandb804f22012-01-10 16:41:01 +08001074 if (extent_start >= block_group->key.objectid +
Josef Bacika67509c2011-10-05 15:18:58 -04001075 block_group->key.offset)
Miao Xie5349d6c2014-06-19 10:42:49 +08001076 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001077
Li Zefandb804f22012-01-10 16:41:01 +08001078 extent_start = max(extent_start, start);
1079 extent_end = min(block_group->key.objectid +
1080 block_group->key.offset, extent_end + 1);
1081 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001082
Chris Masond4452bc2014-05-19 20:47:56 -07001083 *entries += 1;
1084 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001085 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001086 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001087
Li Zefandb804f22012-01-10 16:41:01 +08001088 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001089 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001090
Miao Xie5349d6c2014-06-19 10:42:49 +08001091 return 0;
1092}
1093
1094static noinline_for_stack int
Chris Mason4c6d1d82015-04-06 13:17:20 -07001095write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
Miao Xie5349d6c2014-06-19 10:42:49 +08001096{
Geliang Tang7ae16812015-12-18 22:17:00 +08001097 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001098 int ret;
1099
Josef Bacik0cb59c92010-07-02 12:14:14 -04001100 /* Write out the bitmaps */
Geliang Tang7ae16812015-12-18 22:17:00 +08001101 list_for_each_entry_safe(entry, next, bitmap_list, list) {
Chris Masond4452bc2014-05-19 20:47:56 -07001102 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001103 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001104 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001105 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001106 }
1107
Miao Xie5349d6c2014-06-19 10:42:49 +08001108 return 0;
1109}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001110
Miao Xie5349d6c2014-06-19 10:42:49 +08001111static int flush_dirty_cache(struct inode *inode)
1112{
1113 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001114
Josef Bacik0ef8b722013-10-25 16:13:35 -04001115 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001116 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001117 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001118 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001119
Miao Xie5349d6c2014-06-19 10:42:49 +08001120 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001121}
1122
1123static void noinline_for_stack
Chris Masona3bdccc2015-04-24 11:00:00 -07001124cleanup_bitmap_list(struct list_head *bitmap_list)
Chris Masond4452bc2014-05-19 20:47:56 -07001125{
Geliang Tang7ae16812015-12-18 22:17:00 +08001126 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001127
Geliang Tang7ae16812015-12-18 22:17:00 +08001128 list_for_each_entry_safe(entry, next, bitmap_list, list)
Chris Masond4452bc2014-05-19 20:47:56 -07001129 list_del_init(&entry->list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001130}
1131
1132static void noinline_for_stack
1133cleanup_write_cache_enospc(struct inode *inode,
1134 struct btrfs_io_ctl *io_ctl,
David Sterba7bf1a152017-02-10 20:23:00 +01001135 struct extent_state **cached_state)
Chris Masona3bdccc2015-04-24 11:00:00 -07001136{
Chris Masond4452bc2014-05-19 20:47:56 -07001137 io_ctl_drop_pages(io_ctl);
1138 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001139 i_size_read(inode) - 1, cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001140}
1141
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001142static int __btrfs_wait_cache_io(struct btrfs_root *root,
1143 struct btrfs_trans_handle *trans,
1144 struct btrfs_block_group_cache *block_group,
1145 struct btrfs_io_ctl *io_ctl,
1146 struct btrfs_path *path, u64 offset)
Chris Masonc9dc4c62015-04-04 17:14:42 -07001147{
1148 int ret;
1149 struct inode *inode = io_ctl->inode;
1150
Chris Mason1bbc6212015-04-06 12:46:08 -07001151 if (!inode)
1152 return 0;
1153
Chris Masonc9dc4c62015-04-04 17:14:42 -07001154 /* Flush the dirty pages in the cache file. */
1155 ret = flush_dirty_cache(inode);
1156 if (ret)
1157 goto out;
1158
1159 /* Update the cache item to tell everyone this cache file is valid. */
1160 ret = update_cache_item(trans, root, inode, path, offset,
1161 io_ctl->entries, io_ctl->bitmaps);
1162out:
1163 io_ctl_free(io_ctl);
1164 if (ret) {
1165 invalidate_inode_pages2(inode->i_mapping);
1166 BTRFS_I(inode)->generation = 0;
1167 if (block_group) {
1168#ifdef DEBUG
David Sterba3ffbd682018-06-29 10:56:42 +02001169 btrfs_err(root->fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001170 "failed to write free space cache for block group %llu",
1171 block_group->key.objectid);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001172#endif
1173 }
1174 }
1175 btrfs_update_inode(trans, root, inode);
1176
1177 if (block_group) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001178 /* the dirty list is protected by the dirty_bgs_lock */
1179 spin_lock(&trans->transaction->dirty_bgs_lock);
1180
1181 /* the disk_cache_state is protected by the block group lock */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001182 spin_lock(&block_group->lock);
1183
1184 /*
1185 * only mark this as written if we didn't get put back on
Chris Mason1bbc6212015-04-06 12:46:08 -07001186 * the dirty list while waiting for IO. Otherwise our
1187 * cache state won't be right, and we won't get written again
Chris Masonc9dc4c62015-04-04 17:14:42 -07001188 */
1189 if (!ret && list_empty(&block_group->dirty_list))
1190 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1191 else if (ret)
1192 block_group->disk_cache_state = BTRFS_DC_ERROR;
1193
1194 spin_unlock(&block_group->lock);
Chris Mason1bbc6212015-04-06 12:46:08 -07001195 spin_unlock(&trans->transaction->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001196 io_ctl->inode = NULL;
1197 iput(inode);
1198 }
1199
1200 return ret;
1201
1202}
1203
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001204static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1205 struct btrfs_trans_handle *trans,
1206 struct btrfs_io_ctl *io_ctl,
1207 struct btrfs_path *path)
1208{
1209 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1210}
1211
1212int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
1213 struct btrfs_block_group_cache *block_group,
1214 struct btrfs_path *path)
1215{
1216 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1217 block_group, &block_group->io_ctl,
1218 path, block_group->key.objectid);
1219}
1220
Chris Masond4452bc2014-05-19 20:47:56 -07001221/**
1222 * __btrfs_write_out_cache - write out cached info to an inode
1223 * @root - the root the inode belongs to
1224 * @ctl - the free space cache we are going to write out
1225 * @block_group - the block_group for this cache if it belongs to a block_group
1226 * @trans - the trans handle
Chris Masond4452bc2014-05-19 20:47:56 -07001227 *
1228 * This function writes out a free space cache struct to disk for quick recovery
Geliang Tang8cd1e732015-10-04 17:05:32 +08001229 * on mount. This will return 0 if it was successful in writing the cache out,
Omar Sandovalb8605452015-02-24 02:47:06 -08001230 * or an errno if it was not.
Chris Masond4452bc2014-05-19 20:47:56 -07001231 */
1232static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1233 struct btrfs_free_space_ctl *ctl,
1234 struct btrfs_block_group_cache *block_group,
Chris Masonc9dc4c62015-04-04 17:14:42 -07001235 struct btrfs_io_ctl *io_ctl,
David Sterba0e8d9312017-02-10 20:26:24 +01001236 struct btrfs_trans_handle *trans)
Chris Masond4452bc2014-05-19 20:47:56 -07001237{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001238 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masond4452bc2014-05-19 20:47:56 -07001239 struct extent_state *cached_state = NULL;
Miao Xie5349d6c2014-06-19 10:42:49 +08001240 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001241 int entries = 0;
1242 int bitmaps = 0;
1243 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001244 int must_iput = 0;
Chris Masond4452bc2014-05-19 20:47:56 -07001245
1246 if (!i_size_read(inode))
Omar Sandovalb8605452015-02-24 02:47:06 -08001247 return -EIO;
Chris Masond4452bc2014-05-19 20:47:56 -07001248
Chris Masonc9dc4c62015-04-04 17:14:42 -07001249 WARN_ON(io_ctl->pages);
Jeff Mahoneyf15376d2016-06-22 18:56:18 -04001250 ret = io_ctl_init(io_ctl, inode, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001251 if (ret)
Omar Sandovalb8605452015-02-24 02:47:06 -08001252 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001253
Miao Xiee570fd22014-06-19 10:42:50 +08001254 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1255 down_write(&block_group->data_rwsem);
1256 spin_lock(&block_group->lock);
1257 if (block_group->delalloc_bytes) {
1258 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1259 spin_unlock(&block_group->lock);
1260 up_write(&block_group->data_rwsem);
1261 BTRFS_I(inode)->generation = 0;
1262 ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001263 must_iput = 1;
Miao Xiee570fd22014-06-19 10:42:50 +08001264 goto out;
1265 }
1266 spin_unlock(&block_group->lock);
1267 }
1268
Chris Masond4452bc2014-05-19 20:47:56 -07001269 /* Lock all pages first so we can lock the extent safely. */
Omar Sandovalb8605452015-02-24 02:47:06 -08001270 ret = io_ctl_prepare_pages(io_ctl, inode, 0);
1271 if (ret)
Josef Bacikb77000e2017-11-15 16:20:52 -05001272 goto out_unlock;
Chris Masond4452bc2014-05-19 20:47:56 -07001273
1274 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
David Sterbaff13db42015-12-03 14:30:40 +01001275 &cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001276
Chris Masonc9dc4c62015-04-04 17:14:42 -07001277 io_ctl_set_generation(io_ctl, trans->transid);
Chris Masond4452bc2014-05-19 20:47:56 -07001278
Filipe Manana55507ce2014-12-01 17:04:09 +00001279 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001280 /* Write out the extent entries in the free space cache */
Chris Mason1bbc6212015-04-06 12:46:08 -07001281 spin_lock(&ctl->tree_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001282 ret = write_cache_extent_entries(io_ctl, ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001283 block_group, &entries, &bitmaps,
1284 &bitmap_list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001285 if (ret)
1286 goto out_nospc_locked;
Chris Masond4452bc2014-05-19 20:47:56 -07001287
Miao Xie5349d6c2014-06-19 10:42:49 +08001288 /*
1289 * Some spaces that are freed in the current transaction are pinned,
1290 * they will be added into free space cache after the transaction is
1291 * committed, we shouldn't lose them.
Chris Mason1bbc6212015-04-06 12:46:08 -07001292 *
1293 * If this changes while we are working we'll get added back to
1294 * the dirty list and redo it. No locking needed
Miao Xie5349d6c2014-06-19 10:42:49 +08001295 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001296 ret = write_pinned_extent_entries(fs_info, block_group,
1297 io_ctl, &entries);
Chris Masona3bdccc2015-04-24 11:00:00 -07001298 if (ret)
1299 goto out_nospc_locked;
Miao Xie5349d6c2014-06-19 10:42:49 +08001300
Filipe Manana55507ce2014-12-01 17:04:09 +00001301 /*
1302 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1303 * locked while doing it because a concurrent trim can be manipulating
1304 * or freeing the bitmap.
1305 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001306 ret = write_bitmap_entries(io_ctl, &bitmap_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07001307 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00001308 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001309 if (ret)
1310 goto out_nospc;
1311
1312 /* Zero out the rest of the pages just to make sure */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001313 io_ctl_zero_remaining_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001314
1315 /* Everything is written out, now we dirty the pages in the file. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001316 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1317 i_size_read(inode), &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001318 if (ret)
1319 goto out_nospc;
1320
Miao Xiee570fd22014-06-19 10:42:50 +08001321 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1322 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001323 /*
1324 * Release the pages and unlock the extent, we will flush
1325 * them out later
1326 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001327 io_ctl_drop_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001328
1329 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001330 i_size_read(inode) - 1, &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001331
Chris Masonc9dc4c62015-04-04 17:14:42 -07001332 /*
1333 * at this point the pages are under IO and we're happy,
1334 * The caller is responsible for waiting on them and updating the
1335 * the cache and the inode
1336 */
1337 io_ctl->entries = entries;
1338 io_ctl->bitmaps = bitmaps;
1339
1340 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001341 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001342 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001343
Chris Masonc9dc4c62015-04-04 17:14:42 -07001344 return 0;
1345
Josef Bacik2f356122011-06-10 15:31:13 -04001346out:
Chris Masonc9dc4c62015-04-04 17:14:42 -07001347 io_ctl->inode = NULL;
1348 io_ctl_free(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001349 if (ret) {
Josef Bacika67509c2011-10-05 15:18:58 -04001350 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001351 BTRFS_I(inode)->generation = 0;
1352 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001353 btrfs_update_inode(trans, root, inode);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001354 if (must_iput)
1355 iput(inode);
Miao Xie5349d6c2014-06-19 10:42:49 +08001356 return ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001357
Chris Masona3bdccc2015-04-24 11:00:00 -07001358out_nospc_locked:
1359 cleanup_bitmap_list(&bitmap_list);
1360 spin_unlock(&ctl->tree_lock);
1361 mutex_unlock(&ctl->cache_writeout_mutex);
1362
Josef Bacika67509c2011-10-05 15:18:58 -04001363out_nospc:
David Sterba7bf1a152017-02-10 20:23:00 +01001364 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
Miao Xiee570fd22014-06-19 10:42:50 +08001365
Josef Bacikb77000e2017-11-15 16:20:52 -05001366out_unlock:
Miao Xiee570fd22014-06-19 10:42:50 +08001367 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1368 up_write(&block_group->data_rwsem);
1369
Josef Bacika67509c2011-10-05 15:18:58 -04001370 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001371}
1372
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001373int btrfs_write_out_cache(struct btrfs_fs_info *fs_info,
Li Zefan0414efa2011-04-20 10:20:14 +08001374 struct btrfs_trans_handle *trans,
1375 struct btrfs_block_group_cache *block_group,
1376 struct btrfs_path *path)
1377{
1378 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1379 struct inode *inode;
1380 int ret = 0;
1381
Li Zefan0414efa2011-04-20 10:20:14 +08001382 spin_lock(&block_group->lock);
1383 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1384 spin_unlock(&block_group->lock);
1385 return 0;
1386 }
1387 spin_unlock(&block_group->lock);
1388
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001389 inode = lookup_free_space_inode(fs_info, block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +08001390 if (IS_ERR(inode))
1391 return 0;
1392
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001393 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1394 block_group, &block_group->io_ctl, trans);
Josef Bacikc09544e2011-08-30 10:19:10 -04001395 if (ret) {
Josef Bacikc09544e2011-08-30 10:19:10 -04001396#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001397 btrfs_err(fs_info,
1398 "failed to write free space cache for block group %llu",
1399 block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001400#endif
Chris Masonc9dc4c62015-04-04 17:14:42 -07001401 spin_lock(&block_group->lock);
1402 block_group->disk_cache_state = BTRFS_DC_ERROR;
1403 spin_unlock(&block_group->lock);
1404
1405 block_group->io_ctl.inode = NULL;
1406 iput(inode);
Li Zefan0414efa2011-04-20 10:20:14 +08001407 }
1408
Chris Masonc9dc4c62015-04-04 17:14:42 -07001409 /*
1410 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1411 * to wait for IO and put the inode
1412 */
1413
Josef Bacik0cb59c92010-07-02 12:14:14 -04001414 return ret;
1415}
1416
Li Zefan34d52cb2011-03-29 13:46:06 +08001417static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001418 u64 offset)
1419{
Josef Bacikb12d6862013-08-26 17:14:08 -04001420 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001421 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001422 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001423}
1424
Li Zefan34d52cb2011-03-29 13:46:06 +08001425static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001426{
Li Zefan34d52cb2011-03-29 13:46:06 +08001427 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001428}
1429
Li Zefan34d52cb2011-03-29 13:46:06 +08001430static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001431 u64 offset)
1432{
1433 u64 bitmap_start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001434 u64 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001435
Li Zefan34d52cb2011-03-29 13:46:06 +08001436 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1437 bitmap_start = offset - ctl->start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001438 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001439 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001440 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001441
1442 return bitmap_start;
1443}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001444
1445static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001446 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001447{
1448 struct rb_node **p = &root->rb_node;
1449 struct rb_node *parent = NULL;
1450 struct btrfs_free_space *info;
1451
1452 while (*p) {
1453 parent = *p;
1454 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1455
Josef Bacik96303082009-07-13 21:29:25 -04001456 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001457 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001458 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001459 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001460 } else {
1461 /*
1462 * we could have a bitmap entry and an extent entry
1463 * share the same offset. If this is the case, we want
1464 * the extent entry to always be found first if we do a
1465 * linear search through the tree, since we want to have
1466 * the quickest allocation time, and allocating from an
1467 * extent is faster than allocating from a bitmap. So
1468 * if we're inserting a bitmap and we find an entry at
1469 * this offset, we want to go right, or after this entry
1470 * logically. If we are inserting an extent and we've
1471 * found a bitmap, we want to go left, or before
1472 * logically.
1473 */
1474 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001475 if (info->bitmap) {
1476 WARN_ON_ONCE(1);
1477 return -EEXIST;
1478 }
Josef Bacik96303082009-07-13 21:29:25 -04001479 p = &(*p)->rb_right;
1480 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001481 if (!info->bitmap) {
1482 WARN_ON_ONCE(1);
1483 return -EEXIST;
1484 }
Josef Bacik96303082009-07-13 21:29:25 -04001485 p = &(*p)->rb_left;
1486 }
1487 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001488 }
1489
1490 rb_link_node(node, parent, p);
1491 rb_insert_color(node, root);
1492
1493 return 0;
1494}
1495
1496/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001497 * searches the tree for the given offset.
1498 *
Josef Bacik96303082009-07-13 21:29:25 -04001499 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1500 * want a section that has at least bytes size and comes at or after the given
1501 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001502 */
Josef Bacik96303082009-07-13 21:29:25 -04001503static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001504tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001505 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001506{
Li Zefan34d52cb2011-03-29 13:46:06 +08001507 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001508 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001509
Josef Bacik96303082009-07-13 21:29:25 -04001510 /* find entry that is closest to the 'offset' */
1511 while (1) {
1512 if (!n) {
1513 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001514 break;
1515 }
Josef Bacik96303082009-07-13 21:29:25 -04001516
1517 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1518 prev = entry;
1519
1520 if (offset < entry->offset)
1521 n = n->rb_left;
1522 else if (offset > entry->offset)
1523 n = n->rb_right;
1524 else
1525 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001526 }
1527
Josef Bacik96303082009-07-13 21:29:25 -04001528 if (bitmap_only) {
1529 if (!entry)
1530 return NULL;
1531 if (entry->bitmap)
1532 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001533
Josef Bacik96303082009-07-13 21:29:25 -04001534 /*
1535 * bitmap entry and extent entry may share same offset,
1536 * in that case, bitmap entry comes after extent entry.
1537 */
1538 n = rb_next(n);
1539 if (!n)
1540 return NULL;
1541 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1542 if (entry->offset != offset)
1543 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001544
Josef Bacik96303082009-07-13 21:29:25 -04001545 WARN_ON(!entry->bitmap);
1546 return entry;
1547 } else if (entry) {
1548 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001549 /*
Josef Bacik96303082009-07-13 21:29:25 -04001550 * if previous extent entry covers the offset,
1551 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001552 */
Miao Xiede6c4112012-10-18 08:18:01 +00001553 n = rb_prev(&entry->offset_index);
1554 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001555 prev = rb_entry(n, struct btrfs_free_space,
1556 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001557 if (!prev->bitmap &&
1558 prev->offset + prev->bytes > offset)
1559 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001560 }
Josef Bacik96303082009-07-13 21:29:25 -04001561 }
1562 return entry;
1563 }
1564
1565 if (!prev)
1566 return NULL;
1567
1568 /* find last entry before the 'offset' */
1569 entry = prev;
1570 if (entry->offset > offset) {
1571 n = rb_prev(&entry->offset_index);
1572 if (n) {
1573 entry = rb_entry(n, struct btrfs_free_space,
1574 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001575 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001576 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001577 if (fuzzy)
1578 return entry;
1579 else
1580 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001581 }
1582 }
1583
Josef Bacik96303082009-07-13 21:29:25 -04001584 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001585 n = rb_prev(&entry->offset_index);
1586 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001587 prev = rb_entry(n, struct btrfs_free_space,
1588 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001589 if (!prev->bitmap &&
1590 prev->offset + prev->bytes > offset)
1591 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001592 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001593 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001594 return entry;
1595 } else if (entry->offset + entry->bytes > offset)
1596 return entry;
1597
1598 if (!fuzzy)
1599 return NULL;
1600
1601 while (1) {
1602 if (entry->bitmap) {
1603 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001604 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001605 break;
1606 } else {
1607 if (entry->offset + entry->bytes > offset)
1608 break;
1609 }
1610
1611 n = rb_next(&entry->offset_index);
1612 if (!n)
1613 return NULL;
1614 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1615 }
1616 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001617}
1618
Li Zefanf333adb2010-11-09 14:57:39 +08001619static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001620__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001621 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001622{
Li Zefan34d52cb2011-03-29 13:46:06 +08001623 rb_erase(&info->offset_index, &ctl->free_space_offset);
1624 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001625}
1626
Li Zefan34d52cb2011-03-29 13:46:06 +08001627static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001628 struct btrfs_free_space *info)
1629{
Li Zefan34d52cb2011-03-29 13:46:06 +08001630 __unlink_free_space(ctl, info);
1631 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001632}
1633
Li Zefan34d52cb2011-03-29 13:46:06 +08001634static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001635 struct btrfs_free_space *info)
1636{
1637 int ret = 0;
1638
Josef Bacikb12d6862013-08-26 17:14:08 -04001639 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001640 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001641 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001642 if (ret)
1643 return ret;
1644
Li Zefan34d52cb2011-03-29 13:46:06 +08001645 ctl->free_space += info->bytes;
1646 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001647 return ret;
1648}
1649
Li Zefan34d52cb2011-03-29 13:46:06 +08001650static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001651{
Li Zefan34d52cb2011-03-29 13:46:06 +08001652 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001653 u64 max_bytes;
1654 u64 bitmap_bytes;
1655 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001656 u64 size = block_group->key.offset;
Feifei Xu0ef64472016-06-01 19:18:24 +08001657 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1658 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
Li Zefan34d52cb2011-03-29 13:46:06 +08001659
Feifei Xu0ef64472016-06-01 19:18:24 +08001660 max_bitmaps = max_t(u64, max_bitmaps, 1);
Josef Bacikdde57402013-02-12 14:07:51 -05001661
Josef Bacikb12d6862013-08-26 17:14:08 -04001662 ASSERT(ctl->total_bitmaps <= max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001663
1664 /*
1665 * The goal is to keep the total amount of memory used per 1gb of space
1666 * at or below 32k, so we need to adjust how much memory we allow to be
1667 * used by extent based free space tracking
1668 */
Byongho Leeee221842015-12-15 01:42:10 +09001669 if (size < SZ_1G)
Li Zefan8eb2d822010-11-09 14:48:01 +08001670 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1671 else
Byongho Leeee221842015-12-15 01:42:10 +09001672 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
Josef Bacik96303082009-07-13 21:29:25 -04001673
Josef Bacik25891f72009-09-11 16:11:20 -04001674 /*
1675 * we want to account for 1 more bitmap than what we have so we can make
1676 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1677 * we add more bitmaps.
1678 */
Feifei Xub9ef22d2016-06-01 19:18:25 +08001679 bitmap_bytes = (ctl->total_bitmaps + 1) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001680
Josef Bacik25891f72009-09-11 16:11:20 -04001681 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001682 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001683 return;
Josef Bacik96303082009-07-13 21:29:25 -04001684 }
Josef Bacik25891f72009-09-11 16:11:20 -04001685
1686 /*
David Sterbaf8c269d2015-01-16 17:21:12 +01001687 * we want the extent entry threshold to always be at most 1/2 the max
Josef Bacik25891f72009-09-11 16:11:20 -04001688 * bytes we can have, or whatever is less than that.
1689 */
1690 extent_bytes = max_bytes - bitmap_bytes;
David Sterbaf8c269d2015-01-16 17:21:12 +01001691 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
Josef Bacik25891f72009-09-11 16:11:20 -04001692
Li Zefan34d52cb2011-03-29 13:46:06 +08001693 ctl->extents_thresh =
David Sterbaf8c269d2015-01-16 17:21:12 +01001694 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
Josef Bacik96303082009-07-13 21:29:25 -04001695}
1696
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001697static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1698 struct btrfs_free_space *info,
1699 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001700{
Li Zefanf38b6e72011-03-14 13:40:51 +08001701 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001702
Li Zefan34d52cb2011-03-29 13:46:06 +08001703 start = offset_to_bit(info->offset, ctl->unit, offset);
1704 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001705 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001706
Li Zefanf38b6e72011-03-14 13:40:51 +08001707 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001708
1709 info->bytes -= bytes;
Josef Bacik553cceb2018-09-28 07:18:00 -04001710 if (info->max_extent_size > ctl->unit)
1711 info->max_extent_size = 0;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001712}
1713
1714static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1715 struct btrfs_free_space *info, u64 offset,
1716 u64 bytes)
1717{
1718 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001719 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001720}
1721
Li Zefan34d52cb2011-03-29 13:46:06 +08001722static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001723 struct btrfs_free_space *info, u64 offset,
1724 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001725{
Li Zefanf38b6e72011-03-14 13:40:51 +08001726 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001727
Li Zefan34d52cb2011-03-29 13:46:06 +08001728 start = offset_to_bit(info->offset, ctl->unit, offset);
1729 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001730 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001731
Li Zefanf38b6e72011-03-14 13:40:51 +08001732 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001733
1734 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001735 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001736}
1737
Miao Xiea4820392013-09-09 13:19:42 +08001738/*
1739 * If we can not find suitable extent, we will use bytes to record
1740 * the size of the max extent.
1741 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001742static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001743 struct btrfs_free_space *bitmap_info, u64 *offset,
Josef Bacik0584f712015-10-02 16:12:23 -04001744 u64 *bytes, bool for_alloc)
Josef Bacik96303082009-07-13 21:29:25 -04001745{
1746 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001747 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001748 unsigned long bits, i;
1749 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001750 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001751
Josef Bacikcef40482015-10-02 16:09:42 -04001752 /*
1753 * Skip searching the bitmap if we don't have a contiguous section that
1754 * is large enough for this allocation.
1755 */
Josef Bacik0584f712015-10-02 16:12:23 -04001756 if (for_alloc &&
1757 bitmap_info->max_extent_size &&
Josef Bacikcef40482015-10-02 16:09:42 -04001758 bitmap_info->max_extent_size < *bytes) {
1759 *bytes = bitmap_info->max_extent_size;
1760 return -1;
1761 }
1762
Li Zefan34d52cb2011-03-29 13:46:06 +08001763 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001764 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001765 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001766
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001767 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik0584f712015-10-02 16:12:23 -04001768 if (for_alloc && bits == 1) {
1769 found_bits = 1;
1770 break;
1771 }
Josef Bacik96303082009-07-13 21:29:25 -04001772 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1773 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001774 extent_bits = next_zero - i;
1775 if (extent_bits >= bits) {
1776 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001777 break;
Miao Xiea4820392013-09-09 13:19:42 +08001778 } else if (extent_bits > max_bits) {
1779 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001780 }
1781 i = next_zero;
1782 }
1783
1784 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001785 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1786 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001787 return 0;
1788 }
1789
Miao Xiea4820392013-09-09 13:19:42 +08001790 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacikcef40482015-10-02 16:09:42 -04001791 bitmap_info->max_extent_size = *bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001792 return -1;
1793}
1794
Josef Bacikad22cf62018-10-12 15:32:33 -04001795static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
1796{
1797 if (entry->bitmap)
1798 return entry->max_extent_size;
1799 return entry->bytes;
1800}
1801
Miao Xiea4820392013-09-09 13:19:42 +08001802/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001803static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001804find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Miao Xiea4820392013-09-09 13:19:42 +08001805 unsigned long align, u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04001806{
1807 struct btrfs_free_space *entry;
1808 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001809 u64 tmp;
1810 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001811 int ret;
1812
Li Zefan34d52cb2011-03-29 13:46:06 +08001813 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001814 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001815
Li Zefan34d52cb2011-03-29 13:46:06 +08001816 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001817 if (!entry)
Miao Xiea4820392013-09-09 13:19:42 +08001818 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001819
1820 for (node = &entry->offset_index; node; node = rb_next(node)) {
1821 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Miao Xiea4820392013-09-09 13:19:42 +08001822 if (entry->bytes < *bytes) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001823 *max_extent_size = max(get_max_extent_size(entry),
1824 *max_extent_size);
Josef Bacik96303082009-07-13 21:29:25 -04001825 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001826 }
Josef Bacik96303082009-07-13 21:29:25 -04001827
David Woodhouse53b381b2013-01-29 18:40:14 -05001828 /* make sure the space returned is big enough
1829 * to match our requested alignment
1830 */
1831 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001832 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01001833 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05001834 tmp = tmp * align + ctl->start;
1835 align_off = tmp - entry->offset;
1836 } else {
1837 align_off = 0;
1838 tmp = entry->offset;
1839 }
1840
Miao Xiea4820392013-09-09 13:19:42 +08001841 if (entry->bytes < *bytes + align_off) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001842 *max_extent_size = max(get_max_extent_size(entry),
1843 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001844 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001845 }
David Woodhouse53b381b2013-01-29 18:40:14 -05001846
Josef Bacik96303082009-07-13 21:29:25 -04001847 if (entry->bitmap) {
Miao Xiea4820392013-09-09 13:19:42 +08001848 u64 size = *bytes;
1849
Josef Bacik0584f712015-10-02 16:12:23 -04001850 ret = search_bitmap(ctl, entry, &tmp, &size, true);
David Woodhouse53b381b2013-01-29 18:40:14 -05001851 if (!ret) {
1852 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08001853 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04001854 return entry;
Josef Bacikad22cf62018-10-12 15:32:33 -04001855 } else {
1856 *max_extent_size =
1857 max(get_max_extent_size(entry),
1858 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001859 }
Josef Bacik96303082009-07-13 21:29:25 -04001860 continue;
1861 }
1862
David Woodhouse53b381b2013-01-29 18:40:14 -05001863 *offset = tmp;
1864 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001865 return entry;
1866 }
Miao Xiea4820392013-09-09 13:19:42 +08001867out:
Josef Bacik96303082009-07-13 21:29:25 -04001868 return NULL;
1869}
1870
Li Zefan34d52cb2011-03-29 13:46:06 +08001871static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001872 struct btrfs_free_space *info, u64 offset)
1873{
Li Zefan34d52cb2011-03-29 13:46:06 +08001874 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001875 info->bytes = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02001876 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08001877 link_free_space(ctl, info);
1878 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001879
Li Zefan34d52cb2011-03-29 13:46:06 +08001880 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001881}
1882
Li Zefan34d52cb2011-03-29 13:46:06 +08001883static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001884 struct btrfs_free_space *bitmap_info)
1885{
Li Zefan34d52cb2011-03-29 13:46:06 +08001886 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001887 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001888 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001889 ctl->total_bitmaps--;
1890 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001891}
1892
Li Zefan34d52cb2011-03-29 13:46:06 +08001893static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001894 struct btrfs_free_space *bitmap_info,
1895 u64 *offset, u64 *bytes)
1896{
1897 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001898 u64 search_start, search_bytes;
1899 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001900
1901again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001902 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001903
Josef Bacik6606bb92009-07-31 11:03:58 -04001904 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04001905 * We need to search for bits in this bitmap. We could only cover some
1906 * of the extent in this bitmap thanks to how we add space, so we need
1907 * to search for as much as it as we can and clear that amount, and then
1908 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04001909 */
1910 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001911 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00001912 search_bytes = min(search_bytes, end - search_start + 1);
Josef Bacik0584f712015-10-02 16:12:23 -04001913 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1914 false);
Josef Bacikb50c6e22013-04-25 15:55:30 -04001915 if (ret < 0 || search_start != *offset)
1916 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04001917
Josef Bacikbdb7d302012-06-27 15:10:56 -04001918 /* We may have found more bits than what we need */
1919 search_bytes = min(search_bytes, *bytes);
1920
1921 /* Cannot clear past the end of the bitmap */
1922 search_bytes = min(search_bytes, end - search_start + 1);
1923
1924 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1925 *offset += search_bytes;
1926 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001927
1928 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001929 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001930 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001931 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001932
Josef Bacik6606bb92009-07-31 11:03:58 -04001933 /*
1934 * no entry after this bitmap, but we still have bytes to
1935 * remove, so something has gone wrong.
1936 */
1937 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001938 return -EINVAL;
1939
Josef Bacik6606bb92009-07-31 11:03:58 -04001940 bitmap_info = rb_entry(next, struct btrfs_free_space,
1941 offset_index);
1942
1943 /*
1944 * if the next entry isn't a bitmap we need to return to let the
1945 * extent stuff do its work.
1946 */
Josef Bacik96303082009-07-13 21:29:25 -04001947 if (!bitmap_info->bitmap)
1948 return -EAGAIN;
1949
Josef Bacik6606bb92009-07-31 11:03:58 -04001950 /*
1951 * Ok the next item is a bitmap, but it may not actually hold
1952 * the information for the rest of this free space stuff, so
1953 * look for it, and if we don't find it return so we can try
1954 * everything over again.
1955 */
1956 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001957 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08001958 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik0584f712015-10-02 16:12:23 -04001959 &search_bytes, false);
Josef Bacik6606bb92009-07-31 11:03:58 -04001960 if (ret < 0 || search_start != *offset)
1961 return -EAGAIN;
1962
Josef Bacik96303082009-07-13 21:29:25 -04001963 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001964 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001965 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001966
1967 return 0;
1968}
1969
Josef Bacik2cdc3422011-05-27 14:07:49 -04001970static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1971 struct btrfs_free_space *info, u64 offset,
1972 u64 bytes)
1973{
1974 u64 bytes_to_set = 0;
1975 u64 end;
1976
1977 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1978
1979 bytes_to_set = min(end - offset, bytes);
1980
1981 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1982
Josef Bacikcef40482015-10-02 16:09:42 -04001983 /*
1984 * We set some bytes, we have no idea what the max extent size is
1985 * anymore.
1986 */
1987 info->max_extent_size = 0;
1988
Josef Bacik2cdc3422011-05-27 14:07:49 -04001989 return bytes_to_set;
1990
1991}
1992
Li Zefan34d52cb2011-03-29 13:46:06 +08001993static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1994 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001995{
Li Zefan34d52cb2011-03-29 13:46:06 +08001996 struct btrfs_block_group_cache *block_group = ctl->private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001997 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -04001998 bool forced = false;
1999
2000#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002001 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -04002002 forced = true;
2003#endif
Josef Bacik96303082009-07-13 21:29:25 -04002004
2005 /*
2006 * If we are below the extents threshold then we can add this as an
2007 * extent, and don't have to deal with the bitmap
2008 */
Josef Bacikd0bd4562015-09-23 14:54:14 -04002009 if (!forced && ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04002010 /*
2011 * If this block group has some small extents we don't want to
2012 * use up all of our free slots in the cache with them, we want
Nicholas D Steeves01327612016-05-19 21:18:45 -04002013 * to reserve them to larger extents, however if we have plenty
Josef Bacik32cb0842011-03-18 16:16:21 -04002014 * of cache left then go ahead an dadd them, no sense in adding
2015 * the overhead of a bitmap if we don't have to.
2016 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002017 if (info->bytes <= fs_info->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002018 if (ctl->free_extents * 2 <= ctl->extents_thresh)
2019 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002020 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002021 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002022 }
2023 }
Josef Bacik96303082009-07-13 21:29:25 -04002024
2025 /*
Josef Bacikdde57402013-02-12 14:07:51 -05002026 * The original block groups from mkfs can be really small, like 8
2027 * megabytes, so don't bother with a bitmap for those entries. However
2028 * some block groups can be smaller than what a bitmap would cover but
2029 * are still large enough that they could overflow the 32k memory limit,
2030 * so allow those block groups to still be allowed to have a bitmap
2031 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04002032 */
Josef Bacikdde57402013-02-12 14:07:51 -05002033 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08002034 return false;
2035
2036 return true;
2037}
2038
David Sterba20e55062015-11-19 11:42:28 +01002039static const struct btrfs_free_space_op free_space_op = {
Josef Bacik2cdc3422011-05-27 14:07:49 -04002040 .recalc_thresholds = recalculate_thresholds,
2041 .use_bitmap = use_bitmap,
2042};
2043
Li Zefan34d52cb2011-03-29 13:46:06 +08002044static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2045 struct btrfs_free_space *info)
2046{
2047 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002048 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08002049 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002050 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08002051 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002052
2053 bytes = info->bytes;
2054 offset = info->offset;
2055
Li Zefan34d52cb2011-03-29 13:46:06 +08002056 if (!ctl->op->use_bitmap(ctl, info))
2057 return 0;
2058
Josef Bacik2cdc3422011-05-27 14:07:49 -04002059 if (ctl->op == &free_space_op)
2060 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04002061again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04002062 /*
2063 * Since we link bitmaps right into the cluster we need to see if we
2064 * have a cluster here, and if so and it has our bitmap we need to add
2065 * the free space to that bitmap.
2066 */
2067 if (block_group && !list_empty(&block_group->cluster_list)) {
2068 struct btrfs_free_cluster *cluster;
2069 struct rb_node *node;
2070 struct btrfs_free_space *entry;
2071
2072 cluster = list_entry(block_group->cluster_list.next,
2073 struct btrfs_free_cluster,
2074 block_group_list);
2075 spin_lock(&cluster->lock);
2076 node = rb_first(&cluster->root);
2077 if (!node) {
2078 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002079 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002080 }
2081
2082 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2083 if (!entry->bitmap) {
2084 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002085 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002086 }
2087
2088 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2089 bytes_added = add_bytes_to_bitmap(ctl, entry,
2090 offset, bytes);
2091 bytes -= bytes_added;
2092 offset += bytes_added;
2093 }
2094 spin_unlock(&cluster->lock);
2095 if (!bytes) {
2096 ret = 1;
2097 goto out;
2098 }
2099 }
Chris Mason38e87882011-06-10 16:36:57 -04002100
2101no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08002102 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04002103 1, 0);
2104 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04002105 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04002106 goto new_bitmap;
2107 }
2108
Josef Bacik2cdc3422011-05-27 14:07:49 -04002109 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2110 bytes -= bytes_added;
2111 offset += bytes_added;
2112 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002113
2114 if (!bytes) {
2115 ret = 1;
2116 goto out;
2117 } else
2118 goto again;
2119
2120new_bitmap:
2121 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002122 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04002123 added = 1;
2124 info = NULL;
2125 goto again;
2126 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002127 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002128
2129 /* no pre-allocated info, allocate a new one */
2130 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05002131 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2132 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04002133 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002134 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002135 ret = -ENOMEM;
2136 goto out;
2137 }
2138 }
2139
2140 /* allocate the bitmap */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002141 info->bitmap = kzalloc(PAGE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08002142 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002143 if (!info->bitmap) {
2144 ret = -ENOMEM;
2145 goto out;
2146 }
2147 goto again;
2148 }
2149
2150out:
2151 if (info) {
zhong jiangf8b00e02018-08-13 14:06:08 +08002152 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002153 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002154 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002155
2156 return ret;
2157}
2158
Chris Mason945d8962011-05-22 12:33:42 -04002159static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08002160 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002161{
Li Zefan120d66e2010-11-09 14:56:50 +08002162 struct btrfs_free_space *left_info;
2163 struct btrfs_free_space *right_info;
2164 bool merged = false;
2165 u64 offset = info->offset;
2166 u64 bytes = info->bytes;
Josef Bacik6226cb02009-04-03 10:14:18 -04002167
Josef Bacik0f9dd462008-09-23 13:14:11 -04002168 /*
2169 * first we want to see if there is free space adjacent to the range we
2170 * are adding, if there is remove that struct and add a new one to
2171 * cover the entire range
2172 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002173 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002174 if (right_info && rb_prev(&right_info->offset_index))
2175 left_info = rb_entry(rb_prev(&right_info->offset_index),
2176 struct btrfs_free_space, offset_index);
2177 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002178 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002179
Josef Bacik96303082009-07-13 21:29:25 -04002180 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08002181 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002182 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002183 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002184 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002185 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002186 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002187 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002188 }
2189
Josef Bacik96303082009-07-13 21:29:25 -04002190 if (left_info && !left_info->bitmap &&
2191 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08002192 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002193 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002194 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002195 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002196 info->offset = left_info->offset;
2197 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002198 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002199 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002200 }
2201
Li Zefan120d66e2010-11-09 14:56:50 +08002202 return merged;
2203}
2204
Filipe Manana20005522014-08-29 13:35:13 +01002205static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2206 struct btrfs_free_space *info,
2207 bool update_stat)
2208{
2209 struct btrfs_free_space *bitmap;
2210 unsigned long i;
2211 unsigned long j;
2212 const u64 end = info->offset + info->bytes;
2213 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2214 u64 bytes;
2215
2216 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2217 if (!bitmap)
2218 return false;
2219
2220 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2221 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2222 if (j == i)
2223 return false;
2224 bytes = (j - i) * ctl->unit;
2225 info->bytes += bytes;
2226
2227 if (update_stat)
2228 bitmap_clear_bits(ctl, bitmap, end, bytes);
2229 else
2230 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2231
2232 if (!bitmap->bytes)
2233 free_bitmap(ctl, bitmap);
2234
2235 return true;
2236}
2237
2238static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2239 struct btrfs_free_space *info,
2240 bool update_stat)
2241{
2242 struct btrfs_free_space *bitmap;
2243 u64 bitmap_offset;
2244 unsigned long i;
2245 unsigned long j;
2246 unsigned long prev_j;
2247 u64 bytes;
2248
2249 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2250 /* If we're on a boundary, try the previous logical bitmap. */
2251 if (bitmap_offset == info->offset) {
2252 if (info->offset == 0)
2253 return false;
2254 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2255 }
2256
2257 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2258 if (!bitmap)
2259 return false;
2260
2261 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2262 j = 0;
2263 prev_j = (unsigned long)-1;
2264 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2265 if (j > i)
2266 break;
2267 prev_j = j;
2268 }
2269 if (prev_j == i)
2270 return false;
2271
2272 if (prev_j == (unsigned long)-1)
2273 bytes = (i + 1) * ctl->unit;
2274 else
2275 bytes = (i - prev_j) * ctl->unit;
2276
2277 info->offset -= bytes;
2278 info->bytes += bytes;
2279
2280 if (update_stat)
2281 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2282 else
2283 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2284
2285 if (!bitmap->bytes)
2286 free_bitmap(ctl, bitmap);
2287
2288 return true;
2289}
2290
2291/*
2292 * We prefer always to allocate from extent entries, both for clustered and
2293 * non-clustered allocation requests. So when attempting to add a new extent
2294 * entry, try to see if there's adjacent free space in bitmap entries, and if
2295 * there is, migrate that space from the bitmaps to the extent.
2296 * Like this we get better chances of satisfying space allocation requests
2297 * because we attempt to satisfy them based on a single cache entry, and never
2298 * on 2 or more entries - even if the entries represent a contiguous free space
2299 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2300 * ends).
2301 */
2302static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2303 struct btrfs_free_space *info,
2304 bool update_stat)
2305{
2306 /*
2307 * Only work with disconnected entries, as we can change their offset,
2308 * and must be extent entries.
2309 */
2310 ASSERT(!info->bitmap);
2311 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2312
2313 if (ctl->total_bitmaps > 0) {
2314 bool stole_end;
2315 bool stole_front = false;
2316
2317 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2318 if (ctl->total_bitmaps > 0)
2319 stole_front = steal_from_bitmap_to_front(ctl, info,
2320 update_stat);
2321
2322 if (stole_end || stole_front)
2323 try_merge_free_space(ctl, info, update_stat);
2324 }
2325}
2326
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002327int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2328 struct btrfs_free_space_ctl *ctl,
Li Zefan581bb052011-04-20 10:06:11 +08002329 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08002330{
2331 struct btrfs_free_space *info;
2332 int ret = 0;
2333
Josef Bacikdc89e982011-01-28 17:05:48 -05002334 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002335 if (!info)
2336 return -ENOMEM;
2337
2338 info->offset = offset;
2339 info->bytes = bytes;
Filipe Manana20005522014-08-29 13:35:13 +01002340 RB_CLEAR_NODE(&info->offset_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002341
Li Zefan34d52cb2011-03-29 13:46:06 +08002342 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002343
Li Zefan34d52cb2011-03-29 13:46:06 +08002344 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002345 goto link;
2346
2347 /*
2348 * There was no extent directly to the left or right of this new
2349 * extent then we know we're going to have to allocate a new extent, so
2350 * before we do that see if we need to drop this into a bitmap
2351 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002352 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002353 if (ret < 0) {
2354 goto out;
2355 } else if (ret) {
2356 ret = 0;
2357 goto out;
2358 }
2359link:
Filipe Manana20005522014-08-29 13:35:13 +01002360 /*
2361 * Only steal free space from adjacent bitmaps if we're sure we're not
2362 * going to add the new free space to existing bitmap entries - because
2363 * that would mean unnecessary work that would be reverted. Therefore
2364 * attempt to steal space from bitmaps if we're adding an extent entry.
2365 */
2366 steal_from_bitmap(ctl, info, true);
2367
Li Zefan34d52cb2011-03-29 13:46:06 +08002368 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002369 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002370 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002371out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002372 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002373
Josef Bacik0f9dd462008-09-23 13:14:11 -04002374 if (ret) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002375 btrfs_crit(fs_info, "unable to add free space :%d", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002376 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002377 }
2378
Josef Bacik0f9dd462008-09-23 13:14:11 -04002379 return ret;
2380}
2381
Josef Bacik6226cb02009-04-03 10:14:18 -04002382int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2383 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002384{
Li Zefan34d52cb2011-03-29 13:46:06 +08002385 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002386 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002387 int ret;
2388 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002389
Li Zefan34d52cb2011-03-29 13:46:06 +08002390 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002391
Josef Bacik96303082009-07-13 21:29:25 -04002392again:
Josef Bacikb0175112012-12-18 11:39:19 -05002393 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002394 if (!bytes)
2395 goto out_lock;
2396
Li Zefan34d52cb2011-03-29 13:46:06 +08002397 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002398 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002399 /*
2400 * oops didn't find an extent that matched the space we wanted
2401 * to remove, look for a bitmap instead
2402 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002403 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002404 1, 0);
2405 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002406 /*
2407 * If we found a partial bit of our free space in a
2408 * bitmap but then couldn't find the other part this may
2409 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002410 */
Josef Bacikb0175112012-12-18 11:39:19 -05002411 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002412 goto out_lock;
2413 }
Josef Bacik96303082009-07-13 21:29:25 -04002414 }
2415
Josef Bacikb0175112012-12-18 11:39:19 -05002416 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002417 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002418 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002419 if (offset == info->offset) {
2420 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002421
Josef Bacikbdb7d302012-06-27 15:10:56 -04002422 info->bytes -= to_free;
2423 info->offset += to_free;
2424 if (info->bytes) {
2425 ret = link_free_space(ctl, info);
2426 WARN_ON(ret);
2427 } else {
2428 kmem_cache_free(btrfs_free_space_cachep, info);
2429 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002430
Josef Bacikbdb7d302012-06-27 15:10:56 -04002431 offset += to_free;
2432 bytes -= to_free;
2433 goto again;
2434 } else {
2435 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002436
Josef Bacikbdb7d302012-06-27 15:10:56 -04002437 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002438 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002439 WARN_ON(ret);
2440 if (ret)
2441 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002442
Josef Bacikbdb7d302012-06-27 15:10:56 -04002443 /* Not enough bytes in this entry to satisfy us */
2444 if (old_end < offset + bytes) {
2445 bytes -= old_end - offset;
2446 offset = old_end;
2447 goto again;
2448 } else if (old_end == offset + bytes) {
2449 /* all done */
2450 goto out_lock;
2451 }
2452 spin_unlock(&ctl->tree_lock);
2453
2454 ret = btrfs_add_free_space(block_group, offset + bytes,
2455 old_end - (offset + bytes));
2456 WARN_ON(ret);
2457 goto out;
2458 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002459 }
Josef Bacik96303082009-07-13 21:29:25 -04002460
Li Zefan34d52cb2011-03-29 13:46:06 +08002461 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002462 if (ret == -EAGAIN) {
2463 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002464 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002465 }
Josef Bacik96303082009-07-13 21:29:25 -04002466out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08002467 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002468out:
Josef Bacik25179202008-10-29 14:49:05 -04002469 return ret;
2470}
2471
Josef Bacik0f9dd462008-09-23 13:14:11 -04002472void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2473 u64 bytes)
2474{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002475 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002476 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002477 struct btrfs_free_space *info;
2478 struct rb_node *n;
2479 int count = 0;
2480
Filipe Manana9084cb62018-10-22 10:43:06 +01002481 spin_lock(&ctl->tree_lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002482 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002483 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002484 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002485 count++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002486 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
Frank Holtonefe120a2013-12-20 11:37:06 -05002487 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002488 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002489 }
Filipe Manana9084cb62018-10-22 10:43:06 +01002490 spin_unlock(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002491 btrfs_info(fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002492 list_empty(&block_group->cluster_list) ? "no" : "yes");
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002493 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002494 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002495}
2496
Li Zefan34d52cb2011-03-29 13:46:06 +08002497void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002498{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002499 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002500 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002501
Li Zefan34d52cb2011-03-29 13:46:06 +08002502 spin_lock_init(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002503 ctl->unit = fs_info->sectorsize;
Li Zefan34d52cb2011-03-29 13:46:06 +08002504 ctl->start = block_group->key.objectid;
2505 ctl->private = block_group;
2506 ctl->op = &free_space_op;
Filipe Manana55507ce2014-12-01 17:04:09 +00002507 INIT_LIST_HEAD(&ctl->trimming_ranges);
2508 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002509
Li Zefan34d52cb2011-03-29 13:46:06 +08002510 /*
2511 * we only want to have 32k of ram per block group for keeping
2512 * track of free space, and if we pass 1/2 of that we want to
2513 * start converting things over to using bitmaps
2514 */
Byongho Leeee221842015-12-15 01:42:10 +09002515 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002516}
2517
Chris Masonfa9c0d792009-04-03 09:47:43 -04002518/*
2519 * for a given cluster, put all of its extents back into the free
2520 * space cache. If the block group passed doesn't match the block group
2521 * pointed to by the cluster, someone else raced in and freed the
2522 * cluster already. In that case, we just return without changing anything
2523 */
2524static int
2525__btrfs_return_cluster_to_free_space(
2526 struct btrfs_block_group_cache *block_group,
2527 struct btrfs_free_cluster *cluster)
2528{
Li Zefan34d52cb2011-03-29 13:46:06 +08002529 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002530 struct btrfs_free_space *entry;
2531 struct rb_node *node;
2532
2533 spin_lock(&cluster->lock);
2534 if (cluster->block_group != block_group)
2535 goto out;
2536
Josef Bacik96303082009-07-13 21:29:25 -04002537 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002538 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002539 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002540
Chris Masonfa9c0d792009-04-03 09:47:43 -04002541 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002542 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002543 bool bitmap;
2544
Chris Masonfa9c0d792009-04-03 09:47:43 -04002545 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2546 node = rb_next(&entry->offset_index);
2547 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002548 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002549
2550 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002551 if (!bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002552 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002553 steal_from_bitmap(ctl, entry, false);
2554 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002555 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002556 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002557 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002558 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002559
Chris Masonfa9c0d792009-04-03 09:47:43 -04002560out:
2561 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002562 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002563 return 0;
2564}
2565
Eric Sandeen48a3b632013-04-25 20:41:01 +00002566static void __btrfs_remove_free_space_cache_locked(
2567 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002568{
2569 struct btrfs_free_space *info;
2570 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002571
Li Zefan581bb052011-04-20 10:06:11 +08002572 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2573 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002574 if (!info->bitmap) {
2575 unlink_free_space(ctl, info);
2576 kmem_cache_free(btrfs_free_space_cachep, info);
2577 } else {
2578 free_bitmap(ctl, info);
2579 }
David Sterba351810c2015-01-08 15:20:54 +01002580
2581 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002582 }
Chris Mason09655372011-05-21 09:27:38 -04002583}
2584
2585void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2586{
2587 spin_lock(&ctl->tree_lock);
2588 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002589 spin_unlock(&ctl->tree_lock);
2590}
2591
Josef Bacik0f9dd462008-09-23 13:14:11 -04002592void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2593{
Li Zefan34d52cb2011-03-29 13:46:06 +08002594 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002595 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002596 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002597
Li Zefan34d52cb2011-03-29 13:46:06 +08002598 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002599 while ((head = block_group->cluster_list.next) !=
2600 &block_group->cluster_list) {
2601 cluster = list_entry(head, struct btrfs_free_cluster,
2602 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002603
2604 WARN_ON(cluster->block_group != block_group);
2605 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01002606
2607 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002608 }
Chris Mason09655372011-05-21 09:27:38 -04002609 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002610 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002611
Josef Bacik0f9dd462008-09-23 13:14:11 -04002612}
2613
Josef Bacik6226cb02009-04-03 10:14:18 -04002614u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002615 u64 offset, u64 bytes, u64 empty_size,
2616 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002617{
Li Zefan34d52cb2011-03-29 13:46:06 +08002618 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb02009-04-03 10:14:18 -04002619 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002620 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb02009-04-03 10:14:18 -04002621 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002622 u64 align_gap = 0;
2623 u64 align_gap_len = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002624
Li Zefan34d52cb2011-03-29 13:46:06 +08002625 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002626 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002627 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb02009-04-03 10:14:18 -04002628 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002629 goto out;
2630
2631 ret = offset;
2632 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002633 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002634 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002635 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002636 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002637 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002638 align_gap_len = offset - entry->offset;
2639 align_gap = entry->offset;
2640
2641 entry->offset = offset + bytes;
2642 WARN_ON(entry->bytes < bytes + align_gap_len);
2643
2644 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb02009-04-03 10:14:18 -04002645 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002646 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002647 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002648 link_free_space(ctl, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002649 }
Josef Bacik96303082009-07-13 21:29:25 -04002650out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002651 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002652
David Woodhouse53b381b2013-01-29 18:40:14 -05002653 if (align_gap_len)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002654 __btrfs_add_free_space(block_group->fs_info, ctl,
2655 align_gap, align_gap_len);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002656 return ret;
2657}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002658
2659/*
2660 * given a cluster, put all of its extents back into the free space
2661 * cache. If a block group is passed, this function will only free
2662 * a cluster that belongs to the passed block group.
2663 *
2664 * Otherwise, it'll get a reference on the block group pointed to by the
2665 * cluster and remove the cluster from it.
2666 */
2667int btrfs_return_cluster_to_free_space(
2668 struct btrfs_block_group_cache *block_group,
2669 struct btrfs_free_cluster *cluster)
2670{
Li Zefan34d52cb2011-03-29 13:46:06 +08002671 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002672 int ret;
2673
2674 /* first, get a safe pointer to the block group */
2675 spin_lock(&cluster->lock);
2676 if (!block_group) {
2677 block_group = cluster->block_group;
2678 if (!block_group) {
2679 spin_unlock(&cluster->lock);
2680 return 0;
2681 }
2682 } else if (cluster->block_group != block_group) {
2683 /* someone else has already freed it don't redo their work */
2684 spin_unlock(&cluster->lock);
2685 return 0;
2686 }
2687 atomic_inc(&block_group->count);
2688 spin_unlock(&cluster->lock);
2689
Li Zefan34d52cb2011-03-29 13:46:06 +08002690 ctl = block_group->free_space_ctl;
2691
Chris Masonfa9c0d792009-04-03 09:47:43 -04002692 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002693 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002694 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002695 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002696
2697 /* finally drop our ref */
2698 btrfs_put_block_group(block_group);
2699 return ret;
2700}
2701
Josef Bacik96303082009-07-13 21:29:25 -04002702static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2703 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002704 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002705 u64 bytes, u64 min_start,
2706 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002707{
Li Zefan34d52cb2011-03-29 13:46:06 +08002708 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002709 int err;
2710 u64 search_start = cluster->window_start;
2711 u64 search_bytes = bytes;
2712 u64 ret = 0;
2713
Josef Bacik96303082009-07-13 21:29:25 -04002714 search_start = min_start;
2715 search_bytes = bytes;
2716
Josef Bacik0584f712015-10-02 16:12:23 -04002717 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
Miao Xiea4820392013-09-09 13:19:42 +08002718 if (err) {
Josef Bacikad22cf62018-10-12 15:32:33 -04002719 *max_extent_size = max(get_max_extent_size(entry),
2720 *max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002721 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002722 }
Josef Bacik96303082009-07-13 21:29:25 -04002723
2724 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002725 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002726
2727 return ret;
2728}
2729
Chris Masonfa9c0d792009-04-03 09:47:43 -04002730/*
2731 * given a cluster, try to allocate 'bytes' from it, returns 0
2732 * if it couldn't find anything suitably large, or a logical disk offset
2733 * if things worked out
2734 */
2735u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2736 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002737 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002738{
Li Zefan34d52cb2011-03-29 13:46:06 +08002739 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002740 struct btrfs_free_space *entry = NULL;
2741 struct rb_node *node;
2742 u64 ret = 0;
2743
2744 spin_lock(&cluster->lock);
2745 if (bytes > cluster->max_size)
2746 goto out;
2747
2748 if (cluster->block_group != block_group)
2749 goto out;
2750
2751 node = rb_first(&cluster->root);
2752 if (!node)
2753 goto out;
2754
2755 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302756 while (1) {
Josef Bacikad22cf62018-10-12 15:32:33 -04002757 if (entry->bytes < bytes)
2758 *max_extent_size = max(get_max_extent_size(entry),
2759 *max_extent_size);
Miao Xiea4820392013-09-09 13:19:42 +08002760
Josef Bacik4e69b592011-03-21 10:11:24 -04002761 if (entry->bytes < bytes ||
2762 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002763 node = rb_next(&entry->offset_index);
2764 if (!node)
2765 break;
2766 entry = rb_entry(node, struct btrfs_free_space,
2767 offset_index);
2768 continue;
2769 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002770
Josef Bacik4e69b592011-03-21 10:11:24 -04002771 if (entry->bitmap) {
2772 ret = btrfs_alloc_from_bitmap(block_group,
2773 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002774 cluster->window_start,
2775 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002776 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002777 node = rb_next(&entry->offset_index);
2778 if (!node)
2779 break;
2780 entry = rb_entry(node, struct btrfs_free_space,
2781 offset_index);
2782 continue;
2783 }
Josef Bacik9b230622012-01-26 15:01:12 -05002784 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002785 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002786 ret = entry->offset;
2787
2788 entry->offset += bytes;
2789 entry->bytes -= bytes;
2790 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002791
Li Zefan5e71b5d2010-11-09 14:55:34 +08002792 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002793 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002794 break;
2795 }
2796out:
2797 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002798
Li Zefan5e71b5d2010-11-09 14:55:34 +08002799 if (!ret)
2800 return 0;
2801
Li Zefan34d52cb2011-03-29 13:46:06 +08002802 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002803
Li Zefan34d52cb2011-03-29 13:46:06 +08002804 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002805 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002806 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002807 if (entry->bitmap) {
2808 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002809 ctl->total_bitmaps--;
2810 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002811 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002812 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002813 }
2814
Li Zefan34d52cb2011-03-29 13:46:06 +08002815 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002816
Chris Masonfa9c0d792009-04-03 09:47:43 -04002817 return ret;
2818}
2819
Josef Bacik96303082009-07-13 21:29:25 -04002820static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2821 struct btrfs_free_space *entry,
2822 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002823 u64 offset, u64 bytes,
2824 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04002825{
Li Zefan34d52cb2011-03-29 13:46:06 +08002826 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002827 unsigned long next_zero;
2828 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002829 unsigned long want_bits;
2830 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002831 unsigned long found_bits;
Josef Bacikcef40482015-10-02 16:09:42 -04002832 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002833 unsigned long start = 0;
2834 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002835 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002836
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002837 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04002838 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002839 want_bits = bytes_to_bits(bytes, ctl->unit);
2840 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04002841
Josef Bacikcef40482015-10-02 16:09:42 -04002842 /*
2843 * Don't bother looking for a cluster in this bitmap if it's heavily
2844 * fragmented.
2845 */
2846 if (entry->max_extent_size &&
2847 entry->max_extent_size < cont1_bytes)
2848 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002849again:
2850 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06002851 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04002852 next_zero = find_next_zero_bit(entry->bitmap,
2853 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002854 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04002855 found_bits = next_zero - i;
Josef Bacikcef40482015-10-02 16:09:42 -04002856 if (found_bits > max_bits)
2857 max_bits = found_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002858 break;
2859 }
Josef Bacikcef40482015-10-02 16:09:42 -04002860 if (next_zero - i > max_bits)
2861 max_bits = next_zero - i;
Josef Bacik96303082009-07-13 21:29:25 -04002862 i = next_zero;
2863 }
2864
Josef Bacikcef40482015-10-02 16:09:42 -04002865 if (!found_bits) {
2866 entry->max_extent_size = (u64)max_bits * ctl->unit;
Josef Bacik4e69b592011-03-21 10:11:24 -04002867 return -ENOSPC;
Josef Bacikcef40482015-10-02 16:09:42 -04002868 }
Josef Bacik96303082009-07-13 21:29:25 -04002869
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002870 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04002871 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05002872 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002873 }
2874
2875 total_found += found_bits;
2876
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002877 if (cluster->max_size < found_bits * ctl->unit)
2878 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04002879
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002880 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2881 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04002882 goto again;
2883 }
2884
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002885 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002886 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002887 ret = tree_insert_offset(&cluster->root, entry->offset,
2888 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04002889 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04002890
Josef Bacik3f7de032011-11-10 08:29:20 -05002891 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002892 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04002893 return 0;
2894}
2895
Chris Masonfa9c0d792009-04-03 09:47:43 -04002896/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002897 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002898 * Try to find a cluster with at least bytes total bytes, at least one
2899 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04002900 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002901static noinline int
2902setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2903 struct btrfs_free_cluster *cluster,
2904 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002905 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002906{
Li Zefan34d52cb2011-03-29 13:46:06 +08002907 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002908 struct btrfs_free_space *first = NULL;
2909 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002910 struct btrfs_free_space *last;
2911 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04002912 u64 window_free;
2913 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002914 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002915
Li Zefan34d52cb2011-03-29 13:46:06 +08002916 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002917 if (!entry)
2918 return -ENOSPC;
2919
2920 /*
2921 * We don't want bitmaps, so just move along until we find a normal
2922 * extent entry.
2923 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002924 while (entry->bitmap || entry->bytes < min_bytes) {
2925 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04002926 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002927 node = rb_next(&entry->offset_index);
2928 if (!node)
2929 return -ENOSPC;
2930 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2931 }
2932
Josef Bacik4e69b592011-03-21 10:11:24 -04002933 window_free = entry->bytes;
2934 max_extent = entry->bytes;
2935 first = entry;
2936 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002937
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002938 for (node = rb_next(&entry->offset_index); node;
2939 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002940 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2941
Josef Bacik86d4a772011-05-25 13:03:16 -04002942 if (entry->bitmap) {
2943 if (list_empty(&entry->list))
2944 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002945 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002946 }
2947
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002948 if (entry->bytes < min_bytes)
2949 continue;
2950
2951 last = entry;
2952 window_free += entry->bytes;
2953 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04002954 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002955 }
2956
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002957 if (window_free < bytes || max_extent < cont1_bytes)
2958 return -ENOSPC;
2959
Josef Bacik4e69b592011-03-21 10:11:24 -04002960 cluster->window_start = first->offset;
2961
2962 node = &first->offset_index;
2963
2964 /*
2965 * now we've found our entries, pull them out of the free space
2966 * cache and put them into the cluster rbtree
2967 */
2968 do {
2969 int ret;
2970
2971 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2972 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002973 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002974 continue;
2975
Li Zefan34d52cb2011-03-29 13:46:06 +08002976 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002977 ret = tree_insert_offset(&cluster->root, entry->offset,
2978 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05002979 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04002980 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04002981 } while (node && entry != last);
2982
2983 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002984 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04002985 return 0;
2986}
2987
2988/*
2989 * This specifically looks for bitmaps that may work in the cluster, we assume
2990 * that we have already failed to find extents that will work.
2991 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002992static noinline int
2993setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2994 struct btrfs_free_cluster *cluster,
2995 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002996 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002997{
Li Zefan34d52cb2011-03-29 13:46:06 +08002998 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Mason1b9b9222015-12-15 07:15:32 -08002999 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04003000 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05003001 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04003002
Li Zefan34d52cb2011-03-29 13:46:06 +08003003 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04003004 return -ENOSPC;
3005
Josef Bacik86d4a772011-05-25 13:03:16 -04003006 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05003007 * The bitmap that covers offset won't be in the list unless offset
3008 * is just its start offset.
3009 */
Chris Mason1b9b9222015-12-15 07:15:32 -08003010 if (!list_empty(bitmaps))
3011 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3012
3013 if (!entry || entry->offset != bitmap_offset) {
Li Zefan0f0fbf12011-11-20 07:33:38 -05003014 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3015 if (entry && list_empty(&entry->list))
3016 list_add(&entry->list, bitmaps);
3017 }
3018
Josef Bacik86d4a772011-05-25 13:03:16 -04003019 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05003020 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04003021 continue;
3022 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003023 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003024 if (!ret)
3025 return 0;
3026 }
3027
3028 /*
Li Zefan52621cb2011-11-20 07:33:38 -05003029 * The bitmaps list has all the bitmaps that record free space
3030 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04003031 */
Li Zefan52621cb2011-11-20 07:33:38 -05003032 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04003033}
3034
3035/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04003036 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003037 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04003038 * We might not find them all in one contiguous area.
3039 *
3040 * returns zero and sets up cluster if things worked out, otherwise
3041 * it returns -enospc
3042 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003043int btrfs_find_space_cluster(struct btrfs_fs_info *fs_info,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003044 struct btrfs_block_group_cache *block_group,
3045 struct btrfs_free_cluster *cluster,
3046 u64 offset, u64 bytes, u64 empty_size)
3047{
Li Zefan34d52cb2011-03-29 13:46:06 +08003048 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04003049 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05003050 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003051 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003052 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003053 int ret;
3054
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003055 /*
3056 * Choose the minimum extent size we'll require for this
3057 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3058 * For metadata, allow allocates with smaller extents. For
3059 * data, keep it dense.
3060 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003061 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003062 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04003063 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003064 cont1_bytes = bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003065 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003066 } else {
3067 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003068 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003069 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003070
Li Zefan34d52cb2011-03-29 13:46:06 +08003071 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003072
3073 /*
3074 * If we know we don't have enough space to make a cluster don't even
3075 * bother doing all the work to try and find one.
3076 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003077 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003078 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003079 return -ENOSPC;
3080 }
3081
Chris Masonfa9c0d792009-04-03 09:47:43 -04003082 spin_lock(&cluster->lock);
3083
3084 /* someone already found a cluster, hooray */
3085 if (cluster->block_group) {
3086 ret = 0;
3087 goto out;
3088 }
Josef Bacik4e69b592011-03-21 10:11:24 -04003089
Josef Bacik3f7de032011-11-10 08:29:20 -05003090 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3091 min_bytes);
3092
Josef Bacik86d4a772011-05-25 13:03:16 -04003093 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003094 bytes + empty_size,
3095 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04003096 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04003097 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003098 offset, bytes + empty_size,
3099 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003100
3101 /* Clear our temporary list */
3102 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3103 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04003104
3105 if (!ret) {
3106 atomic_inc(&block_group->count);
3107 list_add_tail(&cluster->block_group_list,
3108 &block_group->cluster_list);
3109 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05003110 } else {
3111 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003112 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003113out:
3114 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08003115 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003116
3117 return ret;
3118}
3119
3120/*
3121 * simple code to zero out a cluster
3122 */
3123void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3124{
3125 spin_lock_init(&cluster->lock);
3126 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00003127 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003128 cluster->max_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003129 cluster->fragmented = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003130 INIT_LIST_HEAD(&cluster->block_group_list);
3131 cluster->block_group = NULL;
3132}
3133
Li Zefan7fe1e642011-12-29 14:47:27 +08003134static int do_trimming(struct btrfs_block_group_cache *block_group,
3135 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003136 u64 reserved_start, u64 reserved_bytes,
3137 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08003138{
3139 struct btrfs_space_info *space_info = block_group->space_info;
3140 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00003141 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003142 int ret;
3143 int update = 0;
3144 u64 trimmed = 0;
3145
3146 spin_lock(&space_info->lock);
3147 spin_lock(&block_group->lock);
3148 if (!block_group->ro) {
3149 block_group->reserved += reserved_bytes;
3150 space_info->bytes_reserved += reserved_bytes;
3151 update = 1;
3152 }
3153 spin_unlock(&block_group->lock);
3154 spin_unlock(&space_info->lock);
3155
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003156 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
Li Zefan7fe1e642011-12-29 14:47:27 +08003157 if (!ret)
3158 *total_trimmed += trimmed;
3159
Filipe Manana55507ce2014-12-01 17:04:09 +00003160 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003161 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
Filipe Manana55507ce2014-12-01 17:04:09 +00003162 list_del(&trim_entry->list);
3163 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003164
3165 if (update) {
3166 spin_lock(&space_info->lock);
3167 spin_lock(&block_group->lock);
3168 if (block_group->ro)
3169 space_info->bytes_readonly += reserved_bytes;
3170 block_group->reserved -= reserved_bytes;
3171 space_info->bytes_reserved -= reserved_bytes;
3172 spin_unlock(&space_info->lock);
3173 spin_unlock(&block_group->lock);
3174 }
3175
3176 return ret;
3177}
3178
3179static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
3180 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
Li Dongyangf7039b12011-03-24 10:24:28 +00003181{
Li Zefan34d52cb2011-03-29 13:46:06 +08003182 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003183 struct btrfs_free_space *entry;
3184 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00003185 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08003186 u64 extent_start;
3187 u64 extent_bytes;
3188 u64 bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003189
3190 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00003191 struct btrfs_trim_range trim_entry;
3192
3193 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08003194 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003195
Li Zefan34d52cb2011-03-29 13:46:06 +08003196 if (ctl->free_space < minlen) {
3197 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003198 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003199 break;
3200 }
3201
Li Zefan34d52cb2011-03-29 13:46:06 +08003202 entry = tree_search_offset(ctl, start, 0, 1);
Li Zefan7fe1e642011-12-29 14:47:27 +08003203 if (!entry) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003204 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003205 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003206 break;
3207 }
3208
Li Zefan7fe1e642011-12-29 14:47:27 +08003209 /* skip bitmaps */
3210 while (entry->bitmap) {
3211 node = rb_next(&entry->offset_index);
3212 if (!node) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003213 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003214 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003215 goto out;
Li Dongyangf7039b12011-03-24 10:24:28 +00003216 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003217 entry = rb_entry(node, struct btrfs_free_space,
3218 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003219 }
3220
Li Zefan7fe1e642011-12-29 14:47:27 +08003221 if (entry->offset >= end) {
3222 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003223 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003224 break;
3225 }
3226
3227 extent_start = entry->offset;
3228 extent_bytes = entry->bytes;
3229 start = max(start, extent_start);
3230 bytes = min(extent_start + extent_bytes, end) - start;
3231 if (bytes < minlen) {
3232 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003233 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003234 goto next;
3235 }
3236
3237 unlink_free_space(ctl, entry);
3238 kmem_cache_free(btrfs_free_space_cachep, entry);
3239
Li Zefan34d52cb2011-03-29 13:46:06 +08003240 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003241 trim_entry.start = extent_start;
3242 trim_entry.bytes = extent_bytes;
3243 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3244 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003245
Li Zefan7fe1e642011-12-29 14:47:27 +08003246 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003247 extent_start, extent_bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003248 if (ret)
3249 break;
3250next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003251 start += bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003252
3253 if (fatal_signal_pending(current)) {
3254 ret = -ERESTARTSYS;
3255 break;
3256 }
3257
3258 cond_resched();
3259 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003260out:
3261 return ret;
3262}
3263
3264static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3265 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3266{
3267 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3268 struct btrfs_free_space *entry;
3269 int ret = 0;
3270 int ret2;
3271 u64 bytes;
3272 u64 offset = offset_to_bitmap(ctl, start);
3273
3274 while (offset < end) {
3275 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003276 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003277
Filipe Manana55507ce2014-12-01 17:04:09 +00003278 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003279 spin_lock(&ctl->tree_lock);
3280
3281 if (ctl->free_space < minlen) {
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 break;
3285 }
3286
3287 entry = tree_search_offset(ctl, offset, 1, 0);
3288 if (!entry) {
3289 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003290 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003291 next_bitmap = true;
3292 goto next;
3293 }
3294
3295 bytes = minlen;
Josef Bacik0584f712015-10-02 16:12:23 -04003296 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
Li Zefan7fe1e642011-12-29 14:47:27 +08003297 if (ret2 || start >= end) {
3298 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003299 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003300 next_bitmap = true;
3301 goto next;
3302 }
3303
3304 bytes = min(bytes, end - start);
3305 if (bytes < minlen) {
3306 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003307 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003308 goto next;
3309 }
3310
3311 bitmap_clear_bits(ctl, entry, start, bytes);
3312 if (entry->bytes == 0)
3313 free_bitmap(ctl, entry);
3314
3315 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003316 trim_entry.start = start;
3317 trim_entry.bytes = bytes;
3318 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3319 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003320
3321 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003322 start, bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003323 if (ret)
3324 break;
3325next:
3326 if (next_bitmap) {
3327 offset += BITS_PER_BITMAP * ctl->unit;
3328 } else {
3329 start += bytes;
3330 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3331 offset += BITS_PER_BITMAP * ctl->unit;
3332 }
3333
3334 if (fatal_signal_pending(current)) {
3335 ret = -ERESTARTSYS;
3336 break;
3337 }
3338
3339 cond_resched();
3340 }
3341
3342 return ret;
3343}
3344
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003345void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache)
Li Zefan7fe1e642011-12-29 14:47:27 +08003346{
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003347 atomic_inc(&cache->trimming);
3348}
Li Zefan7fe1e642011-12-29 14:47:27 +08003349
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003350void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
3351{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003352 struct btrfs_fs_info *fs_info = block_group->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003353 struct extent_map_tree *em_tree;
3354 struct extent_map *em;
3355 bool cleanup;
Li Zefan7fe1e642011-12-29 14:47:27 +08003356
Filipe Manana04216822014-11-27 21:14:15 +00003357 spin_lock(&block_group->lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003358 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3359 block_group->removed);
Filipe Manana04216822014-11-27 21:14:15 +00003360 spin_unlock(&block_group->lock);
3361
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003362 if (cleanup) {
David Sterba34441362016-10-04 19:34:27 +02003363 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003364 em_tree = &fs_info->mapping_tree.map_tree;
Filipe Manana04216822014-11-27 21:14:15 +00003365 write_lock(&em_tree->lock);
3366 em = lookup_extent_mapping(em_tree, block_group->key.objectid,
3367 1);
3368 BUG_ON(!em); /* logic error, can't happen */
Filipe Mananaa1e7e162014-12-04 15:31:01 +00003369 /*
3370 * remove_extent_mapping() will delete us from the pinned_chunks
3371 * list, which is protected by the chunk mutex.
3372 */
Filipe Manana04216822014-11-27 21:14:15 +00003373 remove_extent_mapping(em_tree, em);
3374 write_unlock(&em_tree->lock);
David Sterba34441362016-10-04 19:34:27 +02003375 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana04216822014-11-27 21:14:15 +00003376
3377 /* once for us and once for the tree */
3378 free_extent_map(em);
3379 free_extent_map(em);
Filipe Manana946ddbe2014-12-01 17:04:40 +00003380
3381 /*
3382 * We've left one free space entry and other tasks trimming
3383 * this block group have left 1 entry each one. Free them.
3384 */
3385 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
Filipe Manana04216822014-11-27 21:14:15 +00003386 }
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003387}
Li Dongyangf7039b12011-03-24 10:24:28 +00003388
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003389int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
3390 u64 *trimmed, u64 start, u64 end, u64 minlen)
3391{
3392 int ret;
3393
3394 *trimmed = 0;
3395
3396 spin_lock(&block_group->lock);
3397 if (block_group->removed) {
3398 spin_unlock(&block_group->lock);
3399 return 0;
3400 }
3401 btrfs_get_block_group_trimming(block_group);
3402 spin_unlock(&block_group->lock);
3403
3404 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3405 if (ret)
3406 goto out;
3407
3408 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
3409out:
3410 btrfs_put_block_group_trimming(block_group);
Li Dongyangf7039b12011-03-24 10:24:28 +00003411 return ret;
3412}
Li Zefan581bb052011-04-20 10:06:11 +08003413
3414/*
3415 * Find the left-most item in the cache tree, and then return the
3416 * smallest inode number in the item.
3417 *
3418 * Note: the returned inode number may not be the smallest one in
3419 * the tree, if the left-most item is a bitmap.
3420 */
3421u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3422{
3423 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3424 struct btrfs_free_space *entry = NULL;
3425 u64 ino = 0;
3426
3427 spin_lock(&ctl->tree_lock);
3428
3429 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3430 goto out;
3431
3432 entry = rb_entry(rb_first(&ctl->free_space_offset),
3433 struct btrfs_free_space, offset_index);
3434
3435 if (!entry->bitmap) {
3436 ino = entry->offset;
3437
3438 unlink_free_space(ctl, entry);
3439 entry->offset++;
3440 entry->bytes--;
3441 if (!entry->bytes)
3442 kmem_cache_free(btrfs_free_space_cachep, entry);
3443 else
3444 link_free_space(ctl, entry);
3445 } else {
3446 u64 offset = 0;
3447 u64 count = 1;
3448 int ret;
3449
Josef Bacik0584f712015-10-02 16:12:23 -04003450 ret = search_bitmap(ctl, entry, &offset, &count, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003451 /* Logic error; Should be empty if it can't find anything */
Josef Bacikb12d6862013-08-26 17:14:08 -04003452 ASSERT(!ret);
Li Zefan581bb052011-04-20 10:06:11 +08003453
3454 ino = offset;
3455 bitmap_clear_bits(ctl, entry, offset, 1);
3456 if (entry->bytes == 0)
3457 free_bitmap(ctl, entry);
3458 }
3459out:
3460 spin_unlock(&ctl->tree_lock);
3461
3462 return ino;
3463}
Li Zefan82d59022011-04-20 10:33:24 +08003464
3465struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3466 struct btrfs_path *path)
3467{
3468 struct inode *inode = NULL;
3469
David Sterba57cdc8d2014-02-05 02:37:48 +01003470 spin_lock(&root->ino_cache_lock);
3471 if (root->ino_cache_inode)
3472 inode = igrab(root->ino_cache_inode);
3473 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003474 if (inode)
3475 return inode;
3476
3477 inode = __lookup_free_space_inode(root, path, 0);
3478 if (IS_ERR(inode))
3479 return inode;
3480
David Sterba57cdc8d2014-02-05 02:37:48 +01003481 spin_lock(&root->ino_cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02003482 if (!btrfs_fs_closing(root->fs_info))
David Sterba57cdc8d2014-02-05 02:37:48 +01003483 root->ino_cache_inode = igrab(inode);
3484 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003485
3486 return inode;
3487}
3488
3489int create_free_ino_inode(struct btrfs_root *root,
3490 struct btrfs_trans_handle *trans,
3491 struct btrfs_path *path)
3492{
3493 return __create_free_space_inode(root, trans, path,
3494 BTRFS_FREE_INO_OBJECTID, 0);
3495}
3496
3497int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3498{
3499 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3500 struct btrfs_path *path;
3501 struct inode *inode;
3502 int ret = 0;
3503 u64 root_gen = btrfs_root_generation(&root->root_item);
3504
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003505 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04003506 return 0;
3507
Li Zefan82d59022011-04-20 10:33:24 +08003508 /*
3509 * If we're unmounting then just return, since this does a search on the
3510 * normal root and not the commit root and we could deadlock.
3511 */
David Sterba7841cb22011-05-31 18:07:27 +02003512 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08003513 return 0;
3514
3515 path = btrfs_alloc_path();
3516 if (!path)
3517 return 0;
3518
3519 inode = lookup_free_ino_inode(root, path);
3520 if (IS_ERR(inode))
3521 goto out;
3522
3523 if (root_gen != BTRFS_I(inode)->generation)
3524 goto out_put;
3525
3526 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3527
3528 if (ret < 0)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003529 btrfs_err(fs_info,
3530 "failed to load free ino cache for root %llu",
3531 root->root_key.objectid);
Li Zefan82d59022011-04-20 10:33:24 +08003532out_put:
3533 iput(inode);
3534out:
3535 btrfs_free_path(path);
3536 return ret;
3537}
3538
3539int btrfs_write_out_ino_cache(struct btrfs_root *root,
3540 struct btrfs_trans_handle *trans,
Filipe David Borba Manana53645a92013-09-20 14:43:28 +01003541 struct btrfs_path *path,
3542 struct inode *inode)
Li Zefan82d59022011-04-20 10:33:24 +08003543{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003544 struct btrfs_fs_info *fs_info = root->fs_info;
Li Zefan82d59022011-04-20 10:33:24 +08003545 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
Li Zefan82d59022011-04-20 10:33:24 +08003546 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003547 struct btrfs_io_ctl io_ctl;
Filipe Mananae43699d2015-05-05 15:21:27 +01003548 bool release_metadata = true;
Li Zefan82d59022011-04-20 10:33:24 +08003549
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003550 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04003551 return 0;
3552
Chris Mason85db36c2015-04-23 08:02:49 -07003553 memset(&io_ctl, 0, sizeof(io_ctl));
David Sterba0e8d9312017-02-10 20:26:24 +01003554 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
Filipe Mananae43699d2015-05-05 15:21:27 +01003555 if (!ret) {
3556 /*
3557 * At this point writepages() didn't error out, so our metadata
3558 * reservation is released when the writeback finishes, at
3559 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3560 * with or without an error.
3561 */
3562 release_metadata = false;
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003563 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
Filipe Mananae43699d2015-05-05 15:21:27 +01003564 }
Chris Mason85db36c2015-04-23 08:02:49 -07003565
Josef Bacikc09544e2011-08-30 10:19:10 -04003566 if (ret) {
Filipe Mananae43699d2015-05-05 15:21:27 +01003567 if (release_metadata)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003568 btrfs_delalloc_release_metadata(BTRFS_I(inode),
Qu Wenruo43b18592017-12-12 15:34:32 +08003569 inode->i_size, true);
Josef Bacikc09544e2011-08-30 10:19:10 -04003570#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003571 btrfs_err(fs_info,
3572 "failed to write free ino cache for root %llu",
3573 root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04003574#endif
3575 }
Li Zefan82d59022011-04-20 10:33:24 +08003576
Li Zefan82d59022011-04-20 10:33:24 +08003577 return ret;
3578}
Josef Bacik74255aa2013-03-15 09:47:08 -04003579
3580#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd52013-08-14 15:05:12 -04003581/*
3582 * Use this if you need to make a bitmap or extent entry specifically, it
3583 * doesn't do any of the merging that add_free_space does, this acts a lot like
3584 * how the free space cache loading stuff works, so you can get really weird
3585 * configurations.
3586 */
3587int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3588 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04003589{
Josef Bacikdc11dd52013-08-14 15:05:12 -04003590 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3591 struct btrfs_free_space *info = NULL, *bitmap_info;
3592 void *map = NULL;
3593 u64 bytes_added;
3594 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04003595
Josef Bacikdc11dd52013-08-14 15:05:12 -04003596again:
3597 if (!info) {
3598 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3599 if (!info)
3600 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04003601 }
3602
Josef Bacikdc11dd52013-08-14 15:05:12 -04003603 if (!bitmap) {
3604 spin_lock(&ctl->tree_lock);
3605 info->offset = offset;
3606 info->bytes = bytes;
Josef Bacikcef40482015-10-02 16:09:42 -04003607 info->max_extent_size = 0;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003608 ret = link_free_space(ctl, info);
3609 spin_unlock(&ctl->tree_lock);
3610 if (ret)
3611 kmem_cache_free(btrfs_free_space_cachep, info);
3612 return ret;
3613 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003614
Josef Bacikdc11dd52013-08-14 15:05:12 -04003615 if (!map) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003616 map = kzalloc(PAGE_SIZE, GFP_NOFS);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003617 if (!map) {
3618 kmem_cache_free(btrfs_free_space_cachep, info);
3619 return -ENOMEM;
3620 }
3621 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003622
Josef Bacikdc11dd52013-08-14 15:05:12 -04003623 spin_lock(&ctl->tree_lock);
3624 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3625 1, 0);
3626 if (!bitmap_info) {
3627 info->bitmap = map;
3628 map = NULL;
3629 add_new_bitmap(ctl, info, offset);
3630 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01003631 info = NULL;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003632 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003633
Josef Bacikdc11dd52013-08-14 15:05:12 -04003634 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
Josef Bacikcef40482015-10-02 16:09:42 -04003635
Josef Bacikdc11dd52013-08-14 15:05:12 -04003636 bytes -= bytes_added;
3637 offset += bytes_added;
3638 spin_unlock(&ctl->tree_lock);
3639
3640 if (bytes)
3641 goto again;
3642
Filipe Manana20005522014-08-29 13:35:13 +01003643 if (info)
3644 kmem_cache_free(btrfs_free_space_cachep, info);
zhong jiangf8b00e02018-08-13 14:06:08 +08003645 kfree(map);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003646 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003647}
3648
3649/*
3650 * Checks to see if the given range is in the free space cache. This is really
3651 * just used to check the absence of space, so if there is free space in the
3652 * range at all we will return 1.
3653 */
Josef Bacikdc11dd52013-08-14 15:05:12 -04003654int test_check_exists(struct btrfs_block_group_cache *cache,
3655 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04003656{
3657 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3658 struct btrfs_free_space *info;
3659 int ret = 0;
3660
3661 spin_lock(&ctl->tree_lock);
3662 info = tree_search_offset(ctl, offset, 0, 0);
3663 if (!info) {
3664 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3665 1, 0);
3666 if (!info)
3667 goto out;
3668 }
3669
3670have_info:
3671 if (info->bitmap) {
3672 u64 bit_off, bit_bytes;
3673 struct rb_node *n;
3674 struct btrfs_free_space *tmp;
3675
3676 bit_off = offset;
3677 bit_bytes = ctl->unit;
Josef Bacik0584f712015-10-02 16:12:23 -04003678 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
Josef Bacik74255aa2013-03-15 09:47:08 -04003679 if (!ret) {
3680 if (bit_off == offset) {
3681 ret = 1;
3682 goto out;
3683 } else if (bit_off > offset &&
3684 offset + bytes > bit_off) {
3685 ret = 1;
3686 goto out;
3687 }
3688 }
3689
3690 n = rb_prev(&info->offset_index);
3691 while (n) {
3692 tmp = rb_entry(n, struct btrfs_free_space,
3693 offset_index);
3694 if (tmp->offset + tmp->bytes < offset)
3695 break;
3696 if (offset + bytes < tmp->offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08003697 n = rb_prev(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04003698 continue;
3699 }
3700 info = tmp;
3701 goto have_info;
3702 }
3703
3704 n = rb_next(&info->offset_index);
3705 while (n) {
3706 tmp = rb_entry(n, struct btrfs_free_space,
3707 offset_index);
3708 if (offset + bytes < tmp->offset)
3709 break;
3710 if (tmp->offset + tmp->bytes < offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08003711 n = rb_next(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04003712 continue;
3713 }
3714 info = tmp;
3715 goto have_info;
3716 }
3717
Filipe Manana20005522014-08-29 13:35:13 +01003718 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003719 goto out;
3720 }
3721
3722 if (info->offset == offset) {
3723 ret = 1;
3724 goto out;
3725 }
3726
3727 if (offset > info->offset && offset < info->offset + info->bytes)
3728 ret = 1;
3729out:
3730 spin_unlock(&ctl->tree_lock);
3731 return ret;
3732}
Josef Bacikdc11dd52013-08-14 15:05:12 -04003733#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */