blob: 6814fa42eba3ccf8a2f47cb1e6bc73591b75075a [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
David Sterba7949f332019-03-20 13:40:19 +010091struct inode *lookup_free_space_inode(
92 struct btrfs_block_group_cache *block_group,
93 struct btrfs_path *path)
Li Zefan0414efa2011-04-20 10:20:14 +080094{
David Sterba7949f332019-03-20 13:40:19 +010095 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +080096 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -040097 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +080098
99 spin_lock(&block_group->lock);
100 if (block_group->inode)
101 inode = igrab(block_group->inode);
102 spin_unlock(&block_group->lock);
103 if (inode)
104 return inode;
105
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500106 inode = __lookup_free_space_inode(fs_info->tree_root, path,
Li Zefan0414efa2011-04-20 10:20:14 +0800107 block_group->key.objectid);
108 if (IS_ERR(inode))
109 return inode;
110
Josef Bacik0af3d002010-06-21 14:48:16 -0400111 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400112 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400113 btrfs_info(fs_info, "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400114 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
115 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400116 block_group->disk_cache_state = BTRFS_DC_CLEAR;
117 }
118
Josef Bacik300e4f82011-08-29 14:06:00 -0400119 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400120 block_group->inode = igrab(inode);
121 block_group->iref = 1;
122 }
123 spin_unlock(&block_group->lock);
124
125 return inode;
126}
127
Eric Sandeen48a3b632013-04-25 20:41:01 +0000128static int __create_free_space_inode(struct btrfs_root *root,
129 struct btrfs_trans_handle *trans,
130 struct btrfs_path *path,
131 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400132{
133 struct btrfs_key key;
134 struct btrfs_disk_key disk_key;
135 struct btrfs_free_space_header *header;
136 struct btrfs_inode_item *inode_item;
137 struct extent_buffer *leaf;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400138 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
Josef Bacik0af3d002010-06-21 14:48:16 -0400139 int ret;
140
Li Zefan0414efa2011-04-20 10:20:14 +0800141 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400142 if (ret)
143 return ret;
144
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400145 /* We inline crc's for the free disk space cache */
146 if (ino != BTRFS_FREE_INO_OBJECTID)
147 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
148
Josef Bacik0af3d002010-06-21 14:48:16 -0400149 leaf = path->nodes[0];
150 inode_item = btrfs_item_ptr(leaf, path->slots[0],
151 struct btrfs_inode_item);
152 btrfs_item_key(leaf, &disk_key, path->slots[0]);
David Sterbab159fa22016-11-08 18:09:03 +0100153 memzero_extent_buffer(leaf, (unsigned long)inode_item,
Josef Bacik0af3d002010-06-21 14:48:16 -0400154 sizeof(*inode_item));
155 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
156 btrfs_set_inode_size(leaf, inode_item, 0);
157 btrfs_set_inode_nbytes(leaf, inode_item, 0);
158 btrfs_set_inode_uid(leaf, inode_item, 0);
159 btrfs_set_inode_gid(leaf, inode_item, 0);
160 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400161 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400162 btrfs_set_inode_nlink(leaf, inode_item, 1);
163 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800164 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400165 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200166 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400167
168 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800169 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400170 key.type = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400171 ret = btrfs_insert_empty_item(trans, root, path, &key,
172 sizeof(struct btrfs_free_space_header));
173 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200174 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400175 return ret;
176 }
Chris Masonc9dc4c62015-04-04 17:14:42 -0700177
Josef Bacik0af3d002010-06-21 14:48:16 -0400178 leaf = path->nodes[0];
179 header = btrfs_item_ptr(leaf, path->slots[0],
180 struct btrfs_free_space_header);
David Sterbab159fa22016-11-08 18:09:03 +0100181 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
Josef Bacik0af3d002010-06-21 14:48:16 -0400182 btrfs_set_free_space_key(leaf, header, &disk_key);
183 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200184 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400185
186 return 0;
187}
188
David Sterba4ca75f12019-03-20 13:42:57 +0100189int create_free_space_inode(struct btrfs_trans_handle *trans,
Li Zefan0414efa2011-04-20 10:20:14 +0800190 struct btrfs_block_group_cache *block_group,
191 struct btrfs_path *path)
192{
193 int ret;
194 u64 ino;
195
David Sterba4ca75f12019-03-20 13:42:57 +0100196 ret = btrfs_find_free_objectid(trans->fs_info->tree_root, &ino);
Li Zefan0414efa2011-04-20 10:20:14 +0800197 if (ret < 0)
198 return ret;
199
David Sterba4ca75f12019-03-20 13:42:57 +0100200 return __create_free_space_inode(trans->fs_info->tree_root, trans, path,
201 ino, block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800202}
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
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200468 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
469 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400470 io_ctl_unmap_page(io_ctl);
Chris Mason2b108262015-04-06 07:48:20 -0700471 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400472 tmp += index;
473 *tmp = crc;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400474}
475
Chris Mason4c6d1d82015-04-06 13:17:20 -0700476static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400477{
478 u32 *tmp, val;
479 u32 crc = ~(u32)0;
480 unsigned offset = 0;
481
482 if (!io_ctl->check_crcs) {
483 io_ctl_map_page(io_ctl, 0);
484 return 0;
485 }
486
487 if (index == 0)
488 offset = sizeof(u32) * io_ctl->num_pages;
489
Chris Mason2b108262015-04-06 07:48:20 -0700490 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400491 tmp += index;
492 val = *tmp;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400493
494 io_ctl_map_page(io_ctl, 0);
Johannes Thumshirn4bb3c2e2019-05-22 10:19:00 +0200495 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
496 btrfs_crc32c_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400497 if (val != crc) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400498 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200499 "csum mismatch on free space cache");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400500 io_ctl_unmap_page(io_ctl);
501 return -EIO;
502 }
503
Josef Bacika67509c2011-10-05 15:18:58 -0400504 return 0;
505}
506
Chris Mason4c6d1d82015-04-06 13:17:20 -0700507static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400508 void *bitmap)
509{
510 struct btrfs_free_space_entry *entry;
511
512 if (!io_ctl->cur)
513 return -ENOSPC;
514
515 entry = io_ctl->cur;
516 entry->offset = cpu_to_le64(offset);
517 entry->bytes = cpu_to_le64(bytes);
518 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
519 BTRFS_FREE_SPACE_EXTENT;
520 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
521 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
522
523 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
524 return 0;
525
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400526 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400527
528 /* No more pages to map */
529 if (io_ctl->index >= io_ctl->num_pages)
530 return 0;
531
532 /* map the next page */
533 io_ctl_map_page(io_ctl, 1);
534 return 0;
535}
536
Chris Mason4c6d1d82015-04-06 13:17:20 -0700537static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
Josef Bacika67509c2011-10-05 15:18:58 -0400538{
539 if (!io_ctl->cur)
540 return -ENOSPC;
541
542 /*
543 * If we aren't at the start of the current page, unmap this one and
544 * map the next one if there is any left.
545 */
546 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400547 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400548 if (io_ctl->index >= io_ctl->num_pages)
549 return -ENOSPC;
550 io_ctl_map_page(io_ctl, 0);
551 }
552
David Sterba69d24802018-06-29 10:56:44 +0200553 copy_page(io_ctl->cur, bitmap);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400554 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400555 if (io_ctl->index < io_ctl->num_pages)
556 io_ctl_map_page(io_ctl, 0);
557 return 0;
558}
559
Chris Mason4c6d1d82015-04-06 13:17:20 -0700560static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400561{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400562 /*
563 * If we're not on the boundary we know we've modified the page and we
564 * need to crc the page.
565 */
566 if (io_ctl->cur != io_ctl->orig)
567 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
568 else
569 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400570
571 while (io_ctl->index < io_ctl->num_pages) {
572 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400573 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400574 }
575}
576
Chris Mason4c6d1d82015-04-06 13:17:20 -0700577static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400578 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400579{
580 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500581 int ret;
582
583 if (!io_ctl->cur) {
584 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
585 if (ret)
586 return ret;
587 }
Josef Bacika67509c2011-10-05 15:18:58 -0400588
589 e = io_ctl->cur;
590 entry->offset = le64_to_cpu(e->offset);
591 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400592 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400593 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
594 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
595
596 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400597 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400598
599 io_ctl_unmap_page(io_ctl);
600
Josef Bacik2f120c02011-11-10 20:45:05 -0500601 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400602}
603
Chris Mason4c6d1d82015-04-06 13:17:20 -0700604static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400605 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400606{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400607 int ret;
608
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400609 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
610 if (ret)
611 return ret;
612
David Sterba69d24802018-06-29 10:56:44 +0200613 copy_page(entry->bitmap, io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400614 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400615
616 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400617}
618
Josef Bacikcd023e72012-05-14 10:06:40 -0400619/*
620 * Since we attach pinned extents after the fact we can have contiguous sections
621 * of free space that are split up in entries. This poses a problem with the
622 * tree logging stuff since it could have allocated across what appears to be 2
623 * entries since we would have merged the entries when adding the pinned extents
624 * back to the free space cache. So run through the space cache that we just
625 * loaded and merge contiguous entries. This will make the log replay stuff not
626 * blow up and it will make for nicer allocator behavior.
627 */
628static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
629{
630 struct btrfs_free_space *e, *prev = NULL;
631 struct rb_node *n;
632
633again:
634 spin_lock(&ctl->tree_lock);
635 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
636 e = rb_entry(n, struct btrfs_free_space, offset_index);
637 if (!prev)
638 goto next;
639 if (e->bitmap || prev->bitmap)
640 goto next;
641 if (prev->offset + prev->bytes == e->offset) {
642 unlink_free_space(ctl, prev);
643 unlink_free_space(ctl, e);
644 prev->bytes += e->bytes;
645 kmem_cache_free(btrfs_free_space_cachep, e);
646 link_free_space(ctl, prev);
647 prev = NULL;
648 spin_unlock(&ctl->tree_lock);
649 goto again;
650 }
651next:
652 prev = e;
653 }
654 spin_unlock(&ctl->tree_lock);
655}
656
Eric Sandeen48a3b632013-04-25 20:41:01 +0000657static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
658 struct btrfs_free_space_ctl *ctl,
659 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400660{
David Sterba3ffbd682018-06-29 10:56:42 +0200661 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik9d66e232010-08-25 16:54:15 -0400662 struct btrfs_free_space_header *header;
663 struct extent_buffer *leaf;
Chris Mason4c6d1d82015-04-06 13:17:20 -0700664 struct btrfs_io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400665 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400666 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800667 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400668 u64 num_entries;
669 u64 num_bitmaps;
670 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400671 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400672 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400673
Josef Bacik9d66e232010-08-25 16:54:15 -0400674 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800675 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400676 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400677
678 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800679 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400680 key.type = 0;
681
682 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800683 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400684 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800685 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400686 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400687 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400688 }
689
Li Zefan0414efa2011-04-20 10:20:14 +0800690 ret = -1;
691
Josef Bacik9d66e232010-08-25 16:54:15 -0400692 leaf = path->nodes[0];
693 header = btrfs_item_ptr(leaf, path->slots[0],
694 struct btrfs_free_space_header);
695 num_entries = btrfs_free_space_entries(leaf, header);
696 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
697 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400698 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400699
Miao Xiee570fd22014-06-19 10:42:50 +0800700 if (!BTRFS_I(inode)->generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400701 btrfs_info(fs_info,
David Sterba913e1532017-07-13 15:32:18 +0200702 "the free space cache file (%llu) is invalid, skip it",
Miao Xiee570fd22014-06-19 10:42:50 +0800703 offset);
704 return 0;
705 }
706
Josef Bacik9d66e232010-08-25 16:54:15 -0400707 if (BTRFS_I(inode)->generation != generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400708 btrfs_err(fs_info,
709 "free space inode generation (%llu) did not match free space cache generation (%llu)",
710 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400711 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400712 }
713
714 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400715 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400716
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400717 ret = io_ctl_init(&io_ctl, inode, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800718 if (ret)
719 return ret;
720
David Sterba1d480532017-01-23 17:28:19 +0100721 readahead_cache(inode);
Josef Bacik9d66e232010-08-25 16:54:15 -0400722
Josef Bacika67509c2011-10-05 15:18:58 -0400723 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
724 if (ret)
725 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400726
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400727 ret = io_ctl_check_crc(&io_ctl, 0);
728 if (ret)
729 goto free_cache;
730
Josef Bacika67509c2011-10-05 15:18:58 -0400731 ret = io_ctl_check_generation(&io_ctl, generation);
732 if (ret)
733 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400734
Josef Bacika67509c2011-10-05 15:18:58 -0400735 while (num_entries) {
736 e = kmem_cache_zalloc(btrfs_free_space_cachep,
737 GFP_NOFS);
738 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400739 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400740
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400741 ret = io_ctl_read_entry(&io_ctl, e, &type);
742 if (ret) {
743 kmem_cache_free(btrfs_free_space_cachep, e);
744 goto free_cache;
745 }
746
Josef Bacika67509c2011-10-05 15:18:58 -0400747 if (!e->bytes) {
748 kmem_cache_free(btrfs_free_space_cachep, e);
749 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400750 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400751
Josef Bacika67509c2011-10-05 15:18:58 -0400752 if (type == BTRFS_FREE_SPACE_EXTENT) {
753 spin_lock(&ctl->tree_lock);
754 ret = link_free_space(ctl, e);
755 spin_unlock(&ctl->tree_lock);
756 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400757 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000758 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500759 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400760 goto free_cache;
761 }
Josef Bacika67509c2011-10-05 15:18:58 -0400762 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400763 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400764 num_bitmaps--;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300765 e->bitmap = kzalloc(PAGE_SIZE, GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400766 if (!e->bitmap) {
767 kmem_cache_free(
768 btrfs_free_space_cachep, e);
769 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400770 }
Josef Bacika67509c2011-10-05 15:18:58 -0400771 spin_lock(&ctl->tree_lock);
772 ret = link_free_space(ctl, e);
773 ctl->total_bitmaps++;
774 ctl->op->recalc_thresholds(ctl);
775 spin_unlock(&ctl->tree_lock);
776 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400777 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000778 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400779 kmem_cache_free(btrfs_free_space_cachep, e);
780 goto free_cache;
781 }
782 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400783 }
784
Josef Bacika67509c2011-10-05 15:18:58 -0400785 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400786 }
787
Josef Bacik2f120c02011-11-10 20:45:05 -0500788 io_ctl_unmap_page(&io_ctl);
789
Josef Bacika67509c2011-10-05 15:18:58 -0400790 /*
791 * We add the bitmaps at the end of the entries in order that
792 * the bitmap entries are added to the cache.
793 */
794 list_for_each_entry_safe(e, n, &bitmaps, list) {
795 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400796 ret = io_ctl_read_bitmap(&io_ctl, e);
797 if (ret)
798 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400799 }
800
801 io_ctl_drop_pages(&io_ctl);
Josef Bacikcd023e72012-05-14 10:06:40 -0400802 merge_space_tree(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400803 ret = 1;
804out:
Josef Bacika67509c2011-10-05 15:18:58 -0400805 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400806 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400807free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400808 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800809 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400810 goto out;
811}
812
David Sterbabb6cb1c2019-03-20 13:47:15 +0100813int load_free_space_cache(struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400814{
David Sterbabb6cb1c2019-03-20 13:47:15 +0100815 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +0800816 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800817 struct inode *inode;
818 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400819 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800820 bool matched;
821 u64 used = btrfs_block_group_used(&block_group->item);
822
823 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800824 * If this block group has been marked to be cleared for one reason or
825 * another then we can't trust the on disk cache, so just return.
826 */
827 spin_lock(&block_group->lock);
828 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
829 spin_unlock(&block_group->lock);
830 return 0;
831 }
832 spin_unlock(&block_group->lock);
833
834 path = btrfs_alloc_path();
835 if (!path)
836 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400837 path->search_commit_root = 1;
838 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800839
Filipe Manana4222ea72018-10-24 10:13:03 +0100840 /*
841 * We must pass a path with search_commit_root set to btrfs_iget in
842 * order to avoid a deadlock when allocating extents for the tree root.
843 *
844 * When we are COWing an extent buffer from the tree root, when looking
845 * for a free extent, at extent-tree.c:find_free_extent(), we can find
846 * block group without its free space cache loaded. When we find one
847 * we must load its space cache which requires reading its free space
848 * cache's inode item from the root tree. If this inode item is located
849 * in the same leaf that we started COWing before, then we end up in
850 * deadlock on the extent buffer (trying to read lock it when we
851 * previously write locked it).
852 *
853 * It's safe to read the inode item using the commit root because
854 * block groups, once loaded, stay in memory forever (until they are
855 * removed) as well as their space caches once loaded. New block groups
856 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
857 * we will never try to read their inode item while the fs is mounted.
858 */
David Sterba7949f332019-03-20 13:40:19 +0100859 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +0800860 if (IS_ERR(inode)) {
861 btrfs_free_path(path);
862 return 0;
863 }
864
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400865 /* We may have converted the inode and made the cache invalid. */
866 spin_lock(&block_group->lock);
867 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
868 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900869 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400870 goto out;
871 }
872 spin_unlock(&block_group->lock);
873
Li Zefan0414efa2011-04-20 10:20:14 +0800874 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
875 path, block_group->key.objectid);
876 btrfs_free_path(path);
877 if (ret <= 0)
878 goto out;
879
880 spin_lock(&ctl->tree_lock);
881 matched = (ctl->free_space == (block_group->key.offset - used -
882 block_group->bytes_super));
883 spin_unlock(&ctl->tree_lock);
884
885 if (!matched) {
886 __btrfs_remove_free_space_cache(ctl);
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400887 btrfs_warn(fs_info,
888 "block group %llu has wrong amount of free space",
889 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800890 ret = -1;
891 }
892out:
893 if (ret < 0) {
894 /* This cache is bogus, make sure it gets cleared */
895 spin_lock(&block_group->lock);
896 block_group->disk_cache_state = BTRFS_DC_CLEAR;
897 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800898 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800899
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400900 btrfs_warn(fs_info,
901 "failed to load free space cache for block group %llu, rebuilding it now",
902 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800903 }
904
905 iput(inode);
906 return ret;
907}
908
Chris Masond4452bc2014-05-19 20:47:56 -0700909static noinline_for_stack
Chris Mason4c6d1d82015-04-06 13:17:20 -0700910int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -0700911 struct btrfs_free_space_ctl *ctl,
912 struct btrfs_block_group_cache *block_group,
913 int *entries, int *bitmaps,
914 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400915{
Josef Bacikc09544e2011-08-30 10:19:10 -0400916 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -0700917 struct btrfs_free_cluster *cluster = NULL;
Chris Mason1bbc6212015-04-06 12:46:08 -0700918 struct btrfs_free_cluster *cluster_locked = NULL;
Chris Masond4452bc2014-05-19 20:47:56 -0700919 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +0000920 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400921
Josef Bacik43be2142011-04-01 14:55:00 +0000922 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -0700923 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000924 cluster = list_entry(block_group->cluster_list.next,
925 struct btrfs_free_cluster,
926 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -0700927 }
Josef Bacik43be2142011-04-01 14:55:00 +0000928
Josef Bacikf75b1302011-10-05 10:00:18 -0400929 if (!node && cluster) {
Chris Mason1bbc6212015-04-06 12:46:08 -0700930 cluster_locked = cluster;
931 spin_lock(&cluster_locked->lock);
Josef Bacikf75b1302011-10-05 10:00:18 -0400932 node = rb_first(&cluster->root);
933 cluster = NULL;
934 }
935
Josef Bacik0cb59c92010-07-02 12:14:14 -0400936 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400937 while (node) {
938 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400939
Josef Bacika67509c2011-10-05 15:18:58 -0400940 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -0700941 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +0000942
Chris Masond4452bc2014-05-19 20:47:56 -0700943 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400944 e->bitmap);
945 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -0700946 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -0400947
948 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -0700949 list_add_tail(&e->list, bitmap_list);
950 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400951 }
952 node = rb_next(node);
953 if (!node && cluster) {
954 node = rb_first(&cluster->root);
Chris Mason1bbc6212015-04-06 12:46:08 -0700955 cluster_locked = cluster;
956 spin_lock(&cluster_locked->lock);
Josef Bacika67509c2011-10-05 15:18:58 -0400957 cluster = NULL;
958 }
959 }
Chris Mason1bbc6212015-04-06 12:46:08 -0700960 if (cluster_locked) {
961 spin_unlock(&cluster_locked->lock);
962 cluster_locked = NULL;
963 }
Filipe Manana55507ce2014-12-01 17:04:09 +0000964
965 /*
966 * Make sure we don't miss any range that was removed from our rbtree
967 * because trimming is running. Otherwise after a umount+mount (or crash
968 * after committing the transaction) we would leak free space and get
969 * an inconsistent free space cache report from fsck.
970 */
971 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
972 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
973 trim_entry->bytes, NULL);
974 if (ret)
975 goto fail;
976 *entries += 1;
977 }
978
Chris Masond4452bc2014-05-19 20:47:56 -0700979 return 0;
980fail:
Chris Mason1bbc6212015-04-06 12:46:08 -0700981 if (cluster_locked)
982 spin_unlock(&cluster_locked->lock);
Chris Masond4452bc2014-05-19 20:47:56 -0700983 return -ENOSPC;
984}
985
986static noinline_for_stack int
987update_cache_item(struct btrfs_trans_handle *trans,
988 struct btrfs_root *root,
989 struct inode *inode,
990 struct btrfs_path *path, u64 offset,
991 int entries, int bitmaps)
992{
993 struct btrfs_key key;
994 struct btrfs_free_space_header *header;
995 struct extent_buffer *leaf;
996 int ret;
997
998 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
999 key.offset = offset;
1000 key.type = 0;
1001
1002 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1003 if (ret < 0) {
1004 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001005 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001006 goto fail;
1007 }
1008 leaf = path->nodes[0];
1009 if (ret > 0) {
1010 struct btrfs_key found_key;
1011 ASSERT(path->slots[0]);
1012 path->slots[0]--;
1013 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1014 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1015 found_key.offset != offset) {
1016 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
1017 inode->i_size - 1,
1018 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001019 NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001020 btrfs_release_path(path);
1021 goto fail;
1022 }
1023 }
1024
1025 BTRFS_I(inode)->generation = trans->transid;
1026 header = btrfs_item_ptr(leaf, path->slots[0],
1027 struct btrfs_free_space_header);
1028 btrfs_set_free_space_entries(leaf, header, entries);
1029 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1030 btrfs_set_free_space_generation(leaf, header, trans->transid);
1031 btrfs_mark_buffer_dirty(leaf);
1032 btrfs_release_path(path);
1033
1034 return 0;
1035
1036fail:
1037 return -1;
1038}
1039
David Sterba6701bdb2019-03-20 13:49:09 +01001040static noinline_for_stack int write_pinned_extent_entries(
Miao Xie5349d6c2014-06-19 10:42:49 +08001041 struct btrfs_block_group_cache *block_group,
Chris Mason4c6d1d82015-04-06 13:17:20 -07001042 struct btrfs_io_ctl *io_ctl,
Miao Xie5349d6c2014-06-19 10:42:49 +08001043 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -07001044{
1045 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -07001046 struct extent_io_tree *unpin = NULL;
1047 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001048
Miao Xie5349d6c2014-06-19 10:42:49 +08001049 if (!block_group)
1050 return 0;
1051
Josef Bacika67509c2011-10-05 15:18:58 -04001052 /*
1053 * We want to add any pinned extents to our free space cache
1054 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001055 *
Li Zefandb804f22012-01-10 16:41:01 +08001056 * We shouldn't have switched the pinned extents yet so this is the
1057 * right one
1058 */
David Sterba6701bdb2019-03-20 13:49:09 +01001059 unpin = block_group->fs_info->pinned_extents;
Li Zefandb804f22012-01-10 16:41:01 +08001060
Miao Xie5349d6c2014-06-19 10:42:49 +08001061 start = block_group->key.objectid;
Li Zefandb804f22012-01-10 16:41:01 +08001062
Miao Xie5349d6c2014-06-19 10:42:49 +08001063 while (start < block_group->key.objectid + block_group->key.offset) {
Li Zefandb804f22012-01-10 16:41:01 +08001064 ret = find_first_extent_bit(unpin, start,
1065 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001066 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001067 if (ret)
1068 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001069
Josef Bacika67509c2011-10-05 15:18:58 -04001070 /* This pinned extent is out of our range */
Li Zefandb804f22012-01-10 16:41:01 +08001071 if (extent_start >= block_group->key.objectid +
Josef Bacika67509c2011-10-05 15:18:58 -04001072 block_group->key.offset)
Miao Xie5349d6c2014-06-19 10:42:49 +08001073 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001074
Li Zefandb804f22012-01-10 16:41:01 +08001075 extent_start = max(extent_start, start);
1076 extent_end = min(block_group->key.objectid +
1077 block_group->key.offset, extent_end + 1);
1078 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001079
Chris Masond4452bc2014-05-19 20:47:56 -07001080 *entries += 1;
1081 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001082 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001083 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001084
Li Zefandb804f22012-01-10 16:41:01 +08001085 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001086 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001087
Miao Xie5349d6c2014-06-19 10:42:49 +08001088 return 0;
1089}
1090
1091static noinline_for_stack int
Chris Mason4c6d1d82015-04-06 13:17:20 -07001092write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
Miao Xie5349d6c2014-06-19 10:42:49 +08001093{
Geliang Tang7ae16812015-12-18 22:17:00 +08001094 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001095 int ret;
1096
Josef Bacik0cb59c92010-07-02 12:14:14 -04001097 /* Write out the bitmaps */
Geliang Tang7ae16812015-12-18 22:17:00 +08001098 list_for_each_entry_safe(entry, next, bitmap_list, list) {
Chris Masond4452bc2014-05-19 20:47:56 -07001099 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001100 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001101 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001102 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001103 }
1104
Miao Xie5349d6c2014-06-19 10:42:49 +08001105 return 0;
1106}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001107
Miao Xie5349d6c2014-06-19 10:42:49 +08001108static int flush_dirty_cache(struct inode *inode)
1109{
1110 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001111
Josef Bacik0ef8b722013-10-25 16:13:35 -04001112 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001113 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001114 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001115 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001116
Miao Xie5349d6c2014-06-19 10:42:49 +08001117 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001118}
1119
1120static void noinline_for_stack
Chris Masona3bdccc2015-04-24 11:00:00 -07001121cleanup_bitmap_list(struct list_head *bitmap_list)
Chris Masond4452bc2014-05-19 20:47:56 -07001122{
Geliang Tang7ae16812015-12-18 22:17:00 +08001123 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001124
Geliang Tang7ae16812015-12-18 22:17:00 +08001125 list_for_each_entry_safe(entry, next, bitmap_list, list)
Chris Masond4452bc2014-05-19 20:47:56 -07001126 list_del_init(&entry->list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001127}
1128
1129static void noinline_for_stack
1130cleanup_write_cache_enospc(struct inode *inode,
1131 struct btrfs_io_ctl *io_ctl,
David Sterba7bf1a152017-02-10 20:23:00 +01001132 struct extent_state **cached_state)
Chris Masona3bdccc2015-04-24 11:00:00 -07001133{
Chris Masond4452bc2014-05-19 20:47:56 -07001134 io_ctl_drop_pages(io_ctl);
1135 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001136 i_size_read(inode) - 1, cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001137}
1138
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001139static int __btrfs_wait_cache_io(struct btrfs_root *root,
1140 struct btrfs_trans_handle *trans,
1141 struct btrfs_block_group_cache *block_group,
1142 struct btrfs_io_ctl *io_ctl,
1143 struct btrfs_path *path, u64 offset)
Chris Masonc9dc4c62015-04-04 17:14:42 -07001144{
1145 int ret;
1146 struct inode *inode = io_ctl->inode;
1147
Chris Mason1bbc6212015-04-06 12:46:08 -07001148 if (!inode)
1149 return 0;
1150
Chris Masonc9dc4c62015-04-04 17:14:42 -07001151 /* Flush the dirty pages in the cache file. */
1152 ret = flush_dirty_cache(inode);
1153 if (ret)
1154 goto out;
1155
1156 /* Update the cache item to tell everyone this cache file is valid. */
1157 ret = update_cache_item(trans, root, inode, path, offset,
1158 io_ctl->entries, io_ctl->bitmaps);
1159out:
1160 io_ctl_free(io_ctl);
1161 if (ret) {
1162 invalidate_inode_pages2(inode->i_mapping);
1163 BTRFS_I(inode)->generation = 0;
1164 if (block_group) {
1165#ifdef DEBUG
David Sterba3ffbd682018-06-29 10:56:42 +02001166 btrfs_err(root->fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001167 "failed to write free space cache for block group %llu",
1168 block_group->key.objectid);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001169#endif
1170 }
1171 }
1172 btrfs_update_inode(trans, root, inode);
1173
1174 if (block_group) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001175 /* the dirty list is protected by the dirty_bgs_lock */
1176 spin_lock(&trans->transaction->dirty_bgs_lock);
1177
1178 /* the disk_cache_state is protected by the block group lock */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001179 spin_lock(&block_group->lock);
1180
1181 /*
1182 * only mark this as written if we didn't get put back on
Chris Mason1bbc6212015-04-06 12:46:08 -07001183 * the dirty list while waiting for IO. Otherwise our
1184 * cache state won't be right, and we won't get written again
Chris Masonc9dc4c62015-04-04 17:14:42 -07001185 */
1186 if (!ret && list_empty(&block_group->dirty_list))
1187 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1188 else if (ret)
1189 block_group->disk_cache_state = BTRFS_DC_ERROR;
1190
1191 spin_unlock(&block_group->lock);
Chris Mason1bbc6212015-04-06 12:46:08 -07001192 spin_unlock(&trans->transaction->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001193 io_ctl->inode = NULL;
1194 iput(inode);
1195 }
1196
1197 return ret;
1198
1199}
1200
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001201static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1202 struct btrfs_trans_handle *trans,
1203 struct btrfs_io_ctl *io_ctl,
1204 struct btrfs_path *path)
1205{
1206 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1207}
1208
1209int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
1210 struct btrfs_block_group_cache *block_group,
1211 struct btrfs_path *path)
1212{
1213 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1214 block_group, &block_group->io_ctl,
1215 path, block_group->key.objectid);
1216}
1217
Chris Masond4452bc2014-05-19 20:47:56 -07001218/**
1219 * __btrfs_write_out_cache - write out cached info to an inode
1220 * @root - the root the inode belongs to
1221 * @ctl - the free space cache we are going to write out
1222 * @block_group - the block_group for this cache if it belongs to a block_group
1223 * @trans - the trans handle
Chris Masond4452bc2014-05-19 20:47:56 -07001224 *
1225 * This function writes out a free space cache struct to disk for quick recovery
Geliang Tang8cd1e732015-10-04 17:05:32 +08001226 * on mount. This will return 0 if it was successful in writing the cache out,
Omar Sandovalb8605452015-02-24 02:47:06 -08001227 * or an errno if it was not.
Chris Masond4452bc2014-05-19 20:47:56 -07001228 */
1229static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1230 struct btrfs_free_space_ctl *ctl,
1231 struct btrfs_block_group_cache *block_group,
Chris Masonc9dc4c62015-04-04 17:14:42 -07001232 struct btrfs_io_ctl *io_ctl,
David Sterba0e8d9312017-02-10 20:26:24 +01001233 struct btrfs_trans_handle *trans)
Chris Masond4452bc2014-05-19 20:47:56 -07001234{
1235 struct extent_state *cached_state = NULL;
Miao Xie5349d6c2014-06-19 10:42:49 +08001236 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001237 int entries = 0;
1238 int bitmaps = 0;
1239 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001240 int must_iput = 0;
Chris Masond4452bc2014-05-19 20:47:56 -07001241
1242 if (!i_size_read(inode))
Omar Sandovalb8605452015-02-24 02:47:06 -08001243 return -EIO;
Chris Masond4452bc2014-05-19 20:47:56 -07001244
Chris Masonc9dc4c62015-04-04 17:14:42 -07001245 WARN_ON(io_ctl->pages);
Jeff Mahoneyf15376d2016-06-22 18:56:18 -04001246 ret = io_ctl_init(io_ctl, inode, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001247 if (ret)
Omar Sandovalb8605452015-02-24 02:47:06 -08001248 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001249
Miao Xiee570fd22014-06-19 10:42:50 +08001250 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1251 down_write(&block_group->data_rwsem);
1252 spin_lock(&block_group->lock);
1253 if (block_group->delalloc_bytes) {
1254 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1255 spin_unlock(&block_group->lock);
1256 up_write(&block_group->data_rwsem);
1257 BTRFS_I(inode)->generation = 0;
1258 ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001259 must_iput = 1;
Miao Xiee570fd22014-06-19 10:42:50 +08001260 goto out;
1261 }
1262 spin_unlock(&block_group->lock);
1263 }
1264
Chris Masond4452bc2014-05-19 20:47:56 -07001265 /* Lock all pages first so we can lock the extent safely. */
Omar Sandovalb8605452015-02-24 02:47:06 -08001266 ret = io_ctl_prepare_pages(io_ctl, inode, 0);
1267 if (ret)
Josef Bacikb77000e2017-11-15 16:20:52 -05001268 goto out_unlock;
Chris Masond4452bc2014-05-19 20:47:56 -07001269
1270 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
David Sterbaff13db42015-12-03 14:30:40 +01001271 &cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001272
Chris Masonc9dc4c62015-04-04 17:14:42 -07001273 io_ctl_set_generation(io_ctl, trans->transid);
Chris Masond4452bc2014-05-19 20:47:56 -07001274
Filipe Manana55507ce2014-12-01 17:04:09 +00001275 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001276 /* Write out the extent entries in the free space cache */
Chris Mason1bbc6212015-04-06 12:46:08 -07001277 spin_lock(&ctl->tree_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001278 ret = write_cache_extent_entries(io_ctl, ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001279 block_group, &entries, &bitmaps,
1280 &bitmap_list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001281 if (ret)
1282 goto out_nospc_locked;
Chris Masond4452bc2014-05-19 20:47:56 -07001283
Miao Xie5349d6c2014-06-19 10:42:49 +08001284 /*
1285 * Some spaces that are freed in the current transaction are pinned,
1286 * they will be added into free space cache after the transaction is
1287 * committed, we shouldn't lose them.
Chris Mason1bbc6212015-04-06 12:46:08 -07001288 *
1289 * If this changes while we are working we'll get added back to
1290 * the dirty list and redo it. No locking needed
Miao Xie5349d6c2014-06-19 10:42:49 +08001291 */
David Sterba6701bdb2019-03-20 13:49:09 +01001292 ret = write_pinned_extent_entries(block_group, io_ctl, &entries);
Chris Masona3bdccc2015-04-24 11:00:00 -07001293 if (ret)
1294 goto out_nospc_locked;
Miao Xie5349d6c2014-06-19 10:42:49 +08001295
Filipe Manana55507ce2014-12-01 17:04:09 +00001296 /*
1297 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1298 * locked while doing it because a concurrent trim can be manipulating
1299 * or freeing the bitmap.
1300 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001301 ret = write_bitmap_entries(io_ctl, &bitmap_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07001302 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00001303 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001304 if (ret)
1305 goto out_nospc;
1306
1307 /* Zero out the rest of the pages just to make sure */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001308 io_ctl_zero_remaining_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001309
1310 /* Everything is written out, now we dirty the pages in the file. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001311 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1312 i_size_read(inode), &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001313 if (ret)
1314 goto out_nospc;
1315
Miao Xiee570fd22014-06-19 10:42:50 +08001316 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1317 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001318 /*
1319 * Release the pages and unlock the extent, we will flush
1320 * them out later
1321 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001322 io_ctl_drop_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001323
1324 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001325 i_size_read(inode) - 1, &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001326
Chris Masonc9dc4c62015-04-04 17:14:42 -07001327 /*
1328 * at this point the pages are under IO and we're happy,
1329 * The caller is responsible for waiting on them and updating the
1330 * the cache and the inode
1331 */
1332 io_ctl->entries = entries;
1333 io_ctl->bitmaps = bitmaps;
1334
1335 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001336 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001337 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001338
Chris Masonc9dc4c62015-04-04 17:14:42 -07001339 return 0;
1340
Josef Bacik2f356122011-06-10 15:31:13 -04001341out:
Chris Masonc9dc4c62015-04-04 17:14:42 -07001342 io_ctl->inode = NULL;
1343 io_ctl_free(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001344 if (ret) {
Josef Bacika67509c2011-10-05 15:18:58 -04001345 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001346 BTRFS_I(inode)->generation = 0;
1347 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001348 btrfs_update_inode(trans, root, inode);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001349 if (must_iput)
1350 iput(inode);
Miao Xie5349d6c2014-06-19 10:42:49 +08001351 return ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001352
Chris Masona3bdccc2015-04-24 11:00:00 -07001353out_nospc_locked:
1354 cleanup_bitmap_list(&bitmap_list);
1355 spin_unlock(&ctl->tree_lock);
1356 mutex_unlock(&ctl->cache_writeout_mutex);
1357
Josef Bacika67509c2011-10-05 15:18:58 -04001358out_nospc:
David Sterba7bf1a152017-02-10 20:23:00 +01001359 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
Miao Xiee570fd22014-06-19 10:42:50 +08001360
Josef Bacikb77000e2017-11-15 16:20:52 -05001361out_unlock:
Miao Xiee570fd22014-06-19 10:42:50 +08001362 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1363 up_write(&block_group->data_rwsem);
1364
Josef Bacika67509c2011-10-05 15:18:58 -04001365 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001366}
1367
David Sterbafe041532019-03-20 13:51:56 +01001368int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
Li Zefan0414efa2011-04-20 10:20:14 +08001369 struct btrfs_block_group_cache *block_group,
1370 struct btrfs_path *path)
1371{
David Sterbafe041532019-03-20 13:51:56 +01001372 struct btrfs_fs_info *fs_info = trans->fs_info;
Li Zefan0414efa2011-04-20 10:20:14 +08001373 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1374 struct inode *inode;
1375 int ret = 0;
1376
Li Zefan0414efa2011-04-20 10:20:14 +08001377 spin_lock(&block_group->lock);
1378 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1379 spin_unlock(&block_group->lock);
1380 return 0;
1381 }
1382 spin_unlock(&block_group->lock);
1383
David Sterba7949f332019-03-20 13:40:19 +01001384 inode = lookup_free_space_inode(block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +08001385 if (IS_ERR(inode))
1386 return 0;
1387
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001388 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1389 block_group, &block_group->io_ctl, trans);
Josef Bacikc09544e2011-08-30 10:19:10 -04001390 if (ret) {
Josef Bacikc09544e2011-08-30 10:19:10 -04001391#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001392 btrfs_err(fs_info,
1393 "failed to write free space cache for block group %llu",
1394 block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001395#endif
Chris Masonc9dc4c62015-04-04 17:14:42 -07001396 spin_lock(&block_group->lock);
1397 block_group->disk_cache_state = BTRFS_DC_ERROR;
1398 spin_unlock(&block_group->lock);
1399
1400 block_group->io_ctl.inode = NULL;
1401 iput(inode);
Li Zefan0414efa2011-04-20 10:20:14 +08001402 }
1403
Chris Masonc9dc4c62015-04-04 17:14:42 -07001404 /*
1405 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1406 * to wait for IO and put the inode
1407 */
1408
Josef Bacik0cb59c92010-07-02 12:14:14 -04001409 return ret;
1410}
1411
Li Zefan34d52cb2011-03-29 13:46:06 +08001412static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001413 u64 offset)
1414{
Josef Bacikb12d6862013-08-26 17:14:08 -04001415 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001416 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001417 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001418}
1419
Li Zefan34d52cb2011-03-29 13:46:06 +08001420static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001421{
Li Zefan34d52cb2011-03-29 13:46:06 +08001422 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001423}
1424
Li Zefan34d52cb2011-03-29 13:46:06 +08001425static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001426 u64 offset)
1427{
1428 u64 bitmap_start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001429 u64 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001430
Li Zefan34d52cb2011-03-29 13:46:06 +08001431 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1432 bitmap_start = offset - ctl->start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001433 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001434 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001435 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001436
1437 return bitmap_start;
1438}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001439
1440static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001441 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001442{
1443 struct rb_node **p = &root->rb_node;
1444 struct rb_node *parent = NULL;
1445 struct btrfs_free_space *info;
1446
1447 while (*p) {
1448 parent = *p;
1449 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1450
Josef Bacik96303082009-07-13 21:29:25 -04001451 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001452 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001453 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001454 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001455 } else {
1456 /*
1457 * we could have a bitmap entry and an extent entry
1458 * share the same offset. If this is the case, we want
1459 * the extent entry to always be found first if we do a
1460 * linear search through the tree, since we want to have
1461 * the quickest allocation time, and allocating from an
1462 * extent is faster than allocating from a bitmap. So
1463 * if we're inserting a bitmap and we find an entry at
1464 * this offset, we want to go right, or after this entry
1465 * logically. If we are inserting an extent and we've
1466 * found a bitmap, we want to go left, or before
1467 * logically.
1468 */
1469 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001470 if (info->bitmap) {
1471 WARN_ON_ONCE(1);
1472 return -EEXIST;
1473 }
Josef Bacik96303082009-07-13 21:29:25 -04001474 p = &(*p)->rb_right;
1475 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001476 if (!info->bitmap) {
1477 WARN_ON_ONCE(1);
1478 return -EEXIST;
1479 }
Josef Bacik96303082009-07-13 21:29:25 -04001480 p = &(*p)->rb_left;
1481 }
1482 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001483 }
1484
1485 rb_link_node(node, parent, p);
1486 rb_insert_color(node, root);
1487
1488 return 0;
1489}
1490
1491/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001492 * searches the tree for the given offset.
1493 *
Josef Bacik96303082009-07-13 21:29:25 -04001494 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1495 * want a section that has at least bytes size and comes at or after the given
1496 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001497 */
Josef Bacik96303082009-07-13 21:29:25 -04001498static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001499tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001500 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001501{
Li Zefan34d52cb2011-03-29 13:46:06 +08001502 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001503 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001504
Josef Bacik96303082009-07-13 21:29:25 -04001505 /* find entry that is closest to the 'offset' */
1506 while (1) {
1507 if (!n) {
1508 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001509 break;
1510 }
Josef Bacik96303082009-07-13 21:29:25 -04001511
1512 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1513 prev = entry;
1514
1515 if (offset < entry->offset)
1516 n = n->rb_left;
1517 else if (offset > entry->offset)
1518 n = n->rb_right;
1519 else
1520 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001521 }
1522
Josef Bacik96303082009-07-13 21:29:25 -04001523 if (bitmap_only) {
1524 if (!entry)
1525 return NULL;
1526 if (entry->bitmap)
1527 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001528
Josef Bacik96303082009-07-13 21:29:25 -04001529 /*
1530 * bitmap entry and extent entry may share same offset,
1531 * in that case, bitmap entry comes after extent entry.
1532 */
1533 n = rb_next(n);
1534 if (!n)
1535 return NULL;
1536 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1537 if (entry->offset != offset)
1538 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001539
Josef Bacik96303082009-07-13 21:29:25 -04001540 WARN_ON(!entry->bitmap);
1541 return entry;
1542 } else if (entry) {
1543 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001544 /*
Josef Bacik96303082009-07-13 21:29:25 -04001545 * if previous extent entry covers the offset,
1546 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001547 */
Miao Xiede6c4112012-10-18 08:18:01 +00001548 n = rb_prev(&entry->offset_index);
1549 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001550 prev = rb_entry(n, struct btrfs_free_space,
1551 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001552 if (!prev->bitmap &&
1553 prev->offset + prev->bytes > offset)
1554 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001555 }
Josef Bacik96303082009-07-13 21:29:25 -04001556 }
1557 return entry;
1558 }
1559
1560 if (!prev)
1561 return NULL;
1562
1563 /* find last entry before the 'offset' */
1564 entry = prev;
1565 if (entry->offset > offset) {
1566 n = rb_prev(&entry->offset_index);
1567 if (n) {
1568 entry = rb_entry(n, struct btrfs_free_space,
1569 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001570 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001571 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001572 if (fuzzy)
1573 return entry;
1574 else
1575 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001576 }
1577 }
1578
Josef Bacik96303082009-07-13 21:29:25 -04001579 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001580 n = rb_prev(&entry->offset_index);
1581 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001582 prev = rb_entry(n, struct btrfs_free_space,
1583 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001584 if (!prev->bitmap &&
1585 prev->offset + prev->bytes > offset)
1586 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001587 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001588 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001589 return entry;
1590 } else if (entry->offset + entry->bytes > offset)
1591 return entry;
1592
1593 if (!fuzzy)
1594 return NULL;
1595
1596 while (1) {
1597 if (entry->bitmap) {
1598 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001599 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001600 break;
1601 } else {
1602 if (entry->offset + entry->bytes > offset)
1603 break;
1604 }
1605
1606 n = rb_next(&entry->offset_index);
1607 if (!n)
1608 return NULL;
1609 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1610 }
1611 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001612}
1613
Li Zefanf333adb2010-11-09 14:57:39 +08001614static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001615__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001616 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001617{
Li Zefan34d52cb2011-03-29 13:46:06 +08001618 rb_erase(&info->offset_index, &ctl->free_space_offset);
1619 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001620}
1621
Li Zefan34d52cb2011-03-29 13:46:06 +08001622static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001623 struct btrfs_free_space *info)
1624{
Li Zefan34d52cb2011-03-29 13:46:06 +08001625 __unlink_free_space(ctl, info);
1626 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001627}
1628
Li Zefan34d52cb2011-03-29 13:46:06 +08001629static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001630 struct btrfs_free_space *info)
1631{
1632 int ret = 0;
1633
Josef Bacikb12d6862013-08-26 17:14:08 -04001634 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001635 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001636 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001637 if (ret)
1638 return ret;
1639
Li Zefan34d52cb2011-03-29 13:46:06 +08001640 ctl->free_space += info->bytes;
1641 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001642 return ret;
1643}
1644
Li Zefan34d52cb2011-03-29 13:46:06 +08001645static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001646{
Li Zefan34d52cb2011-03-29 13:46:06 +08001647 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001648 u64 max_bytes;
1649 u64 bitmap_bytes;
1650 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001651 u64 size = block_group->key.offset;
Feifei Xu0ef64472016-06-01 19:18:24 +08001652 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1653 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
Li Zefan34d52cb2011-03-29 13:46:06 +08001654
Feifei Xu0ef64472016-06-01 19:18:24 +08001655 max_bitmaps = max_t(u64, max_bitmaps, 1);
Josef Bacikdde57402013-02-12 14:07:51 -05001656
Josef Bacikb12d6862013-08-26 17:14:08 -04001657 ASSERT(ctl->total_bitmaps <= max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001658
1659 /*
1660 * The goal is to keep the total amount of memory used per 1gb of space
1661 * at or below 32k, so we need to adjust how much memory we allow to be
1662 * used by extent based free space tracking
1663 */
Byongho Leeee221842015-12-15 01:42:10 +09001664 if (size < SZ_1G)
Li Zefan8eb2d822010-11-09 14:48:01 +08001665 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1666 else
Byongho Leeee221842015-12-15 01:42:10 +09001667 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
Josef Bacik96303082009-07-13 21:29:25 -04001668
Josef Bacik25891f72009-09-11 16:11:20 -04001669 /*
1670 * we want to account for 1 more bitmap than what we have so we can make
1671 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1672 * we add more bitmaps.
1673 */
Feifei Xub9ef22d2016-06-01 19:18:25 +08001674 bitmap_bytes = (ctl->total_bitmaps + 1) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001675
Josef Bacik25891f72009-09-11 16:11:20 -04001676 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001677 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001678 return;
Josef Bacik96303082009-07-13 21:29:25 -04001679 }
Josef Bacik25891f72009-09-11 16:11:20 -04001680
1681 /*
David Sterbaf8c269d2015-01-16 17:21:12 +01001682 * we want the extent entry threshold to always be at most 1/2 the max
Josef Bacik25891f72009-09-11 16:11:20 -04001683 * bytes we can have, or whatever is less than that.
1684 */
1685 extent_bytes = max_bytes - bitmap_bytes;
David Sterbaf8c269d2015-01-16 17:21:12 +01001686 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
Josef Bacik25891f72009-09-11 16:11:20 -04001687
Li Zefan34d52cb2011-03-29 13:46:06 +08001688 ctl->extents_thresh =
David Sterbaf8c269d2015-01-16 17:21:12 +01001689 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
Josef Bacik96303082009-07-13 21:29:25 -04001690}
1691
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001692static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1693 struct btrfs_free_space *info,
1694 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001695{
Li Zefanf38b6e72011-03-14 13:40:51 +08001696 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001697
Li Zefan34d52cb2011-03-29 13:46:06 +08001698 start = offset_to_bit(info->offset, ctl->unit, offset);
1699 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001700 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001701
Li Zefanf38b6e72011-03-14 13:40:51 +08001702 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001703
1704 info->bytes -= bytes;
Josef Bacik553cceb2018-09-28 07:18:00 -04001705 if (info->max_extent_size > ctl->unit)
1706 info->max_extent_size = 0;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001707}
1708
1709static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1710 struct btrfs_free_space *info, u64 offset,
1711 u64 bytes)
1712{
1713 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001714 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001715}
1716
Li Zefan34d52cb2011-03-29 13:46:06 +08001717static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001718 struct btrfs_free_space *info, u64 offset,
1719 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001720{
Li Zefanf38b6e72011-03-14 13:40:51 +08001721 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001722
Li Zefan34d52cb2011-03-29 13:46:06 +08001723 start = offset_to_bit(info->offset, ctl->unit, offset);
1724 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001725 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001726
Li Zefanf38b6e72011-03-14 13:40:51 +08001727 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001728
1729 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001730 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001731}
1732
Miao Xiea4820392013-09-09 13:19:42 +08001733/*
1734 * If we can not find suitable extent, we will use bytes to record
1735 * the size of the max extent.
1736 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001737static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001738 struct btrfs_free_space *bitmap_info, u64 *offset,
Josef Bacik0584f712015-10-02 16:12:23 -04001739 u64 *bytes, bool for_alloc)
Josef Bacik96303082009-07-13 21:29:25 -04001740{
1741 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001742 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001743 unsigned long bits, i;
1744 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001745 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001746
Josef Bacikcef40482015-10-02 16:09:42 -04001747 /*
1748 * Skip searching the bitmap if we don't have a contiguous section that
1749 * is large enough for this allocation.
1750 */
Josef Bacik0584f712015-10-02 16:12:23 -04001751 if (for_alloc &&
1752 bitmap_info->max_extent_size &&
Josef Bacikcef40482015-10-02 16:09:42 -04001753 bitmap_info->max_extent_size < *bytes) {
1754 *bytes = bitmap_info->max_extent_size;
1755 return -1;
1756 }
1757
Li Zefan34d52cb2011-03-29 13:46:06 +08001758 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001759 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001760 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001761
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001762 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik0584f712015-10-02 16:12:23 -04001763 if (for_alloc && bits == 1) {
1764 found_bits = 1;
1765 break;
1766 }
Josef Bacik96303082009-07-13 21:29:25 -04001767 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1768 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001769 extent_bits = next_zero - i;
1770 if (extent_bits >= bits) {
1771 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001772 break;
Miao Xiea4820392013-09-09 13:19:42 +08001773 } else if (extent_bits > max_bits) {
1774 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001775 }
1776 i = next_zero;
1777 }
1778
1779 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001780 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1781 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001782 return 0;
1783 }
1784
Miao Xiea4820392013-09-09 13:19:42 +08001785 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacikcef40482015-10-02 16:09:42 -04001786 bitmap_info->max_extent_size = *bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001787 return -1;
1788}
1789
Josef Bacikad22cf62018-10-12 15:32:33 -04001790static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
1791{
1792 if (entry->bitmap)
1793 return entry->max_extent_size;
1794 return entry->bytes;
1795}
1796
Miao Xiea4820392013-09-09 13:19:42 +08001797/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001798static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001799find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Miao Xiea4820392013-09-09 13:19:42 +08001800 unsigned long align, u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04001801{
1802 struct btrfs_free_space *entry;
1803 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001804 u64 tmp;
1805 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001806 int ret;
1807
Li Zefan34d52cb2011-03-29 13:46:06 +08001808 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001809 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001810
Li Zefan34d52cb2011-03-29 13:46:06 +08001811 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001812 if (!entry)
Miao Xiea4820392013-09-09 13:19:42 +08001813 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001814
1815 for (node = &entry->offset_index; node; node = rb_next(node)) {
1816 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Miao Xiea4820392013-09-09 13:19:42 +08001817 if (entry->bytes < *bytes) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001818 *max_extent_size = max(get_max_extent_size(entry),
1819 *max_extent_size);
Josef Bacik96303082009-07-13 21:29:25 -04001820 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001821 }
Josef Bacik96303082009-07-13 21:29:25 -04001822
David Woodhouse53b381b2013-01-29 18:40:14 -05001823 /* make sure the space returned is big enough
1824 * to match our requested alignment
1825 */
1826 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001827 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01001828 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05001829 tmp = tmp * align + ctl->start;
1830 align_off = tmp - entry->offset;
1831 } else {
1832 align_off = 0;
1833 tmp = entry->offset;
1834 }
1835
Miao Xiea4820392013-09-09 13:19:42 +08001836 if (entry->bytes < *bytes + align_off) {
Josef Bacikad22cf62018-10-12 15:32:33 -04001837 *max_extent_size = max(get_max_extent_size(entry),
1838 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001839 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001840 }
David Woodhouse53b381b2013-01-29 18:40:14 -05001841
Josef Bacik96303082009-07-13 21:29:25 -04001842 if (entry->bitmap) {
Miao Xiea4820392013-09-09 13:19:42 +08001843 u64 size = *bytes;
1844
Josef Bacik0584f712015-10-02 16:12:23 -04001845 ret = search_bitmap(ctl, entry, &tmp, &size, true);
David Woodhouse53b381b2013-01-29 18:40:14 -05001846 if (!ret) {
1847 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08001848 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04001849 return entry;
Josef Bacikad22cf62018-10-12 15:32:33 -04001850 } else {
1851 *max_extent_size =
1852 max(get_max_extent_size(entry),
1853 *max_extent_size);
David Woodhouse53b381b2013-01-29 18:40:14 -05001854 }
Josef Bacik96303082009-07-13 21:29:25 -04001855 continue;
1856 }
1857
David Woodhouse53b381b2013-01-29 18:40:14 -05001858 *offset = tmp;
1859 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001860 return entry;
1861 }
Miao Xiea4820392013-09-09 13:19:42 +08001862out:
Josef Bacik96303082009-07-13 21:29:25 -04001863 return NULL;
1864}
1865
Li Zefan34d52cb2011-03-29 13:46:06 +08001866static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001867 struct btrfs_free_space *info, u64 offset)
1868{
Li Zefan34d52cb2011-03-29 13:46:06 +08001869 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001870 info->bytes = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02001871 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08001872 link_free_space(ctl, info);
1873 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001874
Li Zefan34d52cb2011-03-29 13:46:06 +08001875 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001876}
1877
Li Zefan34d52cb2011-03-29 13:46:06 +08001878static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001879 struct btrfs_free_space *bitmap_info)
1880{
Li Zefan34d52cb2011-03-29 13:46:06 +08001881 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001882 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001883 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001884 ctl->total_bitmaps--;
1885 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001886}
1887
Li Zefan34d52cb2011-03-29 13:46:06 +08001888static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001889 struct btrfs_free_space *bitmap_info,
1890 u64 *offset, u64 *bytes)
1891{
1892 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001893 u64 search_start, search_bytes;
1894 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001895
1896again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001897 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001898
Josef Bacik6606bb92009-07-31 11:03:58 -04001899 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04001900 * We need to search for bits in this bitmap. We could only cover some
1901 * of the extent in this bitmap thanks to how we add space, so we need
1902 * to search for as much as it as we can and clear that amount, and then
1903 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04001904 */
1905 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001906 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00001907 search_bytes = min(search_bytes, end - search_start + 1);
Josef Bacik0584f712015-10-02 16:12:23 -04001908 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1909 false);
Josef Bacikb50c6e22013-04-25 15:55:30 -04001910 if (ret < 0 || search_start != *offset)
1911 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04001912
Josef Bacikbdb7d302012-06-27 15:10:56 -04001913 /* We may have found more bits than what we need */
1914 search_bytes = min(search_bytes, *bytes);
1915
1916 /* Cannot clear past the end of the bitmap */
1917 search_bytes = min(search_bytes, end - search_start + 1);
1918
1919 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1920 *offset += search_bytes;
1921 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001922
1923 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001924 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001925 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001926 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001927
Josef Bacik6606bb92009-07-31 11:03:58 -04001928 /*
1929 * no entry after this bitmap, but we still have bytes to
1930 * remove, so something has gone wrong.
1931 */
1932 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001933 return -EINVAL;
1934
Josef Bacik6606bb92009-07-31 11:03:58 -04001935 bitmap_info = rb_entry(next, struct btrfs_free_space,
1936 offset_index);
1937
1938 /*
1939 * if the next entry isn't a bitmap we need to return to let the
1940 * extent stuff do its work.
1941 */
Josef Bacik96303082009-07-13 21:29:25 -04001942 if (!bitmap_info->bitmap)
1943 return -EAGAIN;
1944
Josef Bacik6606bb92009-07-31 11:03:58 -04001945 /*
1946 * Ok the next item is a bitmap, but it may not actually hold
1947 * the information for the rest of this free space stuff, so
1948 * look for it, and if we don't find it return so we can try
1949 * everything over again.
1950 */
1951 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001952 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08001953 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik0584f712015-10-02 16:12:23 -04001954 &search_bytes, false);
Josef Bacik6606bb92009-07-31 11:03:58 -04001955 if (ret < 0 || search_start != *offset)
1956 return -EAGAIN;
1957
Josef Bacik96303082009-07-13 21:29:25 -04001958 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001959 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001960 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001961
1962 return 0;
1963}
1964
Josef Bacik2cdc3422011-05-27 14:07:49 -04001965static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1966 struct btrfs_free_space *info, u64 offset,
1967 u64 bytes)
1968{
1969 u64 bytes_to_set = 0;
1970 u64 end;
1971
1972 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1973
1974 bytes_to_set = min(end - offset, bytes);
1975
1976 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1977
Josef Bacikcef40482015-10-02 16:09:42 -04001978 /*
1979 * We set some bytes, we have no idea what the max extent size is
1980 * anymore.
1981 */
1982 info->max_extent_size = 0;
1983
Josef Bacik2cdc3422011-05-27 14:07:49 -04001984 return bytes_to_set;
1985
1986}
1987
Li Zefan34d52cb2011-03-29 13:46:06 +08001988static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1989 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001990{
Li Zefan34d52cb2011-03-29 13:46:06 +08001991 struct btrfs_block_group_cache *block_group = ctl->private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001992 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -04001993 bool forced = false;
1994
1995#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001996 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -04001997 forced = true;
1998#endif
Josef Bacik96303082009-07-13 21:29:25 -04001999
2000 /*
2001 * If we are below the extents threshold then we can add this as an
2002 * extent, and don't have to deal with the bitmap
2003 */
Josef Bacikd0bd4562015-09-23 14:54:14 -04002004 if (!forced && ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04002005 /*
2006 * If this block group has some small extents we don't want to
2007 * use up all of our free slots in the cache with them, we want
Nicholas D Steeves01327612016-05-19 21:18:45 -04002008 * to reserve them to larger extents, however if we have plenty
Josef Bacik32cb0842011-03-18 16:16:21 -04002009 * of cache left then go ahead an dadd them, no sense in adding
2010 * the overhead of a bitmap if we don't have to.
2011 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002012 if (info->bytes <= fs_info->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002013 if (ctl->free_extents * 2 <= ctl->extents_thresh)
2014 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002015 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002016 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04002017 }
2018 }
Josef Bacik96303082009-07-13 21:29:25 -04002019
2020 /*
Josef Bacikdde57402013-02-12 14:07:51 -05002021 * The original block groups from mkfs can be really small, like 8
2022 * megabytes, so don't bother with a bitmap for those entries. However
2023 * some block groups can be smaller than what a bitmap would cover but
2024 * are still large enough that they could overflow the 32k memory limit,
2025 * so allow those block groups to still be allowed to have a bitmap
2026 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04002027 */
Josef Bacikdde57402013-02-12 14:07:51 -05002028 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08002029 return false;
2030
2031 return true;
2032}
2033
David Sterba20e55062015-11-19 11:42:28 +01002034static const struct btrfs_free_space_op free_space_op = {
Josef Bacik2cdc3422011-05-27 14:07:49 -04002035 .recalc_thresholds = recalculate_thresholds,
2036 .use_bitmap = use_bitmap,
2037};
2038
Li Zefan34d52cb2011-03-29 13:46:06 +08002039static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2040 struct btrfs_free_space *info)
2041{
2042 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002043 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08002044 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002045 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08002046 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002047
2048 bytes = info->bytes;
2049 offset = info->offset;
2050
Li Zefan34d52cb2011-03-29 13:46:06 +08002051 if (!ctl->op->use_bitmap(ctl, info))
2052 return 0;
2053
Josef Bacik2cdc3422011-05-27 14:07:49 -04002054 if (ctl->op == &free_space_op)
2055 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04002056again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04002057 /*
2058 * Since we link bitmaps right into the cluster we need to see if we
2059 * have a cluster here, and if so and it has our bitmap we need to add
2060 * the free space to that bitmap.
2061 */
2062 if (block_group && !list_empty(&block_group->cluster_list)) {
2063 struct btrfs_free_cluster *cluster;
2064 struct rb_node *node;
2065 struct btrfs_free_space *entry;
2066
2067 cluster = list_entry(block_group->cluster_list.next,
2068 struct btrfs_free_cluster,
2069 block_group_list);
2070 spin_lock(&cluster->lock);
2071 node = rb_first(&cluster->root);
2072 if (!node) {
2073 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002074 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002075 }
2076
2077 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2078 if (!entry->bitmap) {
2079 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002080 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002081 }
2082
2083 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2084 bytes_added = add_bytes_to_bitmap(ctl, entry,
2085 offset, bytes);
2086 bytes -= bytes_added;
2087 offset += bytes_added;
2088 }
2089 spin_unlock(&cluster->lock);
2090 if (!bytes) {
2091 ret = 1;
2092 goto out;
2093 }
2094 }
Chris Mason38e87882011-06-10 16:36:57 -04002095
2096no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08002097 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04002098 1, 0);
2099 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04002100 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04002101 goto new_bitmap;
2102 }
2103
Josef Bacik2cdc3422011-05-27 14:07:49 -04002104 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2105 bytes -= bytes_added;
2106 offset += bytes_added;
2107 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002108
2109 if (!bytes) {
2110 ret = 1;
2111 goto out;
2112 } else
2113 goto again;
2114
2115new_bitmap:
2116 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002117 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04002118 added = 1;
2119 info = NULL;
2120 goto again;
2121 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002122 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002123
2124 /* no pre-allocated info, allocate a new one */
2125 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05002126 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2127 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04002128 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002129 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002130 ret = -ENOMEM;
2131 goto out;
2132 }
2133 }
2134
2135 /* allocate the bitmap */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002136 info->bitmap = kzalloc(PAGE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08002137 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002138 if (!info->bitmap) {
2139 ret = -ENOMEM;
2140 goto out;
2141 }
2142 goto again;
2143 }
2144
2145out:
2146 if (info) {
zhong jiangf8b00e02018-08-13 14:06:08 +08002147 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002148 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002149 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002150
2151 return ret;
2152}
2153
Chris Mason945d8962011-05-22 12:33:42 -04002154static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08002155 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002156{
Li Zefan120d66e2010-11-09 14:56:50 +08002157 struct btrfs_free_space *left_info;
2158 struct btrfs_free_space *right_info;
2159 bool merged = false;
2160 u64 offset = info->offset;
2161 u64 bytes = info->bytes;
Josef Bacik6226cb02009-04-03 10:14:18 -04002162
Josef Bacik0f9dd462008-09-23 13:14:11 -04002163 /*
2164 * first we want to see if there is free space adjacent to the range we
2165 * are adding, if there is remove that struct and add a new one to
2166 * cover the entire range
2167 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002168 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002169 if (right_info && rb_prev(&right_info->offset_index))
2170 left_info = rb_entry(rb_prev(&right_info->offset_index),
2171 struct btrfs_free_space, offset_index);
2172 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002173 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002174
Josef Bacik96303082009-07-13 21:29:25 -04002175 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08002176 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002177 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002178 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002179 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002180 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002181 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002182 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002183 }
2184
Josef Bacik96303082009-07-13 21:29:25 -04002185 if (left_info && !left_info->bitmap &&
2186 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08002187 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002188 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002189 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002190 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002191 info->offset = left_info->offset;
2192 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002193 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002194 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002195 }
2196
Li Zefan120d66e2010-11-09 14:56:50 +08002197 return merged;
2198}
2199
Filipe Manana20005522014-08-29 13:35:13 +01002200static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2201 struct btrfs_free_space *info,
2202 bool update_stat)
2203{
2204 struct btrfs_free_space *bitmap;
2205 unsigned long i;
2206 unsigned long j;
2207 const u64 end = info->offset + info->bytes;
2208 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2209 u64 bytes;
2210
2211 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2212 if (!bitmap)
2213 return false;
2214
2215 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2216 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2217 if (j == i)
2218 return false;
2219 bytes = (j - i) * ctl->unit;
2220 info->bytes += bytes;
2221
2222 if (update_stat)
2223 bitmap_clear_bits(ctl, bitmap, end, bytes);
2224 else
2225 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2226
2227 if (!bitmap->bytes)
2228 free_bitmap(ctl, bitmap);
2229
2230 return true;
2231}
2232
2233static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2234 struct btrfs_free_space *info,
2235 bool update_stat)
2236{
2237 struct btrfs_free_space *bitmap;
2238 u64 bitmap_offset;
2239 unsigned long i;
2240 unsigned long j;
2241 unsigned long prev_j;
2242 u64 bytes;
2243
2244 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2245 /* If we're on a boundary, try the previous logical bitmap. */
2246 if (bitmap_offset == info->offset) {
2247 if (info->offset == 0)
2248 return false;
2249 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2250 }
2251
2252 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2253 if (!bitmap)
2254 return false;
2255
2256 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2257 j = 0;
2258 prev_j = (unsigned long)-1;
2259 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2260 if (j > i)
2261 break;
2262 prev_j = j;
2263 }
2264 if (prev_j == i)
2265 return false;
2266
2267 if (prev_j == (unsigned long)-1)
2268 bytes = (i + 1) * ctl->unit;
2269 else
2270 bytes = (i - prev_j) * ctl->unit;
2271
2272 info->offset -= bytes;
2273 info->bytes += bytes;
2274
2275 if (update_stat)
2276 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2277 else
2278 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2279
2280 if (!bitmap->bytes)
2281 free_bitmap(ctl, bitmap);
2282
2283 return true;
2284}
2285
2286/*
2287 * We prefer always to allocate from extent entries, both for clustered and
2288 * non-clustered allocation requests. So when attempting to add a new extent
2289 * entry, try to see if there's adjacent free space in bitmap entries, and if
2290 * there is, migrate that space from the bitmaps to the extent.
2291 * Like this we get better chances of satisfying space allocation requests
2292 * because we attempt to satisfy them based on a single cache entry, and never
2293 * on 2 or more entries - even if the entries represent a contiguous free space
2294 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2295 * ends).
2296 */
2297static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2298 struct btrfs_free_space *info,
2299 bool update_stat)
2300{
2301 /*
2302 * Only work with disconnected entries, as we can change their offset,
2303 * and must be extent entries.
2304 */
2305 ASSERT(!info->bitmap);
2306 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2307
2308 if (ctl->total_bitmaps > 0) {
2309 bool stole_end;
2310 bool stole_front = false;
2311
2312 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2313 if (ctl->total_bitmaps > 0)
2314 stole_front = steal_from_bitmap_to_front(ctl, info,
2315 update_stat);
2316
2317 if (stole_end || stole_front)
2318 try_merge_free_space(ctl, info, update_stat);
2319 }
2320}
2321
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002322int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2323 struct btrfs_free_space_ctl *ctl,
Li Zefan581bb052011-04-20 10:06:11 +08002324 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08002325{
2326 struct btrfs_free_space *info;
2327 int ret = 0;
2328
Josef Bacikdc89e982011-01-28 17:05:48 -05002329 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002330 if (!info)
2331 return -ENOMEM;
2332
2333 info->offset = offset;
2334 info->bytes = bytes;
Filipe Manana20005522014-08-29 13:35:13 +01002335 RB_CLEAR_NODE(&info->offset_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002336
Li Zefan34d52cb2011-03-29 13:46:06 +08002337 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002338
Li Zefan34d52cb2011-03-29 13:46:06 +08002339 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002340 goto link;
2341
2342 /*
2343 * There was no extent directly to the left or right of this new
2344 * extent then we know we're going to have to allocate a new extent, so
2345 * before we do that see if we need to drop this into a bitmap
2346 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002347 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002348 if (ret < 0) {
2349 goto out;
2350 } else if (ret) {
2351 ret = 0;
2352 goto out;
2353 }
2354link:
Filipe Manana20005522014-08-29 13:35:13 +01002355 /*
2356 * Only steal free space from adjacent bitmaps if we're sure we're not
2357 * going to add the new free space to existing bitmap entries - because
2358 * that would mean unnecessary work that would be reverted. Therefore
2359 * attempt to steal space from bitmaps if we're adding an extent entry.
2360 */
2361 steal_from_bitmap(ctl, info, true);
2362
Li Zefan34d52cb2011-03-29 13:46:06 +08002363 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002364 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002365 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002366out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002367 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002368
Josef Bacik0f9dd462008-09-23 13:14:11 -04002369 if (ret) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002370 btrfs_crit(fs_info, "unable to add free space :%d", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002371 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002372 }
2373
Josef Bacik0f9dd462008-09-23 13:14:11 -04002374 return ret;
2375}
2376
Josef Bacik6226cb02009-04-03 10:14:18 -04002377int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2378 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002379{
Li Zefan34d52cb2011-03-29 13:46:06 +08002380 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002381 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002382 int ret;
2383 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002384
Li Zefan34d52cb2011-03-29 13:46:06 +08002385 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002386
Josef Bacik96303082009-07-13 21:29:25 -04002387again:
Josef Bacikb0175112012-12-18 11:39:19 -05002388 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002389 if (!bytes)
2390 goto out_lock;
2391
Li Zefan34d52cb2011-03-29 13:46:06 +08002392 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002393 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002394 /*
2395 * oops didn't find an extent that matched the space we wanted
2396 * to remove, look for a bitmap instead
2397 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002398 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002399 1, 0);
2400 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002401 /*
2402 * If we found a partial bit of our free space in a
2403 * bitmap but then couldn't find the other part this may
2404 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002405 */
Josef Bacikb0175112012-12-18 11:39:19 -05002406 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002407 goto out_lock;
2408 }
Josef Bacik96303082009-07-13 21:29:25 -04002409 }
2410
Josef Bacikb0175112012-12-18 11:39:19 -05002411 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002412 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002413 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002414 if (offset == info->offset) {
2415 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002416
Josef Bacikbdb7d302012-06-27 15:10:56 -04002417 info->bytes -= to_free;
2418 info->offset += to_free;
2419 if (info->bytes) {
2420 ret = link_free_space(ctl, info);
2421 WARN_ON(ret);
2422 } else {
2423 kmem_cache_free(btrfs_free_space_cachep, info);
2424 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002425
Josef Bacikbdb7d302012-06-27 15:10:56 -04002426 offset += to_free;
2427 bytes -= to_free;
2428 goto again;
2429 } else {
2430 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002431
Josef Bacikbdb7d302012-06-27 15:10:56 -04002432 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002433 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002434 WARN_ON(ret);
2435 if (ret)
2436 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002437
Josef Bacikbdb7d302012-06-27 15:10:56 -04002438 /* Not enough bytes in this entry to satisfy us */
2439 if (old_end < offset + bytes) {
2440 bytes -= old_end - offset;
2441 offset = old_end;
2442 goto again;
2443 } else if (old_end == offset + bytes) {
2444 /* all done */
2445 goto out_lock;
2446 }
2447 spin_unlock(&ctl->tree_lock);
2448
2449 ret = btrfs_add_free_space(block_group, offset + bytes,
2450 old_end - (offset + bytes));
2451 WARN_ON(ret);
2452 goto out;
2453 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002454 }
Josef Bacik96303082009-07-13 21:29:25 -04002455
Li Zefan34d52cb2011-03-29 13:46:06 +08002456 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002457 if (ret == -EAGAIN) {
2458 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002459 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002460 }
Josef Bacik96303082009-07-13 21:29:25 -04002461out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08002462 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002463out:
Josef Bacik25179202008-10-29 14:49:05 -04002464 return ret;
2465}
2466
Josef Bacik0f9dd462008-09-23 13:14:11 -04002467void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2468 u64 bytes)
2469{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002470 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002471 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002472 struct btrfs_free_space *info;
2473 struct rb_node *n;
2474 int count = 0;
2475
Filipe Manana9084cb62018-10-22 10:43:06 +01002476 spin_lock(&ctl->tree_lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002477 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002478 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002479 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002480 count++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002481 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
Frank Holtonefe120a2013-12-20 11:37:06 -05002482 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002483 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002484 }
Filipe Manana9084cb62018-10-22 10:43:06 +01002485 spin_unlock(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002486 btrfs_info(fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002487 list_empty(&block_group->cluster_list) ? "no" : "yes");
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002488 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002489 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002490}
2491
Li Zefan34d52cb2011-03-29 13:46:06 +08002492void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002493{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002494 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002495 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002496
Li Zefan34d52cb2011-03-29 13:46:06 +08002497 spin_lock_init(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002498 ctl->unit = fs_info->sectorsize;
Li Zefan34d52cb2011-03-29 13:46:06 +08002499 ctl->start = block_group->key.objectid;
2500 ctl->private = block_group;
2501 ctl->op = &free_space_op;
Filipe Manana55507ce2014-12-01 17:04:09 +00002502 INIT_LIST_HEAD(&ctl->trimming_ranges);
2503 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002504
Li Zefan34d52cb2011-03-29 13:46:06 +08002505 /*
2506 * we only want to have 32k of ram per block group for keeping
2507 * track of free space, and if we pass 1/2 of that we want to
2508 * start converting things over to using bitmaps
2509 */
Byongho Leeee221842015-12-15 01:42:10 +09002510 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002511}
2512
Chris Masonfa9c0d792009-04-03 09:47:43 -04002513/*
2514 * for a given cluster, put all of its extents back into the free
2515 * space cache. If the block group passed doesn't match the block group
2516 * pointed to by the cluster, someone else raced in and freed the
2517 * cluster already. In that case, we just return without changing anything
2518 */
2519static int
2520__btrfs_return_cluster_to_free_space(
2521 struct btrfs_block_group_cache *block_group,
2522 struct btrfs_free_cluster *cluster)
2523{
Li Zefan34d52cb2011-03-29 13:46:06 +08002524 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002525 struct btrfs_free_space *entry;
2526 struct rb_node *node;
2527
2528 spin_lock(&cluster->lock);
2529 if (cluster->block_group != block_group)
2530 goto out;
2531
Josef Bacik96303082009-07-13 21:29:25 -04002532 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002533 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002534 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002535
Chris Masonfa9c0d792009-04-03 09:47:43 -04002536 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002537 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002538 bool bitmap;
2539
Chris Masonfa9c0d792009-04-03 09:47:43 -04002540 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2541 node = rb_next(&entry->offset_index);
2542 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002543 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002544
2545 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002546 if (!bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002547 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002548 steal_from_bitmap(ctl, entry, false);
2549 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002550 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002551 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002552 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002553 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002554
Chris Masonfa9c0d792009-04-03 09:47:43 -04002555out:
2556 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002557 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002558 return 0;
2559}
2560
Eric Sandeen48a3b632013-04-25 20:41:01 +00002561static void __btrfs_remove_free_space_cache_locked(
2562 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002563{
2564 struct btrfs_free_space *info;
2565 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002566
Li Zefan581bb052011-04-20 10:06:11 +08002567 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2568 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002569 if (!info->bitmap) {
2570 unlink_free_space(ctl, info);
2571 kmem_cache_free(btrfs_free_space_cachep, info);
2572 } else {
2573 free_bitmap(ctl, info);
2574 }
David Sterba351810c2015-01-08 15:20:54 +01002575
2576 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002577 }
Chris Mason09655372011-05-21 09:27:38 -04002578}
2579
2580void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2581{
2582 spin_lock(&ctl->tree_lock);
2583 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002584 spin_unlock(&ctl->tree_lock);
2585}
2586
Josef Bacik0f9dd462008-09-23 13:14:11 -04002587void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2588{
Li Zefan34d52cb2011-03-29 13:46:06 +08002589 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002590 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002591 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002592
Li Zefan34d52cb2011-03-29 13:46:06 +08002593 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002594 while ((head = block_group->cluster_list.next) !=
2595 &block_group->cluster_list) {
2596 cluster = list_entry(head, struct btrfs_free_cluster,
2597 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002598
2599 WARN_ON(cluster->block_group != block_group);
2600 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01002601
2602 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002603 }
Chris Mason09655372011-05-21 09:27:38 -04002604 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002605 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002606
Josef Bacik0f9dd462008-09-23 13:14:11 -04002607}
2608
Josef Bacik6226cb02009-04-03 10:14:18 -04002609u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002610 u64 offset, u64 bytes, u64 empty_size,
2611 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002612{
Li Zefan34d52cb2011-03-29 13:46:06 +08002613 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb02009-04-03 10:14:18 -04002614 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002615 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb02009-04-03 10:14:18 -04002616 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002617 u64 align_gap = 0;
2618 u64 align_gap_len = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002619
Li Zefan34d52cb2011-03-29 13:46:06 +08002620 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002621 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002622 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb02009-04-03 10:14:18 -04002623 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002624 goto out;
2625
2626 ret = offset;
2627 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002628 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002629 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002630 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002631 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002632 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002633 align_gap_len = offset - entry->offset;
2634 align_gap = entry->offset;
2635
2636 entry->offset = offset + bytes;
2637 WARN_ON(entry->bytes < bytes + align_gap_len);
2638
2639 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb02009-04-03 10:14:18 -04002640 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002641 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002642 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002643 link_free_space(ctl, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002644 }
Josef Bacik96303082009-07-13 21:29:25 -04002645out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002646 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002647
David Woodhouse53b381b2013-01-29 18:40:14 -05002648 if (align_gap_len)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002649 __btrfs_add_free_space(block_group->fs_info, ctl,
2650 align_gap, align_gap_len);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002651 return ret;
2652}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002653
2654/*
2655 * given a cluster, put all of its extents back into the free space
2656 * cache. If a block group is passed, this function will only free
2657 * a cluster that belongs to the passed block group.
2658 *
2659 * Otherwise, it'll get a reference on the block group pointed to by the
2660 * cluster and remove the cluster from it.
2661 */
2662int btrfs_return_cluster_to_free_space(
2663 struct btrfs_block_group_cache *block_group,
2664 struct btrfs_free_cluster *cluster)
2665{
Li Zefan34d52cb2011-03-29 13:46:06 +08002666 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002667 int ret;
2668
2669 /* first, get a safe pointer to the block group */
2670 spin_lock(&cluster->lock);
2671 if (!block_group) {
2672 block_group = cluster->block_group;
2673 if (!block_group) {
2674 spin_unlock(&cluster->lock);
2675 return 0;
2676 }
2677 } else if (cluster->block_group != block_group) {
2678 /* someone else has already freed it don't redo their work */
2679 spin_unlock(&cluster->lock);
2680 return 0;
2681 }
2682 atomic_inc(&block_group->count);
2683 spin_unlock(&cluster->lock);
2684
Li Zefan34d52cb2011-03-29 13:46:06 +08002685 ctl = block_group->free_space_ctl;
2686
Chris Masonfa9c0d792009-04-03 09:47:43 -04002687 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002688 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002689 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002690 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002691
2692 /* finally drop our ref */
2693 btrfs_put_block_group(block_group);
2694 return ret;
2695}
2696
Josef Bacik96303082009-07-13 21:29:25 -04002697static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2698 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002699 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002700 u64 bytes, u64 min_start,
2701 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002702{
Li Zefan34d52cb2011-03-29 13:46:06 +08002703 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002704 int err;
2705 u64 search_start = cluster->window_start;
2706 u64 search_bytes = bytes;
2707 u64 ret = 0;
2708
Josef Bacik96303082009-07-13 21:29:25 -04002709 search_start = min_start;
2710 search_bytes = bytes;
2711
Josef Bacik0584f712015-10-02 16:12:23 -04002712 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
Miao Xiea4820392013-09-09 13:19:42 +08002713 if (err) {
Josef Bacikad22cf62018-10-12 15:32:33 -04002714 *max_extent_size = max(get_max_extent_size(entry),
2715 *max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002716 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002717 }
Josef Bacik96303082009-07-13 21:29:25 -04002718
2719 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002720 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002721
2722 return ret;
2723}
2724
Chris Masonfa9c0d792009-04-03 09:47:43 -04002725/*
2726 * given a cluster, try to allocate 'bytes' from it, returns 0
2727 * if it couldn't find anything suitably large, or a logical disk offset
2728 * if things worked out
2729 */
2730u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2731 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002732 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002733{
Li Zefan34d52cb2011-03-29 13:46:06 +08002734 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002735 struct btrfs_free_space *entry = NULL;
2736 struct rb_node *node;
2737 u64 ret = 0;
2738
2739 spin_lock(&cluster->lock);
2740 if (bytes > cluster->max_size)
2741 goto out;
2742
2743 if (cluster->block_group != block_group)
2744 goto out;
2745
2746 node = rb_first(&cluster->root);
2747 if (!node)
2748 goto out;
2749
2750 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302751 while (1) {
Josef Bacikad22cf62018-10-12 15:32:33 -04002752 if (entry->bytes < bytes)
2753 *max_extent_size = max(get_max_extent_size(entry),
2754 *max_extent_size);
Miao Xiea4820392013-09-09 13:19:42 +08002755
Josef Bacik4e69b592011-03-21 10:11:24 -04002756 if (entry->bytes < bytes ||
2757 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002758 node = rb_next(&entry->offset_index);
2759 if (!node)
2760 break;
2761 entry = rb_entry(node, struct btrfs_free_space,
2762 offset_index);
2763 continue;
2764 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002765
Josef Bacik4e69b592011-03-21 10:11:24 -04002766 if (entry->bitmap) {
2767 ret = btrfs_alloc_from_bitmap(block_group,
2768 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002769 cluster->window_start,
2770 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002771 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002772 node = rb_next(&entry->offset_index);
2773 if (!node)
2774 break;
2775 entry = rb_entry(node, struct btrfs_free_space,
2776 offset_index);
2777 continue;
2778 }
Josef Bacik9b230622012-01-26 15:01:12 -05002779 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002780 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002781 ret = entry->offset;
2782
2783 entry->offset += bytes;
2784 entry->bytes -= bytes;
2785 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002786
Li Zefan5e71b5d2010-11-09 14:55:34 +08002787 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002788 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002789 break;
2790 }
2791out:
2792 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002793
Li Zefan5e71b5d2010-11-09 14:55:34 +08002794 if (!ret)
2795 return 0;
2796
Li Zefan34d52cb2011-03-29 13:46:06 +08002797 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002798
Li Zefan34d52cb2011-03-29 13:46:06 +08002799 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002800 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002801 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002802 if (entry->bitmap) {
2803 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002804 ctl->total_bitmaps--;
2805 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002806 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002807 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002808 }
2809
Li Zefan34d52cb2011-03-29 13:46:06 +08002810 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002811
Chris Masonfa9c0d792009-04-03 09:47:43 -04002812 return ret;
2813}
2814
Josef Bacik96303082009-07-13 21:29:25 -04002815static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2816 struct btrfs_free_space *entry,
2817 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002818 u64 offset, u64 bytes,
2819 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04002820{
Li Zefan34d52cb2011-03-29 13:46:06 +08002821 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002822 unsigned long next_zero;
2823 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002824 unsigned long want_bits;
2825 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002826 unsigned long found_bits;
Josef Bacikcef40482015-10-02 16:09:42 -04002827 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002828 unsigned long start = 0;
2829 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002830 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002831
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002832 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04002833 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002834 want_bits = bytes_to_bits(bytes, ctl->unit);
2835 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04002836
Josef Bacikcef40482015-10-02 16:09:42 -04002837 /*
2838 * Don't bother looking for a cluster in this bitmap if it's heavily
2839 * fragmented.
2840 */
2841 if (entry->max_extent_size &&
2842 entry->max_extent_size < cont1_bytes)
2843 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002844again:
2845 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06002846 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04002847 next_zero = find_next_zero_bit(entry->bitmap,
2848 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002849 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04002850 found_bits = next_zero - i;
Josef Bacikcef40482015-10-02 16:09:42 -04002851 if (found_bits > max_bits)
2852 max_bits = found_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002853 break;
2854 }
Josef Bacikcef40482015-10-02 16:09:42 -04002855 if (next_zero - i > max_bits)
2856 max_bits = next_zero - i;
Josef Bacik96303082009-07-13 21:29:25 -04002857 i = next_zero;
2858 }
2859
Josef Bacikcef40482015-10-02 16:09:42 -04002860 if (!found_bits) {
2861 entry->max_extent_size = (u64)max_bits * ctl->unit;
Josef Bacik4e69b592011-03-21 10:11:24 -04002862 return -ENOSPC;
Josef Bacikcef40482015-10-02 16:09:42 -04002863 }
Josef Bacik96303082009-07-13 21:29:25 -04002864
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002865 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04002866 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05002867 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002868 }
2869
2870 total_found += found_bits;
2871
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002872 if (cluster->max_size < found_bits * ctl->unit)
2873 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04002874
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002875 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2876 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04002877 goto again;
2878 }
2879
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002880 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002881 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002882 ret = tree_insert_offset(&cluster->root, entry->offset,
2883 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04002884 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04002885
Josef Bacik3f7de032011-11-10 08:29:20 -05002886 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002887 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04002888 return 0;
2889}
2890
Chris Masonfa9c0d792009-04-03 09:47:43 -04002891/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002892 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002893 * Try to find a cluster with at least bytes total bytes, at least one
2894 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04002895 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002896static noinline int
2897setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2898 struct btrfs_free_cluster *cluster,
2899 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002900 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002901{
Li Zefan34d52cb2011-03-29 13:46:06 +08002902 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002903 struct btrfs_free_space *first = NULL;
2904 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002905 struct btrfs_free_space *last;
2906 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04002907 u64 window_free;
2908 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002909 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002910
Li Zefan34d52cb2011-03-29 13:46:06 +08002911 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002912 if (!entry)
2913 return -ENOSPC;
2914
2915 /*
2916 * We don't want bitmaps, so just move along until we find a normal
2917 * extent entry.
2918 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002919 while (entry->bitmap || entry->bytes < min_bytes) {
2920 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04002921 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002922 node = rb_next(&entry->offset_index);
2923 if (!node)
2924 return -ENOSPC;
2925 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2926 }
2927
Josef Bacik4e69b592011-03-21 10:11:24 -04002928 window_free = entry->bytes;
2929 max_extent = entry->bytes;
2930 first = entry;
2931 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002932
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002933 for (node = rb_next(&entry->offset_index); node;
2934 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002935 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2936
Josef Bacik86d4a772011-05-25 13:03:16 -04002937 if (entry->bitmap) {
2938 if (list_empty(&entry->list))
2939 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002940 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002941 }
2942
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002943 if (entry->bytes < min_bytes)
2944 continue;
2945
2946 last = entry;
2947 window_free += entry->bytes;
2948 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04002949 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002950 }
2951
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002952 if (window_free < bytes || max_extent < cont1_bytes)
2953 return -ENOSPC;
2954
Josef Bacik4e69b592011-03-21 10:11:24 -04002955 cluster->window_start = first->offset;
2956
2957 node = &first->offset_index;
2958
2959 /*
2960 * now we've found our entries, pull them out of the free space
2961 * cache and put them into the cluster rbtree
2962 */
2963 do {
2964 int ret;
2965
2966 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2967 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002968 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002969 continue;
2970
Li Zefan34d52cb2011-03-29 13:46:06 +08002971 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002972 ret = tree_insert_offset(&cluster->root, entry->offset,
2973 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05002974 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04002975 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04002976 } while (node && entry != last);
2977
2978 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002979 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04002980 return 0;
2981}
2982
2983/*
2984 * This specifically looks for bitmaps that may work in the cluster, we assume
2985 * that we have already failed to find extents that will work.
2986 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002987static noinline int
2988setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2989 struct btrfs_free_cluster *cluster,
2990 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002991 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002992{
Li Zefan34d52cb2011-03-29 13:46:06 +08002993 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Mason1b9b9222015-12-15 07:15:32 -08002994 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002995 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05002996 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002997
Li Zefan34d52cb2011-03-29 13:46:06 +08002998 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002999 return -ENOSPC;
3000
Josef Bacik86d4a772011-05-25 13:03:16 -04003001 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05003002 * The bitmap that covers offset won't be in the list unless offset
3003 * is just its start offset.
3004 */
Chris Mason1b9b9222015-12-15 07:15:32 -08003005 if (!list_empty(bitmaps))
3006 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3007
3008 if (!entry || entry->offset != bitmap_offset) {
Li Zefan0f0fbf12011-11-20 07:33:38 -05003009 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3010 if (entry && list_empty(&entry->list))
3011 list_add(&entry->list, bitmaps);
3012 }
3013
Josef Bacik86d4a772011-05-25 13:03:16 -04003014 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05003015 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04003016 continue;
3017 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003018 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003019 if (!ret)
3020 return 0;
3021 }
3022
3023 /*
Li Zefan52621cb2011-11-20 07:33:38 -05003024 * The bitmaps list has all the bitmaps that record free space
3025 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04003026 */
Li Zefan52621cb2011-11-20 07:33:38 -05003027 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04003028}
3029
3030/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04003031 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003032 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04003033 * We might not find them all in one contiguous area.
3034 *
3035 * returns zero and sets up cluster if things worked out, otherwise
3036 * it returns -enospc
3037 */
David Sterba2ceeae22019-03-20 13:53:49 +01003038int btrfs_find_space_cluster(struct btrfs_block_group_cache *block_group,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003039 struct btrfs_free_cluster *cluster,
3040 u64 offset, u64 bytes, u64 empty_size)
3041{
David Sterba2ceeae22019-03-20 13:53:49 +01003042 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08003043 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04003044 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05003045 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003046 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003047 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003048 int ret;
3049
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003050 /*
3051 * Choose the minimum extent size we'll require for this
3052 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3053 * For metadata, allow allocates with smaller extents. For
3054 * data, keep it dense.
3055 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003056 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003057 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04003058 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003059 cont1_bytes = bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003060 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003061 } else {
3062 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003063 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003064 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003065
Li Zefan34d52cb2011-03-29 13:46:06 +08003066 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003067
3068 /*
3069 * If we know we don't have enough space to make a cluster don't even
3070 * bother doing all the work to try and find one.
3071 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003072 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003073 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003074 return -ENOSPC;
3075 }
3076
Chris Masonfa9c0d792009-04-03 09:47:43 -04003077 spin_lock(&cluster->lock);
3078
3079 /* someone already found a cluster, hooray */
3080 if (cluster->block_group) {
3081 ret = 0;
3082 goto out;
3083 }
Josef Bacik4e69b592011-03-21 10:11:24 -04003084
Josef Bacik3f7de032011-11-10 08:29:20 -05003085 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3086 min_bytes);
3087
Josef Bacik86d4a772011-05-25 13:03:16 -04003088 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003089 bytes + empty_size,
3090 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04003091 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04003092 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003093 offset, bytes + empty_size,
3094 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003095
3096 /* Clear our temporary list */
3097 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3098 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04003099
3100 if (!ret) {
3101 atomic_inc(&block_group->count);
3102 list_add_tail(&cluster->block_group_list,
3103 &block_group->cluster_list);
3104 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05003105 } else {
3106 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003107 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003108out:
3109 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08003110 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003111
3112 return ret;
3113}
3114
3115/*
3116 * simple code to zero out a cluster
3117 */
3118void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3119{
3120 spin_lock_init(&cluster->lock);
3121 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00003122 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003123 cluster->max_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003124 cluster->fragmented = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003125 INIT_LIST_HEAD(&cluster->block_group_list);
3126 cluster->block_group = NULL;
3127}
3128
Li Zefan7fe1e642011-12-29 14:47:27 +08003129static int do_trimming(struct btrfs_block_group_cache *block_group,
3130 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003131 u64 reserved_start, u64 reserved_bytes,
3132 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08003133{
3134 struct btrfs_space_info *space_info = block_group->space_info;
3135 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00003136 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003137 int ret;
3138 int update = 0;
3139 u64 trimmed = 0;
3140
3141 spin_lock(&space_info->lock);
3142 spin_lock(&block_group->lock);
3143 if (!block_group->ro) {
3144 block_group->reserved += reserved_bytes;
3145 space_info->bytes_reserved += reserved_bytes;
3146 update = 1;
3147 }
3148 spin_unlock(&block_group->lock);
3149 spin_unlock(&space_info->lock);
3150
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003151 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
Li Zefan7fe1e642011-12-29 14:47:27 +08003152 if (!ret)
3153 *total_trimmed += trimmed;
3154
Filipe Manana55507ce2014-12-01 17:04:09 +00003155 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003156 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
Filipe Manana55507ce2014-12-01 17:04:09 +00003157 list_del(&trim_entry->list);
3158 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003159
3160 if (update) {
3161 spin_lock(&space_info->lock);
3162 spin_lock(&block_group->lock);
3163 if (block_group->ro)
3164 space_info->bytes_readonly += reserved_bytes;
3165 block_group->reserved -= reserved_bytes;
3166 space_info->bytes_reserved -= reserved_bytes;
Li Zefan7fe1e642011-12-29 14:47:27 +08003167 spin_unlock(&block_group->lock);
Su Yue8f63a842018-11-28 11:21:12 +08003168 spin_unlock(&space_info->lock);
Li Zefan7fe1e642011-12-29 14:47:27 +08003169 }
3170
3171 return ret;
3172}
3173
3174static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
3175 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
Li Dongyangf7039b12011-03-24 10:24:28 +00003176{
Li Zefan34d52cb2011-03-29 13:46:06 +08003177 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003178 struct btrfs_free_space *entry;
3179 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00003180 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08003181 u64 extent_start;
3182 u64 extent_bytes;
3183 u64 bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003184
3185 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00003186 struct btrfs_trim_range trim_entry;
3187
3188 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08003189 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003190
Li Zefan34d52cb2011-03-29 13:46:06 +08003191 if (ctl->free_space < minlen) {
3192 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003193 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003194 break;
3195 }
3196
Li Zefan34d52cb2011-03-29 13:46:06 +08003197 entry = tree_search_offset(ctl, start, 0, 1);
Li Zefan7fe1e642011-12-29 14:47:27 +08003198 if (!entry) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003199 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003200 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003201 break;
3202 }
3203
Li Zefan7fe1e642011-12-29 14:47:27 +08003204 /* skip bitmaps */
3205 while (entry->bitmap) {
3206 node = rb_next(&entry->offset_index);
3207 if (!node) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003208 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003209 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003210 goto out;
Li Dongyangf7039b12011-03-24 10:24:28 +00003211 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003212 entry = rb_entry(node, struct btrfs_free_space,
3213 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003214 }
3215
Li Zefan7fe1e642011-12-29 14:47:27 +08003216 if (entry->offset >= end) {
3217 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003218 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003219 break;
3220 }
3221
3222 extent_start = entry->offset;
3223 extent_bytes = entry->bytes;
3224 start = max(start, extent_start);
3225 bytes = min(extent_start + extent_bytes, end) - start;
3226 if (bytes < minlen) {
3227 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003228 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003229 goto next;
3230 }
3231
3232 unlink_free_space(ctl, entry);
3233 kmem_cache_free(btrfs_free_space_cachep, entry);
3234
Li Zefan34d52cb2011-03-29 13:46:06 +08003235 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003236 trim_entry.start = extent_start;
3237 trim_entry.bytes = extent_bytes;
3238 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3239 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003240
Li Zefan7fe1e642011-12-29 14:47:27 +08003241 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003242 extent_start, extent_bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003243 if (ret)
3244 break;
3245next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003246 start += bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003247
3248 if (fatal_signal_pending(current)) {
3249 ret = -ERESTARTSYS;
3250 break;
3251 }
3252
3253 cond_resched();
3254 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003255out:
3256 return ret;
3257}
3258
3259static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3260 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3261{
3262 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3263 struct btrfs_free_space *entry;
3264 int ret = 0;
3265 int ret2;
3266 u64 bytes;
3267 u64 offset = offset_to_bitmap(ctl, start);
3268
3269 while (offset < end) {
3270 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003271 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003272
Filipe Manana55507ce2014-12-01 17:04:09 +00003273 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003274 spin_lock(&ctl->tree_lock);
3275
3276 if (ctl->free_space < minlen) {
3277 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003278 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003279 break;
3280 }
3281
3282 entry = tree_search_offset(ctl, offset, 1, 0);
3283 if (!entry) {
3284 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003285 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003286 next_bitmap = true;
3287 goto next;
3288 }
3289
3290 bytes = minlen;
Josef Bacik0584f712015-10-02 16:12:23 -04003291 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
Li Zefan7fe1e642011-12-29 14:47:27 +08003292 if (ret2 || start >= end) {
3293 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003294 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003295 next_bitmap = true;
3296 goto next;
3297 }
3298
3299 bytes = min(bytes, end - start);
3300 if (bytes < minlen) {
3301 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003302 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003303 goto next;
3304 }
3305
3306 bitmap_clear_bits(ctl, entry, start, bytes);
3307 if (entry->bytes == 0)
3308 free_bitmap(ctl, entry);
3309
3310 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003311 trim_entry.start = start;
3312 trim_entry.bytes = bytes;
3313 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3314 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003315
3316 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003317 start, bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003318 if (ret)
3319 break;
3320next:
3321 if (next_bitmap) {
3322 offset += BITS_PER_BITMAP * ctl->unit;
3323 } else {
3324 start += bytes;
3325 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3326 offset += BITS_PER_BITMAP * ctl->unit;
3327 }
3328
3329 if (fatal_signal_pending(current)) {
3330 ret = -ERESTARTSYS;
3331 break;
3332 }
3333
3334 cond_resched();
3335 }
3336
3337 return ret;
3338}
3339
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003340void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache)
Li Zefan7fe1e642011-12-29 14:47:27 +08003341{
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003342 atomic_inc(&cache->trimming);
3343}
Li Zefan7fe1e642011-12-29 14:47:27 +08003344
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003345void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
3346{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003347 struct btrfs_fs_info *fs_info = block_group->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003348 struct extent_map_tree *em_tree;
3349 struct extent_map *em;
3350 bool cleanup;
Li Zefan7fe1e642011-12-29 14:47:27 +08003351
Filipe Manana04216822014-11-27 21:14:15 +00003352 spin_lock(&block_group->lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003353 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3354 block_group->removed);
Filipe Manana04216822014-11-27 21:14:15 +00003355 spin_unlock(&block_group->lock);
3356
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003357 if (cleanup) {
David Sterba34441362016-10-04 19:34:27 +02003358 mutex_lock(&fs_info->chunk_mutex);
David Sterbac8bf1b62019-05-17 11:43:17 +02003359 em_tree = &fs_info->mapping_tree;
Filipe Manana04216822014-11-27 21:14:15 +00003360 write_lock(&em_tree->lock);
3361 em = lookup_extent_mapping(em_tree, block_group->key.objectid,
3362 1);
3363 BUG_ON(!em); /* logic error, can't happen */
3364 remove_extent_mapping(em_tree, em);
3365 write_unlock(&em_tree->lock);
David Sterba34441362016-10-04 19:34:27 +02003366 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana04216822014-11-27 21:14:15 +00003367
3368 /* once for us and once for the tree */
3369 free_extent_map(em);
3370 free_extent_map(em);
Filipe Manana946ddbe2014-12-01 17:04:40 +00003371
3372 /*
3373 * We've left one free space entry and other tasks trimming
3374 * this block group have left 1 entry each one. Free them.
3375 */
3376 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
Filipe Manana04216822014-11-27 21:14:15 +00003377 }
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003378}
Li Dongyangf7039b12011-03-24 10:24:28 +00003379
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003380int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
3381 u64 *trimmed, u64 start, u64 end, u64 minlen)
3382{
3383 int ret;
3384
3385 *trimmed = 0;
3386
3387 spin_lock(&block_group->lock);
3388 if (block_group->removed) {
3389 spin_unlock(&block_group->lock);
3390 return 0;
3391 }
3392 btrfs_get_block_group_trimming(block_group);
3393 spin_unlock(&block_group->lock);
3394
3395 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3396 if (ret)
3397 goto out;
3398
3399 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
3400out:
3401 btrfs_put_block_group_trimming(block_group);
Li Dongyangf7039b12011-03-24 10:24:28 +00003402 return ret;
3403}
Li Zefan581bb052011-04-20 10:06:11 +08003404
3405/*
3406 * Find the left-most item in the cache tree, and then return the
3407 * smallest inode number in the item.
3408 *
3409 * Note: the returned inode number may not be the smallest one in
3410 * the tree, if the left-most item is a bitmap.
3411 */
3412u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3413{
3414 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3415 struct btrfs_free_space *entry = NULL;
3416 u64 ino = 0;
3417
3418 spin_lock(&ctl->tree_lock);
3419
3420 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3421 goto out;
3422
3423 entry = rb_entry(rb_first(&ctl->free_space_offset),
3424 struct btrfs_free_space, offset_index);
3425
3426 if (!entry->bitmap) {
3427 ino = entry->offset;
3428
3429 unlink_free_space(ctl, entry);
3430 entry->offset++;
3431 entry->bytes--;
3432 if (!entry->bytes)
3433 kmem_cache_free(btrfs_free_space_cachep, entry);
3434 else
3435 link_free_space(ctl, entry);
3436 } else {
3437 u64 offset = 0;
3438 u64 count = 1;
3439 int ret;
3440
Josef Bacik0584f712015-10-02 16:12:23 -04003441 ret = search_bitmap(ctl, entry, &offset, &count, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003442 /* Logic error; Should be empty if it can't find anything */
Josef Bacikb12d6862013-08-26 17:14:08 -04003443 ASSERT(!ret);
Li Zefan581bb052011-04-20 10:06:11 +08003444
3445 ino = offset;
3446 bitmap_clear_bits(ctl, entry, offset, 1);
3447 if (entry->bytes == 0)
3448 free_bitmap(ctl, entry);
3449 }
3450out:
3451 spin_unlock(&ctl->tree_lock);
3452
3453 return ino;
3454}
Li Zefan82d59022011-04-20 10:33:24 +08003455
3456struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3457 struct btrfs_path *path)
3458{
3459 struct inode *inode = NULL;
3460
David Sterba57cdc8d2014-02-05 02:37:48 +01003461 spin_lock(&root->ino_cache_lock);
3462 if (root->ino_cache_inode)
3463 inode = igrab(root->ino_cache_inode);
3464 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003465 if (inode)
3466 return inode;
3467
3468 inode = __lookup_free_space_inode(root, path, 0);
3469 if (IS_ERR(inode))
3470 return inode;
3471
David Sterba57cdc8d2014-02-05 02:37:48 +01003472 spin_lock(&root->ino_cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02003473 if (!btrfs_fs_closing(root->fs_info))
David Sterba57cdc8d2014-02-05 02:37:48 +01003474 root->ino_cache_inode = igrab(inode);
3475 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003476
3477 return inode;
3478}
3479
3480int create_free_ino_inode(struct btrfs_root *root,
3481 struct btrfs_trans_handle *trans,
3482 struct btrfs_path *path)
3483{
3484 return __create_free_space_inode(root, trans, path,
3485 BTRFS_FREE_INO_OBJECTID, 0);
3486}
3487
3488int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3489{
3490 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3491 struct btrfs_path *path;
3492 struct inode *inode;
3493 int ret = 0;
3494 u64 root_gen = btrfs_root_generation(&root->root_item);
3495
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003496 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04003497 return 0;
3498
Li Zefan82d59022011-04-20 10:33:24 +08003499 /*
3500 * If we're unmounting then just return, since this does a search on the
3501 * normal root and not the commit root and we could deadlock.
3502 */
David Sterba7841cb22011-05-31 18:07:27 +02003503 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08003504 return 0;
3505
3506 path = btrfs_alloc_path();
3507 if (!path)
3508 return 0;
3509
3510 inode = lookup_free_ino_inode(root, path);
3511 if (IS_ERR(inode))
3512 goto out;
3513
3514 if (root_gen != BTRFS_I(inode)->generation)
3515 goto out_put;
3516
3517 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3518
3519 if (ret < 0)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003520 btrfs_err(fs_info,
3521 "failed to load free ino cache for root %llu",
3522 root->root_key.objectid);
Li Zefan82d59022011-04-20 10:33:24 +08003523out_put:
3524 iput(inode);
3525out:
3526 btrfs_free_path(path);
3527 return ret;
3528}
3529
3530int btrfs_write_out_ino_cache(struct btrfs_root *root,
3531 struct btrfs_trans_handle *trans,
Filipe David Borba Manana53645a92013-09-20 14:43:28 +01003532 struct btrfs_path *path,
3533 struct inode *inode)
Li Zefan82d59022011-04-20 10:33:24 +08003534{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003535 struct btrfs_fs_info *fs_info = root->fs_info;
Li Zefan82d59022011-04-20 10:33:24 +08003536 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
Li Zefan82d59022011-04-20 10:33:24 +08003537 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003538 struct btrfs_io_ctl io_ctl;
Filipe Mananae43699d2015-05-05 15:21:27 +01003539 bool release_metadata = true;
Li Zefan82d59022011-04-20 10:33:24 +08003540
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003541 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04003542 return 0;
3543
Chris Mason85db36c2015-04-23 08:02:49 -07003544 memset(&io_ctl, 0, sizeof(io_ctl));
David Sterba0e8d9312017-02-10 20:26:24 +01003545 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
Filipe Mananae43699d2015-05-05 15:21:27 +01003546 if (!ret) {
3547 /*
3548 * At this point writepages() didn't error out, so our metadata
3549 * reservation is released when the writeback finishes, at
3550 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3551 * with or without an error.
3552 */
3553 release_metadata = false;
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003554 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
Filipe Mananae43699d2015-05-05 15:21:27 +01003555 }
Chris Mason85db36c2015-04-23 08:02:49 -07003556
Josef Bacikc09544e2011-08-30 10:19:10 -04003557 if (ret) {
Filipe Mananae43699d2015-05-05 15:21:27 +01003558 if (release_metadata)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003559 btrfs_delalloc_release_metadata(BTRFS_I(inode),
Qu Wenruo43b18592017-12-12 15:34:32 +08003560 inode->i_size, true);
Josef Bacikc09544e2011-08-30 10:19:10 -04003561#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003562 btrfs_err(fs_info,
3563 "failed to write free ino cache for root %llu",
3564 root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04003565#endif
3566 }
Li Zefan82d59022011-04-20 10:33:24 +08003567
Li Zefan82d59022011-04-20 10:33:24 +08003568 return ret;
3569}
Josef Bacik74255aa2013-03-15 09:47:08 -04003570
3571#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd52013-08-14 15:05:12 -04003572/*
3573 * Use this if you need to make a bitmap or extent entry specifically, it
3574 * doesn't do any of the merging that add_free_space does, this acts a lot like
3575 * how the free space cache loading stuff works, so you can get really weird
3576 * configurations.
3577 */
3578int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3579 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04003580{
Josef Bacikdc11dd52013-08-14 15:05:12 -04003581 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3582 struct btrfs_free_space *info = NULL, *bitmap_info;
3583 void *map = NULL;
3584 u64 bytes_added;
3585 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04003586
Josef Bacikdc11dd52013-08-14 15:05:12 -04003587again:
3588 if (!info) {
3589 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3590 if (!info)
3591 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04003592 }
3593
Josef Bacikdc11dd52013-08-14 15:05:12 -04003594 if (!bitmap) {
3595 spin_lock(&ctl->tree_lock);
3596 info->offset = offset;
3597 info->bytes = bytes;
Josef Bacikcef40482015-10-02 16:09:42 -04003598 info->max_extent_size = 0;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003599 ret = link_free_space(ctl, info);
3600 spin_unlock(&ctl->tree_lock);
3601 if (ret)
3602 kmem_cache_free(btrfs_free_space_cachep, info);
3603 return ret;
3604 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003605
Josef Bacikdc11dd52013-08-14 15:05:12 -04003606 if (!map) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003607 map = kzalloc(PAGE_SIZE, GFP_NOFS);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003608 if (!map) {
3609 kmem_cache_free(btrfs_free_space_cachep, info);
3610 return -ENOMEM;
3611 }
3612 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003613
Josef Bacikdc11dd52013-08-14 15:05:12 -04003614 spin_lock(&ctl->tree_lock);
3615 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3616 1, 0);
3617 if (!bitmap_info) {
3618 info->bitmap = map;
3619 map = NULL;
3620 add_new_bitmap(ctl, info, offset);
3621 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01003622 info = NULL;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003623 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003624
Josef Bacikdc11dd52013-08-14 15:05:12 -04003625 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
Josef Bacikcef40482015-10-02 16:09:42 -04003626
Josef Bacikdc11dd52013-08-14 15:05:12 -04003627 bytes -= bytes_added;
3628 offset += bytes_added;
3629 spin_unlock(&ctl->tree_lock);
3630
3631 if (bytes)
3632 goto again;
3633
Filipe Manana20005522014-08-29 13:35:13 +01003634 if (info)
3635 kmem_cache_free(btrfs_free_space_cachep, info);
zhong jiangf8b00e02018-08-13 14:06:08 +08003636 kfree(map);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003637 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003638}
3639
3640/*
3641 * Checks to see if the given range is in the free space cache. This is really
3642 * just used to check the absence of space, so if there is free space in the
3643 * range at all we will return 1.
3644 */
Josef Bacikdc11dd52013-08-14 15:05:12 -04003645int test_check_exists(struct btrfs_block_group_cache *cache,
3646 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04003647{
3648 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3649 struct btrfs_free_space *info;
3650 int ret = 0;
3651
3652 spin_lock(&ctl->tree_lock);
3653 info = tree_search_offset(ctl, offset, 0, 0);
3654 if (!info) {
3655 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3656 1, 0);
3657 if (!info)
3658 goto out;
3659 }
3660
3661have_info:
3662 if (info->bitmap) {
3663 u64 bit_off, bit_bytes;
3664 struct rb_node *n;
3665 struct btrfs_free_space *tmp;
3666
3667 bit_off = offset;
3668 bit_bytes = ctl->unit;
Josef Bacik0584f712015-10-02 16:12:23 -04003669 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
Josef Bacik74255aa2013-03-15 09:47:08 -04003670 if (!ret) {
3671 if (bit_off == offset) {
3672 ret = 1;
3673 goto out;
3674 } else if (bit_off > offset &&
3675 offset + bytes > bit_off) {
3676 ret = 1;
3677 goto out;
3678 }
3679 }
3680
3681 n = rb_prev(&info->offset_index);
3682 while (n) {
3683 tmp = rb_entry(n, struct btrfs_free_space,
3684 offset_index);
3685 if (tmp->offset + tmp->bytes < offset)
3686 break;
3687 if (offset + bytes < tmp->offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08003688 n = rb_prev(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04003689 continue;
3690 }
3691 info = tmp;
3692 goto have_info;
3693 }
3694
3695 n = rb_next(&info->offset_index);
3696 while (n) {
3697 tmp = rb_entry(n, struct btrfs_free_space,
3698 offset_index);
3699 if (offset + bytes < tmp->offset)
3700 break;
3701 if (tmp->offset + tmp->bytes < offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08003702 n = rb_next(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04003703 continue;
3704 }
3705 info = tmp;
3706 goto have_info;
3707 }
3708
Filipe Manana20005522014-08-29 13:35:13 +01003709 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003710 goto out;
3711 }
3712
3713 if (info->offset == offset) {
3714 ret = 1;
3715 goto out;
3716 }
3717
3718 if (offset > info->offset && offset < info->offset + info->bytes)
3719 ret = 1;
3720out:
3721 spin_unlock(&ctl->tree_lock);
3722 return ret;
3723}
Josef Bacikdc11dd52013-08-14 15:05:12 -04003724#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */