blob: 354d55f22d99e4a773cbb8260dcb09edf11ac1ba [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 Bacik0f9dd462008-09-23 13:14:11 -040013#include "ctree.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040014#include "free-space-cache.h"
15#include "transaction.h"
Josef Bacik0af3d002010-06-21 14:48:16 -040016#include "disk-io.h"
Josef Bacik43be2142011-04-01 14:55:00 +000017#include "extent_io.h"
Li Zefan581bb052011-04-20 10:06:11 +080018#include "inode-map.h"
Filipe Manana04216822014-11-27 21:14:15 +000019#include "volumes.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040020
Feifei Xu0ef64472016-06-01 19:18:24 +080021#define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
Byongho Leeee221842015-12-15 01:42:10 +090022#define MAX_CACHE_BYTES_PER_GIG SZ_32K
Josef Bacik96303082009-07-13 21:29:25 -040023
Filipe Manana55507ce2014-12-01 17:04:09 +000024struct btrfs_trim_range {
25 u64 start;
26 u64 bytes;
27 struct list_head list;
28};
29
Li Zefan34d52cb2011-03-29 13:46:06 +080030static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040031 struct btrfs_free_space *info);
Josef Bacikcd023e72012-05-14 10:06:40 -040032static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
33 struct btrfs_free_space *info);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040034static int btrfs_wait_cache_io_root(struct btrfs_root *root,
35 struct btrfs_trans_handle *trans,
36 struct btrfs_io_ctl *io_ctl,
37 struct btrfs_path *path);
Josef Bacik0cb59c92010-07-02 12:14:14 -040038
Li Zefan0414efa2011-04-20 10:20:14 +080039static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
40 struct btrfs_path *path,
41 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040042{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040043 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0af3d002010-06-21 14:48:16 -040044 struct btrfs_key key;
45 struct btrfs_key location;
46 struct btrfs_disk_key disk_key;
47 struct btrfs_free_space_header *header;
48 struct extent_buffer *leaf;
49 struct inode *inode = NULL;
50 int ret;
51
Josef Bacik0af3d002010-06-21 14:48:16 -040052 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080053 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040054 key.type = 0;
55
56 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
57 if (ret < 0)
58 return ERR_PTR(ret);
59 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020060 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040061 return ERR_PTR(-ENOENT);
62 }
63
64 leaf = path->nodes[0];
65 header = btrfs_item_ptr(leaf, path->slots[0],
66 struct btrfs_free_space_header);
67 btrfs_free_space_key(leaf, header, &disk_key);
68 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020069 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040070
Jeff Mahoney0b246af2016-06-22 18:54:23 -040071 inode = btrfs_iget(fs_info->sb, &location, root, NULL);
Josef Bacik0af3d002010-06-21 14:48:16 -040072 if (IS_ERR(inode))
73 return inode;
74 if (is_bad_inode(inode)) {
75 iput(inode);
76 return ERR_PTR(-ENOENT);
77 }
78
Al Viro528c0322012-04-13 11:03:55 -040079 mapping_set_gfp_mask(inode->i_mapping,
Michal Hockoc62d2552015-11-06 16:28:49 -080080 mapping_gfp_constraint(inode->i_mapping,
81 ~(__GFP_FS | __GFP_HIGHMEM)));
Miao Xieadae52b2011-03-31 09:43:23 +000082
Li Zefan0414efa2011-04-20 10:20:14 +080083 return inode;
84}
85
Jeff Mahoney77ab86b2017-02-15 16:28:30 -050086struct inode *lookup_free_space_inode(struct btrfs_fs_info *fs_info,
Li Zefan0414efa2011-04-20 10:20:14 +080087 struct btrfs_block_group_cache
88 *block_group, struct btrfs_path *path)
89{
90 struct inode *inode = NULL;
Josef Bacik5b0e95b2011-10-06 08:58:24 -040091 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +080092
93 spin_lock(&block_group->lock);
94 if (block_group->inode)
95 inode = igrab(block_group->inode);
96 spin_unlock(&block_group->lock);
97 if (inode)
98 return inode;
99
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500100 inode = __lookup_free_space_inode(fs_info->tree_root, path,
Li Zefan0414efa2011-04-20 10:20:14 +0800101 block_group->key.objectid);
102 if (IS_ERR(inode))
103 return inode;
104
Josef Bacik0af3d002010-06-21 14:48:16 -0400105 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400106 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400107 btrfs_info(fs_info, "Old style space inode found, converting.");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400108 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
109 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400110 block_group->disk_cache_state = BTRFS_DC_CLEAR;
111 }
112
Josef Bacik300e4f82011-08-29 14:06:00 -0400113 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400114 block_group->inode = igrab(inode);
115 block_group->iref = 1;
116 }
117 spin_unlock(&block_group->lock);
118
119 return inode;
120}
121
Eric Sandeen48a3b632013-04-25 20:41:01 +0000122static int __create_free_space_inode(struct btrfs_root *root,
123 struct btrfs_trans_handle *trans,
124 struct btrfs_path *path,
125 u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400126{
127 struct btrfs_key key;
128 struct btrfs_disk_key disk_key;
129 struct btrfs_free_space_header *header;
130 struct btrfs_inode_item *inode_item;
131 struct extent_buffer *leaf;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400132 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
Josef Bacik0af3d002010-06-21 14:48:16 -0400133 int ret;
134
Li Zefan0414efa2011-04-20 10:20:14 +0800135 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400136 if (ret)
137 return ret;
138
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400139 /* We inline crc's for the free disk space cache */
140 if (ino != BTRFS_FREE_INO_OBJECTID)
141 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
142
Josef Bacik0af3d002010-06-21 14:48:16 -0400143 leaf = path->nodes[0];
144 inode_item = btrfs_item_ptr(leaf, path->slots[0],
145 struct btrfs_inode_item);
146 btrfs_item_key(leaf, &disk_key, path->slots[0]);
David Sterbab159fa22016-11-08 18:09:03 +0100147 memzero_extent_buffer(leaf, (unsigned long)inode_item,
Josef Bacik0af3d002010-06-21 14:48:16 -0400148 sizeof(*inode_item));
149 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
150 btrfs_set_inode_size(leaf, inode_item, 0);
151 btrfs_set_inode_nbytes(leaf, inode_item, 0);
152 btrfs_set_inode_uid(leaf, inode_item, 0);
153 btrfs_set_inode_gid(leaf, inode_item, 0);
154 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400155 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400156 btrfs_set_inode_nlink(leaf, inode_item, 1);
157 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800158 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400159 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200160 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400161
162 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800163 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400164 key.type = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400165 ret = btrfs_insert_empty_item(trans, root, path, &key,
166 sizeof(struct btrfs_free_space_header));
167 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200168 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400169 return ret;
170 }
Chris Masonc9dc4c62015-04-04 17:14:42 -0700171
Josef Bacik0af3d002010-06-21 14:48:16 -0400172 leaf = path->nodes[0];
173 header = btrfs_item_ptr(leaf, path->slots[0],
174 struct btrfs_free_space_header);
David Sterbab159fa22016-11-08 18:09:03 +0100175 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
Josef Bacik0af3d002010-06-21 14:48:16 -0400176 btrfs_set_free_space_key(leaf, header, &disk_key);
177 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200178 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400179
180 return 0;
181}
182
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500183int create_free_space_inode(struct btrfs_fs_info *fs_info,
Li Zefan0414efa2011-04-20 10:20:14 +0800184 struct btrfs_trans_handle *trans,
185 struct btrfs_block_group_cache *block_group,
186 struct btrfs_path *path)
187{
188 int ret;
189 u64 ino;
190
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500191 ret = btrfs_find_free_objectid(fs_info->tree_root, &ino);
Li Zefan0414efa2011-04-20 10:20:14 +0800192 if (ret < 0)
193 return ret;
194
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500195 return __create_free_space_inode(fs_info->tree_root, trans, path, ino,
Li Zefan0414efa2011-04-20 10:20:14 +0800196 block_group->key.objectid);
197}
198
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400199int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
Miao Xie7b61cd92013-05-13 13:55:09 +0000200 struct btrfs_block_rsv *rsv)
Josef Bacik0af3d002010-06-21 14:48:16 -0400201{
Josef Bacikc8174312011-11-02 09:29:35 -0400202 u64 needed_bytes;
Miao Xie7b61cd92013-05-13 13:55:09 +0000203 int ret;
Josef Bacikc8174312011-11-02 09:29:35 -0400204
205 /* 1 for slack space, 1 for updating the inode */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400206 needed_bytes = btrfs_calc_trunc_metadata_size(fs_info, 1) +
207 btrfs_calc_trans_metadata_size(fs_info, 1);
Josef Bacikc8174312011-11-02 09:29:35 -0400208
Miao Xie7b61cd92013-05-13 13:55:09 +0000209 spin_lock(&rsv->lock);
210 if (rsv->reserved < needed_bytes)
211 ret = -ENOSPC;
212 else
213 ret = 0;
214 spin_unlock(&rsv->lock);
Wei Yongjun4b286cd2013-05-21 02:39:21 +0000215 return ret;
Miao Xie7b61cd92013-05-13 13:55:09 +0000216}
217
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500218int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
Chris Mason1bbc6212015-04-06 12:46:08 -0700219 struct btrfs_block_group_cache *block_group,
Miao Xie7b61cd92013-05-13 13:55:09 +0000220 struct inode *inode)
221{
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500222 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b61cd92013-05-13 13:55:09 +0000223 int ret = 0;
Filipe Manana35c76642015-04-30 17:47:05 +0100224 bool locked = false;
Chris Mason1bbc6212015-04-06 12:46:08 -0700225
Chris Mason1bbc6212015-04-06 12:46:08 -0700226 if (block_group) {
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500227 struct btrfs_path *path = btrfs_alloc_path();
228
229 if (!path) {
230 ret = -ENOMEM;
231 goto fail;
232 }
Filipe Manana35c76642015-04-30 17:47:05 +0100233 locked = true;
Chris Mason1bbc6212015-04-06 12:46:08 -0700234 mutex_lock(&trans->transaction->cache_write_mutex);
235 if (!list_empty(&block_group->io_list)) {
236 list_del_init(&block_group->io_list);
237
Jeff Mahoneyafdb5712016-09-09 12:09:35 -0400238 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700239 btrfs_put_block_group(block_group);
240 }
241
242 /*
243 * now that we've truncated the cache away, its no longer
244 * setup or written
245 */
246 spin_lock(&block_group->lock);
247 block_group->disk_cache_state = BTRFS_DC_CLEAR;
248 spin_unlock(&block_group->lock);
Jeff Mahoney21e75ff2017-02-15 16:28:32 -0500249 btrfs_free_path(path);
Chris Mason1bbc6212015-04-06 12:46:08 -0700250 }
Josef Bacik0af3d002010-06-21 14:48:16 -0400251
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200252 btrfs_i_size_write(BTRFS_I(inode), 0);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700253 truncate_pagecache(inode, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400254
255 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -0700256 * We skip the throttling logic for free space cache inodes, so we don't
257 * need to check for -EAGAIN.
Josef Bacik0af3d002010-06-21 14:48:16 -0400258 */
259 ret = btrfs_truncate_inode_items(trans, root, inode,
260 0, BTRFS_EXTENT_DATA_KEY);
Filipe Manana35c76642015-04-30 17:47:05 +0100261 if (ret)
262 goto fail;
Josef Bacik0af3d002010-06-21 14:48:16 -0400263
Li Zefan82d59022011-04-20 10:33:24 +0800264 ret = btrfs_update_inode(trans, root, inode);
Chris Mason1bbc6212015-04-06 12:46:08 -0700265
Chris Mason1bbc6212015-04-06 12:46:08 -0700266fail:
Filipe Manana35c76642015-04-30 17:47:05 +0100267 if (locked)
268 mutex_unlock(&trans->transaction->cache_write_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100269 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400270 btrfs_abort_transaction(trans, ret);
Josef Bacikc8174312011-11-02 09:29:35 -0400271
Li Zefan82d59022011-04-20 10:33:24 +0800272 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400273}
274
David Sterba1d480532017-01-23 17:28:19 +0100275static void readahead_cache(struct inode *inode)
Josef Bacik9d66e232010-08-25 16:54:15 -0400276{
277 struct file_ra_state *ra;
278 unsigned long last_index;
279
280 ra = kzalloc(sizeof(*ra), GFP_NOFS);
281 if (!ra)
David Sterba1d480532017-01-23 17:28:19 +0100282 return;
Josef Bacik9d66e232010-08-25 16:54:15 -0400283
284 file_ra_state_init(ra, inode->i_mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300285 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
Josef Bacik9d66e232010-08-25 16:54:15 -0400286
287 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
288
289 kfree(ra);
Josef Bacik9d66e232010-08-25 16:54:15 -0400290}
291
Chris Mason4c6d1d82015-04-06 13:17:20 -0700292static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400293 int write)
Josef Bacika67509c2011-10-05 15:18:58 -0400294{
Miao Xie5349d6c2014-06-19 10:42:49 +0800295 int num_pages;
296 int check_crcs = 0;
297
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300298 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Miao Xie5349d6c2014-06-19 10:42:49 +0800299
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200300 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FREE_INO_OBJECTID)
Miao Xie5349d6c2014-06-19 10:42:49 +0800301 check_crcs = 1;
302
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400303 /* Make sure we can fit our crcs and generation into the first page */
Miao Xie5349d6c2014-06-19 10:42:49 +0800304 if (write && check_crcs &&
Zhihui Zhang8f6c72a2018-07-02 20:00:54 -0400305 (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
Miao Xie5349d6c2014-06-19 10:42:49 +0800306 return -ENOSPC;
307
Chris Mason4c6d1d82015-04-06 13:17:20 -0700308 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
Miao Xie5349d6c2014-06-19 10:42:49 +0800309
David Sterba31e818f2015-02-20 18:00:26 +0100310 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400311 if (!io_ctl->pages)
312 return -ENOMEM;
Miao Xie5349d6c2014-06-19 10:42:49 +0800313
314 io_ctl->num_pages = num_pages;
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400315 io_ctl->fs_info = btrfs_sb(inode->i_sb);
Miao Xie5349d6c2014-06-19 10:42:49 +0800316 io_ctl->check_crcs = check_crcs;
Chris Masonc9dc4c62015-04-04 17:14:42 -0700317 io_ctl->inode = inode;
Miao Xie5349d6c2014-06-19 10:42:49 +0800318
Josef Bacika67509c2011-10-05 15:18:58 -0400319 return 0;
320}
Masami Hiramatsu663faf92018-01-13 02:55:33 +0900321ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
Josef Bacika67509c2011-10-05 15:18:58 -0400322
Chris Mason4c6d1d82015-04-06 13:17:20 -0700323static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400324{
325 kfree(io_ctl->pages);
Chris Masonc9dc4c62015-04-04 17:14:42 -0700326 io_ctl->pages = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400327}
328
Chris Mason4c6d1d82015-04-06 13:17:20 -0700329static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400330{
331 if (io_ctl->cur) {
Josef Bacika67509c2011-10-05 15:18:58 -0400332 io_ctl->cur = NULL;
333 io_ctl->orig = NULL;
334 }
335}
336
Chris Mason4c6d1d82015-04-06 13:17:20 -0700337static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
Josef Bacika67509c2011-10-05 15:18:58 -0400338{
Josef Bacikb12d6862013-08-26 17:14:08 -0400339 ASSERT(io_ctl->index < io_ctl->num_pages);
Josef Bacika67509c2011-10-05 15:18:58 -0400340 io_ctl->page = io_ctl->pages[io_ctl->index++];
Chris Mason2b108262015-04-06 07:48:20 -0700341 io_ctl->cur = page_address(io_ctl->page);
Josef Bacika67509c2011-10-05 15:18:58 -0400342 io_ctl->orig = io_ctl->cur;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300343 io_ctl->size = PAGE_SIZE;
Josef Bacika67509c2011-10-05 15:18:58 -0400344 if (clear)
David Sterba619a9742017-03-29 20:48:44 +0200345 clear_page(io_ctl->cur);
Josef Bacika67509c2011-10-05 15:18:58 -0400346}
347
Chris Mason4c6d1d82015-04-06 13:17:20 -0700348static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400349{
350 int i;
351
352 io_ctl_unmap_page(io_ctl);
353
354 for (i = 0; i < io_ctl->num_pages; i++) {
Li Zefana1ee5a42012-01-09 14:27:42 +0800355 if (io_ctl->pages[i]) {
356 ClearPageChecked(io_ctl->pages[i]);
357 unlock_page(io_ctl->pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300358 put_page(io_ctl->pages[i]);
Li Zefana1ee5a42012-01-09 14:27:42 +0800359 }
Josef Bacika67509c2011-10-05 15:18:58 -0400360 }
361}
362
Chris Mason4c6d1d82015-04-06 13:17:20 -0700363static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inode,
Josef Bacika67509c2011-10-05 15:18:58 -0400364 int uptodate)
365{
366 struct page *page;
367 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
368 int i;
369
370 for (i = 0; i < io_ctl->num_pages; i++) {
371 page = find_or_create_page(inode->i_mapping, i, mask);
372 if (!page) {
373 io_ctl_drop_pages(io_ctl);
374 return -ENOMEM;
375 }
376 io_ctl->pages[i] = page;
377 if (uptodate && !PageUptodate(page)) {
378 btrfs_readpage(NULL, page);
379 lock_page(page);
380 if (!PageUptodate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500381 btrfs_err(BTRFS_I(inode)->root->fs_info,
382 "error reading free space cache");
Josef Bacika67509c2011-10-05 15:18:58 -0400383 io_ctl_drop_pages(io_ctl);
384 return -EIO;
385 }
386 }
387 }
388
Josef Bacikf7d61dc2011-11-15 09:31:24 -0500389 for (i = 0; i < io_ctl->num_pages; i++) {
390 clear_page_dirty_for_io(io_ctl->pages[i]);
391 set_page_extent_mapped(io_ctl->pages[i]);
392 }
393
Josef Bacika67509c2011-10-05 15:18:58 -0400394 return 0;
395}
396
Chris Mason4c6d1d82015-04-06 13:17:20 -0700397static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400398{
Al Viro528c0322012-04-13 11:03:55 -0400399 __le64 *val;
Josef Bacika67509c2011-10-05 15:18:58 -0400400
401 io_ctl_map_page(io_ctl, 1);
402
403 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400404 * Skip the csum areas. If we don't check crcs then we just have a
405 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400406 */
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400407 if (io_ctl->check_crcs) {
408 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
409 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
410 } else {
411 io_ctl->cur += sizeof(u64);
412 io_ctl->size -= sizeof(u64) * 2;
413 }
Josef Bacika67509c2011-10-05 15:18:58 -0400414
415 val = io_ctl->cur;
416 *val = cpu_to_le64(generation);
417 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400418}
419
Chris Mason4c6d1d82015-04-06 13:17:20 -0700420static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
Josef Bacika67509c2011-10-05 15:18:58 -0400421{
Al Viro528c0322012-04-13 11:03:55 -0400422 __le64 *gen;
Josef Bacika67509c2011-10-05 15:18:58 -0400423
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400424 /*
425 * Skip the crc area. If we don't check crcs then we just have a 64bit
426 * chunk at the front of the first page.
427 */
428 if (io_ctl->check_crcs) {
429 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
430 io_ctl->size -= sizeof(u64) +
431 (sizeof(u32) * io_ctl->num_pages);
432 } else {
433 io_ctl->cur += sizeof(u64);
434 io_ctl->size -= sizeof(u64) * 2;
435 }
Josef Bacika67509c2011-10-05 15:18:58 -0400436
Josef Bacika67509c2011-10-05 15:18:58 -0400437 gen = io_ctl->cur;
438 if (le64_to_cpu(*gen) != generation) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400439 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200440 "space cache generation (%llu) does not match inode (%llu)",
441 *gen, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400442 io_ctl_unmap_page(io_ctl);
443 return -EIO;
444 }
445 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400446 return 0;
447}
448
Chris Mason4c6d1d82015-04-06 13:17:20 -0700449static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400450{
451 u32 *tmp;
452 u32 crc = ~(u32)0;
453 unsigned offset = 0;
454
455 if (!io_ctl->check_crcs) {
456 io_ctl_unmap_page(io_ctl);
457 return;
458 }
459
460 if (index == 0)
Justin P. Mattockcb54f252011-11-21 08:43:28 -0800461 offset = sizeof(u32) * io_ctl->num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400462
Liu Bob0496682013-03-14 14:57:45 +0000463 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300464 PAGE_SIZE - offset);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +0100465 btrfs_csum_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400466 io_ctl_unmap_page(io_ctl);
Chris Mason2b108262015-04-06 07:48:20 -0700467 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400468 tmp += index;
469 *tmp = crc;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400470}
471
Chris Mason4c6d1d82015-04-06 13:17:20 -0700472static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400473{
474 u32 *tmp, val;
475 u32 crc = ~(u32)0;
476 unsigned offset = 0;
477
478 if (!io_ctl->check_crcs) {
479 io_ctl_map_page(io_ctl, 0);
480 return 0;
481 }
482
483 if (index == 0)
484 offset = sizeof(u32) * io_ctl->num_pages;
485
Chris Mason2b108262015-04-06 07:48:20 -0700486 tmp = page_address(io_ctl->pages[0]);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400487 tmp += index;
488 val = *tmp;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400489
490 io_ctl_map_page(io_ctl, 0);
Liu Bob0496682013-03-14 14:57:45 +0000491 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300492 PAGE_SIZE - offset);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +0100493 btrfs_csum_final(crc, (u8 *)&crc);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400494 if (val != crc) {
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400495 btrfs_err_rl(io_ctl->fs_info,
David Sterba94647322015-10-08 11:01:36 +0200496 "csum mismatch on free space cache");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400497 io_ctl_unmap_page(io_ctl);
498 return -EIO;
499 }
500
Josef Bacika67509c2011-10-05 15:18:58 -0400501 return 0;
502}
503
Chris Mason4c6d1d82015-04-06 13:17:20 -0700504static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400505 void *bitmap)
506{
507 struct btrfs_free_space_entry *entry;
508
509 if (!io_ctl->cur)
510 return -ENOSPC;
511
512 entry = io_ctl->cur;
513 entry->offset = cpu_to_le64(offset);
514 entry->bytes = cpu_to_le64(bytes);
515 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
516 BTRFS_FREE_SPACE_EXTENT;
517 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
518 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
519
520 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
521 return 0;
522
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400523 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400524
525 /* No more pages to map */
526 if (io_ctl->index >= io_ctl->num_pages)
527 return 0;
528
529 /* map the next page */
530 io_ctl_map_page(io_ctl, 1);
531 return 0;
532}
533
Chris Mason4c6d1d82015-04-06 13:17:20 -0700534static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
Josef Bacika67509c2011-10-05 15:18:58 -0400535{
536 if (!io_ctl->cur)
537 return -ENOSPC;
538
539 /*
540 * If we aren't at the start of the current page, unmap this one and
541 * map the next one if there is any left.
542 */
543 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400544 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400545 if (io_ctl->index >= io_ctl->num_pages)
546 return -ENOSPC;
547 io_ctl_map_page(io_ctl, 0);
548 }
549
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300550 memcpy(io_ctl->cur, bitmap, PAGE_SIZE);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400551 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400552 if (io_ctl->index < io_ctl->num_pages)
553 io_ctl_map_page(io_ctl, 0);
554 return 0;
555}
556
Chris Mason4c6d1d82015-04-06 13:17:20 -0700557static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
Josef Bacika67509c2011-10-05 15:18:58 -0400558{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400559 /*
560 * If we're not on the boundary we know we've modified the page and we
561 * need to crc the page.
562 */
563 if (io_ctl->cur != io_ctl->orig)
564 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
565 else
566 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400567
568 while (io_ctl->index < io_ctl->num_pages) {
569 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400570 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400571 }
572}
573
Chris Mason4c6d1d82015-04-06 13:17:20 -0700574static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400575 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400576{
577 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500578 int ret;
579
580 if (!io_ctl->cur) {
581 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
582 if (ret)
583 return ret;
584 }
Josef Bacika67509c2011-10-05 15:18:58 -0400585
586 e = io_ctl->cur;
587 entry->offset = le64_to_cpu(e->offset);
588 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400589 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400590 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
591 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
592
593 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400594 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400595
596 io_ctl_unmap_page(io_ctl);
597
Josef Bacik2f120c02011-11-10 20:45:05 -0500598 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400599}
600
Chris Mason4c6d1d82015-04-06 13:17:20 -0700601static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400602 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400603{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400604 int ret;
605
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400606 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
607 if (ret)
608 return ret;
609
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300610 memcpy(entry->bitmap, io_ctl->cur, PAGE_SIZE);
Josef Bacika67509c2011-10-05 15:18:58 -0400611 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400612
613 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400614}
615
Josef Bacikcd023e72012-05-14 10:06:40 -0400616/*
617 * Since we attach pinned extents after the fact we can have contiguous sections
618 * of free space that are split up in entries. This poses a problem with the
619 * tree logging stuff since it could have allocated across what appears to be 2
620 * entries since we would have merged the entries when adding the pinned extents
621 * back to the free space cache. So run through the space cache that we just
622 * loaded and merge contiguous entries. This will make the log replay stuff not
623 * blow up and it will make for nicer allocator behavior.
624 */
625static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
626{
627 struct btrfs_free_space *e, *prev = NULL;
628 struct rb_node *n;
629
630again:
631 spin_lock(&ctl->tree_lock);
632 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
633 e = rb_entry(n, struct btrfs_free_space, offset_index);
634 if (!prev)
635 goto next;
636 if (e->bitmap || prev->bitmap)
637 goto next;
638 if (prev->offset + prev->bytes == e->offset) {
639 unlink_free_space(ctl, prev);
640 unlink_free_space(ctl, e);
641 prev->bytes += e->bytes;
642 kmem_cache_free(btrfs_free_space_cachep, e);
643 link_free_space(ctl, prev);
644 prev = NULL;
645 spin_unlock(&ctl->tree_lock);
646 goto again;
647 }
648next:
649 prev = e;
650 }
651 spin_unlock(&ctl->tree_lock);
652}
653
Eric Sandeen48a3b632013-04-25 20:41:01 +0000654static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
655 struct btrfs_free_space_ctl *ctl,
656 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400657{
David Sterba3ffbd682018-06-29 10:56:42 +0200658 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik9d66e232010-08-25 16:54:15 -0400659 struct btrfs_free_space_header *header;
660 struct extent_buffer *leaf;
Chris Mason4c6d1d82015-04-06 13:17:20 -0700661 struct btrfs_io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400662 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400663 struct btrfs_free_space *e, *n;
Gui Hechengb76808f2014-12-31 09:51:35 +0800664 LIST_HEAD(bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400665 u64 num_entries;
666 u64 num_bitmaps;
667 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400668 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400669 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400670
Josef Bacik9d66e232010-08-25 16:54:15 -0400671 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800672 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400673 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400674
675 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800676 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400677 key.type = 0;
678
679 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800680 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400681 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800682 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400683 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400684 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400685 }
686
Li Zefan0414efa2011-04-20 10:20:14 +0800687 ret = -1;
688
Josef Bacik9d66e232010-08-25 16:54:15 -0400689 leaf = path->nodes[0];
690 header = btrfs_item_ptr(leaf, path->slots[0],
691 struct btrfs_free_space_header);
692 num_entries = btrfs_free_space_entries(leaf, header);
693 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
694 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400695 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400696
Miao Xiee570fd22014-06-19 10:42:50 +0800697 if (!BTRFS_I(inode)->generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400698 btrfs_info(fs_info,
David Sterba913e1532017-07-13 15:32:18 +0200699 "the free space cache file (%llu) is invalid, skip it",
Miao Xiee570fd22014-06-19 10:42:50 +0800700 offset);
701 return 0;
702 }
703
Josef Bacik9d66e232010-08-25 16:54:15 -0400704 if (BTRFS_I(inode)->generation != generation) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400705 btrfs_err(fs_info,
706 "free space inode generation (%llu) did not match free space cache generation (%llu)",
707 BTRFS_I(inode)->generation, generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400708 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400709 }
710
711 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400712 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400713
Jeff Mahoneyf15376d2016-06-22 18:56:18 -0400714 ret = io_ctl_init(&io_ctl, inode, 0);
Li Zefan706efc62012-01-09 14:36:28 +0800715 if (ret)
716 return ret;
717
David Sterba1d480532017-01-23 17:28:19 +0100718 readahead_cache(inode);
Josef Bacik9d66e232010-08-25 16:54:15 -0400719
Josef Bacika67509c2011-10-05 15:18:58 -0400720 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
721 if (ret)
722 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400723
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400724 ret = io_ctl_check_crc(&io_ctl, 0);
725 if (ret)
726 goto free_cache;
727
Josef Bacika67509c2011-10-05 15:18:58 -0400728 ret = io_ctl_check_generation(&io_ctl, generation);
729 if (ret)
730 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400731
Josef Bacika67509c2011-10-05 15:18:58 -0400732 while (num_entries) {
733 e = kmem_cache_zalloc(btrfs_free_space_cachep,
734 GFP_NOFS);
735 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400736 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400737
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400738 ret = io_ctl_read_entry(&io_ctl, e, &type);
739 if (ret) {
740 kmem_cache_free(btrfs_free_space_cachep, e);
741 goto free_cache;
742 }
743
Josef Bacika67509c2011-10-05 15:18:58 -0400744 if (!e->bytes) {
745 kmem_cache_free(btrfs_free_space_cachep, e);
746 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400747 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400748
Josef Bacika67509c2011-10-05 15:18:58 -0400749 if (type == BTRFS_FREE_SPACE_EXTENT) {
750 spin_lock(&ctl->tree_lock);
751 ret = link_free_space(ctl, e);
752 spin_unlock(&ctl->tree_lock);
753 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400754 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000755 "Duplicate entries in free space cache, dumping");
Josef Bacikdc89e982011-01-28 17:05:48 -0500756 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400757 goto free_cache;
758 }
Josef Bacika67509c2011-10-05 15:18:58 -0400759 } else {
Josef Bacikb12d6862013-08-26 17:14:08 -0400760 ASSERT(num_bitmaps);
Josef Bacika67509c2011-10-05 15:18:58 -0400761 num_bitmaps--;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300762 e->bitmap = kzalloc(PAGE_SIZE, GFP_NOFS);
Josef Bacika67509c2011-10-05 15:18:58 -0400763 if (!e->bitmap) {
764 kmem_cache_free(
765 btrfs_free_space_cachep, e);
766 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400767 }
Josef Bacika67509c2011-10-05 15:18:58 -0400768 spin_lock(&ctl->tree_lock);
769 ret = link_free_space(ctl, e);
770 ctl->total_bitmaps++;
771 ctl->op->recalc_thresholds(ctl);
772 spin_unlock(&ctl->tree_lock);
773 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400774 btrfs_err(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000775 "Duplicate entries in free space cache, dumping");
Josef Bacika67509c2011-10-05 15:18:58 -0400776 kmem_cache_free(btrfs_free_space_cachep, e);
777 goto free_cache;
778 }
779 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400780 }
781
Josef Bacika67509c2011-10-05 15:18:58 -0400782 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400783 }
784
Josef Bacik2f120c02011-11-10 20:45:05 -0500785 io_ctl_unmap_page(&io_ctl);
786
Josef Bacika67509c2011-10-05 15:18:58 -0400787 /*
788 * We add the bitmaps at the end of the entries in order that
789 * the bitmap entries are added to the cache.
790 */
791 list_for_each_entry_safe(e, n, &bitmaps, list) {
792 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400793 ret = io_ctl_read_bitmap(&io_ctl, e);
794 if (ret)
795 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400796 }
797
798 io_ctl_drop_pages(&io_ctl);
Josef Bacikcd023e72012-05-14 10:06:40 -0400799 merge_space_tree(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400800 ret = 1;
801out:
Josef Bacika67509c2011-10-05 15:18:58 -0400802 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400803 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400804free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400805 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800806 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400807 goto out;
808}
809
Li Zefan0414efa2011-04-20 10:20:14 +0800810int load_free_space_cache(struct btrfs_fs_info *fs_info,
811 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400812{
Li Zefan34d52cb2011-03-29 13:46:06 +0800813 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800814 struct inode *inode;
815 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400816 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800817 bool matched;
818 u64 used = btrfs_block_group_used(&block_group->item);
819
820 /*
Li Zefan0414efa2011-04-20 10:20:14 +0800821 * If this block group has been marked to be cleared for one reason or
822 * another then we can't trust the on disk cache, so just return.
823 */
824 spin_lock(&block_group->lock);
825 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
826 spin_unlock(&block_group->lock);
827 return 0;
828 }
829 spin_unlock(&block_group->lock);
830
831 path = btrfs_alloc_path();
832 if (!path)
833 return 0;
Josef Bacikd53ba472012-04-12 16:03:57 -0400834 path->search_commit_root = 1;
835 path->skip_locking = 1;
Li Zefan0414efa2011-04-20 10:20:14 +0800836
Jeff Mahoney77ab86b2017-02-15 16:28:30 -0500837 inode = lookup_free_space_inode(fs_info, block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +0800838 if (IS_ERR(inode)) {
839 btrfs_free_path(path);
840 return 0;
841 }
842
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400843 /* We may have converted the inode and made the cache invalid. */
844 spin_lock(&block_group->lock);
845 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
846 spin_unlock(&block_group->lock);
Tsutomu Itoha7e221e2012-02-14 17:12:23 +0900847 btrfs_free_path(path);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400848 goto out;
849 }
850 spin_unlock(&block_group->lock);
851
Li Zefan0414efa2011-04-20 10:20:14 +0800852 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
853 path, block_group->key.objectid);
854 btrfs_free_path(path);
855 if (ret <= 0)
856 goto out;
857
858 spin_lock(&ctl->tree_lock);
859 matched = (ctl->free_space == (block_group->key.offset - used -
860 block_group->bytes_super));
861 spin_unlock(&ctl->tree_lock);
862
863 if (!matched) {
864 __btrfs_remove_free_space_cache(ctl);
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400865 btrfs_warn(fs_info,
866 "block group %llu has wrong amount of free space",
867 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800868 ret = -1;
869 }
870out:
871 if (ret < 0) {
872 /* This cache is bogus, make sure it gets cleared */
873 spin_lock(&block_group->lock);
874 block_group->disk_cache_state = BTRFS_DC_CLEAR;
875 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800876 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800877
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400878 btrfs_warn(fs_info,
879 "failed to load free space cache for block group %llu, rebuilding it now",
880 block_group->key.objectid);
Li Zefan0414efa2011-04-20 10:20:14 +0800881 }
882
883 iput(inode);
884 return ret;
885}
886
Chris Masond4452bc2014-05-19 20:47:56 -0700887static noinline_for_stack
Chris Mason4c6d1d82015-04-06 13:17:20 -0700888int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
Chris Masond4452bc2014-05-19 20:47:56 -0700889 struct btrfs_free_space_ctl *ctl,
890 struct btrfs_block_group_cache *block_group,
891 int *entries, int *bitmaps,
892 struct list_head *bitmap_list)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400893{
Josef Bacikc09544e2011-08-30 10:19:10 -0400894 int ret;
Chris Masond4452bc2014-05-19 20:47:56 -0700895 struct btrfs_free_cluster *cluster = NULL;
Chris Mason1bbc6212015-04-06 12:46:08 -0700896 struct btrfs_free_cluster *cluster_locked = NULL;
Chris Masond4452bc2014-05-19 20:47:56 -0700897 struct rb_node *node = rb_first(&ctl->free_space_offset);
Filipe Manana55507ce2014-12-01 17:04:09 +0000898 struct btrfs_trim_range *trim_entry;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400899
Josef Bacik43be2142011-04-01 14:55:00 +0000900 /* Get the cluster for this block_group if it exists */
Chris Masond4452bc2014-05-19 20:47:56 -0700901 if (block_group && !list_empty(&block_group->cluster_list)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000902 cluster = list_entry(block_group->cluster_list.next,
903 struct btrfs_free_cluster,
904 block_group_list);
Chris Masond4452bc2014-05-19 20:47:56 -0700905 }
Josef Bacik43be2142011-04-01 14:55:00 +0000906
Josef Bacikf75b1302011-10-05 10:00:18 -0400907 if (!node && cluster) {
Chris Mason1bbc6212015-04-06 12:46:08 -0700908 cluster_locked = cluster;
909 spin_lock(&cluster_locked->lock);
Josef Bacikf75b1302011-10-05 10:00:18 -0400910 node = rb_first(&cluster->root);
911 cluster = NULL;
912 }
913
Josef Bacik0cb59c92010-07-02 12:14:14 -0400914 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400915 while (node) {
916 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400917
Josef Bacika67509c2011-10-05 15:18:58 -0400918 e = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masond4452bc2014-05-19 20:47:56 -0700919 *entries += 1;
Josef Bacik43be2142011-04-01 14:55:00 +0000920
Chris Masond4452bc2014-05-19 20:47:56 -0700921 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
Josef Bacika67509c2011-10-05 15:18:58 -0400922 e->bitmap);
923 if (ret)
Chris Masond4452bc2014-05-19 20:47:56 -0700924 goto fail;
Josef Bacika67509c2011-10-05 15:18:58 -0400925
926 if (e->bitmap) {
Chris Masond4452bc2014-05-19 20:47:56 -0700927 list_add_tail(&e->list, bitmap_list);
928 *bitmaps += 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400929 }
930 node = rb_next(node);
931 if (!node && cluster) {
932 node = rb_first(&cluster->root);
Chris Mason1bbc6212015-04-06 12:46:08 -0700933 cluster_locked = cluster;
934 spin_lock(&cluster_locked->lock);
Josef Bacika67509c2011-10-05 15:18:58 -0400935 cluster = NULL;
936 }
937 }
Chris Mason1bbc6212015-04-06 12:46:08 -0700938 if (cluster_locked) {
939 spin_unlock(&cluster_locked->lock);
940 cluster_locked = NULL;
941 }
Filipe Manana55507ce2014-12-01 17:04:09 +0000942
943 /*
944 * Make sure we don't miss any range that was removed from our rbtree
945 * because trimming is running. Otherwise after a umount+mount (or crash
946 * after committing the transaction) we would leak free space and get
947 * an inconsistent free space cache report from fsck.
948 */
949 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
950 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
951 trim_entry->bytes, NULL);
952 if (ret)
953 goto fail;
954 *entries += 1;
955 }
956
Chris Masond4452bc2014-05-19 20:47:56 -0700957 return 0;
958fail:
Chris Mason1bbc6212015-04-06 12:46:08 -0700959 if (cluster_locked)
960 spin_unlock(&cluster_locked->lock);
Chris Masond4452bc2014-05-19 20:47:56 -0700961 return -ENOSPC;
962}
963
964static noinline_for_stack int
965update_cache_item(struct btrfs_trans_handle *trans,
966 struct btrfs_root *root,
967 struct inode *inode,
968 struct btrfs_path *path, u64 offset,
969 int entries, int bitmaps)
970{
971 struct btrfs_key key;
972 struct btrfs_free_space_header *header;
973 struct extent_buffer *leaf;
974 int ret;
975
976 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
977 key.offset = offset;
978 key.type = 0;
979
980 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
981 if (ret < 0) {
982 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
David Sterbaae0f1622017-10-31 16:37:52 +0100983 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -0700984 goto fail;
985 }
986 leaf = path->nodes[0];
987 if (ret > 0) {
988 struct btrfs_key found_key;
989 ASSERT(path->slots[0]);
990 path->slots[0]--;
991 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
992 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
993 found_key.offset != offset) {
994 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
995 inode->i_size - 1,
996 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +0100997 NULL);
Chris Masond4452bc2014-05-19 20:47:56 -0700998 btrfs_release_path(path);
999 goto fail;
1000 }
1001 }
1002
1003 BTRFS_I(inode)->generation = trans->transid;
1004 header = btrfs_item_ptr(leaf, path->slots[0],
1005 struct btrfs_free_space_header);
1006 btrfs_set_free_space_entries(leaf, header, entries);
1007 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1008 btrfs_set_free_space_generation(leaf, header, trans->transid);
1009 btrfs_mark_buffer_dirty(leaf);
1010 btrfs_release_path(path);
1011
1012 return 0;
1013
1014fail:
1015 return -1;
1016}
1017
1018static noinline_for_stack int
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001019write_pinned_extent_entries(struct btrfs_fs_info *fs_info,
Miao Xie5349d6c2014-06-19 10:42:49 +08001020 struct btrfs_block_group_cache *block_group,
Chris Mason4c6d1d82015-04-06 13:17:20 -07001021 struct btrfs_io_ctl *io_ctl,
Miao Xie5349d6c2014-06-19 10:42:49 +08001022 int *entries)
Chris Masond4452bc2014-05-19 20:47:56 -07001023{
1024 u64 start, extent_start, extent_end, len;
Chris Masond4452bc2014-05-19 20:47:56 -07001025 struct extent_io_tree *unpin = NULL;
1026 int ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001027
Miao Xie5349d6c2014-06-19 10:42:49 +08001028 if (!block_group)
1029 return 0;
1030
Josef Bacika67509c2011-10-05 15:18:58 -04001031 /*
1032 * We want to add any pinned extents to our free space cache
1033 * so we don't leak the space
Chris Masond4452bc2014-05-19 20:47:56 -07001034 *
Li Zefandb804f22012-01-10 16:41:01 +08001035 * We shouldn't have switched the pinned extents yet so this is the
1036 * right one
1037 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001038 unpin = fs_info->pinned_extents;
Li Zefandb804f22012-01-10 16:41:01 +08001039
Miao Xie5349d6c2014-06-19 10:42:49 +08001040 start = block_group->key.objectid;
Li Zefandb804f22012-01-10 16:41:01 +08001041
Miao Xie5349d6c2014-06-19 10:42:49 +08001042 while (start < block_group->key.objectid + block_group->key.offset) {
Li Zefandb804f22012-01-10 16:41:01 +08001043 ret = find_first_extent_bit(unpin, start,
1044 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -04001045 EXTENT_DIRTY, NULL);
Miao Xie5349d6c2014-06-19 10:42:49 +08001046 if (ret)
1047 return 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001048
Josef Bacika67509c2011-10-05 15:18:58 -04001049 /* This pinned extent is out of our range */
Li Zefandb804f22012-01-10 16:41:01 +08001050 if (extent_start >= block_group->key.objectid +
Josef Bacika67509c2011-10-05 15:18:58 -04001051 block_group->key.offset)
Miao Xie5349d6c2014-06-19 10:42:49 +08001052 return 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001053
Li Zefandb804f22012-01-10 16:41:01 +08001054 extent_start = max(extent_start, start);
1055 extent_end = min(block_group->key.objectid +
1056 block_group->key.offset, extent_end + 1);
1057 len = extent_end - extent_start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001058
Chris Masond4452bc2014-05-19 20:47:56 -07001059 *entries += 1;
1060 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
Josef Bacika67509c2011-10-05 15:18:58 -04001061 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001062 return -ENOSPC;
Josef Bacik2f356122011-06-10 15:31:13 -04001063
Li Zefandb804f22012-01-10 16:41:01 +08001064 start = extent_end;
Josef Bacika67509c2011-10-05 15:18:58 -04001065 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001066
Miao Xie5349d6c2014-06-19 10:42:49 +08001067 return 0;
1068}
1069
1070static noinline_for_stack int
Chris Mason4c6d1d82015-04-06 13:17:20 -07001071write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
Miao Xie5349d6c2014-06-19 10:42:49 +08001072{
Geliang Tang7ae16812015-12-18 22:17:00 +08001073 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001074 int ret;
1075
Josef Bacik0cb59c92010-07-02 12:14:14 -04001076 /* Write out the bitmaps */
Geliang Tang7ae16812015-12-18 22:17:00 +08001077 list_for_each_entry_safe(entry, next, bitmap_list, list) {
Chris Masond4452bc2014-05-19 20:47:56 -07001078 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
Josef Bacika67509c2011-10-05 15:18:58 -04001079 if (ret)
Miao Xie5349d6c2014-06-19 10:42:49 +08001080 return -ENOSPC;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001081 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001082 }
1083
Miao Xie5349d6c2014-06-19 10:42:49 +08001084 return 0;
1085}
Josef Bacik0cb59c92010-07-02 12:14:14 -04001086
Miao Xie5349d6c2014-06-19 10:42:49 +08001087static int flush_dirty_cache(struct inode *inode)
1088{
1089 int ret;
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001090
Josef Bacik0ef8b722013-10-25 16:13:35 -04001091 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001092 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001093 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01001094 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL);
Chris Masond4452bc2014-05-19 20:47:56 -07001095
Miao Xie5349d6c2014-06-19 10:42:49 +08001096 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001097}
1098
1099static void noinline_for_stack
Chris Masona3bdccc2015-04-24 11:00:00 -07001100cleanup_bitmap_list(struct list_head *bitmap_list)
Chris Masond4452bc2014-05-19 20:47:56 -07001101{
Geliang Tang7ae16812015-12-18 22:17:00 +08001102 struct btrfs_free_space *entry, *next;
Miao Xie5349d6c2014-06-19 10:42:49 +08001103
Geliang Tang7ae16812015-12-18 22:17:00 +08001104 list_for_each_entry_safe(entry, next, bitmap_list, list)
Chris Masond4452bc2014-05-19 20:47:56 -07001105 list_del_init(&entry->list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001106}
1107
1108static void noinline_for_stack
1109cleanup_write_cache_enospc(struct inode *inode,
1110 struct btrfs_io_ctl *io_ctl,
David Sterba7bf1a152017-02-10 20:23:00 +01001111 struct extent_state **cached_state)
Chris Masona3bdccc2015-04-24 11:00:00 -07001112{
Chris Masond4452bc2014-05-19 20:47:56 -07001113 io_ctl_drop_pages(io_ctl);
1114 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001115 i_size_read(inode) - 1, cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001116}
1117
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001118static int __btrfs_wait_cache_io(struct btrfs_root *root,
1119 struct btrfs_trans_handle *trans,
1120 struct btrfs_block_group_cache *block_group,
1121 struct btrfs_io_ctl *io_ctl,
1122 struct btrfs_path *path, u64 offset)
Chris Masonc9dc4c62015-04-04 17:14:42 -07001123{
1124 int ret;
1125 struct inode *inode = io_ctl->inode;
1126
Chris Mason1bbc6212015-04-06 12:46:08 -07001127 if (!inode)
1128 return 0;
1129
Chris Masonc9dc4c62015-04-04 17:14:42 -07001130 /* Flush the dirty pages in the cache file. */
1131 ret = flush_dirty_cache(inode);
1132 if (ret)
1133 goto out;
1134
1135 /* Update the cache item to tell everyone this cache file is valid. */
1136 ret = update_cache_item(trans, root, inode, path, offset,
1137 io_ctl->entries, io_ctl->bitmaps);
1138out:
1139 io_ctl_free(io_ctl);
1140 if (ret) {
1141 invalidate_inode_pages2(inode->i_mapping);
1142 BTRFS_I(inode)->generation = 0;
1143 if (block_group) {
1144#ifdef DEBUG
David Sterba3ffbd682018-06-29 10:56:42 +02001145 btrfs_err(root->fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001146 "failed to write free space cache for block group %llu",
1147 block_group->key.objectid);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001148#endif
1149 }
1150 }
1151 btrfs_update_inode(trans, root, inode);
1152
1153 if (block_group) {
Chris Mason1bbc6212015-04-06 12:46:08 -07001154 /* the dirty list is protected by the dirty_bgs_lock */
1155 spin_lock(&trans->transaction->dirty_bgs_lock);
1156
1157 /* the disk_cache_state is protected by the block group lock */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001158 spin_lock(&block_group->lock);
1159
1160 /*
1161 * only mark this as written if we didn't get put back on
Chris Mason1bbc6212015-04-06 12:46:08 -07001162 * the dirty list while waiting for IO. Otherwise our
1163 * cache state won't be right, and we won't get written again
Chris Masonc9dc4c62015-04-04 17:14:42 -07001164 */
1165 if (!ret && list_empty(&block_group->dirty_list))
1166 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1167 else if (ret)
1168 block_group->disk_cache_state = BTRFS_DC_ERROR;
1169
1170 spin_unlock(&block_group->lock);
Chris Mason1bbc6212015-04-06 12:46:08 -07001171 spin_unlock(&trans->transaction->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001172 io_ctl->inode = NULL;
1173 iput(inode);
1174 }
1175
1176 return ret;
1177
1178}
1179
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04001180static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1181 struct btrfs_trans_handle *trans,
1182 struct btrfs_io_ctl *io_ctl,
1183 struct btrfs_path *path)
1184{
1185 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1186}
1187
1188int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
1189 struct btrfs_block_group_cache *block_group,
1190 struct btrfs_path *path)
1191{
1192 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1193 block_group, &block_group->io_ctl,
1194 path, block_group->key.objectid);
1195}
1196
Chris Masond4452bc2014-05-19 20:47:56 -07001197/**
1198 * __btrfs_write_out_cache - write out cached info to an inode
1199 * @root - the root the inode belongs to
1200 * @ctl - the free space cache we are going to write out
1201 * @block_group - the block_group for this cache if it belongs to a block_group
1202 * @trans - the trans handle
Chris Masond4452bc2014-05-19 20:47:56 -07001203 *
1204 * This function writes out a free space cache struct to disk for quick recovery
Geliang Tang8cd1e732015-10-04 17:05:32 +08001205 * on mount. This will return 0 if it was successful in writing the cache out,
Omar Sandovalb8605452015-02-24 02:47:06 -08001206 * or an errno if it was not.
Chris Masond4452bc2014-05-19 20:47:56 -07001207 */
1208static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1209 struct btrfs_free_space_ctl *ctl,
1210 struct btrfs_block_group_cache *block_group,
Chris Masonc9dc4c62015-04-04 17:14:42 -07001211 struct btrfs_io_ctl *io_ctl,
David Sterba0e8d9312017-02-10 20:26:24 +01001212 struct btrfs_trans_handle *trans)
Chris Masond4452bc2014-05-19 20:47:56 -07001213{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001214 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masond4452bc2014-05-19 20:47:56 -07001215 struct extent_state *cached_state = NULL;
Miao Xie5349d6c2014-06-19 10:42:49 +08001216 LIST_HEAD(bitmap_list);
Chris Masond4452bc2014-05-19 20:47:56 -07001217 int entries = 0;
1218 int bitmaps = 0;
1219 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001220 int must_iput = 0;
Chris Masond4452bc2014-05-19 20:47:56 -07001221
1222 if (!i_size_read(inode))
Omar Sandovalb8605452015-02-24 02:47:06 -08001223 return -EIO;
Chris Masond4452bc2014-05-19 20:47:56 -07001224
Chris Masonc9dc4c62015-04-04 17:14:42 -07001225 WARN_ON(io_ctl->pages);
Jeff Mahoneyf15376d2016-06-22 18:56:18 -04001226 ret = io_ctl_init(io_ctl, inode, 1);
Chris Masond4452bc2014-05-19 20:47:56 -07001227 if (ret)
Omar Sandovalb8605452015-02-24 02:47:06 -08001228 return ret;
Chris Masond4452bc2014-05-19 20:47:56 -07001229
Miao Xiee570fd22014-06-19 10:42:50 +08001230 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1231 down_write(&block_group->data_rwsem);
1232 spin_lock(&block_group->lock);
1233 if (block_group->delalloc_bytes) {
1234 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1235 spin_unlock(&block_group->lock);
1236 up_write(&block_group->data_rwsem);
1237 BTRFS_I(inode)->generation = 0;
1238 ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07001239 must_iput = 1;
Miao Xiee570fd22014-06-19 10:42:50 +08001240 goto out;
1241 }
1242 spin_unlock(&block_group->lock);
1243 }
1244
Chris Masond4452bc2014-05-19 20:47:56 -07001245 /* Lock all pages first so we can lock the extent safely. */
Omar Sandovalb8605452015-02-24 02:47:06 -08001246 ret = io_ctl_prepare_pages(io_ctl, inode, 0);
1247 if (ret)
Josef Bacikb77000e2017-11-15 16:20:52 -05001248 goto out_unlock;
Chris Masond4452bc2014-05-19 20:47:56 -07001249
1250 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
David Sterbaff13db42015-12-03 14:30:40 +01001251 &cached_state);
Chris Masond4452bc2014-05-19 20:47:56 -07001252
Chris Masonc9dc4c62015-04-04 17:14:42 -07001253 io_ctl_set_generation(io_ctl, trans->transid);
Chris Masond4452bc2014-05-19 20:47:56 -07001254
Filipe Manana55507ce2014-12-01 17:04:09 +00001255 mutex_lock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001256 /* Write out the extent entries in the free space cache */
Chris Mason1bbc6212015-04-06 12:46:08 -07001257 spin_lock(&ctl->tree_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001258 ret = write_cache_extent_entries(io_ctl, ctl,
Chris Masond4452bc2014-05-19 20:47:56 -07001259 block_group, &entries, &bitmaps,
1260 &bitmap_list);
Chris Masona3bdccc2015-04-24 11:00:00 -07001261 if (ret)
1262 goto out_nospc_locked;
Chris Masond4452bc2014-05-19 20:47:56 -07001263
Miao Xie5349d6c2014-06-19 10:42:49 +08001264 /*
1265 * Some spaces that are freed in the current transaction are pinned,
1266 * they will be added into free space cache after the transaction is
1267 * committed, we shouldn't lose them.
Chris Mason1bbc6212015-04-06 12:46:08 -07001268 *
1269 * If this changes while we are working we'll get added back to
1270 * the dirty list and redo it. No locking needed
Miao Xie5349d6c2014-06-19 10:42:49 +08001271 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001272 ret = write_pinned_extent_entries(fs_info, block_group,
1273 io_ctl, &entries);
Chris Masona3bdccc2015-04-24 11:00:00 -07001274 if (ret)
1275 goto out_nospc_locked;
Miao Xie5349d6c2014-06-19 10:42:49 +08001276
Filipe Manana55507ce2014-12-01 17:04:09 +00001277 /*
1278 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1279 * locked while doing it because a concurrent trim can be manipulating
1280 * or freeing the bitmap.
1281 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001282 ret = write_bitmap_entries(io_ctl, &bitmap_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07001283 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00001284 mutex_unlock(&ctl->cache_writeout_mutex);
Miao Xie5349d6c2014-06-19 10:42:49 +08001285 if (ret)
1286 goto out_nospc;
1287
1288 /* Zero out the rest of the pages just to make sure */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001289 io_ctl_zero_remaining_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001290
1291 /* Everything is written out, now we dirty the pages in the file. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001292 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1293 i_size_read(inode), &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001294 if (ret)
1295 goto out_nospc;
1296
Miao Xiee570fd22014-06-19 10:42:50 +08001297 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1298 up_write(&block_group->data_rwsem);
Miao Xie5349d6c2014-06-19 10:42:49 +08001299 /*
1300 * Release the pages and unlock the extent, we will flush
1301 * them out later
1302 */
Chris Masonc9dc4c62015-04-04 17:14:42 -07001303 io_ctl_drop_pages(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001304
1305 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
David Sterbae43bbe52017-12-12 21:43:52 +01001306 i_size_read(inode) - 1, &cached_state);
Miao Xie5349d6c2014-06-19 10:42:49 +08001307
Chris Masonc9dc4c62015-04-04 17:14:42 -07001308 /*
1309 * at this point the pages are under IO and we're happy,
1310 * The caller is responsible for waiting on them and updating the
1311 * the cache and the inode
1312 */
1313 io_ctl->entries = entries;
1314 io_ctl->bitmaps = bitmaps;
1315
1316 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
Miao Xie5349d6c2014-06-19 10:42:49 +08001317 if (ret)
Josef Bacik0ef8b722013-10-25 16:13:35 -04001318 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001319
Chris Masonc9dc4c62015-04-04 17:14:42 -07001320 return 0;
1321
Josef Bacik2f356122011-06-10 15:31:13 -04001322out:
Chris Masonc9dc4c62015-04-04 17:14:42 -07001323 io_ctl->inode = NULL;
1324 io_ctl_free(io_ctl);
Miao Xie5349d6c2014-06-19 10:42:49 +08001325 if (ret) {
Josef Bacika67509c2011-10-05 15:18:58 -04001326 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001327 BTRFS_I(inode)->generation = 0;
1328 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001329 btrfs_update_inode(trans, root, inode);
Chris Masonc9dc4c62015-04-04 17:14:42 -07001330 if (must_iput)
1331 iput(inode);
Miao Xie5349d6c2014-06-19 10:42:49 +08001332 return ret;
Josef Bacika67509c2011-10-05 15:18:58 -04001333
Chris Masona3bdccc2015-04-24 11:00:00 -07001334out_nospc_locked:
1335 cleanup_bitmap_list(&bitmap_list);
1336 spin_unlock(&ctl->tree_lock);
1337 mutex_unlock(&ctl->cache_writeout_mutex);
1338
Josef Bacika67509c2011-10-05 15:18:58 -04001339out_nospc:
David Sterba7bf1a152017-02-10 20:23:00 +01001340 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
Miao Xiee570fd22014-06-19 10:42:50 +08001341
Josef Bacikb77000e2017-11-15 16:20:52 -05001342out_unlock:
Miao Xiee570fd22014-06-19 10:42:50 +08001343 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1344 up_write(&block_group->data_rwsem);
1345
Josef Bacika67509c2011-10-05 15:18:58 -04001346 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001347}
1348
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001349int btrfs_write_out_cache(struct btrfs_fs_info *fs_info,
Li Zefan0414efa2011-04-20 10:20:14 +08001350 struct btrfs_trans_handle *trans,
1351 struct btrfs_block_group_cache *block_group,
1352 struct btrfs_path *path)
1353{
1354 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1355 struct inode *inode;
1356 int ret = 0;
1357
Li Zefan0414efa2011-04-20 10:20:14 +08001358 spin_lock(&block_group->lock);
1359 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1360 spin_unlock(&block_group->lock);
1361 return 0;
1362 }
1363 spin_unlock(&block_group->lock);
1364
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001365 inode = lookup_free_space_inode(fs_info, block_group, path);
Li Zefan0414efa2011-04-20 10:20:14 +08001366 if (IS_ERR(inode))
1367 return 0;
1368
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05001369 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1370 block_group, &block_group->io_ctl, trans);
Josef Bacikc09544e2011-08-30 10:19:10 -04001371 if (ret) {
Josef Bacikc09544e2011-08-30 10:19:10 -04001372#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001373 btrfs_err(fs_info,
1374 "failed to write free space cache for block group %llu",
1375 block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001376#endif
Chris Masonc9dc4c62015-04-04 17:14:42 -07001377 spin_lock(&block_group->lock);
1378 block_group->disk_cache_state = BTRFS_DC_ERROR;
1379 spin_unlock(&block_group->lock);
1380
1381 block_group->io_ctl.inode = NULL;
1382 iput(inode);
Li Zefan0414efa2011-04-20 10:20:14 +08001383 }
1384
Chris Masonc9dc4c62015-04-04 17:14:42 -07001385 /*
1386 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1387 * to wait for IO and put the inode
1388 */
1389
Josef Bacik0cb59c92010-07-02 12:14:14 -04001390 return ret;
1391}
1392
Li Zefan34d52cb2011-03-29 13:46:06 +08001393static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001394 u64 offset)
1395{
Josef Bacikb12d6862013-08-26 17:14:08 -04001396 ASSERT(offset >= bitmap_start);
Josef Bacik96303082009-07-13 21:29:25 -04001397 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001398 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001399}
1400
Li Zefan34d52cb2011-03-29 13:46:06 +08001401static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001402{
Li Zefan34d52cb2011-03-29 13:46:06 +08001403 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001404}
1405
Li Zefan34d52cb2011-03-29 13:46:06 +08001406static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001407 u64 offset)
1408{
1409 u64 bitmap_start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001410 u64 bytes_per_bitmap;
Josef Bacik96303082009-07-13 21:29:25 -04001411
Li Zefan34d52cb2011-03-29 13:46:06 +08001412 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1413 bitmap_start = offset - ctl->start;
Feifei Xu0ef64472016-06-01 19:18:24 +08001414 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
Josef Bacik96303082009-07-13 21:29:25 -04001415 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001416 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001417
1418 return bitmap_start;
1419}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001420
1421static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001422 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001423{
1424 struct rb_node **p = &root->rb_node;
1425 struct rb_node *parent = NULL;
1426 struct btrfs_free_space *info;
1427
1428 while (*p) {
1429 parent = *p;
1430 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1431
Josef Bacik96303082009-07-13 21:29:25 -04001432 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001433 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001434 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001435 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001436 } else {
1437 /*
1438 * we could have a bitmap entry and an extent entry
1439 * share the same offset. If this is the case, we want
1440 * the extent entry to always be found first if we do a
1441 * linear search through the tree, since we want to have
1442 * the quickest allocation time, and allocating from an
1443 * extent is faster than allocating from a bitmap. So
1444 * if we're inserting a bitmap and we find an entry at
1445 * this offset, we want to go right, or after this entry
1446 * logically. If we are inserting an extent and we've
1447 * found a bitmap, we want to go left, or before
1448 * logically.
1449 */
1450 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001451 if (info->bitmap) {
1452 WARN_ON_ONCE(1);
1453 return -EEXIST;
1454 }
Josef Bacik96303082009-07-13 21:29:25 -04001455 p = &(*p)->rb_right;
1456 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001457 if (!info->bitmap) {
1458 WARN_ON_ONCE(1);
1459 return -EEXIST;
1460 }
Josef Bacik96303082009-07-13 21:29:25 -04001461 p = &(*p)->rb_left;
1462 }
1463 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001464 }
1465
1466 rb_link_node(node, parent, p);
1467 rb_insert_color(node, root);
1468
1469 return 0;
1470}
1471
1472/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001473 * searches the tree for the given offset.
1474 *
Josef Bacik96303082009-07-13 21:29:25 -04001475 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1476 * want a section that has at least bytes size and comes at or after the given
1477 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001478 */
Josef Bacik96303082009-07-13 21:29:25 -04001479static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001480tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001481 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001482{
Li Zefan34d52cb2011-03-29 13:46:06 +08001483 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001484 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001485
Josef Bacik96303082009-07-13 21:29:25 -04001486 /* find entry that is closest to the 'offset' */
1487 while (1) {
1488 if (!n) {
1489 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001490 break;
1491 }
Josef Bacik96303082009-07-13 21:29:25 -04001492
1493 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1494 prev = entry;
1495
1496 if (offset < entry->offset)
1497 n = n->rb_left;
1498 else if (offset > entry->offset)
1499 n = n->rb_right;
1500 else
1501 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001502 }
1503
Josef Bacik96303082009-07-13 21:29:25 -04001504 if (bitmap_only) {
1505 if (!entry)
1506 return NULL;
1507 if (entry->bitmap)
1508 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001509
Josef Bacik96303082009-07-13 21:29:25 -04001510 /*
1511 * bitmap entry and extent entry may share same offset,
1512 * in that case, bitmap entry comes after extent entry.
1513 */
1514 n = rb_next(n);
1515 if (!n)
1516 return NULL;
1517 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1518 if (entry->offset != offset)
1519 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001520
Josef Bacik96303082009-07-13 21:29:25 -04001521 WARN_ON(!entry->bitmap);
1522 return entry;
1523 } else if (entry) {
1524 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001525 /*
Josef Bacik96303082009-07-13 21:29:25 -04001526 * if previous extent entry covers the offset,
1527 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001528 */
Miao Xiede6c4112012-10-18 08:18:01 +00001529 n = rb_prev(&entry->offset_index);
1530 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001531 prev = rb_entry(n, struct btrfs_free_space,
1532 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001533 if (!prev->bitmap &&
1534 prev->offset + prev->bytes > offset)
1535 entry = prev;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001536 }
Josef Bacik96303082009-07-13 21:29:25 -04001537 }
1538 return entry;
1539 }
1540
1541 if (!prev)
1542 return NULL;
1543
1544 /* find last entry before the 'offset' */
1545 entry = prev;
1546 if (entry->offset > offset) {
1547 n = rb_prev(&entry->offset_index);
1548 if (n) {
1549 entry = rb_entry(n, struct btrfs_free_space,
1550 offset_index);
Josef Bacikb12d6862013-08-26 17:14:08 -04001551 ASSERT(entry->offset <= offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001552 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001553 if (fuzzy)
1554 return entry;
1555 else
1556 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001557 }
1558 }
1559
Josef Bacik96303082009-07-13 21:29:25 -04001560 if (entry->bitmap) {
Miao Xiede6c4112012-10-18 08:18:01 +00001561 n = rb_prev(&entry->offset_index);
1562 if (n) {
Josef Bacik96303082009-07-13 21:29:25 -04001563 prev = rb_entry(n, struct btrfs_free_space,
1564 offset_index);
Miao Xiede6c4112012-10-18 08:18:01 +00001565 if (!prev->bitmap &&
1566 prev->offset + prev->bytes > offset)
1567 return prev;
Josef Bacik96303082009-07-13 21:29:25 -04001568 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001569 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001570 return entry;
1571 } else if (entry->offset + entry->bytes > offset)
1572 return entry;
1573
1574 if (!fuzzy)
1575 return NULL;
1576
1577 while (1) {
1578 if (entry->bitmap) {
1579 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001580 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001581 break;
1582 } else {
1583 if (entry->offset + entry->bytes > offset)
1584 break;
1585 }
1586
1587 n = rb_next(&entry->offset_index);
1588 if (!n)
1589 return NULL;
1590 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1591 }
1592 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001593}
1594
Li Zefanf333adb2010-11-09 14:57:39 +08001595static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001596__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001597 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001598{
Li Zefan34d52cb2011-03-29 13:46:06 +08001599 rb_erase(&info->offset_index, &ctl->free_space_offset);
1600 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001601}
1602
Li Zefan34d52cb2011-03-29 13:46:06 +08001603static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001604 struct btrfs_free_space *info)
1605{
Li Zefan34d52cb2011-03-29 13:46:06 +08001606 __unlink_free_space(ctl, info);
1607 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001608}
1609
Li Zefan34d52cb2011-03-29 13:46:06 +08001610static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001611 struct btrfs_free_space *info)
1612{
1613 int ret = 0;
1614
Josef Bacikb12d6862013-08-26 17:14:08 -04001615 ASSERT(info->bytes || info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001616 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001617 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001618 if (ret)
1619 return ret;
1620
Li Zefan34d52cb2011-03-29 13:46:06 +08001621 ctl->free_space += info->bytes;
1622 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001623 return ret;
1624}
1625
Li Zefan34d52cb2011-03-29 13:46:06 +08001626static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001627{
Li Zefan34d52cb2011-03-29 13:46:06 +08001628 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001629 u64 max_bytes;
1630 u64 bitmap_bytes;
1631 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001632 u64 size = block_group->key.offset;
Feifei Xu0ef64472016-06-01 19:18:24 +08001633 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1634 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
Li Zefan34d52cb2011-03-29 13:46:06 +08001635
Feifei Xu0ef64472016-06-01 19:18:24 +08001636 max_bitmaps = max_t(u64, max_bitmaps, 1);
Josef Bacikdde57402013-02-12 14:07:51 -05001637
Josef Bacikb12d6862013-08-26 17:14:08 -04001638 ASSERT(ctl->total_bitmaps <= max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001639
1640 /*
1641 * The goal is to keep the total amount of memory used per 1gb of space
1642 * at or below 32k, so we need to adjust how much memory we allow to be
1643 * used by extent based free space tracking
1644 */
Byongho Leeee221842015-12-15 01:42:10 +09001645 if (size < SZ_1G)
Li Zefan8eb2d822010-11-09 14:48:01 +08001646 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1647 else
Byongho Leeee221842015-12-15 01:42:10 +09001648 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
Josef Bacik96303082009-07-13 21:29:25 -04001649
Josef Bacik25891f72009-09-11 16:11:20 -04001650 /*
1651 * we want to account for 1 more bitmap than what we have so we can make
1652 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1653 * we add more bitmaps.
1654 */
Feifei Xub9ef22d2016-06-01 19:18:25 +08001655 bitmap_bytes = (ctl->total_bitmaps + 1) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001656
Josef Bacik25891f72009-09-11 16:11:20 -04001657 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001658 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001659 return;
Josef Bacik96303082009-07-13 21:29:25 -04001660 }
Josef Bacik25891f72009-09-11 16:11:20 -04001661
1662 /*
David Sterbaf8c269d2015-01-16 17:21:12 +01001663 * we want the extent entry threshold to always be at most 1/2 the max
Josef Bacik25891f72009-09-11 16:11:20 -04001664 * bytes we can have, or whatever is less than that.
1665 */
1666 extent_bytes = max_bytes - bitmap_bytes;
David Sterbaf8c269d2015-01-16 17:21:12 +01001667 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
Josef Bacik25891f72009-09-11 16:11:20 -04001668
Li Zefan34d52cb2011-03-29 13:46:06 +08001669 ctl->extents_thresh =
David Sterbaf8c269d2015-01-16 17:21:12 +01001670 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
Josef Bacik96303082009-07-13 21:29:25 -04001671}
1672
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001673static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1674 struct btrfs_free_space *info,
1675 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001676{
Li Zefanf38b6e72011-03-14 13:40:51 +08001677 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001678
Li Zefan34d52cb2011-03-29 13:46:06 +08001679 start = offset_to_bit(info->offset, ctl->unit, offset);
1680 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001681 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001682
Li Zefanf38b6e72011-03-14 13:40:51 +08001683 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001684
1685 info->bytes -= bytes;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001686}
1687
1688static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1689 struct btrfs_free_space *info, u64 offset,
1690 u64 bytes)
1691{
1692 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001693 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001694}
1695
Li Zefan34d52cb2011-03-29 13:46:06 +08001696static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001697 struct btrfs_free_space *info, u64 offset,
1698 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001699{
Li Zefanf38b6e72011-03-14 13:40:51 +08001700 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001701
Li Zefan34d52cb2011-03-29 13:46:06 +08001702 start = offset_to_bit(info->offset, ctl->unit, offset);
1703 count = bytes_to_bits(bytes, ctl->unit);
Josef Bacikb12d6862013-08-26 17:14:08 -04001704 ASSERT(start + count <= BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001705
Li Zefanf38b6e72011-03-14 13:40:51 +08001706 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001707
1708 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001709 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001710}
1711
Miao Xiea4820392013-09-09 13:19:42 +08001712/*
1713 * If we can not find suitable extent, we will use bytes to record
1714 * the size of the max extent.
1715 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001716static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001717 struct btrfs_free_space *bitmap_info, u64 *offset,
Josef Bacik0584f712015-10-02 16:12:23 -04001718 u64 *bytes, bool for_alloc)
Josef Bacik96303082009-07-13 21:29:25 -04001719{
1720 unsigned long found_bits = 0;
Miao Xiea4820392013-09-09 13:19:42 +08001721 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001722 unsigned long bits, i;
1723 unsigned long next_zero;
Miao Xiea4820392013-09-09 13:19:42 +08001724 unsigned long extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001725
Josef Bacikcef40482015-10-02 16:09:42 -04001726 /*
1727 * Skip searching the bitmap if we don't have a contiguous section that
1728 * is large enough for this allocation.
1729 */
Josef Bacik0584f712015-10-02 16:12:23 -04001730 if (for_alloc &&
1731 bitmap_info->max_extent_size &&
Josef Bacikcef40482015-10-02 16:09:42 -04001732 bitmap_info->max_extent_size < *bytes) {
1733 *bytes = bitmap_info->max_extent_size;
1734 return -1;
1735 }
1736
Li Zefan34d52cb2011-03-29 13:46:06 +08001737 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001738 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001739 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001740
Wei Yongjunebb3dad2012-09-13 20:29:02 -06001741 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
Josef Bacik0584f712015-10-02 16:12:23 -04001742 if (for_alloc && bits == 1) {
1743 found_bits = 1;
1744 break;
1745 }
Josef Bacik96303082009-07-13 21:29:25 -04001746 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1747 BITS_PER_BITMAP, i);
Miao Xiea4820392013-09-09 13:19:42 +08001748 extent_bits = next_zero - i;
1749 if (extent_bits >= bits) {
1750 found_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001751 break;
Miao Xiea4820392013-09-09 13:19:42 +08001752 } else if (extent_bits > max_bits) {
1753 max_bits = extent_bits;
Josef Bacik96303082009-07-13 21:29:25 -04001754 }
1755 i = next_zero;
1756 }
1757
1758 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001759 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1760 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001761 return 0;
1762 }
1763
Miao Xiea4820392013-09-09 13:19:42 +08001764 *bytes = (u64)(max_bits) * ctl->unit;
Josef Bacikcef40482015-10-02 16:09:42 -04001765 bitmap_info->max_extent_size = *bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001766 return -1;
1767}
1768
Miao Xiea4820392013-09-09 13:19:42 +08001769/* Cache the size of the max extent in bytes */
Li Zefan34d52cb2011-03-29 13:46:06 +08001770static struct btrfs_free_space *
David Woodhouse53b381b2013-01-29 18:40:14 -05001771find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
Miao Xiea4820392013-09-09 13:19:42 +08001772 unsigned long align, u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04001773{
1774 struct btrfs_free_space *entry;
1775 struct rb_node *node;
David Woodhouse53b381b2013-01-29 18:40:14 -05001776 u64 tmp;
1777 u64 align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001778 int ret;
1779
Li Zefan34d52cb2011-03-29 13:46:06 +08001780 if (!ctl->free_space_offset.rb_node)
Miao Xiea4820392013-09-09 13:19:42 +08001781 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001782
Li Zefan34d52cb2011-03-29 13:46:06 +08001783 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001784 if (!entry)
Miao Xiea4820392013-09-09 13:19:42 +08001785 goto out;
Josef Bacik96303082009-07-13 21:29:25 -04001786
1787 for (node = &entry->offset_index; node; node = rb_next(node)) {
1788 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Miao Xiea4820392013-09-09 13:19:42 +08001789 if (entry->bytes < *bytes) {
1790 if (entry->bytes > *max_extent_size)
1791 *max_extent_size = entry->bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001792 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001793 }
Josef Bacik96303082009-07-13 21:29:25 -04001794
David Woodhouse53b381b2013-01-29 18:40:14 -05001795 /* make sure the space returned is big enough
1796 * to match our requested alignment
1797 */
1798 if (*bytes >= align) {
Miao Xiea4820392013-09-09 13:19:42 +08001799 tmp = entry->offset - ctl->start + align - 1;
David Sterba47c57132015-02-20 18:43:47 +01001800 tmp = div64_u64(tmp, align);
David Woodhouse53b381b2013-01-29 18:40:14 -05001801 tmp = tmp * align + ctl->start;
1802 align_off = tmp - entry->offset;
1803 } else {
1804 align_off = 0;
1805 tmp = entry->offset;
1806 }
1807
Miao Xiea4820392013-09-09 13:19:42 +08001808 if (entry->bytes < *bytes + align_off) {
1809 if (entry->bytes > *max_extent_size)
1810 *max_extent_size = entry->bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05001811 continue;
Miao Xiea4820392013-09-09 13:19:42 +08001812 }
David Woodhouse53b381b2013-01-29 18:40:14 -05001813
Josef Bacik96303082009-07-13 21:29:25 -04001814 if (entry->bitmap) {
Miao Xiea4820392013-09-09 13:19:42 +08001815 u64 size = *bytes;
1816
Josef Bacik0584f712015-10-02 16:12:23 -04001817 ret = search_bitmap(ctl, entry, &tmp, &size, true);
David Woodhouse53b381b2013-01-29 18:40:14 -05001818 if (!ret) {
1819 *offset = tmp;
Miao Xiea4820392013-09-09 13:19:42 +08001820 *bytes = size;
Josef Bacik96303082009-07-13 21:29:25 -04001821 return entry;
Miao Xiea4820392013-09-09 13:19:42 +08001822 } else if (size > *max_extent_size) {
1823 *max_extent_size = size;
David Woodhouse53b381b2013-01-29 18:40:14 -05001824 }
Josef Bacik96303082009-07-13 21:29:25 -04001825 continue;
1826 }
1827
David Woodhouse53b381b2013-01-29 18:40:14 -05001828 *offset = tmp;
1829 *bytes = entry->bytes - align_off;
Josef Bacik96303082009-07-13 21:29:25 -04001830 return entry;
1831 }
Miao Xiea4820392013-09-09 13:19:42 +08001832out:
Josef Bacik96303082009-07-13 21:29:25 -04001833 return NULL;
1834}
1835
Li Zefan34d52cb2011-03-29 13:46:06 +08001836static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001837 struct btrfs_free_space *info, u64 offset)
1838{
Li Zefan34d52cb2011-03-29 13:46:06 +08001839 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001840 info->bytes = 0;
Alexandre Olivaf2d0f672011-11-28 12:04:43 -02001841 INIT_LIST_HEAD(&info->list);
Li Zefan34d52cb2011-03-29 13:46:06 +08001842 link_free_space(ctl, info);
1843 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001844
Li Zefan34d52cb2011-03-29 13:46:06 +08001845 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001846}
1847
Li Zefan34d52cb2011-03-29 13:46:06 +08001848static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001849 struct btrfs_free_space *bitmap_info)
1850{
Li Zefan34d52cb2011-03-29 13:46:06 +08001851 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001852 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001853 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001854 ctl->total_bitmaps--;
1855 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001856}
1857
Li Zefan34d52cb2011-03-29 13:46:06 +08001858static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001859 struct btrfs_free_space *bitmap_info,
1860 u64 *offset, u64 *bytes)
1861{
1862 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001863 u64 search_start, search_bytes;
1864 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001865
1866again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001867 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001868
Josef Bacik6606bb92009-07-31 11:03:58 -04001869 /*
Josef Bacikbdb7d302012-06-27 15:10:56 -04001870 * We need to search for bits in this bitmap. We could only cover some
1871 * of the extent in this bitmap thanks to how we add space, so we need
1872 * to search for as much as it as we can and clear that amount, and then
1873 * go searching for the next bit.
Josef Bacik6606bb92009-07-31 11:03:58 -04001874 */
1875 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001876 search_bytes = ctl->unit;
Josef Bacik13dbc082011-02-03 02:39:52 +00001877 search_bytes = min(search_bytes, end - search_start + 1);
Josef Bacik0584f712015-10-02 16:12:23 -04001878 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1879 false);
Josef Bacikb50c6e22013-04-25 15:55:30 -04001880 if (ret < 0 || search_start != *offset)
1881 return -EINVAL;
Josef Bacik6606bb92009-07-31 11:03:58 -04001882
Josef Bacikbdb7d302012-06-27 15:10:56 -04001883 /* We may have found more bits than what we need */
1884 search_bytes = min(search_bytes, *bytes);
1885
1886 /* Cannot clear past the end of the bitmap */
1887 search_bytes = min(search_bytes, end - search_start + 1);
1888
1889 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1890 *offset += search_bytes;
1891 *bytes -= search_bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001892
1893 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001894 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001895 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001896 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001897
Josef Bacik6606bb92009-07-31 11:03:58 -04001898 /*
1899 * no entry after this bitmap, but we still have bytes to
1900 * remove, so something has gone wrong.
1901 */
1902 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001903 return -EINVAL;
1904
Josef Bacik6606bb92009-07-31 11:03:58 -04001905 bitmap_info = rb_entry(next, struct btrfs_free_space,
1906 offset_index);
1907
1908 /*
1909 * if the next entry isn't a bitmap we need to return to let the
1910 * extent stuff do its work.
1911 */
Josef Bacik96303082009-07-13 21:29:25 -04001912 if (!bitmap_info->bitmap)
1913 return -EAGAIN;
1914
Josef Bacik6606bb92009-07-31 11:03:58 -04001915 /*
1916 * Ok the next item is a bitmap, but it may not actually hold
1917 * the information for the rest of this free space stuff, so
1918 * look for it, and if we don't find it return so we can try
1919 * everything over again.
1920 */
1921 search_start = *offset;
Josef Bacikbdb7d302012-06-27 15:10:56 -04001922 search_bytes = ctl->unit;
Li Zefan34d52cb2011-03-29 13:46:06 +08001923 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik0584f712015-10-02 16:12:23 -04001924 &search_bytes, false);
Josef Bacik6606bb92009-07-31 11:03:58 -04001925 if (ret < 0 || search_start != *offset)
1926 return -EAGAIN;
1927
Josef Bacik96303082009-07-13 21:29:25 -04001928 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001929 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001930 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001931
1932 return 0;
1933}
1934
Josef Bacik2cdc3422011-05-27 14:07:49 -04001935static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1936 struct btrfs_free_space *info, u64 offset,
1937 u64 bytes)
1938{
1939 u64 bytes_to_set = 0;
1940 u64 end;
1941
1942 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1943
1944 bytes_to_set = min(end - offset, bytes);
1945
1946 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1947
Josef Bacikcef40482015-10-02 16:09:42 -04001948 /*
1949 * We set some bytes, we have no idea what the max extent size is
1950 * anymore.
1951 */
1952 info->max_extent_size = 0;
1953
Josef Bacik2cdc3422011-05-27 14:07:49 -04001954 return bytes_to_set;
1955
1956}
1957
Li Zefan34d52cb2011-03-29 13:46:06 +08001958static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1959 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001960{
Li Zefan34d52cb2011-03-29 13:46:06 +08001961 struct btrfs_block_group_cache *block_group = ctl->private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001962 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -04001963 bool forced = false;
1964
1965#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001966 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -04001967 forced = true;
1968#endif
Josef Bacik96303082009-07-13 21:29:25 -04001969
1970 /*
1971 * If we are below the extents threshold then we can add this as an
1972 * extent, and don't have to deal with the bitmap
1973 */
Josef Bacikd0bd4562015-09-23 14:54:14 -04001974 if (!forced && ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001975 /*
1976 * If this block group has some small extents we don't want to
1977 * use up all of our free slots in the cache with them, we want
Nicholas D Steeves01327612016-05-19 21:18:45 -04001978 * to reserve them to larger extents, however if we have plenty
Josef Bacik32cb0842011-03-18 16:16:21 -04001979 * of cache left then go ahead an dadd them, no sense in adding
1980 * the overhead of a bitmap if we don't have to.
1981 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001982 if (info->bytes <= fs_info->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001983 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1984 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001985 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001986 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001987 }
1988 }
Josef Bacik96303082009-07-13 21:29:25 -04001989
1990 /*
Josef Bacikdde57402013-02-12 14:07:51 -05001991 * The original block groups from mkfs can be really small, like 8
1992 * megabytes, so don't bother with a bitmap for those entries. However
1993 * some block groups can be smaller than what a bitmap would cover but
1994 * are still large enough that they could overflow the 32k memory limit,
1995 * so allow those block groups to still be allowed to have a bitmap
1996 * entry.
Josef Bacik96303082009-07-13 21:29:25 -04001997 */
Josef Bacikdde57402013-02-12 14:07:51 -05001998 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08001999 return false;
2000
2001 return true;
2002}
2003
David Sterba20e55062015-11-19 11:42:28 +01002004static const struct btrfs_free_space_op free_space_op = {
Josef Bacik2cdc3422011-05-27 14:07:49 -04002005 .recalc_thresholds = recalculate_thresholds,
2006 .use_bitmap = use_bitmap,
2007};
2008
Li Zefan34d52cb2011-03-29 13:46:06 +08002009static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2010 struct btrfs_free_space *info)
2011{
2012 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002013 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08002014 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002015 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08002016 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002017
2018 bytes = info->bytes;
2019 offset = info->offset;
2020
Li Zefan34d52cb2011-03-29 13:46:06 +08002021 if (!ctl->op->use_bitmap(ctl, info))
2022 return 0;
2023
Josef Bacik2cdc3422011-05-27 14:07:49 -04002024 if (ctl->op == &free_space_op)
2025 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04002026again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04002027 /*
2028 * Since we link bitmaps right into the cluster we need to see if we
2029 * have a cluster here, and if so and it has our bitmap we need to add
2030 * the free space to that bitmap.
2031 */
2032 if (block_group && !list_empty(&block_group->cluster_list)) {
2033 struct btrfs_free_cluster *cluster;
2034 struct rb_node *node;
2035 struct btrfs_free_space *entry;
2036
2037 cluster = list_entry(block_group->cluster_list.next,
2038 struct btrfs_free_cluster,
2039 block_group_list);
2040 spin_lock(&cluster->lock);
2041 node = rb_first(&cluster->root);
2042 if (!node) {
2043 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002044 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002045 }
2046
2047 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2048 if (!entry->bitmap) {
2049 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04002050 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04002051 }
2052
2053 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2054 bytes_added = add_bytes_to_bitmap(ctl, entry,
2055 offset, bytes);
2056 bytes -= bytes_added;
2057 offset += bytes_added;
2058 }
2059 spin_unlock(&cluster->lock);
2060 if (!bytes) {
2061 ret = 1;
2062 goto out;
2063 }
2064 }
Chris Mason38e87882011-06-10 16:36:57 -04002065
2066no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08002067 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04002068 1, 0);
2069 if (!bitmap_info) {
Josef Bacikb12d6862013-08-26 17:14:08 -04002070 ASSERT(added == 0);
Josef Bacik96303082009-07-13 21:29:25 -04002071 goto new_bitmap;
2072 }
2073
Josef Bacik2cdc3422011-05-27 14:07:49 -04002074 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2075 bytes -= bytes_added;
2076 offset += bytes_added;
2077 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002078
2079 if (!bytes) {
2080 ret = 1;
2081 goto out;
2082 } else
2083 goto again;
2084
2085new_bitmap:
2086 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002087 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04002088 added = 1;
2089 info = NULL;
2090 goto again;
2091 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002092 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002093
2094 /* no pre-allocated info, allocate a new one */
2095 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05002096 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2097 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04002098 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002099 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002100 ret = -ENOMEM;
2101 goto out;
2102 }
2103 }
2104
2105 /* allocate the bitmap */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002106 info->bitmap = kzalloc(PAGE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08002107 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002108 if (!info->bitmap) {
2109 ret = -ENOMEM;
2110 goto out;
2111 }
2112 goto again;
2113 }
2114
2115out:
2116 if (info) {
2117 if (info->bitmap)
2118 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05002119 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002120 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002121
2122 return ret;
2123}
2124
Chris Mason945d8962011-05-22 12:33:42 -04002125static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08002126 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002127{
Li Zefan120d66e2010-11-09 14:56:50 +08002128 struct btrfs_free_space *left_info;
2129 struct btrfs_free_space *right_info;
2130 bool merged = false;
2131 u64 offset = info->offset;
2132 u64 bytes = info->bytes;
Josef Bacik6226cb02009-04-03 10:14:18 -04002133
Josef Bacik0f9dd462008-09-23 13:14:11 -04002134 /*
2135 * first we want to see if there is free space adjacent to the range we
2136 * are adding, if there is remove that struct and add a new one to
2137 * cover the entire range
2138 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002139 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002140 if (right_info && rb_prev(&right_info->offset_index))
2141 left_info = rb_entry(rb_prev(&right_info->offset_index),
2142 struct btrfs_free_space, offset_index);
2143 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002144 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002145
Josef Bacik96303082009-07-13 21:29:25 -04002146 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08002147 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002148 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002149 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002150 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002151 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002152 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002153 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002154 }
2155
Josef Bacik96303082009-07-13 21:29:25 -04002156 if (left_info && !left_info->bitmap &&
2157 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08002158 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08002159 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08002160 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002161 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04002162 info->offset = left_info->offset;
2163 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05002164 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08002165 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002166 }
2167
Li Zefan120d66e2010-11-09 14:56:50 +08002168 return merged;
2169}
2170
Filipe Manana20005522014-08-29 13:35:13 +01002171static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2172 struct btrfs_free_space *info,
2173 bool update_stat)
2174{
2175 struct btrfs_free_space *bitmap;
2176 unsigned long i;
2177 unsigned long j;
2178 const u64 end = info->offset + info->bytes;
2179 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2180 u64 bytes;
2181
2182 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2183 if (!bitmap)
2184 return false;
2185
2186 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2187 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2188 if (j == i)
2189 return false;
2190 bytes = (j - i) * ctl->unit;
2191 info->bytes += bytes;
2192
2193 if (update_stat)
2194 bitmap_clear_bits(ctl, bitmap, end, bytes);
2195 else
2196 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2197
2198 if (!bitmap->bytes)
2199 free_bitmap(ctl, bitmap);
2200
2201 return true;
2202}
2203
2204static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2205 struct btrfs_free_space *info,
2206 bool update_stat)
2207{
2208 struct btrfs_free_space *bitmap;
2209 u64 bitmap_offset;
2210 unsigned long i;
2211 unsigned long j;
2212 unsigned long prev_j;
2213 u64 bytes;
2214
2215 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2216 /* If we're on a boundary, try the previous logical bitmap. */
2217 if (bitmap_offset == info->offset) {
2218 if (info->offset == 0)
2219 return false;
2220 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2221 }
2222
2223 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2224 if (!bitmap)
2225 return false;
2226
2227 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2228 j = 0;
2229 prev_j = (unsigned long)-1;
2230 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2231 if (j > i)
2232 break;
2233 prev_j = j;
2234 }
2235 if (prev_j == i)
2236 return false;
2237
2238 if (prev_j == (unsigned long)-1)
2239 bytes = (i + 1) * ctl->unit;
2240 else
2241 bytes = (i - prev_j) * ctl->unit;
2242
2243 info->offset -= bytes;
2244 info->bytes += bytes;
2245
2246 if (update_stat)
2247 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2248 else
2249 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2250
2251 if (!bitmap->bytes)
2252 free_bitmap(ctl, bitmap);
2253
2254 return true;
2255}
2256
2257/*
2258 * We prefer always to allocate from extent entries, both for clustered and
2259 * non-clustered allocation requests. So when attempting to add a new extent
2260 * entry, try to see if there's adjacent free space in bitmap entries, and if
2261 * there is, migrate that space from the bitmaps to the extent.
2262 * Like this we get better chances of satisfying space allocation requests
2263 * because we attempt to satisfy them based on a single cache entry, and never
2264 * on 2 or more entries - even if the entries represent a contiguous free space
2265 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2266 * ends).
2267 */
2268static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2269 struct btrfs_free_space *info,
2270 bool update_stat)
2271{
2272 /*
2273 * Only work with disconnected entries, as we can change their offset,
2274 * and must be extent entries.
2275 */
2276 ASSERT(!info->bitmap);
2277 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2278
2279 if (ctl->total_bitmaps > 0) {
2280 bool stole_end;
2281 bool stole_front = false;
2282
2283 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2284 if (ctl->total_bitmaps > 0)
2285 stole_front = steal_from_bitmap_to_front(ctl, info,
2286 update_stat);
2287
2288 if (stole_end || stole_front)
2289 try_merge_free_space(ctl, info, update_stat);
2290 }
2291}
2292
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002293int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2294 struct btrfs_free_space_ctl *ctl,
Li Zefan581bb052011-04-20 10:06:11 +08002295 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08002296{
2297 struct btrfs_free_space *info;
2298 int ret = 0;
2299
Josef Bacikdc89e982011-01-28 17:05:48 -05002300 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08002301 if (!info)
2302 return -ENOMEM;
2303
2304 info->offset = offset;
2305 info->bytes = bytes;
Filipe Manana20005522014-08-29 13:35:13 +01002306 RB_CLEAR_NODE(&info->offset_index);
Li Zefan120d66e2010-11-09 14:56:50 +08002307
Li Zefan34d52cb2011-03-29 13:46:06 +08002308 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08002309
Li Zefan34d52cb2011-03-29 13:46:06 +08002310 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08002311 goto link;
2312
2313 /*
2314 * There was no extent directly to the left or right of this new
2315 * extent then we know we're going to have to allocate a new extent, so
2316 * before we do that see if we need to drop this into a bitmap
2317 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002318 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08002319 if (ret < 0) {
2320 goto out;
2321 } else if (ret) {
2322 ret = 0;
2323 goto out;
2324 }
2325link:
Filipe Manana20005522014-08-29 13:35:13 +01002326 /*
2327 * Only steal free space from adjacent bitmaps if we're sure we're not
2328 * going to add the new free space to existing bitmap entries - because
2329 * that would mean unnecessary work that would be reverted. Therefore
2330 * attempt to steal space from bitmaps if we're adding an extent entry.
2331 */
2332 steal_from_bitmap(ctl, info, true);
2333
Li Zefan34d52cb2011-03-29 13:46:06 +08002334 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002335 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05002336 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04002337out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002338 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002339
Josef Bacik0f9dd462008-09-23 13:14:11 -04002340 if (ret) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002341 btrfs_crit(fs_info, "unable to add free space :%d", ret);
Josef Bacikb12d6862013-08-26 17:14:08 -04002342 ASSERT(ret != -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002343 }
2344
Josef Bacik0f9dd462008-09-23 13:14:11 -04002345 return ret;
2346}
2347
Josef Bacik6226cb02009-04-03 10:14:18 -04002348int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2349 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002350{
Li Zefan34d52cb2011-03-29 13:46:06 +08002351 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002352 struct btrfs_free_space *info;
Josef Bacikb0175112012-12-18 11:39:19 -05002353 int ret;
2354 bool re_search = false;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002355
Li Zefan34d52cb2011-03-29 13:46:06 +08002356 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04002357
Josef Bacik96303082009-07-13 21:29:25 -04002358again:
Josef Bacikb0175112012-12-18 11:39:19 -05002359 ret = 0;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002360 if (!bytes)
2361 goto out_lock;
2362
Li Zefan34d52cb2011-03-29 13:46:06 +08002363 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04002364 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04002365 /*
2366 * oops didn't find an extent that matched the space we wanted
2367 * to remove, look for a bitmap instead
2368 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002369 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04002370 1, 0);
2371 if (!info) {
Josef Bacikb0175112012-12-18 11:39:19 -05002372 /*
2373 * If we found a partial bit of our free space in a
2374 * bitmap but then couldn't find the other part this may
2375 * be a problem, so WARN about it.
Chris Mason24a70312011-11-21 09:39:11 -05002376 */
Josef Bacikb0175112012-12-18 11:39:19 -05002377 WARN_ON(re_search);
Josef Bacik6606bb92009-07-31 11:03:58 -04002378 goto out_lock;
2379 }
Josef Bacik96303082009-07-13 21:29:25 -04002380 }
2381
Josef Bacikb0175112012-12-18 11:39:19 -05002382 re_search = false;
Josef Bacikbdb7d302012-06-27 15:10:56 -04002383 if (!info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002384 unlink_free_space(ctl, info);
Josef Bacikbdb7d302012-06-27 15:10:56 -04002385 if (offset == info->offset) {
2386 u64 to_free = min(bytes, info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002387
Josef Bacikbdb7d302012-06-27 15:10:56 -04002388 info->bytes -= to_free;
2389 info->offset += to_free;
2390 if (info->bytes) {
2391 ret = link_free_space(ctl, info);
2392 WARN_ON(ret);
2393 } else {
2394 kmem_cache_free(btrfs_free_space_cachep, info);
2395 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002396
Josef Bacikbdb7d302012-06-27 15:10:56 -04002397 offset += to_free;
2398 bytes -= to_free;
2399 goto again;
2400 } else {
2401 u64 old_end = info->bytes + info->offset;
Chris Mason9b49c9b2008-09-24 11:23:25 -04002402
Josef Bacikbdb7d302012-06-27 15:10:56 -04002403 info->bytes = offset - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002404 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04002405 WARN_ON(ret);
2406 if (ret)
2407 goto out_lock;
Josef Bacik96303082009-07-13 21:29:25 -04002408
Josef Bacikbdb7d302012-06-27 15:10:56 -04002409 /* Not enough bytes in this entry to satisfy us */
2410 if (old_end < offset + bytes) {
2411 bytes -= old_end - offset;
2412 offset = old_end;
2413 goto again;
2414 } else if (old_end == offset + bytes) {
2415 /* all done */
2416 goto out_lock;
2417 }
2418 spin_unlock(&ctl->tree_lock);
2419
2420 ret = btrfs_add_free_space(block_group, offset + bytes,
2421 old_end - (offset + bytes));
2422 WARN_ON(ret);
2423 goto out;
2424 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002425 }
Josef Bacik96303082009-07-13 21:29:25 -04002426
Li Zefan34d52cb2011-03-29 13:46:06 +08002427 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacikb0175112012-12-18 11:39:19 -05002428 if (ret == -EAGAIN) {
2429 re_search = true;
Josef Bacik96303082009-07-13 21:29:25 -04002430 goto again;
Josef Bacikb0175112012-12-18 11:39:19 -05002431 }
Josef Bacik96303082009-07-13 21:29:25 -04002432out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08002433 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002434out:
Josef Bacik25179202008-10-29 14:49:05 -04002435 return ret;
2436}
2437
Josef Bacik0f9dd462008-09-23 13:14:11 -04002438void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2439 u64 bytes)
2440{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002441 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002442 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002443 struct btrfs_free_space *info;
2444 struct rb_node *n;
2445 int count = 0;
2446
Li Zefan34d52cb2011-03-29 13:46:06 +08002447 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002448 info = rb_entry(n, struct btrfs_free_space, offset_index);
Liu Bof6175ef2012-07-06 03:31:36 -06002449 if (info->bytes >= bytes && !block_group->ro)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002450 count++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002451 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
Frank Holtonefe120a2013-12-20 11:37:06 -05002452 info->offset, info->bytes,
Josef Bacik96303082009-07-13 21:29:25 -04002453 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002454 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002455 btrfs_info(fs_info, "block group has cluster?: %s",
Josef Bacik96303082009-07-13 21:29:25 -04002456 list_empty(&block_group->cluster_list) ? "no" : "yes");
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002457 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002458 "%d blocks of free space at or bigger than bytes is", count);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002459}
2460
Li Zefan34d52cb2011-03-29 13:46:06 +08002461void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002462{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002463 struct btrfs_fs_info *fs_info = block_group->fs_info;
Li Zefan34d52cb2011-03-29 13:46:06 +08002464 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002465
Li Zefan34d52cb2011-03-29 13:46:06 +08002466 spin_lock_init(&ctl->tree_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002467 ctl->unit = fs_info->sectorsize;
Li Zefan34d52cb2011-03-29 13:46:06 +08002468 ctl->start = block_group->key.objectid;
2469 ctl->private = block_group;
2470 ctl->op = &free_space_op;
Filipe Manana55507ce2014-12-01 17:04:09 +00002471 INIT_LIST_HEAD(&ctl->trimming_ranges);
2472 mutex_init(&ctl->cache_writeout_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002473
Li Zefan34d52cb2011-03-29 13:46:06 +08002474 /*
2475 * we only want to have 32k of ram per block group for keeping
2476 * track of free space, and if we pass 1/2 of that we want to
2477 * start converting things over to using bitmaps
2478 */
Byongho Leeee221842015-12-15 01:42:10 +09002479 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002480}
2481
Chris Masonfa9c0d792009-04-03 09:47:43 -04002482/*
2483 * for a given cluster, put all of its extents back into the free
2484 * space cache. If the block group passed doesn't match the block group
2485 * pointed to by the cluster, someone else raced in and freed the
2486 * cluster already. In that case, we just return without changing anything
2487 */
2488static int
2489__btrfs_return_cluster_to_free_space(
2490 struct btrfs_block_group_cache *block_group,
2491 struct btrfs_free_cluster *cluster)
2492{
Li Zefan34d52cb2011-03-29 13:46:06 +08002493 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002494 struct btrfs_free_space *entry;
2495 struct rb_node *node;
2496
2497 spin_lock(&cluster->lock);
2498 if (cluster->block_group != block_group)
2499 goto out;
2500
Josef Bacik96303082009-07-13 21:29:25 -04002501 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002502 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002503 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002504
Chris Masonfa9c0d792009-04-03 09:47:43 -04002505 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002506 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002507 bool bitmap;
2508
Chris Masonfa9c0d792009-04-03 09:47:43 -04002509 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2510 node = rb_next(&entry->offset_index);
2511 rb_erase(&entry->offset_index, &cluster->root);
Filipe Manana20005522014-08-29 13:35:13 +01002512 RB_CLEAR_NODE(&entry->offset_index);
Josef Bacik4e69b592011-03-21 10:11:24 -04002513
2514 bitmap = (entry->bitmap != NULL);
Filipe Manana20005522014-08-29 13:35:13 +01002515 if (!bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002516 try_merge_free_space(ctl, entry, false);
Filipe Manana20005522014-08-29 13:35:13 +01002517 steal_from_bitmap(ctl, entry, false);
2518 }
Li Zefan34d52cb2011-03-29 13:46:06 +08002519 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002520 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002521 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002522 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002523
Chris Masonfa9c0d792009-04-03 09:47:43 -04002524out:
2525 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002526 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002527 return 0;
2528}
2529
Eric Sandeen48a3b632013-04-25 20:41:01 +00002530static void __btrfs_remove_free_space_cache_locked(
2531 struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002532{
2533 struct btrfs_free_space *info;
2534 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002535
Li Zefan581bb052011-04-20 10:06:11 +08002536 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2537 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002538 if (!info->bitmap) {
2539 unlink_free_space(ctl, info);
2540 kmem_cache_free(btrfs_free_space_cachep, info);
2541 } else {
2542 free_bitmap(ctl, info);
2543 }
David Sterba351810c2015-01-08 15:20:54 +01002544
2545 cond_resched_lock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08002546 }
Chris Mason09655372011-05-21 09:27:38 -04002547}
2548
2549void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2550{
2551 spin_lock(&ctl->tree_lock);
2552 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002553 spin_unlock(&ctl->tree_lock);
2554}
2555
Josef Bacik0f9dd462008-09-23 13:14:11 -04002556void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2557{
Li Zefan34d52cb2011-03-29 13:46:06 +08002558 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002559 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002560 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002561
Li Zefan34d52cb2011-03-29 13:46:06 +08002562 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002563 while ((head = block_group->cluster_list.next) !=
2564 &block_group->cluster_list) {
2565 cluster = list_entry(head, struct btrfs_free_cluster,
2566 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002567
2568 WARN_ON(cluster->block_group != block_group);
2569 __btrfs_return_cluster_to_free_space(block_group, cluster);
David Sterba351810c2015-01-08 15:20:54 +01002570
2571 cond_resched_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002572 }
Chris Mason09655372011-05-21 09:27:38 -04002573 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002574 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002575
Josef Bacik0f9dd462008-09-23 13:14:11 -04002576}
2577
Josef Bacik6226cb02009-04-03 10:14:18 -04002578u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
Miao Xiea4820392013-09-09 13:19:42 +08002579 u64 offset, u64 bytes, u64 empty_size,
2580 u64 *max_extent_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002581{
Li Zefan34d52cb2011-03-29 13:46:06 +08002582 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb02009-04-03 10:14:18 -04002583 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002584 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb02009-04-03 10:14:18 -04002585 u64 ret = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002586 u64 align_gap = 0;
2587 u64 align_gap_len = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002588
Li Zefan34d52cb2011-03-29 13:46:06 +08002589 spin_lock(&ctl->tree_lock);
David Woodhouse53b381b2013-01-29 18:40:14 -05002590 entry = find_free_space(ctl, &offset, &bytes_search,
Miao Xiea4820392013-09-09 13:19:42 +08002591 block_group->full_stripe_len, max_extent_size);
Josef Bacik6226cb02009-04-03 10:14:18 -04002592 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002593 goto out;
2594
2595 ret = offset;
2596 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002597 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002598 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002599 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002600 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002601 unlink_free_space(ctl, entry);
David Woodhouse53b381b2013-01-29 18:40:14 -05002602 align_gap_len = offset - entry->offset;
2603 align_gap = entry->offset;
2604
2605 entry->offset = offset + bytes;
2606 WARN_ON(entry->bytes < bytes + align_gap_len);
2607
2608 entry->bytes -= bytes + align_gap_len;
Josef Bacik6226cb02009-04-03 10:14:18 -04002609 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002610 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002611 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002612 link_free_space(ctl, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04002613 }
Josef Bacik96303082009-07-13 21:29:25 -04002614out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002615 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002616
David Woodhouse53b381b2013-01-29 18:40:14 -05002617 if (align_gap_len)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002618 __btrfs_add_free_space(block_group->fs_info, ctl,
2619 align_gap, align_gap_len);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002620 return ret;
2621}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002622
2623/*
2624 * given a cluster, put all of its extents back into the free space
2625 * cache. If a block group is passed, this function will only free
2626 * a cluster that belongs to the passed block group.
2627 *
2628 * Otherwise, it'll get a reference on the block group pointed to by the
2629 * cluster and remove the cluster from it.
2630 */
2631int btrfs_return_cluster_to_free_space(
2632 struct btrfs_block_group_cache *block_group,
2633 struct btrfs_free_cluster *cluster)
2634{
Li Zefan34d52cb2011-03-29 13:46:06 +08002635 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002636 int ret;
2637
2638 /* first, get a safe pointer to the block group */
2639 spin_lock(&cluster->lock);
2640 if (!block_group) {
2641 block_group = cluster->block_group;
2642 if (!block_group) {
2643 spin_unlock(&cluster->lock);
2644 return 0;
2645 }
2646 } else if (cluster->block_group != block_group) {
2647 /* someone else has already freed it don't redo their work */
2648 spin_unlock(&cluster->lock);
2649 return 0;
2650 }
2651 atomic_inc(&block_group->count);
2652 spin_unlock(&cluster->lock);
2653
Li Zefan34d52cb2011-03-29 13:46:06 +08002654 ctl = block_group->free_space_ctl;
2655
Chris Masonfa9c0d792009-04-03 09:47:43 -04002656 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002657 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002658 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002659 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002660
2661 /* finally drop our ref */
2662 btrfs_put_block_group(block_group);
2663 return ret;
2664}
2665
Josef Bacik96303082009-07-13 21:29:25 -04002666static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2667 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002668 struct btrfs_free_space *entry,
Miao Xiea4820392013-09-09 13:19:42 +08002669 u64 bytes, u64 min_start,
2670 u64 *max_extent_size)
Josef Bacik96303082009-07-13 21:29:25 -04002671{
Li Zefan34d52cb2011-03-29 13:46:06 +08002672 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002673 int err;
2674 u64 search_start = cluster->window_start;
2675 u64 search_bytes = bytes;
2676 u64 ret = 0;
2677
Josef Bacik96303082009-07-13 21:29:25 -04002678 search_start = min_start;
2679 search_bytes = bytes;
2680
Josef Bacik0584f712015-10-02 16:12:23 -04002681 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
Miao Xiea4820392013-09-09 13:19:42 +08002682 if (err) {
2683 if (search_bytes > *max_extent_size)
2684 *max_extent_size = search_bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002685 return 0;
Miao Xiea4820392013-09-09 13:19:42 +08002686 }
Josef Bacik96303082009-07-13 21:29:25 -04002687
2688 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002689 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002690
2691 return ret;
2692}
2693
Chris Masonfa9c0d792009-04-03 09:47:43 -04002694/*
2695 * given a cluster, try to allocate 'bytes' from it, returns 0
2696 * if it couldn't find anything suitably large, or a logical disk offset
2697 * if things worked out
2698 */
2699u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2700 struct btrfs_free_cluster *cluster, u64 bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002701 u64 min_start, u64 *max_extent_size)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002702{
Li Zefan34d52cb2011-03-29 13:46:06 +08002703 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002704 struct btrfs_free_space *entry = NULL;
2705 struct rb_node *node;
2706 u64 ret = 0;
2707
2708 spin_lock(&cluster->lock);
2709 if (bytes > cluster->max_size)
2710 goto out;
2711
2712 if (cluster->block_group != block_group)
2713 goto out;
2714
2715 node = rb_first(&cluster->root);
2716 if (!node)
2717 goto out;
2718
2719 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302720 while (1) {
Miao Xiea4820392013-09-09 13:19:42 +08002721 if (entry->bytes < bytes && entry->bytes > *max_extent_size)
2722 *max_extent_size = entry->bytes;
2723
Josef Bacik4e69b592011-03-21 10:11:24 -04002724 if (entry->bytes < bytes ||
2725 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002726 node = rb_next(&entry->offset_index);
2727 if (!node)
2728 break;
2729 entry = rb_entry(node, struct btrfs_free_space,
2730 offset_index);
2731 continue;
2732 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002733
Josef Bacik4e69b592011-03-21 10:11:24 -04002734 if (entry->bitmap) {
2735 ret = btrfs_alloc_from_bitmap(block_group,
2736 cluster, entry, bytes,
Miao Xiea4820392013-09-09 13:19:42 +08002737 cluster->window_start,
2738 max_extent_size);
Josef Bacik4e69b592011-03-21 10:11:24 -04002739 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002740 node = rb_next(&entry->offset_index);
2741 if (!node)
2742 break;
2743 entry = rb_entry(node, struct btrfs_free_space,
2744 offset_index);
2745 continue;
2746 }
Josef Bacik9b230622012-01-26 15:01:12 -05002747 cluster->window_start += bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002748 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002749 ret = entry->offset;
2750
2751 entry->offset += bytes;
2752 entry->bytes -= bytes;
2753 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002754
Li Zefan5e71b5d2010-11-09 14:55:34 +08002755 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002756 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002757 break;
2758 }
2759out:
2760 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002761
Li Zefan5e71b5d2010-11-09 14:55:34 +08002762 if (!ret)
2763 return 0;
2764
Li Zefan34d52cb2011-03-29 13:46:06 +08002765 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002766
Li Zefan34d52cb2011-03-29 13:46:06 +08002767 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002768 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002769 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002770 if (entry->bitmap) {
2771 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002772 ctl->total_bitmaps--;
2773 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002774 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002775 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002776 }
2777
Li Zefan34d52cb2011-03-29 13:46:06 +08002778 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002779
Chris Masonfa9c0d792009-04-03 09:47:43 -04002780 return ret;
2781}
2782
Josef Bacik96303082009-07-13 21:29:25 -04002783static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2784 struct btrfs_free_space *entry,
2785 struct btrfs_free_cluster *cluster,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002786 u64 offset, u64 bytes,
2787 u64 cont1_bytes, u64 min_bytes)
Josef Bacik96303082009-07-13 21:29:25 -04002788{
Li Zefan34d52cb2011-03-29 13:46:06 +08002789 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002790 unsigned long next_zero;
2791 unsigned long i;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002792 unsigned long want_bits;
2793 unsigned long min_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002794 unsigned long found_bits;
Josef Bacikcef40482015-10-02 16:09:42 -04002795 unsigned long max_bits = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002796 unsigned long start = 0;
2797 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002798 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002799
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002800 i = offset_to_bit(entry->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04002801 max_t(u64, offset, entry->offset));
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002802 want_bits = bytes_to_bits(bytes, ctl->unit);
2803 min_bits = bytes_to_bits(min_bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04002804
Josef Bacikcef40482015-10-02 16:09:42 -04002805 /*
2806 * Don't bother looking for a cluster in this bitmap if it's heavily
2807 * fragmented.
2808 */
2809 if (entry->max_extent_size &&
2810 entry->max_extent_size < cont1_bytes)
2811 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002812again:
2813 found_bits = 0;
Wei Yongjunebb3dad2012-09-13 20:29:02 -06002814 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
Josef Bacik96303082009-07-13 21:29:25 -04002815 next_zero = find_next_zero_bit(entry->bitmap,
2816 BITS_PER_BITMAP, i);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002817 if (next_zero - i >= min_bits) {
Josef Bacik96303082009-07-13 21:29:25 -04002818 found_bits = next_zero - i;
Josef Bacikcef40482015-10-02 16:09:42 -04002819 if (found_bits > max_bits)
2820 max_bits = found_bits;
Josef Bacik96303082009-07-13 21:29:25 -04002821 break;
2822 }
Josef Bacikcef40482015-10-02 16:09:42 -04002823 if (next_zero - i > max_bits)
2824 max_bits = next_zero - i;
Josef Bacik96303082009-07-13 21:29:25 -04002825 i = next_zero;
2826 }
2827
Josef Bacikcef40482015-10-02 16:09:42 -04002828 if (!found_bits) {
2829 entry->max_extent_size = (u64)max_bits * ctl->unit;
Josef Bacik4e69b592011-03-21 10:11:24 -04002830 return -ENOSPC;
Josef Bacikcef40482015-10-02 16:09:42 -04002831 }
Josef Bacik96303082009-07-13 21:29:25 -04002832
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002833 if (!total_found) {
Josef Bacik96303082009-07-13 21:29:25 -04002834 start = i;
Alexandre Olivab78d09b2011-11-30 13:43:00 -05002835 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002836 }
2837
2838 total_found += found_bits;
2839
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002840 if (cluster->max_size < found_bits * ctl->unit)
2841 cluster->max_size = found_bits * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04002842
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002843 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2844 i = next_zero + 1;
Josef Bacik96303082009-07-13 21:29:25 -04002845 goto again;
2846 }
2847
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002848 cluster->window_start = start * ctl->unit + entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002849 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002850 ret = tree_insert_offset(&cluster->root, entry->offset,
2851 &entry->offset_index, 1);
Josef Bacikb12d6862013-08-26 17:14:08 -04002852 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik96303082009-07-13 21:29:25 -04002853
Josef Bacik3f7de032011-11-10 08:29:20 -05002854 trace_btrfs_setup_cluster(block_group, cluster,
Wang Sheng-Hui96009762012-11-30 06:30:14 +00002855 total_found * ctl->unit, 1);
Josef Bacik96303082009-07-13 21:29:25 -04002856 return 0;
2857}
2858
Chris Masonfa9c0d792009-04-03 09:47:43 -04002859/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002860 * This searches the block group for just extents to fill the cluster with.
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002861 * Try to find a cluster with at least bytes total bytes, at least one
2862 * extent of cont1_bytes, and other clusters of at least min_bytes.
Josef Bacik4e69b592011-03-21 10:11:24 -04002863 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002864static noinline int
2865setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2866 struct btrfs_free_cluster *cluster,
2867 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002868 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002869{
Li Zefan34d52cb2011-03-29 13:46:06 +08002870 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002871 struct btrfs_free_space *first = NULL;
2872 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002873 struct btrfs_free_space *last;
2874 struct rb_node *node;
Josef Bacik4e69b592011-03-21 10:11:24 -04002875 u64 window_free;
2876 u64 max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002877 u64 total_size = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002878
Li Zefan34d52cb2011-03-29 13:46:06 +08002879 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002880 if (!entry)
2881 return -ENOSPC;
2882
2883 /*
2884 * We don't want bitmaps, so just move along until we find a normal
2885 * extent entry.
2886 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002887 while (entry->bitmap || entry->bytes < min_bytes) {
2888 if (entry->bitmap && list_empty(&entry->list))
Josef Bacik86d4a772011-05-25 13:03:16 -04002889 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002890 node = rb_next(&entry->offset_index);
2891 if (!node)
2892 return -ENOSPC;
2893 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2894 }
2895
Josef Bacik4e69b592011-03-21 10:11:24 -04002896 window_free = entry->bytes;
2897 max_extent = entry->bytes;
2898 first = entry;
2899 last = entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002900
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002901 for (node = rb_next(&entry->offset_index); node;
2902 node = rb_next(&entry->offset_index)) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002903 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2904
Josef Bacik86d4a772011-05-25 13:03:16 -04002905 if (entry->bitmap) {
2906 if (list_empty(&entry->list))
2907 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002908 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002909 }
2910
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002911 if (entry->bytes < min_bytes)
2912 continue;
2913
2914 last = entry;
2915 window_free += entry->bytes;
2916 if (entry->bytes > max_extent)
Josef Bacik4e69b592011-03-21 10:11:24 -04002917 max_extent = entry->bytes;
Josef Bacik4e69b592011-03-21 10:11:24 -04002918 }
2919
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002920 if (window_free < bytes || max_extent < cont1_bytes)
2921 return -ENOSPC;
2922
Josef Bacik4e69b592011-03-21 10:11:24 -04002923 cluster->window_start = first->offset;
2924
2925 node = &first->offset_index;
2926
2927 /*
2928 * now we've found our entries, pull them out of the free space
2929 * cache and put them into the cluster rbtree
2930 */
2931 do {
2932 int ret;
2933
2934 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2935 node = rb_next(&entry->offset_index);
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002936 if (entry->bitmap || entry->bytes < min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002937 continue;
2938
Li Zefan34d52cb2011-03-29 13:46:06 +08002939 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002940 ret = tree_insert_offset(&cluster->root, entry->offset,
2941 &entry->offset_index, 0);
Josef Bacik3f7de032011-11-10 08:29:20 -05002942 total_size += entry->bytes;
Josef Bacikb12d6862013-08-26 17:14:08 -04002943 ASSERT(!ret); /* -EEXIST; Logic error */
Josef Bacik4e69b592011-03-21 10:11:24 -04002944 } while (node && entry != last);
2945
2946 cluster->max_size = max_extent;
Josef Bacik3f7de032011-11-10 08:29:20 -05002947 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
Josef Bacik4e69b592011-03-21 10:11:24 -04002948 return 0;
2949}
2950
2951/*
2952 * This specifically looks for bitmaps that may work in the cluster, we assume
2953 * that we have already failed to find extents that will work.
2954 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002955static noinline int
2956setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2957 struct btrfs_free_cluster *cluster,
2958 struct list_head *bitmaps, u64 offset, u64 bytes,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002959 u64 cont1_bytes, u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002960{
Li Zefan34d52cb2011-03-29 13:46:06 +08002961 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Mason1b9b9222015-12-15 07:15:32 -08002962 struct btrfs_free_space *entry = NULL;
Josef Bacik4e69b592011-03-21 10:11:24 -04002963 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05002964 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002965
Li Zefan34d52cb2011-03-29 13:46:06 +08002966 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002967 return -ENOSPC;
2968
Josef Bacik86d4a772011-05-25 13:03:16 -04002969 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05002970 * The bitmap that covers offset won't be in the list unless offset
2971 * is just its start offset.
2972 */
Chris Mason1b9b9222015-12-15 07:15:32 -08002973 if (!list_empty(bitmaps))
2974 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
2975
2976 if (!entry || entry->offset != bitmap_offset) {
Li Zefan0f0fbf12011-11-20 07:33:38 -05002977 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
2978 if (entry && list_empty(&entry->list))
2979 list_add(&entry->list, bitmaps);
2980 }
2981
Josef Bacik86d4a772011-05-25 13:03:16 -04002982 list_for_each_entry(entry, bitmaps, list) {
Josef Bacik357b9782012-01-26 15:01:11 -05002983 if (entry->bytes < bytes)
Josef Bacik86d4a772011-05-25 13:03:16 -04002984 continue;
2985 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03002986 bytes, cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04002987 if (!ret)
2988 return 0;
2989 }
2990
2991 /*
Li Zefan52621cb2011-11-20 07:33:38 -05002992 * The bitmaps list has all the bitmaps that record free space
2993 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04002994 */
Li Zefan52621cb2011-11-20 07:33:38 -05002995 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04002996}
2997
2998/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002999 * here we try to find a cluster of blocks in a block group. The goal
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003000 * is to find at least bytes+empty_size.
Chris Masonfa9c0d792009-04-03 09:47:43 -04003001 * We might not find them all in one contiguous area.
3002 *
3003 * returns zero and sets up cluster if things worked out, otherwise
3004 * it returns -enospc
3005 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003006int btrfs_find_space_cluster(struct btrfs_fs_info *fs_info,
Chris Masonfa9c0d792009-04-03 09:47:43 -04003007 struct btrfs_block_group_cache *block_group,
3008 struct btrfs_free_cluster *cluster,
3009 u64 offset, u64 bytes, u64 empty_size)
3010{
Li Zefan34d52cb2011-03-29 13:46:06 +08003011 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04003012 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05003013 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003014 u64 min_bytes;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003015 u64 cont1_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003016 int ret;
3017
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003018 /*
3019 * Choose the minimum extent size we'll require for this
3020 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3021 * For metadata, allow allocates with smaller extents. For
3022 * data, keep it dense.
3023 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003024 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003025 cont1_bytes = min_bytes = bytes + empty_size;
Chris Mason451d7582009-06-09 20:28:34 -04003026 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003027 cont1_bytes = bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003028 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003029 } else {
3030 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003031 min_bytes = fs_info->sectorsize;
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003032 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003033
Li Zefan34d52cb2011-03-29 13:46:06 +08003034 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003035
3036 /*
3037 * If we know we don't have enough space to make a cluster don't even
3038 * bother doing all the work to try and find one.
3039 */
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003040 if (ctl->free_space < bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003041 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04003042 return -ENOSPC;
3043 }
3044
Chris Masonfa9c0d792009-04-03 09:47:43 -04003045 spin_lock(&cluster->lock);
3046
3047 /* someone already found a cluster, hooray */
3048 if (cluster->block_group) {
3049 ret = 0;
3050 goto out;
3051 }
Josef Bacik4e69b592011-03-21 10:11:24 -04003052
Josef Bacik3f7de032011-11-10 08:29:20 -05003053 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3054 min_bytes);
3055
Josef Bacik86d4a772011-05-25 13:03:16 -04003056 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003057 bytes + empty_size,
3058 cont1_bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04003059 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04003060 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
Alexandre Oliva1bb91902011-10-14 12:10:36 -03003061 offset, bytes + empty_size,
3062 cont1_bytes, min_bytes);
Josef Bacik86d4a772011-05-25 13:03:16 -04003063
3064 /* Clear our temporary list */
3065 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3066 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04003067
3068 if (!ret) {
3069 atomic_inc(&block_group->count);
3070 list_add_tail(&cluster->block_group_list,
3071 &block_group->cluster_list);
3072 cluster->block_group = block_group;
Josef Bacik3f7de032011-11-10 08:29:20 -05003073 } else {
3074 trace_btrfs_failed_cluster_setup(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003075 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003076out:
3077 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08003078 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04003079
3080 return ret;
3081}
3082
3083/*
3084 * simple code to zero out a cluster
3085 */
3086void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3087{
3088 spin_lock_init(&cluster->lock);
3089 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00003090 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003091 cluster->max_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003092 cluster->fragmented = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003093 INIT_LIST_HEAD(&cluster->block_group_list);
3094 cluster->block_group = NULL;
3095}
3096
Li Zefan7fe1e642011-12-29 14:47:27 +08003097static int do_trimming(struct btrfs_block_group_cache *block_group,
3098 u64 *total_trimmed, u64 start, u64 bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003099 u64 reserved_start, u64 reserved_bytes,
3100 struct btrfs_trim_range *trim_entry)
Li Zefan7fe1e642011-12-29 14:47:27 +08003101{
3102 struct btrfs_space_info *space_info = block_group->space_info;
3103 struct btrfs_fs_info *fs_info = block_group->fs_info;
Filipe Manana55507ce2014-12-01 17:04:09 +00003104 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003105 int ret;
3106 int update = 0;
3107 u64 trimmed = 0;
3108
3109 spin_lock(&space_info->lock);
3110 spin_lock(&block_group->lock);
3111 if (!block_group->ro) {
3112 block_group->reserved += reserved_bytes;
3113 space_info->bytes_reserved += reserved_bytes;
3114 update = 1;
3115 }
3116 spin_unlock(&block_group->lock);
3117 spin_unlock(&space_info->lock);
3118
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003119 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
Li Zefan7fe1e642011-12-29 14:47:27 +08003120 if (!ret)
3121 *total_trimmed += trimmed;
3122
Filipe Manana55507ce2014-12-01 17:04:09 +00003123 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003124 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
Filipe Manana55507ce2014-12-01 17:04:09 +00003125 list_del(&trim_entry->list);
3126 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003127
3128 if (update) {
3129 spin_lock(&space_info->lock);
3130 spin_lock(&block_group->lock);
3131 if (block_group->ro)
3132 space_info->bytes_readonly += reserved_bytes;
3133 block_group->reserved -= reserved_bytes;
3134 space_info->bytes_reserved -= reserved_bytes;
3135 spin_unlock(&space_info->lock);
3136 spin_unlock(&block_group->lock);
3137 }
3138
3139 return ret;
3140}
3141
3142static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
3143 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
Li Dongyangf7039b12011-03-24 10:24:28 +00003144{
Li Zefan34d52cb2011-03-29 13:46:06 +08003145 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan7fe1e642011-12-29 14:47:27 +08003146 struct btrfs_free_space *entry;
3147 struct rb_node *node;
Li Dongyangf7039b12011-03-24 10:24:28 +00003148 int ret = 0;
Li Zefan7fe1e642011-12-29 14:47:27 +08003149 u64 extent_start;
3150 u64 extent_bytes;
3151 u64 bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003152
3153 while (start < end) {
Filipe Manana55507ce2014-12-01 17:04:09 +00003154 struct btrfs_trim_range trim_entry;
3155
3156 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan34d52cb2011-03-29 13:46:06 +08003157 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00003158
Li Zefan34d52cb2011-03-29 13:46:06 +08003159 if (ctl->free_space < minlen) {
3160 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003161 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003162 break;
3163 }
3164
Li Zefan34d52cb2011-03-29 13:46:06 +08003165 entry = tree_search_offset(ctl, start, 0, 1);
Li Zefan7fe1e642011-12-29 14:47:27 +08003166 if (!entry) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003167 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003168 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003169 break;
3170 }
3171
Li Zefan7fe1e642011-12-29 14:47:27 +08003172 /* skip bitmaps */
3173 while (entry->bitmap) {
3174 node = rb_next(&entry->offset_index);
3175 if (!node) {
Li Zefan34d52cb2011-03-29 13:46:06 +08003176 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003177 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003178 goto out;
Li Dongyangf7039b12011-03-24 10:24:28 +00003179 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003180 entry = rb_entry(node, struct btrfs_free_space,
3181 offset_index);
Li Dongyangf7039b12011-03-24 10:24:28 +00003182 }
3183
Li Zefan7fe1e642011-12-29 14:47:27 +08003184 if (entry->offset >= end) {
3185 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003186 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003187 break;
3188 }
3189
3190 extent_start = entry->offset;
3191 extent_bytes = entry->bytes;
3192 start = max(start, extent_start);
3193 bytes = min(extent_start + extent_bytes, end) - start;
3194 if (bytes < minlen) {
3195 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003196 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003197 goto next;
3198 }
3199
3200 unlink_free_space(ctl, entry);
3201 kmem_cache_free(btrfs_free_space_cachep, entry);
3202
Li Zefan34d52cb2011-03-29 13:46:06 +08003203 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003204 trim_entry.start = extent_start;
3205 trim_entry.bytes = extent_bytes;
3206 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3207 mutex_unlock(&ctl->cache_writeout_mutex);
Li Dongyangf7039b12011-03-24 10:24:28 +00003208
Li Zefan7fe1e642011-12-29 14:47:27 +08003209 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003210 extent_start, extent_bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003211 if (ret)
3212 break;
3213next:
Li Dongyangf7039b12011-03-24 10:24:28 +00003214 start += bytes;
Li Dongyangf7039b12011-03-24 10:24:28 +00003215
3216 if (fatal_signal_pending(current)) {
3217 ret = -ERESTARTSYS;
3218 break;
3219 }
3220
3221 cond_resched();
3222 }
Li Zefan7fe1e642011-12-29 14:47:27 +08003223out:
3224 return ret;
3225}
3226
3227static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3228 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3229{
3230 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3231 struct btrfs_free_space *entry;
3232 int ret = 0;
3233 int ret2;
3234 u64 bytes;
3235 u64 offset = offset_to_bitmap(ctl, start);
3236
3237 while (offset < end) {
3238 bool next_bitmap = false;
Filipe Manana55507ce2014-12-01 17:04:09 +00003239 struct btrfs_trim_range trim_entry;
Li Zefan7fe1e642011-12-29 14:47:27 +08003240
Filipe Manana55507ce2014-12-01 17:04:09 +00003241 mutex_lock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003242 spin_lock(&ctl->tree_lock);
3243
3244 if (ctl->free_space < minlen) {
3245 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003246 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003247 break;
3248 }
3249
3250 entry = tree_search_offset(ctl, offset, 1, 0);
3251 if (!entry) {
3252 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003253 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003254 next_bitmap = true;
3255 goto next;
3256 }
3257
3258 bytes = minlen;
Josef Bacik0584f712015-10-02 16:12:23 -04003259 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
Li Zefan7fe1e642011-12-29 14:47:27 +08003260 if (ret2 || start >= end) {
3261 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003262 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003263 next_bitmap = true;
3264 goto next;
3265 }
3266
3267 bytes = min(bytes, end - start);
3268 if (bytes < minlen) {
3269 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003270 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003271 goto next;
3272 }
3273
3274 bitmap_clear_bits(ctl, entry, start, bytes);
3275 if (entry->bytes == 0)
3276 free_bitmap(ctl, entry);
3277
3278 spin_unlock(&ctl->tree_lock);
Filipe Manana55507ce2014-12-01 17:04:09 +00003279 trim_entry.start = start;
3280 trim_entry.bytes = bytes;
3281 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3282 mutex_unlock(&ctl->cache_writeout_mutex);
Li Zefan7fe1e642011-12-29 14:47:27 +08003283
3284 ret = do_trimming(block_group, total_trimmed, start, bytes,
Filipe Manana55507ce2014-12-01 17:04:09 +00003285 start, bytes, &trim_entry);
Li Zefan7fe1e642011-12-29 14:47:27 +08003286 if (ret)
3287 break;
3288next:
3289 if (next_bitmap) {
3290 offset += BITS_PER_BITMAP * ctl->unit;
3291 } else {
3292 start += bytes;
3293 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3294 offset += BITS_PER_BITMAP * ctl->unit;
3295 }
3296
3297 if (fatal_signal_pending(current)) {
3298 ret = -ERESTARTSYS;
3299 break;
3300 }
3301
3302 cond_resched();
3303 }
3304
3305 return ret;
3306}
3307
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003308void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache)
Li Zefan7fe1e642011-12-29 14:47:27 +08003309{
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003310 atomic_inc(&cache->trimming);
3311}
Li Zefan7fe1e642011-12-29 14:47:27 +08003312
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003313void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
3314{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003315 struct btrfs_fs_info *fs_info = block_group->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003316 struct extent_map_tree *em_tree;
3317 struct extent_map *em;
3318 bool cleanup;
Li Zefan7fe1e642011-12-29 14:47:27 +08003319
Filipe Manana04216822014-11-27 21:14:15 +00003320 spin_lock(&block_group->lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003321 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3322 block_group->removed);
Filipe Manana04216822014-11-27 21:14:15 +00003323 spin_unlock(&block_group->lock);
3324
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003325 if (cleanup) {
David Sterba34441362016-10-04 19:34:27 +02003326 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003327 em_tree = &fs_info->mapping_tree.map_tree;
Filipe Manana04216822014-11-27 21:14:15 +00003328 write_lock(&em_tree->lock);
3329 em = lookup_extent_mapping(em_tree, block_group->key.objectid,
3330 1);
3331 BUG_ON(!em); /* logic error, can't happen */
Filipe Mananaa1e7e162014-12-04 15:31:01 +00003332 /*
3333 * remove_extent_mapping() will delete us from the pinned_chunks
3334 * list, which is protected by the chunk mutex.
3335 */
Filipe Manana04216822014-11-27 21:14:15 +00003336 remove_extent_mapping(em_tree, em);
3337 write_unlock(&em_tree->lock);
David Sterba34441362016-10-04 19:34:27 +02003338 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana04216822014-11-27 21:14:15 +00003339
3340 /* once for us and once for the tree */
3341 free_extent_map(em);
3342 free_extent_map(em);
Filipe Manana946ddbe2014-12-01 17:04:40 +00003343
3344 /*
3345 * We've left one free space entry and other tasks trimming
3346 * this block group have left 1 entry each one. Free them.
3347 */
3348 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
Filipe Manana04216822014-11-27 21:14:15 +00003349 }
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003350}
Li Dongyangf7039b12011-03-24 10:24:28 +00003351
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04003352int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
3353 u64 *trimmed, u64 start, u64 end, u64 minlen)
3354{
3355 int ret;
3356
3357 *trimmed = 0;
3358
3359 spin_lock(&block_group->lock);
3360 if (block_group->removed) {
3361 spin_unlock(&block_group->lock);
3362 return 0;
3363 }
3364 btrfs_get_block_group_trimming(block_group);
3365 spin_unlock(&block_group->lock);
3366
3367 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3368 if (ret)
3369 goto out;
3370
3371 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
3372out:
3373 btrfs_put_block_group_trimming(block_group);
Li Dongyangf7039b12011-03-24 10:24:28 +00003374 return ret;
3375}
Li Zefan581bb052011-04-20 10:06:11 +08003376
3377/*
3378 * Find the left-most item in the cache tree, and then return the
3379 * smallest inode number in the item.
3380 *
3381 * Note: the returned inode number may not be the smallest one in
3382 * the tree, if the left-most item is a bitmap.
3383 */
3384u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3385{
3386 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3387 struct btrfs_free_space *entry = NULL;
3388 u64 ino = 0;
3389
3390 spin_lock(&ctl->tree_lock);
3391
3392 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3393 goto out;
3394
3395 entry = rb_entry(rb_first(&ctl->free_space_offset),
3396 struct btrfs_free_space, offset_index);
3397
3398 if (!entry->bitmap) {
3399 ino = entry->offset;
3400
3401 unlink_free_space(ctl, entry);
3402 entry->offset++;
3403 entry->bytes--;
3404 if (!entry->bytes)
3405 kmem_cache_free(btrfs_free_space_cachep, entry);
3406 else
3407 link_free_space(ctl, entry);
3408 } else {
3409 u64 offset = 0;
3410 u64 count = 1;
3411 int ret;
3412
Josef Bacik0584f712015-10-02 16:12:23 -04003413 ret = search_bitmap(ctl, entry, &offset, &count, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003414 /* Logic error; Should be empty if it can't find anything */
Josef Bacikb12d6862013-08-26 17:14:08 -04003415 ASSERT(!ret);
Li Zefan581bb052011-04-20 10:06:11 +08003416
3417 ino = offset;
3418 bitmap_clear_bits(ctl, entry, offset, 1);
3419 if (entry->bytes == 0)
3420 free_bitmap(ctl, entry);
3421 }
3422out:
3423 spin_unlock(&ctl->tree_lock);
3424
3425 return ino;
3426}
Li Zefan82d59022011-04-20 10:33:24 +08003427
3428struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3429 struct btrfs_path *path)
3430{
3431 struct inode *inode = NULL;
3432
David Sterba57cdc8d2014-02-05 02:37:48 +01003433 spin_lock(&root->ino_cache_lock);
3434 if (root->ino_cache_inode)
3435 inode = igrab(root->ino_cache_inode);
3436 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003437 if (inode)
3438 return inode;
3439
3440 inode = __lookup_free_space_inode(root, path, 0);
3441 if (IS_ERR(inode))
3442 return inode;
3443
David Sterba57cdc8d2014-02-05 02:37:48 +01003444 spin_lock(&root->ino_cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02003445 if (!btrfs_fs_closing(root->fs_info))
David Sterba57cdc8d2014-02-05 02:37:48 +01003446 root->ino_cache_inode = igrab(inode);
3447 spin_unlock(&root->ino_cache_lock);
Li Zefan82d59022011-04-20 10:33:24 +08003448
3449 return inode;
3450}
3451
3452int create_free_ino_inode(struct btrfs_root *root,
3453 struct btrfs_trans_handle *trans,
3454 struct btrfs_path *path)
3455{
3456 return __create_free_space_inode(root, trans, path,
3457 BTRFS_FREE_INO_OBJECTID, 0);
3458}
3459
3460int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3461{
3462 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3463 struct btrfs_path *path;
3464 struct inode *inode;
3465 int ret = 0;
3466 u64 root_gen = btrfs_root_generation(&root->root_item);
3467
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003468 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04003469 return 0;
3470
Li Zefan82d59022011-04-20 10:33:24 +08003471 /*
3472 * If we're unmounting then just return, since this does a search on the
3473 * normal root and not the commit root and we could deadlock.
3474 */
David Sterba7841cb22011-05-31 18:07:27 +02003475 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08003476 return 0;
3477
3478 path = btrfs_alloc_path();
3479 if (!path)
3480 return 0;
3481
3482 inode = lookup_free_ino_inode(root, path);
3483 if (IS_ERR(inode))
3484 goto out;
3485
3486 if (root_gen != BTRFS_I(inode)->generation)
3487 goto out_put;
3488
3489 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3490
3491 if (ret < 0)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003492 btrfs_err(fs_info,
3493 "failed to load free ino cache for root %llu",
3494 root->root_key.objectid);
Li Zefan82d59022011-04-20 10:33:24 +08003495out_put:
3496 iput(inode);
3497out:
3498 btrfs_free_path(path);
3499 return ret;
3500}
3501
3502int btrfs_write_out_ino_cache(struct btrfs_root *root,
3503 struct btrfs_trans_handle *trans,
Filipe David Borba Manana53645a92013-09-20 14:43:28 +01003504 struct btrfs_path *path,
3505 struct inode *inode)
Li Zefan82d59022011-04-20 10:33:24 +08003506{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003507 struct btrfs_fs_info *fs_info = root->fs_info;
Li Zefan82d59022011-04-20 10:33:24 +08003508 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
Li Zefan82d59022011-04-20 10:33:24 +08003509 int ret;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003510 struct btrfs_io_ctl io_ctl;
Filipe Mananae43699d2015-05-05 15:21:27 +01003511 bool release_metadata = true;
Li Zefan82d59022011-04-20 10:33:24 +08003512
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003513 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
Chris Mason4b9465c2011-06-03 09:36:29 -04003514 return 0;
3515
Chris Mason85db36c2015-04-23 08:02:49 -07003516 memset(&io_ctl, 0, sizeof(io_ctl));
David Sterba0e8d9312017-02-10 20:26:24 +01003517 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
Filipe Mananae43699d2015-05-05 15:21:27 +01003518 if (!ret) {
3519 /*
3520 * At this point writepages() didn't error out, so our metadata
3521 * reservation is released when the writeback finishes, at
3522 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3523 * with or without an error.
3524 */
3525 release_metadata = false;
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003526 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
Filipe Mananae43699d2015-05-05 15:21:27 +01003527 }
Chris Mason85db36c2015-04-23 08:02:49 -07003528
Josef Bacikc09544e2011-08-30 10:19:10 -04003529 if (ret) {
Filipe Mananae43699d2015-05-05 15:21:27 +01003530 if (release_metadata)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003531 btrfs_delalloc_release_metadata(BTRFS_I(inode),
Qu Wenruo43b18592017-12-12 15:34:32 +08003532 inode->i_size, true);
Josef Bacikc09544e2011-08-30 10:19:10 -04003533#ifdef DEBUG
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003534 btrfs_err(fs_info,
3535 "failed to write free ino cache for root %llu",
3536 root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04003537#endif
3538 }
Li Zefan82d59022011-04-20 10:33:24 +08003539
Li Zefan82d59022011-04-20 10:33:24 +08003540 return ret;
3541}
Josef Bacik74255aa2013-03-15 09:47:08 -04003542
3543#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Josef Bacikdc11dd52013-08-14 15:05:12 -04003544/*
3545 * Use this if you need to make a bitmap or extent entry specifically, it
3546 * doesn't do any of the merging that add_free_space does, this acts a lot like
3547 * how the free space cache loading stuff works, so you can get really weird
3548 * configurations.
3549 */
3550int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3551 u64 offset, u64 bytes, bool bitmap)
Josef Bacik74255aa2013-03-15 09:47:08 -04003552{
Josef Bacikdc11dd52013-08-14 15:05:12 -04003553 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3554 struct btrfs_free_space *info = NULL, *bitmap_info;
3555 void *map = NULL;
3556 u64 bytes_added;
3557 int ret;
Josef Bacik74255aa2013-03-15 09:47:08 -04003558
Josef Bacikdc11dd52013-08-14 15:05:12 -04003559again:
3560 if (!info) {
3561 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3562 if (!info)
3563 return -ENOMEM;
Josef Bacik74255aa2013-03-15 09:47:08 -04003564 }
3565
Josef Bacikdc11dd52013-08-14 15:05:12 -04003566 if (!bitmap) {
3567 spin_lock(&ctl->tree_lock);
3568 info->offset = offset;
3569 info->bytes = bytes;
Josef Bacikcef40482015-10-02 16:09:42 -04003570 info->max_extent_size = 0;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003571 ret = link_free_space(ctl, info);
3572 spin_unlock(&ctl->tree_lock);
3573 if (ret)
3574 kmem_cache_free(btrfs_free_space_cachep, info);
3575 return ret;
3576 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003577
Josef Bacikdc11dd52013-08-14 15:05:12 -04003578 if (!map) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003579 map = kzalloc(PAGE_SIZE, GFP_NOFS);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003580 if (!map) {
3581 kmem_cache_free(btrfs_free_space_cachep, info);
3582 return -ENOMEM;
3583 }
3584 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003585
Josef Bacikdc11dd52013-08-14 15:05:12 -04003586 spin_lock(&ctl->tree_lock);
3587 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3588 1, 0);
3589 if (!bitmap_info) {
3590 info->bitmap = map;
3591 map = NULL;
3592 add_new_bitmap(ctl, info, offset);
3593 bitmap_info = info;
Filipe Manana20005522014-08-29 13:35:13 +01003594 info = NULL;
Josef Bacikdc11dd52013-08-14 15:05:12 -04003595 }
Josef Bacik74255aa2013-03-15 09:47:08 -04003596
Josef Bacikdc11dd52013-08-14 15:05:12 -04003597 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
Josef Bacikcef40482015-10-02 16:09:42 -04003598
Josef Bacikdc11dd52013-08-14 15:05:12 -04003599 bytes -= bytes_added;
3600 offset += bytes_added;
3601 spin_unlock(&ctl->tree_lock);
3602
3603 if (bytes)
3604 goto again;
3605
Filipe Manana20005522014-08-29 13:35:13 +01003606 if (info)
3607 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacikdc11dd52013-08-14 15:05:12 -04003608 if (map)
3609 kfree(map);
3610 return 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003611}
3612
3613/*
3614 * Checks to see if the given range is in the free space cache. This is really
3615 * just used to check the absence of space, so if there is free space in the
3616 * range at all we will return 1.
3617 */
Josef Bacikdc11dd52013-08-14 15:05:12 -04003618int test_check_exists(struct btrfs_block_group_cache *cache,
3619 u64 offset, u64 bytes)
Josef Bacik74255aa2013-03-15 09:47:08 -04003620{
3621 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3622 struct btrfs_free_space *info;
3623 int ret = 0;
3624
3625 spin_lock(&ctl->tree_lock);
3626 info = tree_search_offset(ctl, offset, 0, 0);
3627 if (!info) {
3628 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3629 1, 0);
3630 if (!info)
3631 goto out;
3632 }
3633
3634have_info:
3635 if (info->bitmap) {
3636 u64 bit_off, bit_bytes;
3637 struct rb_node *n;
3638 struct btrfs_free_space *tmp;
3639
3640 bit_off = offset;
3641 bit_bytes = ctl->unit;
Josef Bacik0584f712015-10-02 16:12:23 -04003642 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
Josef Bacik74255aa2013-03-15 09:47:08 -04003643 if (!ret) {
3644 if (bit_off == offset) {
3645 ret = 1;
3646 goto out;
3647 } else if (bit_off > offset &&
3648 offset + bytes > bit_off) {
3649 ret = 1;
3650 goto out;
3651 }
3652 }
3653
3654 n = rb_prev(&info->offset_index);
3655 while (n) {
3656 tmp = rb_entry(n, struct btrfs_free_space,
3657 offset_index);
3658 if (tmp->offset + tmp->bytes < offset)
3659 break;
3660 if (offset + bytes < tmp->offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08003661 n = rb_prev(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04003662 continue;
3663 }
3664 info = tmp;
3665 goto have_info;
3666 }
3667
3668 n = rb_next(&info->offset_index);
3669 while (n) {
3670 tmp = rb_entry(n, struct btrfs_free_space,
3671 offset_index);
3672 if (offset + bytes < tmp->offset)
3673 break;
3674 if (tmp->offset + tmp->bytes < offset) {
Feifei Xu5473e0c42016-06-01 19:18:23 +08003675 n = rb_next(&tmp->offset_index);
Josef Bacik74255aa2013-03-15 09:47:08 -04003676 continue;
3677 }
3678 info = tmp;
3679 goto have_info;
3680 }
3681
Filipe Manana20005522014-08-29 13:35:13 +01003682 ret = 0;
Josef Bacik74255aa2013-03-15 09:47:08 -04003683 goto out;
3684 }
3685
3686 if (info->offset == offset) {
3687 ret = 1;
3688 goto out;
3689 }
3690
3691 if (offset > info->offset && offset < info->offset + info->bytes)
3692 ret = 1;
3693out:
3694 spin_unlock(&ctl->tree_lock);
3695 return ret;
3696}
Josef Bacikdc11dd52013-08-14 15:05:12 -04003697#endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */