David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008 Red Hat. All rights reserved. |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6 | #include <linux/pagemap.h> |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7 | #include <linux/sched.h> |
Ingo Molnar | f361bf4 | 2017-02-03 23:47:37 +0100 | [diff] [blame] | 8 | #include <linux/sched/signal.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10 | #include <linux/math64.h> |
Josef Bacik | 6ab6060 | 2011-08-08 08:24:46 -0400 | [diff] [blame] | 11 | #include <linux/ratelimit.h> |
Masami Hiramatsu | 540adea | 2018-01-13 02:55:03 +0900 | [diff] [blame] | 12 | #include <linux/error-injection.h> |
Josef Bacik | 84de76a | 2018-09-28 07:17:49 -0400 | [diff] [blame] | 13 | #include <linux/sched/mm.h> |
Johannes Thumshirn | 18bb8bb | 2021-04-19 16:41:02 +0900 | [diff] [blame] | 14 | #include "misc.h" |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 15 | #include "ctree.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 16 | #include "free-space-cache.h" |
| 17 | #include "transaction.h" |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 18 | #include "disk-io.h" |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 19 | #include "extent_io.h" |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 20 | #include "volumes.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 21 | #include "space-info.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 22 | #include "delalloc-space.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 23 | #include "block-group.h" |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 24 | #include "discard.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 25 | |
Feifei Xu | 0ef6447 | 2016-06-01 19:18:24 +0800 | [diff] [blame] | 26 | #define BITS_PER_BITMAP (PAGE_SIZE * 8UL) |
Dennis Zhou | 5d90c5c | 2020-01-02 16:26:43 -0500 | [diff] [blame] | 27 | #define MAX_CACHE_BYTES_PER_GIG SZ_64K |
| 28 | #define FORCE_EXTENT_THRESHOLD SZ_1M |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 29 | |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 30 | struct btrfs_trim_range { |
| 31 | u64 start; |
| 32 | u64 bytes; |
| 33 | struct list_head list; |
| 34 | }; |
| 35 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 36 | static int link_free_space(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 37 | struct btrfs_free_space *info); |
Josef Bacik | cd023e7 | 2012-05-14 10:06:40 -0400 | [diff] [blame] | 38 | static void unlink_free_space(struct btrfs_free_space_ctl *ctl, |
| 39 | struct btrfs_free_space *info); |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 40 | static int search_bitmap(struct btrfs_free_space_ctl *ctl, |
| 41 | struct btrfs_free_space *bitmap_info, u64 *offset, |
| 42 | u64 *bytes, bool for_alloc); |
| 43 | static void free_bitmap(struct btrfs_free_space_ctl *ctl, |
| 44 | struct btrfs_free_space *bitmap_info); |
| 45 | static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, |
| 46 | struct btrfs_free_space *info, u64 offset, |
| 47 | u64 bytes); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 48 | |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 49 | static struct inode *__lookup_free_space_inode(struct btrfs_root *root, |
| 50 | struct btrfs_path *path, |
| 51 | u64 offset) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 52 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 53 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 54 | struct btrfs_key key; |
| 55 | struct btrfs_key location; |
| 56 | struct btrfs_disk_key disk_key; |
| 57 | struct btrfs_free_space_header *header; |
| 58 | struct extent_buffer *leaf; |
| 59 | struct inode *inode = NULL; |
Josef Bacik | 84de76a | 2018-09-28 07:17:49 -0400 | [diff] [blame] | 60 | unsigned nofs_flag; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 61 | int ret; |
| 62 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 63 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 64 | key.offset = offset; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 65 | key.type = 0; |
| 66 | |
| 67 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 68 | if (ret < 0) |
| 69 | return ERR_PTR(ret); |
| 70 | if (ret > 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 71 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 72 | return ERR_PTR(-ENOENT); |
| 73 | } |
| 74 | |
| 75 | leaf = path->nodes[0]; |
| 76 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 77 | struct btrfs_free_space_header); |
| 78 | btrfs_free_space_key(leaf, header, &disk_key); |
| 79 | btrfs_disk_key_to_cpu(&location, &disk_key); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 80 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 81 | |
Josef Bacik | 84de76a | 2018-09-28 07:17:49 -0400 | [diff] [blame] | 82 | /* |
| 83 | * We are often under a trans handle at this point, so we need to make |
| 84 | * sure NOFS is set to keep us from deadlocking. |
| 85 | */ |
| 86 | nofs_flag = memalloc_nofs_save(); |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 87 | inode = btrfs_iget_path(fs_info->sb, location.objectid, root, path); |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 88 | btrfs_release_path(path); |
Josef Bacik | 84de76a | 2018-09-28 07:17:49 -0400 | [diff] [blame] | 89 | memalloc_nofs_restore(nofs_flag); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 90 | if (IS_ERR(inode)) |
| 91 | return inode; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 92 | |
Al Viro | 528c032 | 2012-04-13 11:03:55 -0400 | [diff] [blame] | 93 | mapping_set_gfp_mask(inode->i_mapping, |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 94 | mapping_gfp_constraint(inode->i_mapping, |
| 95 | ~(__GFP_FS | __GFP_HIGHMEM))); |
Miao Xie | adae52b | 2011-03-31 09:43:23 +0000 | [diff] [blame] | 96 | |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 97 | return inode; |
| 98 | } |
| 99 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 100 | struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group, |
David Sterba | 7949f33 | 2019-03-20 13:40:19 +0100 | [diff] [blame] | 101 | struct btrfs_path *path) |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 102 | { |
David Sterba | 7949f33 | 2019-03-20 13:40:19 +0100 | [diff] [blame] | 103 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 104 | struct inode *inode = NULL; |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 105 | u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 106 | |
| 107 | spin_lock(&block_group->lock); |
| 108 | if (block_group->inode) |
| 109 | inode = igrab(block_group->inode); |
| 110 | spin_unlock(&block_group->lock); |
| 111 | if (inode) |
| 112 | return inode; |
| 113 | |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 114 | inode = __lookup_free_space_inode(fs_info->tree_root, path, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 115 | block_group->start); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 116 | if (IS_ERR(inode)) |
| 117 | return inode; |
| 118 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 119 | spin_lock(&block_group->lock); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 120 | if (!((BTRFS_I(inode)->flags & flags) == flags)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 121 | btrfs_info(fs_info, "Old style space inode found, converting."); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 122 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM | |
| 123 | BTRFS_INODE_NODATACOW; |
Josef Bacik | 2f35612 | 2011-06-10 15:31:13 -0400 | [diff] [blame] | 124 | block_group->disk_cache_state = BTRFS_DC_CLEAR; |
| 125 | } |
| 126 | |
Josef Bacik | 300e4f8 | 2011-08-29 14:06:00 -0400 | [diff] [blame] | 127 | if (!block_group->iref) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 128 | block_group->inode = igrab(inode); |
| 129 | block_group->iref = 1; |
| 130 | } |
| 131 | spin_unlock(&block_group->lock); |
| 132 | |
| 133 | return inode; |
| 134 | } |
| 135 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 136 | static int __create_free_space_inode(struct btrfs_root *root, |
| 137 | struct btrfs_trans_handle *trans, |
| 138 | struct btrfs_path *path, |
| 139 | u64 ino, u64 offset) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 140 | { |
| 141 | struct btrfs_key key; |
| 142 | struct btrfs_disk_key disk_key; |
| 143 | struct btrfs_free_space_header *header; |
| 144 | struct btrfs_inode_item *inode_item; |
| 145 | struct extent_buffer *leaf; |
Nikolay Borisov | f0d1219 | 2020-12-03 10:09:49 +0200 | [diff] [blame] | 146 | /* We inline CRCs for the free disk space cache */ |
| 147 | const u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC | |
| 148 | BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 149 | int ret; |
| 150 | |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 151 | ret = btrfs_insert_empty_inode(trans, root, path, ino); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 152 | if (ret) |
| 153 | return ret; |
| 154 | |
| 155 | leaf = path->nodes[0]; |
| 156 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 157 | struct btrfs_inode_item); |
| 158 | btrfs_item_key(leaf, &disk_key, path->slots[0]); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 159 | memzero_extent_buffer(leaf, (unsigned long)inode_item, |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 160 | sizeof(*inode_item)); |
| 161 | btrfs_set_inode_generation(leaf, inode_item, trans->transid); |
| 162 | btrfs_set_inode_size(leaf, inode_item, 0); |
| 163 | btrfs_set_inode_nbytes(leaf, inode_item, 0); |
| 164 | btrfs_set_inode_uid(leaf, inode_item, 0); |
| 165 | btrfs_set_inode_gid(leaf, inode_item, 0); |
| 166 | btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 167 | btrfs_set_inode_flags(leaf, inode_item, flags); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 168 | btrfs_set_inode_nlink(leaf, inode_item, 1); |
| 169 | btrfs_set_inode_transid(leaf, inode_item, trans->transid); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 170 | btrfs_set_inode_block_group(leaf, inode_item, offset); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 171 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 172 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 173 | |
| 174 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 175 | key.offset = offset; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 176 | key.type = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 177 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 178 | sizeof(struct btrfs_free_space_header)); |
| 179 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 180 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 181 | return ret; |
| 182 | } |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 183 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 184 | leaf = path->nodes[0]; |
| 185 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 186 | struct btrfs_free_space_header); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 187 | memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header)); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 188 | btrfs_set_free_space_key(leaf, header, &disk_key); |
| 189 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 190 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
David Sterba | 4ca75f1 | 2019-03-20 13:42:57 +0100 | [diff] [blame] | 195 | int create_free_space_inode(struct btrfs_trans_handle *trans, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 196 | struct btrfs_block_group *block_group, |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 197 | struct btrfs_path *path) |
| 198 | { |
| 199 | int ret; |
| 200 | u64 ino; |
| 201 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 202 | ret = btrfs_get_free_objectid(trans->fs_info->tree_root, &ino); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 203 | if (ret < 0) |
| 204 | return ret; |
| 205 | |
David Sterba | 4ca75f1 | 2019-03-20 13:42:57 +0100 | [diff] [blame] | 206 | return __create_free_space_inode(trans->fs_info->tree_root, trans, path, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 207 | ino, block_group->start); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 208 | } |
| 209 | |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 210 | /* |
| 211 | * inode is an optional sink: if it is NULL, btrfs_remove_free_space_inode |
| 212 | * handles lookup, otherwise it takes ownership and iputs the inode. |
| 213 | * Don't reuse an inode pointer after passing it into this function. |
| 214 | */ |
| 215 | int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans, |
| 216 | struct inode *inode, |
| 217 | struct btrfs_block_group *block_group) |
| 218 | { |
| 219 | struct btrfs_path *path; |
| 220 | struct btrfs_key key; |
| 221 | int ret = 0; |
| 222 | |
| 223 | path = btrfs_alloc_path(); |
| 224 | if (!path) |
| 225 | return -ENOMEM; |
| 226 | |
| 227 | if (!inode) |
| 228 | inode = lookup_free_space_inode(block_group, path); |
| 229 | if (IS_ERR(inode)) { |
| 230 | if (PTR_ERR(inode) != -ENOENT) |
| 231 | ret = PTR_ERR(inode); |
| 232 | goto out; |
| 233 | } |
| 234 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
| 235 | if (ret) { |
| 236 | btrfs_add_delayed_iput(inode); |
| 237 | goto out; |
| 238 | } |
| 239 | clear_nlink(inode); |
| 240 | /* One for the block groups ref */ |
| 241 | spin_lock(&block_group->lock); |
| 242 | if (block_group->iref) { |
| 243 | block_group->iref = 0; |
| 244 | block_group->inode = NULL; |
| 245 | spin_unlock(&block_group->lock); |
| 246 | iput(inode); |
| 247 | } else { |
| 248 | spin_unlock(&block_group->lock); |
| 249 | } |
| 250 | /* One for the lookup ref */ |
| 251 | btrfs_add_delayed_iput(inode); |
| 252 | |
| 253 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 254 | key.type = 0; |
| 255 | key.offset = block_group->start; |
| 256 | ret = btrfs_search_slot(trans, trans->fs_info->tree_root, &key, path, |
| 257 | -1, 1); |
| 258 | if (ret) { |
| 259 | if (ret > 0) |
| 260 | ret = 0; |
| 261 | goto out; |
| 262 | } |
| 263 | ret = btrfs_del_item(trans, trans->fs_info->tree_root, path); |
| 264 | out: |
| 265 | btrfs_free_path(path); |
| 266 | return ret; |
| 267 | } |
| 268 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 269 | int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info, |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 270 | struct btrfs_block_rsv *rsv) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 271 | { |
Josef Bacik | c817431 | 2011-11-02 09:29:35 -0400 | [diff] [blame] | 272 | u64 needed_bytes; |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 273 | int ret; |
Josef Bacik | c817431 | 2011-11-02 09:29:35 -0400 | [diff] [blame] | 274 | |
| 275 | /* 1 for slack space, 1 for updating the inode */ |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 276 | needed_bytes = btrfs_calc_insert_metadata_size(fs_info, 1) + |
| 277 | btrfs_calc_metadata_size(fs_info, 1); |
Josef Bacik | c817431 | 2011-11-02 09:29:35 -0400 | [diff] [blame] | 278 | |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 279 | spin_lock(&rsv->lock); |
| 280 | if (rsv->reserved < needed_bytes) |
| 281 | ret = -ENOSPC; |
| 282 | else |
| 283 | ret = 0; |
| 284 | spin_unlock(&rsv->lock); |
Wei Yongjun | 4b286cd | 2013-05-21 02:39:21 +0000 | [diff] [blame] | 285 | return ret; |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 288 | int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 289 | struct btrfs_block_group *block_group, |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 290 | struct inode *inode) |
| 291 | { |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 292 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 293 | int ret = 0; |
Filipe Manana | 35c7664 | 2015-04-30 17:47:05 +0100 | [diff] [blame] | 294 | bool locked = false; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 295 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 296 | if (block_group) { |
Jeff Mahoney | 21e75ff | 2017-02-15 16:28:32 -0500 | [diff] [blame] | 297 | struct btrfs_path *path = btrfs_alloc_path(); |
| 298 | |
| 299 | if (!path) { |
| 300 | ret = -ENOMEM; |
| 301 | goto fail; |
| 302 | } |
Filipe Manana | 35c7664 | 2015-04-30 17:47:05 +0100 | [diff] [blame] | 303 | locked = true; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 304 | mutex_lock(&trans->transaction->cache_write_mutex); |
| 305 | if (!list_empty(&block_group->io_list)) { |
| 306 | list_del_init(&block_group->io_list); |
| 307 | |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 308 | btrfs_wait_cache_io(trans, block_group, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 309 | btrfs_put_block_group(block_group); |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * now that we've truncated the cache away, its no longer |
| 314 | * setup or written |
| 315 | */ |
| 316 | spin_lock(&block_group->lock); |
| 317 | block_group->disk_cache_state = BTRFS_DC_CLEAR; |
| 318 | spin_unlock(&block_group->lock); |
Jeff Mahoney | 21e75ff | 2017-02-15 16:28:32 -0500 | [diff] [blame] | 319 | btrfs_free_path(path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 320 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 321 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 322 | btrfs_i_size_write(BTRFS_I(inode), 0); |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 323 | truncate_pagecache(inode, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 324 | |
| 325 | /* |
Omar Sandoval | f7e9e8f | 2018-05-11 13:13:32 -0700 | [diff] [blame] | 326 | * We skip the throttling logic for free space cache inodes, so we don't |
| 327 | * need to check for -EAGAIN. |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 328 | */ |
Nikolay Borisov | 5074339 | 2020-11-02 16:48:55 +0200 | [diff] [blame] | 329 | ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), |
Filipe Manana | 0d7d316 | 2021-05-24 11:35:55 +0100 | [diff] [blame] | 330 | 0, BTRFS_EXTENT_DATA_KEY, NULL); |
Filipe Manana | 35c7664 | 2015-04-30 17:47:05 +0100 | [diff] [blame] | 331 | if (ret) |
| 332 | goto fail; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 333 | |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 334 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 335 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 336 | fail: |
Filipe Manana | 35c7664 | 2015-04-30 17:47:05 +0100 | [diff] [blame] | 337 | if (locked) |
| 338 | mutex_unlock(&trans->transaction->cache_write_mutex); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 339 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 340 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | c817431 | 2011-11-02 09:29:35 -0400 | [diff] [blame] | 341 | |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 342 | return ret; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 343 | } |
| 344 | |
David Sterba | 1d48053 | 2017-01-23 17:28:19 +0100 | [diff] [blame] | 345 | static void readahead_cache(struct inode *inode) |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 346 | { |
| 347 | struct file_ra_state *ra; |
| 348 | unsigned long last_index; |
| 349 | |
| 350 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
| 351 | if (!ra) |
David Sterba | 1d48053 | 2017-01-23 17:28:19 +0100 | [diff] [blame] | 352 | return; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 353 | |
| 354 | file_ra_state_init(ra, inode->i_mapping); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 355 | last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 356 | |
| 357 | page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index); |
| 358 | |
| 359 | kfree(ra); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 360 | } |
| 361 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 362 | static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode, |
Jeff Mahoney | f15376d | 2016-06-22 18:56:18 -0400 | [diff] [blame] | 363 | int write) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 364 | { |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 365 | int num_pages; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 366 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 367 | num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 368 | |
Zhihui Zhang | 8f6c72a | 2018-07-02 20:00:54 -0400 | [diff] [blame] | 369 | /* Make sure we can fit our crcs and generation into the first page */ |
Nikolay Borisov | 7dbdb44 | 2020-12-03 10:09:48 +0200 | [diff] [blame] | 370 | if (write && (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE) |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 371 | return -ENOSPC; |
| 372 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 373 | memset(io_ctl, 0, sizeof(struct btrfs_io_ctl)); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 374 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 375 | io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 376 | if (!io_ctl->pages) |
| 377 | return -ENOMEM; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 378 | |
| 379 | io_ctl->num_pages = num_pages; |
Jeff Mahoney | f15376d | 2016-06-22 18:56:18 -0400 | [diff] [blame] | 380 | io_ctl->fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 381 | io_ctl->inode = inode; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 382 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 383 | return 0; |
| 384 | } |
Masami Hiramatsu | 663faf9 | 2018-01-13 02:55:33 +0900 | [diff] [blame] | 385 | ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 386 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 387 | static void io_ctl_free(struct btrfs_io_ctl *io_ctl) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 388 | { |
| 389 | kfree(io_ctl->pages); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 390 | io_ctl->pages = NULL; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 393 | static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 394 | { |
| 395 | if (io_ctl->cur) { |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 396 | io_ctl->cur = NULL; |
| 397 | io_ctl->orig = NULL; |
| 398 | } |
| 399 | } |
| 400 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 401 | static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 402 | { |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 403 | ASSERT(io_ctl->index < io_ctl->num_pages); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 404 | io_ctl->page = io_ctl->pages[io_ctl->index++]; |
Chris Mason | 2b10826 | 2015-04-06 07:48:20 -0700 | [diff] [blame] | 405 | io_ctl->cur = page_address(io_ctl->page); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 406 | io_ctl->orig = io_ctl->cur; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 407 | io_ctl->size = PAGE_SIZE; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 408 | if (clear) |
David Sterba | 619a974 | 2017-03-29 20:48:44 +0200 | [diff] [blame] | 409 | clear_page(io_ctl->cur); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 410 | } |
| 411 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 412 | static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 413 | { |
| 414 | int i; |
| 415 | |
| 416 | io_ctl_unmap_page(io_ctl); |
| 417 | |
| 418 | for (i = 0; i < io_ctl->num_pages; i++) { |
Li Zefan | a1ee5a4 | 2012-01-09 14:27:42 +0800 | [diff] [blame] | 419 | if (io_ctl->pages[i]) { |
| 420 | ClearPageChecked(io_ctl->pages[i]); |
| 421 | unlock_page(io_ctl->pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 422 | put_page(io_ctl->pages[i]); |
Li Zefan | a1ee5a4 | 2012-01-09 14:27:42 +0800 | [diff] [blame] | 423 | } |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Johannes Thumshirn | 7a195f6 | 2020-02-12 00:10:20 +0900 | [diff] [blame] | 427 | static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, bool uptodate) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 428 | { |
| 429 | struct page *page; |
Johannes Thumshirn | 831fa14 | 2020-02-12 00:10:19 +0900 | [diff] [blame] | 430 | struct inode *inode = io_ctl->inode; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 431 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
| 432 | int i; |
| 433 | |
| 434 | for (i = 0; i < io_ctl->num_pages; i++) { |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 435 | int ret; |
| 436 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 437 | page = find_or_create_page(inode->i_mapping, i, mask); |
| 438 | if (!page) { |
| 439 | io_ctl_drop_pages(io_ctl); |
| 440 | return -ENOMEM; |
| 441 | } |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 442 | |
| 443 | ret = set_page_extent_mapped(page); |
| 444 | if (ret < 0) { |
| 445 | unlock_page(page); |
| 446 | put_page(page); |
| 447 | io_ctl_drop_pages(io_ctl); |
| 448 | return ret; |
| 449 | } |
| 450 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 451 | io_ctl->pages[i] = page; |
| 452 | if (uptodate && !PageUptodate(page)) { |
| 453 | btrfs_readpage(NULL, page); |
| 454 | lock_page(page); |
Josef Bacik | 3797136 | 2019-09-24 16:50:43 -0400 | [diff] [blame] | 455 | if (page->mapping != inode->i_mapping) { |
| 456 | btrfs_err(BTRFS_I(inode)->root->fs_info, |
| 457 | "free space cache page truncated"); |
| 458 | io_ctl_drop_pages(io_ctl); |
| 459 | return -EIO; |
| 460 | } |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 461 | if (!PageUptodate(page)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 462 | btrfs_err(BTRFS_I(inode)->root->fs_info, |
| 463 | "error reading free space cache"); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 464 | io_ctl_drop_pages(io_ctl); |
| 465 | return -EIO; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 470 | for (i = 0; i < io_ctl->num_pages; i++) |
Josef Bacik | f7d61dc | 2011-11-15 09:31:24 -0500 | [diff] [blame] | 471 | clear_page_dirty_for_io(io_ctl->pages[i]); |
Josef Bacik | f7d61dc | 2011-11-15 09:31:24 -0500 | [diff] [blame] | 472 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 473 | return 0; |
| 474 | } |
| 475 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 476 | static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 477 | { |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 478 | io_ctl_map_page(io_ctl, 1); |
| 479 | |
| 480 | /* |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 481 | * Skip the csum areas. If we don't check crcs then we just have a |
| 482 | * 64bit chunk at the front of the first page. |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 483 | */ |
Nikolay Borisov | 7dbdb44 | 2020-12-03 10:09:48 +0200 | [diff] [blame] | 484 | io_ctl->cur += (sizeof(u32) * io_ctl->num_pages); |
| 485 | io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 486 | |
David Sterba | 6994ca36 | 2020-09-15 13:32:34 +0200 | [diff] [blame] | 487 | put_unaligned_le64(generation, io_ctl->cur); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 488 | io_ctl->cur += sizeof(u64); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 489 | } |
| 490 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 491 | static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 492 | { |
David Sterba | 6994ca36 | 2020-09-15 13:32:34 +0200 | [diff] [blame] | 493 | u64 cache_gen; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 494 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 495 | /* |
| 496 | * Skip the crc area. If we don't check crcs then we just have a 64bit |
| 497 | * chunk at the front of the first page. |
| 498 | */ |
Nikolay Borisov | 7dbdb44 | 2020-12-03 10:09:48 +0200 | [diff] [blame] | 499 | io_ctl->cur += sizeof(u32) * io_ctl->num_pages; |
| 500 | io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 501 | |
David Sterba | 6994ca36 | 2020-09-15 13:32:34 +0200 | [diff] [blame] | 502 | cache_gen = get_unaligned_le64(io_ctl->cur); |
| 503 | if (cache_gen != generation) { |
Jeff Mahoney | f15376d | 2016-06-22 18:56:18 -0400 | [diff] [blame] | 504 | btrfs_err_rl(io_ctl->fs_info, |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 505 | "space cache generation (%llu) does not match inode (%llu)", |
David Sterba | 6994ca36 | 2020-09-15 13:32:34 +0200 | [diff] [blame] | 506 | cache_gen, generation); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 507 | io_ctl_unmap_page(io_ctl); |
| 508 | return -EIO; |
| 509 | } |
| 510 | io_ctl->cur += sizeof(u64); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 511 | return 0; |
| 512 | } |
| 513 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 514 | static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 515 | { |
| 516 | u32 *tmp; |
| 517 | u32 crc = ~(u32)0; |
| 518 | unsigned offset = 0; |
| 519 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 520 | if (index == 0) |
Justin P. Mattock | cb54f25 | 2011-11-21 08:43:28 -0800 | [diff] [blame] | 521 | offset = sizeof(u32) * io_ctl->num_pages; |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 522 | |
Johannes Thumshirn | 4bb3c2e | 2019-05-22 10:19:00 +0200 | [diff] [blame] | 523 | crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset); |
| 524 | btrfs_crc32c_final(crc, (u8 *)&crc); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 525 | io_ctl_unmap_page(io_ctl); |
Chris Mason | 2b10826 | 2015-04-06 07:48:20 -0700 | [diff] [blame] | 526 | tmp = page_address(io_ctl->pages[0]); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 527 | tmp += index; |
| 528 | *tmp = crc; |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 529 | } |
| 530 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 531 | static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 532 | { |
| 533 | u32 *tmp, val; |
| 534 | u32 crc = ~(u32)0; |
| 535 | unsigned offset = 0; |
| 536 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 537 | if (index == 0) |
| 538 | offset = sizeof(u32) * io_ctl->num_pages; |
| 539 | |
Chris Mason | 2b10826 | 2015-04-06 07:48:20 -0700 | [diff] [blame] | 540 | tmp = page_address(io_ctl->pages[0]); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 541 | tmp += index; |
| 542 | val = *tmp; |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 543 | |
| 544 | io_ctl_map_page(io_ctl, 0); |
Johannes Thumshirn | 4bb3c2e | 2019-05-22 10:19:00 +0200 | [diff] [blame] | 545 | crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset); |
| 546 | btrfs_crc32c_final(crc, (u8 *)&crc); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 547 | if (val != crc) { |
Jeff Mahoney | f15376d | 2016-06-22 18:56:18 -0400 | [diff] [blame] | 548 | btrfs_err_rl(io_ctl->fs_info, |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 549 | "csum mismatch on free space cache"); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 550 | io_ctl_unmap_page(io_ctl); |
| 551 | return -EIO; |
| 552 | } |
| 553 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 554 | return 0; |
| 555 | } |
| 556 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 557 | static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes, |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 558 | void *bitmap) |
| 559 | { |
| 560 | struct btrfs_free_space_entry *entry; |
| 561 | |
| 562 | if (!io_ctl->cur) |
| 563 | return -ENOSPC; |
| 564 | |
| 565 | entry = io_ctl->cur; |
David Sterba | 6994ca36 | 2020-09-15 13:32:34 +0200 | [diff] [blame] | 566 | put_unaligned_le64(offset, &entry->offset); |
| 567 | put_unaligned_le64(bytes, &entry->bytes); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 568 | entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP : |
| 569 | BTRFS_FREE_SPACE_EXTENT; |
| 570 | io_ctl->cur += sizeof(struct btrfs_free_space_entry); |
| 571 | io_ctl->size -= sizeof(struct btrfs_free_space_entry); |
| 572 | |
| 573 | if (io_ctl->size >= sizeof(struct btrfs_free_space_entry)) |
| 574 | return 0; |
| 575 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 576 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 577 | |
| 578 | /* No more pages to map */ |
| 579 | if (io_ctl->index >= io_ctl->num_pages) |
| 580 | return 0; |
| 581 | |
| 582 | /* map the next page */ |
| 583 | io_ctl_map_page(io_ctl, 1); |
| 584 | return 0; |
| 585 | } |
| 586 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 587 | static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 588 | { |
| 589 | if (!io_ctl->cur) |
| 590 | return -ENOSPC; |
| 591 | |
| 592 | /* |
| 593 | * If we aren't at the start of the current page, unmap this one and |
| 594 | * map the next one if there is any left. |
| 595 | */ |
| 596 | if (io_ctl->cur != io_ctl->orig) { |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 597 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 598 | if (io_ctl->index >= io_ctl->num_pages) |
| 599 | return -ENOSPC; |
| 600 | io_ctl_map_page(io_ctl, 0); |
| 601 | } |
| 602 | |
David Sterba | 69d2480 | 2018-06-29 10:56:44 +0200 | [diff] [blame] | 603 | copy_page(io_ctl->cur, bitmap); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 604 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 605 | if (io_ctl->index < io_ctl->num_pages) |
| 606 | io_ctl_map_page(io_ctl, 0); |
| 607 | return 0; |
| 608 | } |
| 609 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 610 | static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 611 | { |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 612 | /* |
| 613 | * If we're not on the boundary we know we've modified the page and we |
| 614 | * need to crc the page. |
| 615 | */ |
| 616 | if (io_ctl->cur != io_ctl->orig) |
| 617 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); |
| 618 | else |
| 619 | io_ctl_unmap_page(io_ctl); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 620 | |
| 621 | while (io_ctl->index < io_ctl->num_pages) { |
| 622 | io_ctl_map_page(io_ctl, 1); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 623 | io_ctl_set_crc(io_ctl, io_ctl->index - 1); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 627 | static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl, |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 628 | struct btrfs_free_space *entry, u8 *type) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 629 | { |
| 630 | struct btrfs_free_space_entry *e; |
Josef Bacik | 2f120c0 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 631 | int ret; |
| 632 | |
| 633 | if (!io_ctl->cur) { |
| 634 | ret = io_ctl_check_crc(io_ctl, io_ctl->index); |
| 635 | if (ret) |
| 636 | return ret; |
| 637 | } |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 638 | |
| 639 | e = io_ctl->cur; |
David Sterba | 6994ca36 | 2020-09-15 13:32:34 +0200 | [diff] [blame] | 640 | entry->offset = get_unaligned_le64(&e->offset); |
| 641 | entry->bytes = get_unaligned_le64(&e->bytes); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 642 | *type = e->type; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 643 | io_ctl->cur += sizeof(struct btrfs_free_space_entry); |
| 644 | io_ctl->size -= sizeof(struct btrfs_free_space_entry); |
| 645 | |
| 646 | if (io_ctl->size >= sizeof(struct btrfs_free_space_entry)) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 647 | return 0; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 648 | |
| 649 | io_ctl_unmap_page(io_ctl); |
| 650 | |
Josef Bacik | 2f120c0 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 651 | return 0; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 652 | } |
| 653 | |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 654 | static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl, |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 655 | struct btrfs_free_space *entry) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 656 | { |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 657 | int ret; |
| 658 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 659 | ret = io_ctl_check_crc(io_ctl, io_ctl->index); |
| 660 | if (ret) |
| 661 | return ret; |
| 662 | |
David Sterba | 69d2480 | 2018-06-29 10:56:44 +0200 | [diff] [blame] | 663 | copy_page(entry->bitmap, io_ctl->cur); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 664 | io_ctl_unmap_page(io_ctl); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 665 | |
| 666 | return 0; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 667 | } |
| 668 | |
David Sterba | fa598b0 | 2020-12-03 17:18:38 +0100 | [diff] [blame] | 669 | static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl) |
| 670 | { |
| 671 | struct btrfs_block_group *block_group = ctl->private; |
| 672 | u64 max_bytes; |
| 673 | u64 bitmap_bytes; |
| 674 | u64 extent_bytes; |
| 675 | u64 size = block_group->length; |
| 676 | u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit; |
| 677 | u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg); |
| 678 | |
| 679 | max_bitmaps = max_t(u64, max_bitmaps, 1); |
| 680 | |
| 681 | ASSERT(ctl->total_bitmaps <= max_bitmaps); |
| 682 | |
| 683 | /* |
| 684 | * We are trying to keep the total amount of memory used per 1GiB of |
| 685 | * space to be MAX_CACHE_BYTES_PER_GIG. However, with a reclamation |
| 686 | * mechanism of pulling extents >= FORCE_EXTENT_THRESHOLD out of |
| 687 | * bitmaps, we may end up using more memory than this. |
| 688 | */ |
| 689 | if (size < SZ_1G) |
| 690 | max_bytes = MAX_CACHE_BYTES_PER_GIG; |
| 691 | else |
| 692 | max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G); |
| 693 | |
| 694 | bitmap_bytes = ctl->total_bitmaps * ctl->unit; |
| 695 | |
| 696 | /* |
| 697 | * we want the extent entry threshold to always be at most 1/2 the max |
| 698 | * bytes we can have, or whatever is less than that. |
| 699 | */ |
| 700 | extent_bytes = max_bytes - bitmap_bytes; |
| 701 | extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1); |
| 702 | |
| 703 | ctl->extents_thresh = |
| 704 | div_u64(extent_bytes, sizeof(struct btrfs_free_space)); |
| 705 | } |
| 706 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 707 | static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode, |
| 708 | struct btrfs_free_space_ctl *ctl, |
| 709 | struct btrfs_path *path, u64 offset) |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 710 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 711 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 712 | struct btrfs_free_space_header *header; |
| 713 | struct extent_buffer *leaf; |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 714 | struct btrfs_io_ctl io_ctl; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 715 | struct btrfs_key key; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 716 | struct btrfs_free_space *e, *n; |
Gui Hecheng | b76808f | 2014-12-31 09:51:35 +0800 | [diff] [blame] | 717 | LIST_HEAD(bitmaps); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 718 | u64 num_entries; |
| 719 | u64 num_bitmaps; |
| 720 | u64 generation; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 721 | u8 type; |
Josef Bacik | f6a3982 | 2011-06-06 10:50:35 -0400 | [diff] [blame] | 722 | int ret = 0; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 723 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 724 | /* Nothing in the space cache, goodbye */ |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 725 | if (!i_size_read(inode)) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 726 | return 0; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 727 | |
| 728 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 729 | key.offset = offset; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 730 | key.type = 0; |
| 731 | |
| 732 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 733 | if (ret < 0) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 734 | return 0; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 735 | else if (ret > 0) { |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 736 | btrfs_release_path(path); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 737 | return 0; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 738 | } |
| 739 | |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 740 | ret = -1; |
| 741 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 742 | leaf = path->nodes[0]; |
| 743 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 744 | struct btrfs_free_space_header); |
| 745 | num_entries = btrfs_free_space_entries(leaf, header); |
| 746 | num_bitmaps = btrfs_free_space_bitmaps(leaf, header); |
| 747 | generation = btrfs_free_space_generation(leaf, header); |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 748 | btrfs_release_path(path); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 749 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 750 | if (!BTRFS_I(inode)->generation) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 751 | btrfs_info(fs_info, |
David Sterba | 913e153 | 2017-07-13 15:32:18 +0200 | [diff] [blame] | 752 | "the free space cache file (%llu) is invalid, skip it", |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 753 | offset); |
| 754 | return 0; |
| 755 | } |
| 756 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 757 | if (BTRFS_I(inode)->generation != generation) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 758 | btrfs_err(fs_info, |
| 759 | "free space inode generation (%llu) did not match free space cache generation (%llu)", |
| 760 | BTRFS_I(inode)->generation, generation); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 761 | return 0; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | if (!num_entries) |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 765 | return 0; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 766 | |
Jeff Mahoney | f15376d | 2016-06-22 18:56:18 -0400 | [diff] [blame] | 767 | ret = io_ctl_init(&io_ctl, inode, 0); |
Li Zefan | 706efc6 | 2012-01-09 14:36:28 +0800 | [diff] [blame] | 768 | if (ret) |
| 769 | return ret; |
| 770 | |
David Sterba | 1d48053 | 2017-01-23 17:28:19 +0100 | [diff] [blame] | 771 | readahead_cache(inode); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 772 | |
Johannes Thumshirn | 7a195f6 | 2020-02-12 00:10:20 +0900 | [diff] [blame] | 773 | ret = io_ctl_prepare_pages(&io_ctl, true); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 774 | if (ret) |
| 775 | goto out; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 776 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 777 | ret = io_ctl_check_crc(&io_ctl, 0); |
| 778 | if (ret) |
| 779 | goto free_cache; |
| 780 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 781 | ret = io_ctl_check_generation(&io_ctl, generation); |
| 782 | if (ret) |
| 783 | goto free_cache; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 784 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 785 | while (num_entries) { |
| 786 | e = kmem_cache_zalloc(btrfs_free_space_cachep, |
| 787 | GFP_NOFS); |
Zhihao Cheng | 3cc64e7 | 2020-11-20 09:08:04 +0800 | [diff] [blame] | 788 | if (!e) { |
| 789 | ret = -ENOMEM; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 790 | goto free_cache; |
Zhihao Cheng | 3cc64e7 | 2020-11-20 09:08:04 +0800 | [diff] [blame] | 791 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 792 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 793 | ret = io_ctl_read_entry(&io_ctl, e, &type); |
| 794 | if (ret) { |
| 795 | kmem_cache_free(btrfs_free_space_cachep, e); |
| 796 | goto free_cache; |
| 797 | } |
| 798 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 799 | if (!e->bytes) { |
Zhihao Cheng | 3cc64e7 | 2020-11-20 09:08:04 +0800 | [diff] [blame] | 800 | ret = -1; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 801 | kmem_cache_free(btrfs_free_space_cachep, e); |
| 802 | goto free_cache; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 803 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 804 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 805 | if (type == BTRFS_FREE_SPACE_EXTENT) { |
| 806 | spin_lock(&ctl->tree_lock); |
| 807 | ret = link_free_space(ctl, e); |
| 808 | spin_unlock(&ctl->tree_lock); |
| 809 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 810 | btrfs_err(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 811 | "Duplicate entries in free space cache, dumping"); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 812 | kmem_cache_free(btrfs_free_space_cachep, e); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 813 | goto free_cache; |
| 814 | } |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 815 | } else { |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 816 | ASSERT(num_bitmaps); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 817 | num_bitmaps--; |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 818 | e->bitmap = kmem_cache_zalloc( |
| 819 | btrfs_free_space_bitmap_cachep, GFP_NOFS); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 820 | if (!e->bitmap) { |
Zhihao Cheng | 3cc64e7 | 2020-11-20 09:08:04 +0800 | [diff] [blame] | 821 | ret = -ENOMEM; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 822 | kmem_cache_free( |
| 823 | btrfs_free_space_cachep, e); |
| 824 | goto free_cache; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 825 | } |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 826 | spin_lock(&ctl->tree_lock); |
| 827 | ret = link_free_space(ctl, e); |
| 828 | ctl->total_bitmaps++; |
David Sterba | fa598b0 | 2020-12-03 17:18:38 +0100 | [diff] [blame] | 829 | recalculate_thresholds(ctl); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 830 | spin_unlock(&ctl->tree_lock); |
| 831 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 832 | btrfs_err(fs_info, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 833 | "Duplicate entries in free space cache, dumping"); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 834 | kmem_cache_free(btrfs_free_space_cachep, e); |
| 835 | goto free_cache; |
| 836 | } |
| 837 | list_add_tail(&e->list, &bitmaps); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 838 | } |
| 839 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 840 | num_entries--; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 841 | } |
| 842 | |
Josef Bacik | 2f120c0 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 843 | io_ctl_unmap_page(&io_ctl); |
| 844 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 845 | /* |
| 846 | * We add the bitmaps at the end of the entries in order that |
| 847 | * the bitmap entries are added to the cache. |
| 848 | */ |
| 849 | list_for_each_entry_safe(e, n, &bitmaps, list) { |
| 850 | list_del_init(&e->list); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 851 | ret = io_ctl_read_bitmap(&io_ctl, e); |
| 852 | if (ret) |
| 853 | goto free_cache; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | io_ctl_drop_pages(&io_ctl); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 857 | ret = 1; |
| 858 | out: |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 859 | io_ctl_free(&io_ctl); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 860 | return ret; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 861 | free_cache: |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 862 | io_ctl_drop_pages(&io_ctl); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 863 | __btrfs_remove_free_space_cache(ctl); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 864 | goto out; |
| 865 | } |
| 866 | |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 867 | static int copy_free_space_cache(struct btrfs_block_group *block_group, |
| 868 | struct btrfs_free_space_ctl *ctl) |
| 869 | { |
| 870 | struct btrfs_free_space *info; |
| 871 | struct rb_node *n; |
| 872 | int ret = 0; |
| 873 | |
| 874 | while (!ret && (n = rb_first(&ctl->free_space_offset)) != NULL) { |
| 875 | info = rb_entry(n, struct btrfs_free_space, offset_index); |
| 876 | if (!info->bitmap) { |
| 877 | unlink_free_space(ctl, info); |
| 878 | ret = btrfs_add_free_space(block_group, info->offset, |
| 879 | info->bytes); |
| 880 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 881 | } else { |
| 882 | u64 offset = info->offset; |
| 883 | u64 bytes = ctl->unit; |
| 884 | |
| 885 | while (search_bitmap(ctl, info, &offset, &bytes, |
| 886 | false) == 0) { |
| 887 | ret = btrfs_add_free_space(block_group, offset, |
| 888 | bytes); |
| 889 | if (ret) |
| 890 | break; |
| 891 | bitmap_clear_bits(ctl, info, offset, bytes); |
| 892 | offset = info->offset; |
| 893 | bytes = ctl->unit; |
| 894 | } |
| 895 | free_bitmap(ctl, info); |
| 896 | } |
| 897 | cond_resched(); |
| 898 | } |
| 899 | return ret; |
| 900 | } |
| 901 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 902 | int load_free_space_cache(struct btrfs_block_group *block_group) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 903 | { |
David Sterba | bb6cb1c | 2019-03-20 13:47:15 +0100 | [diff] [blame] | 904 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 905 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 906 | struct btrfs_free_space_ctl tmp_ctl = {}; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 907 | struct inode *inode; |
| 908 | struct btrfs_path *path; |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 909 | int ret = 0; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 910 | bool matched; |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 911 | u64 used = block_group->used; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 912 | |
| 913 | /* |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 914 | * Because we could potentially discard our loaded free space, we want |
| 915 | * to load everything into a temporary structure first, and then if it's |
| 916 | * valid copy it all into the actual free space ctl. |
| 917 | */ |
| 918 | btrfs_init_free_space_ctl(block_group, &tmp_ctl); |
| 919 | |
| 920 | /* |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 921 | * If this block group has been marked to be cleared for one reason or |
| 922 | * another then we can't trust the on disk cache, so just return. |
| 923 | */ |
| 924 | spin_lock(&block_group->lock); |
| 925 | if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) { |
| 926 | spin_unlock(&block_group->lock); |
| 927 | return 0; |
| 928 | } |
| 929 | spin_unlock(&block_group->lock); |
| 930 | |
| 931 | path = btrfs_alloc_path(); |
| 932 | if (!path) |
| 933 | return 0; |
Josef Bacik | d53ba47 | 2012-04-12 16:03:57 -0400 | [diff] [blame] | 934 | path->search_commit_root = 1; |
| 935 | path->skip_locking = 1; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 936 | |
Filipe Manana | 4222ea7 | 2018-10-24 10:13:03 +0100 | [diff] [blame] | 937 | /* |
| 938 | * We must pass a path with search_commit_root set to btrfs_iget in |
| 939 | * order to avoid a deadlock when allocating extents for the tree root. |
| 940 | * |
| 941 | * When we are COWing an extent buffer from the tree root, when looking |
| 942 | * for a free extent, at extent-tree.c:find_free_extent(), we can find |
| 943 | * block group without its free space cache loaded. When we find one |
| 944 | * we must load its space cache which requires reading its free space |
| 945 | * cache's inode item from the root tree. If this inode item is located |
| 946 | * in the same leaf that we started COWing before, then we end up in |
| 947 | * deadlock on the extent buffer (trying to read lock it when we |
| 948 | * previously write locked it). |
| 949 | * |
| 950 | * It's safe to read the inode item using the commit root because |
| 951 | * block groups, once loaded, stay in memory forever (until they are |
| 952 | * removed) as well as their space caches once loaded. New block groups |
| 953 | * once created get their ->cached field set to BTRFS_CACHE_FINISHED so |
| 954 | * we will never try to read their inode item while the fs is mounted. |
| 955 | */ |
David Sterba | 7949f33 | 2019-03-20 13:40:19 +0100 | [diff] [blame] | 956 | inode = lookup_free_space_inode(block_group, path); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 957 | if (IS_ERR(inode)) { |
| 958 | btrfs_free_path(path); |
| 959 | return 0; |
| 960 | } |
| 961 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 962 | /* We may have converted the inode and made the cache invalid. */ |
| 963 | spin_lock(&block_group->lock); |
| 964 | if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) { |
| 965 | spin_unlock(&block_group->lock); |
Tsutomu Itoh | a7e221e | 2012-02-14 17:12:23 +0900 | [diff] [blame] | 966 | btrfs_free_path(path); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 967 | goto out; |
| 968 | } |
| 969 | spin_unlock(&block_group->lock); |
| 970 | |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 971 | ret = __load_free_space_cache(fs_info->tree_root, inode, &tmp_ctl, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 972 | path, block_group->start); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 973 | btrfs_free_path(path); |
| 974 | if (ret <= 0) |
| 975 | goto out; |
| 976 | |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 977 | matched = (tmp_ctl.free_space == (block_group->length - used - |
| 978 | block_group->bytes_super)); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 979 | |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 980 | if (matched) { |
| 981 | ret = copy_free_space_cache(block_group, &tmp_ctl); |
| 982 | /* |
| 983 | * ret == 1 means we successfully loaded the free space cache, |
| 984 | * so we need to re-set it here. |
| 985 | */ |
| 986 | if (ret == 0) |
| 987 | ret = 1; |
| 988 | } else { |
| 989 | __btrfs_remove_free_space_cache(&tmp_ctl); |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 990 | btrfs_warn(fs_info, |
| 991 | "block group %llu has wrong amount of free space", |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 992 | block_group->start); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 993 | ret = -1; |
| 994 | } |
| 995 | out: |
| 996 | if (ret < 0) { |
| 997 | /* This cache is bogus, make sure it gets cleared */ |
| 998 | spin_lock(&block_group->lock); |
| 999 | block_group->disk_cache_state = BTRFS_DC_CLEAR; |
| 1000 | spin_unlock(&block_group->lock); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1001 | ret = 0; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1002 | |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 1003 | btrfs_warn(fs_info, |
| 1004 | "failed to load free space cache for block group %llu, rebuilding it now", |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 1005 | block_group->start); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1006 | } |
| 1007 | |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 1008 | spin_lock(&ctl->tree_lock); |
| 1009 | btrfs_discard_update_discardable(block_group); |
| 1010 | spin_unlock(&ctl->tree_lock); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1011 | iput(inode); |
| 1012 | return ret; |
| 1013 | } |
| 1014 | |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1015 | static noinline_for_stack |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 1016 | int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl, |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1017 | struct btrfs_free_space_ctl *ctl, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1018 | struct btrfs_block_group *block_group, |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1019 | int *entries, int *bitmaps, |
| 1020 | struct list_head *bitmap_list) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1021 | { |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 1022 | int ret; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1023 | struct btrfs_free_cluster *cluster = NULL; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1024 | struct btrfs_free_cluster *cluster_locked = NULL; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1025 | struct rb_node *node = rb_first(&ctl->free_space_offset); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 1026 | struct btrfs_trim_range *trim_entry; |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 1027 | |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 1028 | /* Get the cluster for this block_group if it exists */ |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1029 | if (block_group && !list_empty(&block_group->cluster_list)) { |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 1030 | cluster = list_entry(block_group->cluster_list.next, |
| 1031 | struct btrfs_free_cluster, |
| 1032 | block_group_list); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1033 | } |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 1034 | |
Josef Bacik | f75b130 | 2011-10-05 10:00:18 -0400 | [diff] [blame] | 1035 | if (!node && cluster) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1036 | cluster_locked = cluster; |
| 1037 | spin_lock(&cluster_locked->lock); |
Josef Bacik | f75b130 | 2011-10-05 10:00:18 -0400 | [diff] [blame] | 1038 | node = rb_first(&cluster->root); |
| 1039 | cluster = NULL; |
| 1040 | } |
| 1041 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1042 | /* Write out the extent entries */ |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1043 | while (node) { |
| 1044 | struct btrfs_free_space *e; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1045 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1046 | e = rb_entry(node, struct btrfs_free_space, offset_index); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1047 | *entries += 1; |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 1048 | |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1049 | ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes, |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1050 | e->bitmap); |
| 1051 | if (ret) |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1052 | goto fail; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1053 | |
| 1054 | if (e->bitmap) { |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1055 | list_add_tail(&e->list, bitmap_list); |
| 1056 | *bitmaps += 1; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1057 | } |
| 1058 | node = rb_next(node); |
| 1059 | if (!node && cluster) { |
| 1060 | node = rb_first(&cluster->root); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1061 | cluster_locked = cluster; |
| 1062 | spin_lock(&cluster_locked->lock); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1063 | cluster = NULL; |
| 1064 | } |
| 1065 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1066 | if (cluster_locked) { |
| 1067 | spin_unlock(&cluster_locked->lock); |
| 1068 | cluster_locked = NULL; |
| 1069 | } |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 1070 | |
| 1071 | /* |
| 1072 | * Make sure we don't miss any range that was removed from our rbtree |
| 1073 | * because trimming is running. Otherwise after a umount+mount (or crash |
| 1074 | * after committing the transaction) we would leak free space and get |
| 1075 | * an inconsistent free space cache report from fsck. |
| 1076 | */ |
| 1077 | list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) { |
| 1078 | ret = io_ctl_add_entry(io_ctl, trim_entry->start, |
| 1079 | trim_entry->bytes, NULL); |
| 1080 | if (ret) |
| 1081 | goto fail; |
| 1082 | *entries += 1; |
| 1083 | } |
| 1084 | |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1085 | return 0; |
| 1086 | fail: |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1087 | if (cluster_locked) |
| 1088 | spin_unlock(&cluster_locked->lock); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1089 | return -ENOSPC; |
| 1090 | } |
| 1091 | |
| 1092 | static noinline_for_stack int |
| 1093 | update_cache_item(struct btrfs_trans_handle *trans, |
| 1094 | struct btrfs_root *root, |
| 1095 | struct inode *inode, |
| 1096 | struct btrfs_path *path, u64 offset, |
| 1097 | int entries, int bitmaps) |
| 1098 | { |
| 1099 | struct btrfs_key key; |
| 1100 | struct btrfs_free_space_header *header; |
| 1101 | struct extent_buffer *leaf; |
| 1102 | int ret; |
| 1103 | |
| 1104 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 1105 | key.offset = offset; |
| 1106 | key.type = 0; |
| 1107 | |
| 1108 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1109 | if (ret < 0) { |
| 1110 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 1111 | EXTENT_DELALLOC, 0, 0, NULL); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1112 | goto fail; |
| 1113 | } |
| 1114 | leaf = path->nodes[0]; |
| 1115 | if (ret > 0) { |
| 1116 | struct btrfs_key found_key; |
| 1117 | ASSERT(path->slots[0]); |
| 1118 | path->slots[0]--; |
| 1119 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1120 | if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID || |
| 1121 | found_key.offset != offset) { |
| 1122 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 1123 | inode->i_size - 1, EXTENT_DELALLOC, 0, |
| 1124 | 0, NULL); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1125 | btrfs_release_path(path); |
| 1126 | goto fail; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | BTRFS_I(inode)->generation = trans->transid; |
| 1131 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 1132 | struct btrfs_free_space_header); |
| 1133 | btrfs_set_free_space_entries(leaf, header, entries); |
| 1134 | btrfs_set_free_space_bitmaps(leaf, header, bitmaps); |
| 1135 | btrfs_set_free_space_generation(leaf, header, trans->transid); |
| 1136 | btrfs_mark_buffer_dirty(leaf); |
| 1137 | btrfs_release_path(path); |
| 1138 | |
| 1139 | return 0; |
| 1140 | |
| 1141 | fail: |
| 1142 | return -1; |
| 1143 | } |
| 1144 | |
David Sterba | 6701bdb | 2019-03-20 13:49:09 +0100 | [diff] [blame] | 1145 | static noinline_for_stack int write_pinned_extent_entries( |
Nikolay Borisov | 6b45f641 | 2020-01-20 16:09:15 +0200 | [diff] [blame] | 1146 | struct btrfs_trans_handle *trans, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1147 | struct btrfs_block_group *block_group, |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 1148 | struct btrfs_io_ctl *io_ctl, |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1149 | int *entries) |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1150 | { |
| 1151 | u64 start, extent_start, extent_end, len; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1152 | struct extent_io_tree *unpin = NULL; |
| 1153 | int ret; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1154 | |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1155 | if (!block_group) |
| 1156 | return 0; |
| 1157 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1158 | /* |
| 1159 | * We want to add any pinned extents to our free space cache |
| 1160 | * so we don't leak the space |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1161 | * |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1162 | * We shouldn't have switched the pinned extents yet so this is the |
| 1163 | * right one |
| 1164 | */ |
Nikolay Borisov | fe119a6 | 2020-01-20 16:09:18 +0200 | [diff] [blame] | 1165 | unpin = &trans->transaction->pinned_extents; |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1166 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 1167 | start = block_group->start; |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1168 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 1169 | while (start < block_group->start + block_group->length) { |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1170 | ret = find_first_extent_bit(unpin, start, |
| 1171 | &extent_start, &extent_end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1172 | EXTENT_DIRTY, NULL); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1173 | if (ret) |
| 1174 | return 0; |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 1175 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1176 | /* This pinned extent is out of our range */ |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 1177 | if (extent_start >= block_group->start + block_group->length) |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1178 | return 0; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1179 | |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1180 | extent_start = max(extent_start, start); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 1181 | extent_end = min(block_group->start + block_group->length, |
| 1182 | extent_end + 1); |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1183 | len = extent_end - extent_start; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1184 | |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1185 | *entries += 1; |
| 1186 | ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1187 | if (ret) |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1188 | return -ENOSPC; |
Josef Bacik | 2f35612 | 2011-06-10 15:31:13 -0400 | [diff] [blame] | 1189 | |
Li Zefan | db804f2 | 2012-01-10 16:41:01 +0800 | [diff] [blame] | 1190 | start = extent_end; |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1191 | } |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1192 | |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | static noinline_for_stack int |
Chris Mason | 4c6d1d8 | 2015-04-06 13:17:20 -0700 | [diff] [blame] | 1197 | write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list) |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1198 | { |
Geliang Tang | 7ae1681 | 2015-12-18 22:17:00 +0800 | [diff] [blame] | 1199 | struct btrfs_free_space *entry, *next; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1200 | int ret; |
| 1201 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1202 | /* Write out the bitmaps */ |
Geliang Tang | 7ae1681 | 2015-12-18 22:17:00 +0800 | [diff] [blame] | 1203 | list_for_each_entry_safe(entry, next, bitmap_list, list) { |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1204 | ret = io_ctl_add_bitmap(io_ctl, entry->bitmap); |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1205 | if (ret) |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1206 | return -ENOSPC; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1207 | list_del_init(&entry->list); |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 1208 | } |
| 1209 | |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1210 | return 0; |
| 1211 | } |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1212 | |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1213 | static int flush_dirty_cache(struct inode *inode) |
| 1214 | { |
| 1215 | int ret; |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 1216 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 1217 | ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1218 | if (ret) |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 1219 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 1220 | EXTENT_DELALLOC, 0, 0, NULL); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1221 | |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1222 | return ret; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | static void noinline_for_stack |
Chris Mason | a3bdccc | 2015-04-24 11:00:00 -0700 | [diff] [blame] | 1226 | cleanup_bitmap_list(struct list_head *bitmap_list) |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1227 | { |
Geliang Tang | 7ae1681 | 2015-12-18 22:17:00 +0800 | [diff] [blame] | 1228 | struct btrfs_free_space *entry, *next; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1229 | |
Geliang Tang | 7ae1681 | 2015-12-18 22:17:00 +0800 | [diff] [blame] | 1230 | list_for_each_entry_safe(entry, next, bitmap_list, list) |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1231 | list_del_init(&entry->list); |
Chris Mason | a3bdccc | 2015-04-24 11:00:00 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | static void noinline_for_stack |
| 1235 | cleanup_write_cache_enospc(struct inode *inode, |
| 1236 | struct btrfs_io_ctl *io_ctl, |
David Sterba | 7bf1a15 | 2017-02-10 20:23:00 +0100 | [diff] [blame] | 1237 | struct extent_state **cached_state) |
Chris Mason | a3bdccc | 2015-04-24 11:00:00 -0700 | [diff] [blame] | 1238 | { |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1239 | io_ctl_drop_pages(io_ctl); |
| 1240 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 1241 | i_size_read(inode) - 1, cached_state); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 1244 | static int __btrfs_wait_cache_io(struct btrfs_root *root, |
| 1245 | struct btrfs_trans_handle *trans, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1246 | struct btrfs_block_group *block_group, |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 1247 | struct btrfs_io_ctl *io_ctl, |
| 1248 | struct btrfs_path *path, u64 offset) |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1249 | { |
| 1250 | int ret; |
| 1251 | struct inode *inode = io_ctl->inode; |
| 1252 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1253 | if (!inode) |
| 1254 | return 0; |
| 1255 | |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1256 | /* Flush the dirty pages in the cache file. */ |
| 1257 | ret = flush_dirty_cache(inode); |
| 1258 | if (ret) |
| 1259 | goto out; |
| 1260 | |
| 1261 | /* Update the cache item to tell everyone this cache file is valid. */ |
| 1262 | ret = update_cache_item(trans, root, inode, path, offset, |
| 1263 | io_ctl->entries, io_ctl->bitmaps); |
| 1264 | out: |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1265 | if (ret) { |
| 1266 | invalidate_inode_pages2(inode->i_mapping); |
| 1267 | BTRFS_I(inode)->generation = 0; |
Filipe Manana | bbcd1f4 | 2020-05-18 17:34:23 +0100 | [diff] [blame] | 1268 | if (block_group) |
| 1269 | btrfs_debug(root->fs_info, |
Filipe Manana | 2e69a7a | 2020-05-18 17:34:11 +0100 | [diff] [blame] | 1270 | "failed to write free space cache for block group %llu error %d", |
| 1271 | block_group->start, ret); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1272 | } |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 1273 | btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1274 | |
| 1275 | if (block_group) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1276 | /* the dirty list is protected by the dirty_bgs_lock */ |
| 1277 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 1278 | |
| 1279 | /* the disk_cache_state is protected by the block group lock */ |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1280 | spin_lock(&block_group->lock); |
| 1281 | |
| 1282 | /* |
| 1283 | * only mark this as written if we didn't get put back on |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1284 | * the dirty list while waiting for IO. Otherwise our |
| 1285 | * cache state won't be right, and we won't get written again |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1286 | */ |
| 1287 | if (!ret && list_empty(&block_group->dirty_list)) |
| 1288 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 1289 | else if (ret) |
| 1290 | block_group->disk_cache_state = BTRFS_DC_ERROR; |
| 1291 | |
| 1292 | spin_unlock(&block_group->lock); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1293 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1294 | io_ctl->inode = NULL; |
| 1295 | iput(inode); |
| 1296 | } |
| 1297 | |
| 1298 | return ret; |
| 1299 | |
| 1300 | } |
| 1301 | |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 1302 | int btrfs_wait_cache_io(struct btrfs_trans_handle *trans, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1303 | struct btrfs_block_group *block_group, |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 1304 | struct btrfs_path *path) |
| 1305 | { |
| 1306 | return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans, |
| 1307 | block_group, &block_group->io_ctl, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 1308 | path, block_group->start); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 1309 | } |
| 1310 | |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1311 | /** |
Nikolay Borisov | f092cf3 | 2021-01-22 11:57:56 +0200 | [diff] [blame] | 1312 | * Write out cached info to an inode |
| 1313 | * |
| 1314 | * @root: root the inode belongs to |
| 1315 | * @inode: freespace inode we are writing out |
| 1316 | * @ctl: free space cache we are going to write out |
| 1317 | * @block_group: block_group for this cache if it belongs to a block_group |
| 1318 | * @io_ctl: holds context for the io |
| 1319 | * @trans: the trans handle |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1320 | * |
| 1321 | * This function writes out a free space cache struct to disk for quick recovery |
Geliang Tang | 8cd1e73 | 2015-10-04 17:05:32 +0800 | [diff] [blame] | 1322 | * on mount. This will return 0 if it was successful in writing the cache out, |
Omar Sandoval | b860545 | 2015-02-24 02:47:06 -0800 | [diff] [blame] | 1323 | * or an errno if it was not. |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1324 | */ |
| 1325 | static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, |
| 1326 | struct btrfs_free_space_ctl *ctl, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1327 | struct btrfs_block_group *block_group, |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1328 | struct btrfs_io_ctl *io_ctl, |
David Sterba | 0e8d931 | 2017-02-10 20:26:24 +0100 | [diff] [blame] | 1329 | struct btrfs_trans_handle *trans) |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1330 | { |
| 1331 | struct extent_state *cached_state = NULL; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1332 | LIST_HEAD(bitmap_list); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1333 | int entries = 0; |
| 1334 | int bitmaps = 0; |
| 1335 | int ret; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1336 | int must_iput = 0; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1337 | |
| 1338 | if (!i_size_read(inode)) |
Omar Sandoval | b860545 | 2015-02-24 02:47:06 -0800 | [diff] [blame] | 1339 | return -EIO; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1340 | |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1341 | WARN_ON(io_ctl->pages); |
Jeff Mahoney | f15376d | 2016-06-22 18:56:18 -0400 | [diff] [blame] | 1342 | ret = io_ctl_init(io_ctl, inode, 1); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1343 | if (ret) |
Omar Sandoval | b860545 | 2015-02-24 02:47:06 -0800 | [diff] [blame] | 1344 | return ret; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1345 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1346 | if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) { |
| 1347 | down_write(&block_group->data_rwsem); |
| 1348 | spin_lock(&block_group->lock); |
| 1349 | if (block_group->delalloc_bytes) { |
| 1350 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 1351 | spin_unlock(&block_group->lock); |
| 1352 | up_write(&block_group->data_rwsem); |
| 1353 | BTRFS_I(inode)->generation = 0; |
| 1354 | ret = 0; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1355 | must_iput = 1; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1356 | goto out; |
| 1357 | } |
| 1358 | spin_unlock(&block_group->lock); |
| 1359 | } |
| 1360 | |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1361 | /* Lock all pages first so we can lock the extent safely. */ |
Johannes Thumshirn | 7a195f6 | 2020-02-12 00:10:20 +0900 | [diff] [blame] | 1362 | ret = io_ctl_prepare_pages(io_ctl, false); |
Omar Sandoval | b860545 | 2015-02-24 02:47:06 -0800 | [diff] [blame] | 1363 | if (ret) |
Josef Bacik | b77000e | 2017-11-15 16:20:52 -0500 | [diff] [blame] | 1364 | goto out_unlock; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1365 | |
| 1366 | lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1367 | &cached_state); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1368 | |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1369 | io_ctl_set_generation(io_ctl, trans->transid); |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1370 | |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 1371 | mutex_lock(&ctl->cache_writeout_mutex); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1372 | /* Write out the extent entries in the free space cache */ |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1373 | spin_lock(&ctl->tree_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1374 | ret = write_cache_extent_entries(io_ctl, ctl, |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1375 | block_group, &entries, &bitmaps, |
| 1376 | &bitmap_list); |
Chris Mason | a3bdccc | 2015-04-24 11:00:00 -0700 | [diff] [blame] | 1377 | if (ret) |
| 1378 | goto out_nospc_locked; |
Chris Mason | d4452bc | 2014-05-19 20:47:56 -0700 | [diff] [blame] | 1379 | |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1380 | /* |
| 1381 | * Some spaces that are freed in the current transaction are pinned, |
| 1382 | * they will be added into free space cache after the transaction is |
| 1383 | * committed, we shouldn't lose them. |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1384 | * |
| 1385 | * If this changes while we are working we'll get added back to |
| 1386 | * the dirty list and redo it. No locking needed |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1387 | */ |
Nikolay Borisov | 6b45f641 | 2020-01-20 16:09:15 +0200 | [diff] [blame] | 1388 | ret = write_pinned_extent_entries(trans, block_group, io_ctl, &entries); |
Chris Mason | a3bdccc | 2015-04-24 11:00:00 -0700 | [diff] [blame] | 1389 | if (ret) |
| 1390 | goto out_nospc_locked; |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1391 | |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 1392 | /* |
| 1393 | * At last, we write out all the bitmaps and keep cache_writeout_mutex |
| 1394 | * locked while doing it because a concurrent trim can be manipulating |
| 1395 | * or freeing the bitmap. |
| 1396 | */ |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1397 | ret = write_bitmap_entries(io_ctl, &bitmap_list); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 1398 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 1399 | mutex_unlock(&ctl->cache_writeout_mutex); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1400 | if (ret) |
| 1401 | goto out_nospc; |
| 1402 | |
| 1403 | /* Zero out the rest of the pages just to make sure */ |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1404 | io_ctl_zero_remaining_pages(io_ctl); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1405 | |
| 1406 | /* Everything is written out, now we dirty the pages in the file. */ |
Nikolay Borisov | 088545f | 2020-06-03 08:55:36 +0300 | [diff] [blame] | 1407 | ret = btrfs_dirty_pages(BTRFS_I(inode), io_ctl->pages, |
| 1408 | io_ctl->num_pages, 0, i_size_read(inode), |
Goldwyn Rodrigues | aa8c1a4 | 2020-10-14 09:55:45 -0500 | [diff] [blame] | 1409 | &cached_state, false); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1410 | if (ret) |
| 1411 | goto out_nospc; |
| 1412 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1413 | if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) |
| 1414 | up_write(&block_group->data_rwsem); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1415 | /* |
| 1416 | * Release the pages and unlock the extent, we will flush |
| 1417 | * them out later |
| 1418 | */ |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1419 | io_ctl_drop_pages(io_ctl); |
Filipe Manana | bbc37d6 | 2020-08-14 11:04:09 +0100 | [diff] [blame] | 1420 | io_ctl_free(io_ctl); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1421 | |
| 1422 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 1423 | i_size_read(inode) - 1, &cached_state); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1424 | |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1425 | /* |
| 1426 | * at this point the pages are under IO and we're happy, |
Randy Dunlap | 260db43 | 2020-08-04 19:48:34 -0700 | [diff] [blame] | 1427 | * The caller is responsible for waiting on them and updating |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1428 | * the cache and the inode |
| 1429 | */ |
| 1430 | io_ctl->entries = entries; |
| 1431 | io_ctl->bitmaps = bitmaps; |
| 1432 | |
| 1433 | ret = btrfs_fdatawrite_range(inode, 0, (u64)-1); |
Miao Xie | 5349d6c | 2014-06-19 10:42:49 +0800 | [diff] [blame] | 1434 | if (ret) |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 1435 | goto out; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1436 | |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1437 | return 0; |
| 1438 | |
Chris Mason | a3bdccc | 2015-04-24 11:00:00 -0700 | [diff] [blame] | 1439 | out_nospc_locked: |
| 1440 | cleanup_bitmap_list(&bitmap_list); |
| 1441 | spin_unlock(&ctl->tree_lock); |
| 1442 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 1443 | |
Josef Bacik | a67509c | 2011-10-05 15:18:58 -0400 | [diff] [blame] | 1444 | out_nospc: |
David Sterba | 7bf1a15 | 2017-02-10 20:23:00 +0100 | [diff] [blame] | 1445 | cleanup_write_cache_enospc(inode, io_ctl, &cached_state); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1446 | |
Josef Bacik | b77000e | 2017-11-15 16:20:52 -0500 | [diff] [blame] | 1447 | out_unlock: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1448 | if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) |
| 1449 | up_write(&block_group->data_rwsem); |
| 1450 | |
Johannes Thumshirn | fd8efa8 | 2020-02-12 00:10:22 +0900 | [diff] [blame] | 1451 | out: |
| 1452 | io_ctl->inode = NULL; |
| 1453 | io_ctl_free(io_ctl); |
| 1454 | if (ret) { |
| 1455 | invalidate_inode_pages2(inode->i_mapping); |
| 1456 | BTRFS_I(inode)->generation = 0; |
| 1457 | } |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 1458 | btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Johannes Thumshirn | fd8efa8 | 2020-02-12 00:10:22 +0900 | [diff] [blame] | 1459 | if (must_iput) |
| 1460 | iput(inode); |
| 1461 | return ret; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1462 | } |
| 1463 | |
David Sterba | fe04153 | 2019-03-20 13:51:56 +0100 | [diff] [blame] | 1464 | int btrfs_write_out_cache(struct btrfs_trans_handle *trans, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1465 | struct btrfs_block_group *block_group, |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1466 | struct btrfs_path *path) |
| 1467 | { |
David Sterba | fe04153 | 2019-03-20 13:51:56 +0100 | [diff] [blame] | 1468 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1469 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
| 1470 | struct inode *inode; |
| 1471 | int ret = 0; |
| 1472 | |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1473 | spin_lock(&block_group->lock); |
| 1474 | if (block_group->disk_cache_state < BTRFS_DC_SETUP) { |
| 1475 | spin_unlock(&block_group->lock); |
| 1476 | return 0; |
| 1477 | } |
| 1478 | spin_unlock(&block_group->lock); |
| 1479 | |
David Sterba | 7949f33 | 2019-03-20 13:40:19 +0100 | [diff] [blame] | 1480 | inode = lookup_free_space_inode(block_group, path); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1481 | if (IS_ERR(inode)) |
| 1482 | return 0; |
| 1483 | |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 1484 | ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl, |
| 1485 | block_group, &block_group->io_ctl, trans); |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 1486 | if (ret) { |
Filipe Manana | bbcd1f4 | 2020-05-18 17:34:23 +0100 | [diff] [blame] | 1487 | btrfs_debug(fs_info, |
Filipe Manana | 2e69a7a | 2020-05-18 17:34:11 +0100 | [diff] [blame] | 1488 | "failed to write free space cache for block group %llu error %d", |
| 1489 | block_group->start, ret); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1490 | spin_lock(&block_group->lock); |
| 1491 | block_group->disk_cache_state = BTRFS_DC_ERROR; |
| 1492 | spin_unlock(&block_group->lock); |
| 1493 | |
| 1494 | block_group->io_ctl.inode = NULL; |
| 1495 | iput(inode); |
Li Zefan | 0414efa | 2011-04-20 10:20:14 +0800 | [diff] [blame] | 1496 | } |
| 1497 | |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 1498 | /* |
| 1499 | * if ret == 0 the caller is expected to call btrfs_wait_cache_io |
| 1500 | * to wait for IO and put the inode |
| 1501 | */ |
| 1502 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1503 | return ret; |
| 1504 | } |
| 1505 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1506 | static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1507 | u64 offset) |
| 1508 | { |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 1509 | ASSERT(offset >= bitmap_start); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1510 | offset -= bitmap_start; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1511 | return (unsigned long)(div_u64(offset, unit)); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1512 | } |
| 1513 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1514 | static inline unsigned long bytes_to_bits(u64 bytes, u32 unit) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1515 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1516 | return (unsigned long)(div_u64(bytes, unit)); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1517 | } |
| 1518 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1519 | static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1520 | u64 offset) |
| 1521 | { |
| 1522 | u64 bitmap_start; |
Feifei Xu | 0ef6447 | 2016-06-01 19:18:24 +0800 | [diff] [blame] | 1523 | u64 bytes_per_bitmap; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1524 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1525 | bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit; |
| 1526 | bitmap_start = offset - ctl->start; |
Feifei Xu | 0ef6447 | 2016-06-01 19:18:24 +0800 | [diff] [blame] | 1527 | bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1528 | bitmap_start *= bytes_per_bitmap; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1529 | bitmap_start += ctl->start; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1530 | |
| 1531 | return bitmap_start; |
| 1532 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1533 | |
| 1534 | static int tree_insert_offset(struct rb_root *root, u64 offset, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1535 | struct rb_node *node, int bitmap) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1536 | { |
| 1537 | struct rb_node **p = &root->rb_node; |
| 1538 | struct rb_node *parent = NULL; |
| 1539 | struct btrfs_free_space *info; |
| 1540 | |
| 1541 | while (*p) { |
| 1542 | parent = *p; |
| 1543 | info = rb_entry(parent, struct btrfs_free_space, offset_index); |
| 1544 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1545 | if (offset < info->offset) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1546 | p = &(*p)->rb_left; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1547 | } else if (offset > info->offset) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1548 | p = &(*p)->rb_right; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1549 | } else { |
| 1550 | /* |
| 1551 | * we could have a bitmap entry and an extent entry |
| 1552 | * share the same offset. If this is the case, we want |
| 1553 | * the extent entry to always be found first if we do a |
| 1554 | * linear search through the tree, since we want to have |
| 1555 | * the quickest allocation time, and allocating from an |
| 1556 | * extent is faster than allocating from a bitmap. So |
| 1557 | * if we're inserting a bitmap and we find an entry at |
| 1558 | * this offset, we want to go right, or after this entry |
| 1559 | * logically. If we are inserting an extent and we've |
| 1560 | * found a bitmap, we want to go left, or before |
| 1561 | * logically. |
| 1562 | */ |
| 1563 | if (bitmap) { |
Josef Bacik | 207dde8 | 2011-05-13 14:49:23 -0400 | [diff] [blame] | 1564 | if (info->bitmap) { |
| 1565 | WARN_ON_ONCE(1); |
| 1566 | return -EEXIST; |
| 1567 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1568 | p = &(*p)->rb_right; |
| 1569 | } else { |
Josef Bacik | 207dde8 | 2011-05-13 14:49:23 -0400 | [diff] [blame] | 1570 | if (!info->bitmap) { |
| 1571 | WARN_ON_ONCE(1); |
| 1572 | return -EEXIST; |
| 1573 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1574 | p = &(*p)->rb_left; |
| 1575 | } |
| 1576 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | rb_link_node(node, parent, p); |
| 1580 | rb_insert_color(node, root); |
| 1581 | |
| 1582 | return 0; |
| 1583 | } |
| 1584 | |
| 1585 | /* |
Josef Bacik | 70cb074 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 1586 | * searches the tree for the given offset. |
| 1587 | * |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1588 | * fuzzy - If this is set, then we are trying to make an allocation, and we just |
| 1589 | * want a section that has at least bytes size and comes at or after the given |
| 1590 | * offset. |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1591 | */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1592 | static struct btrfs_free_space * |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1593 | tree_search_offset(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1594 | u64 offset, int bitmap_only, int fuzzy) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1595 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1596 | struct rb_node *n = ctl->free_space_offset.rb_node; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1597 | struct btrfs_free_space *entry, *prev = NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1598 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1599 | /* find entry that is closest to the 'offset' */ |
| 1600 | while (1) { |
| 1601 | if (!n) { |
| 1602 | entry = NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1603 | break; |
| 1604 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1605 | |
| 1606 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
| 1607 | prev = entry; |
| 1608 | |
| 1609 | if (offset < entry->offset) |
| 1610 | n = n->rb_left; |
| 1611 | else if (offset > entry->offset) |
| 1612 | n = n->rb_right; |
| 1613 | else |
| 1614 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1615 | } |
| 1616 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1617 | if (bitmap_only) { |
| 1618 | if (!entry) |
| 1619 | return NULL; |
| 1620 | if (entry->bitmap) |
| 1621 | return entry; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1622 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1623 | /* |
| 1624 | * bitmap entry and extent entry may share same offset, |
| 1625 | * in that case, bitmap entry comes after extent entry. |
| 1626 | */ |
| 1627 | n = rb_next(n); |
| 1628 | if (!n) |
| 1629 | return NULL; |
| 1630 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
| 1631 | if (entry->offset != offset) |
| 1632 | return NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1633 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1634 | WARN_ON(!entry->bitmap); |
| 1635 | return entry; |
| 1636 | } else if (entry) { |
| 1637 | if (entry->bitmap) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1638 | /* |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1639 | * if previous extent entry covers the offset, |
| 1640 | * we should return it instead of the bitmap entry |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1641 | */ |
Miao Xie | de6c411 | 2012-10-18 08:18:01 +0000 | [diff] [blame] | 1642 | n = rb_prev(&entry->offset_index); |
| 1643 | if (n) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1644 | prev = rb_entry(n, struct btrfs_free_space, |
| 1645 | offset_index); |
Miao Xie | de6c411 | 2012-10-18 08:18:01 +0000 | [diff] [blame] | 1646 | if (!prev->bitmap && |
| 1647 | prev->offset + prev->bytes > offset) |
| 1648 | entry = prev; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1649 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1650 | } |
| 1651 | return entry; |
| 1652 | } |
| 1653 | |
| 1654 | if (!prev) |
| 1655 | return NULL; |
| 1656 | |
| 1657 | /* find last entry before the 'offset' */ |
| 1658 | entry = prev; |
| 1659 | if (entry->offset > offset) { |
| 1660 | n = rb_prev(&entry->offset_index); |
| 1661 | if (n) { |
| 1662 | entry = rb_entry(n, struct btrfs_free_space, |
| 1663 | offset_index); |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 1664 | ASSERT(entry->offset <= offset); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1665 | } else { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1666 | if (fuzzy) |
| 1667 | return entry; |
| 1668 | else |
| 1669 | return NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1670 | } |
| 1671 | } |
| 1672 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1673 | if (entry->bitmap) { |
Miao Xie | de6c411 | 2012-10-18 08:18:01 +0000 | [diff] [blame] | 1674 | n = rb_prev(&entry->offset_index); |
| 1675 | if (n) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1676 | prev = rb_entry(n, struct btrfs_free_space, |
| 1677 | offset_index); |
Miao Xie | de6c411 | 2012-10-18 08:18:01 +0000 | [diff] [blame] | 1678 | if (!prev->bitmap && |
| 1679 | prev->offset + prev->bytes > offset) |
| 1680 | return prev; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1681 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1682 | if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1683 | return entry; |
| 1684 | } else if (entry->offset + entry->bytes > offset) |
| 1685 | return entry; |
| 1686 | |
| 1687 | if (!fuzzy) |
| 1688 | return NULL; |
| 1689 | |
| 1690 | while (1) { |
| 1691 | if (entry->bitmap) { |
| 1692 | if (entry->offset + BITS_PER_BITMAP * |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1693 | ctl->unit > offset) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1694 | break; |
| 1695 | } else { |
| 1696 | if (entry->offset + entry->bytes > offset) |
| 1697 | break; |
| 1698 | } |
| 1699 | |
| 1700 | n = rb_next(&entry->offset_index); |
| 1701 | if (!n) |
| 1702 | return NULL; |
| 1703 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
| 1704 | } |
| 1705 | return entry; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1706 | } |
| 1707 | |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1708 | static inline void |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1709 | __unlink_free_space(struct btrfs_free_space_ctl *ctl, |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1710 | struct btrfs_free_space *info) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1711 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1712 | rb_erase(&info->offset_index, &ctl->free_space_offset); |
| 1713 | ctl->free_extents--; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1714 | |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1715 | if (!info->bitmap && !btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1716 | ctl->discardable_extents[BTRFS_STAT_CURR]--; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1717 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= info->bytes; |
| 1718 | } |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1719 | } |
| 1720 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1721 | static void unlink_free_space(struct btrfs_free_space_ctl *ctl, |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1722 | struct btrfs_free_space *info) |
| 1723 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1724 | __unlink_free_space(ctl, info); |
| 1725 | ctl->free_space -= info->bytes; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1726 | } |
| 1727 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1728 | static int link_free_space(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1729 | struct btrfs_free_space *info) |
| 1730 | { |
| 1731 | int ret = 0; |
| 1732 | |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 1733 | ASSERT(info->bytes || info->bitmap); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1734 | ret = tree_insert_offset(&ctl->free_space_offset, info->offset, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1735 | &info->offset_index, (info->bitmap != NULL)); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1736 | if (ret) |
| 1737 | return ret; |
| 1738 | |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1739 | if (!info->bitmap && !btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1740 | ctl->discardable_extents[BTRFS_STAT_CURR]++; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1741 | ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes; |
| 1742 | } |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1743 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1744 | ctl->free_space += info->bytes; |
| 1745 | ctl->free_extents++; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1746 | return ret; |
| 1747 | } |
| 1748 | |
Miao Xie | bb3ac5a | 2011-08-05 09:32:35 +0000 | [diff] [blame] | 1749 | static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, |
| 1750 | struct btrfs_free_space *info, |
| 1751 | u64 offset, u64 bytes) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1752 | { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1753 | unsigned long start, count, end; |
| 1754 | int extent_delta = -1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1755 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1756 | start = offset_to_bit(info->offset, ctl->unit, offset); |
| 1757 | count = bytes_to_bits(bytes, ctl->unit); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1758 | end = start + count; |
| 1759 | ASSERT(end <= BITS_PER_BITMAP); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1760 | |
Li Zefan | f38b6e7 | 2011-03-14 13:40:51 +0800 | [diff] [blame] | 1761 | bitmap_clear(info->bitmap, start, count); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1762 | |
| 1763 | info->bytes -= bytes; |
Josef Bacik | 553cceb | 2018-09-28 07:18:00 -0400 | [diff] [blame] | 1764 | if (info->max_extent_size > ctl->unit) |
| 1765 | info->max_extent_size = 0; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1766 | |
| 1767 | if (start && test_bit(start - 1, info->bitmap)) |
| 1768 | extent_delta++; |
| 1769 | |
| 1770 | if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap)) |
| 1771 | extent_delta++; |
| 1772 | |
| 1773 | info->bitmap_extents += extent_delta; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1774 | if (!btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1775 | ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1776 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes; |
| 1777 | } |
Miao Xie | bb3ac5a | 2011-08-05 09:32:35 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, |
| 1781 | struct btrfs_free_space *info, u64 offset, |
| 1782 | u64 bytes) |
| 1783 | { |
| 1784 | __bitmap_clear_bits(ctl, info, offset, bytes); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1785 | ctl->free_space -= bytes; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1786 | } |
| 1787 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1788 | static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1789 | struct btrfs_free_space *info, u64 offset, |
| 1790 | u64 bytes) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1791 | { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1792 | unsigned long start, count, end; |
| 1793 | int extent_delta = 1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1794 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1795 | start = offset_to_bit(info->offset, ctl->unit, offset); |
| 1796 | count = bytes_to_bits(bytes, ctl->unit); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1797 | end = start + count; |
| 1798 | ASSERT(end <= BITS_PER_BITMAP); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1799 | |
Li Zefan | f38b6e7 | 2011-03-14 13:40:51 +0800 | [diff] [blame] | 1800 | bitmap_set(info->bitmap, start, count); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1801 | |
| 1802 | info->bytes += bytes; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1803 | ctl->free_space += bytes; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1804 | |
| 1805 | if (start && test_bit(start - 1, info->bitmap)) |
| 1806 | extent_delta--; |
| 1807 | |
| 1808 | if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap)) |
| 1809 | extent_delta--; |
| 1810 | |
| 1811 | info->bitmap_extents += extent_delta; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1812 | if (!btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1813 | ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 1814 | ctl->discardable_bytes[BTRFS_STAT_CURR] += bytes; |
| 1815 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1816 | } |
| 1817 | |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1818 | /* |
| 1819 | * If we can not find suitable extent, we will use bytes to record |
| 1820 | * the size of the max extent. |
| 1821 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1822 | static int search_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1823 | struct btrfs_free_space *bitmap_info, u64 *offset, |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 1824 | u64 *bytes, bool for_alloc) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1825 | { |
| 1826 | unsigned long found_bits = 0; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1827 | unsigned long max_bits = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1828 | unsigned long bits, i; |
| 1829 | unsigned long next_zero; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1830 | unsigned long extent_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1831 | |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 1832 | /* |
| 1833 | * Skip searching the bitmap if we don't have a contiguous section that |
| 1834 | * is large enough for this allocation. |
| 1835 | */ |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 1836 | if (for_alloc && |
| 1837 | bitmap_info->max_extent_size && |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 1838 | bitmap_info->max_extent_size < *bytes) { |
| 1839 | *bytes = bitmap_info->max_extent_size; |
| 1840 | return -1; |
| 1841 | } |
| 1842 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1843 | i = offset_to_bit(bitmap_info->offset, ctl->unit, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1844 | max_t(u64, *offset, bitmap_info->offset)); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1845 | bits = bytes_to_bits(*bytes, ctl->unit); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1846 | |
Wei Yongjun | ebb3dad | 2012-09-13 20:29:02 -0600 | [diff] [blame] | 1847 | for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) { |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 1848 | if (for_alloc && bits == 1) { |
| 1849 | found_bits = 1; |
| 1850 | break; |
| 1851 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1852 | next_zero = find_next_zero_bit(bitmap_info->bitmap, |
| 1853 | BITS_PER_BITMAP, i); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1854 | extent_bits = next_zero - i; |
| 1855 | if (extent_bits >= bits) { |
| 1856 | found_bits = extent_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1857 | break; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1858 | } else if (extent_bits > max_bits) { |
| 1859 | max_bits = extent_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1860 | } |
| 1861 | i = next_zero; |
| 1862 | } |
| 1863 | |
| 1864 | if (found_bits) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1865 | *offset = (u64)(i * ctl->unit) + bitmap_info->offset; |
| 1866 | *bytes = (u64)(found_bits) * ctl->unit; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1867 | return 0; |
| 1868 | } |
| 1869 | |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1870 | *bytes = (u64)(max_bits) * ctl->unit; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 1871 | bitmap_info->max_extent_size = *bytes; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1872 | return -1; |
| 1873 | } |
| 1874 | |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 1875 | static inline u64 get_max_extent_size(struct btrfs_free_space *entry) |
| 1876 | { |
| 1877 | if (entry->bitmap) |
| 1878 | return entry->max_extent_size; |
| 1879 | return entry->bytes; |
| 1880 | } |
| 1881 | |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1882 | /* Cache the size of the max extent in bytes */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1883 | static struct btrfs_free_space * |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1884 | find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1885 | unsigned long align, u64 *max_extent_size) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1886 | { |
| 1887 | struct btrfs_free_space *entry; |
| 1888 | struct rb_node *node; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1889 | u64 tmp; |
| 1890 | u64 align_off; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1891 | int ret; |
| 1892 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1893 | if (!ctl->free_space_offset.rb_node) |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1894 | goto out; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1895 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1896 | entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1897 | if (!entry) |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1898 | goto out; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1899 | |
| 1900 | for (node = &entry->offset_index; node; node = rb_next(node)) { |
| 1901 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1902 | if (entry->bytes < *bytes) { |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 1903 | *max_extent_size = max(get_max_extent_size(entry), |
| 1904 | *max_extent_size); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1905 | continue; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1906 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1907 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1908 | /* make sure the space returned is big enough |
| 1909 | * to match our requested alignment |
| 1910 | */ |
| 1911 | if (*bytes >= align) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1912 | tmp = entry->offset - ctl->start + align - 1; |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 1913 | tmp = div64_u64(tmp, align); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1914 | tmp = tmp * align + ctl->start; |
| 1915 | align_off = tmp - entry->offset; |
| 1916 | } else { |
| 1917 | align_off = 0; |
| 1918 | tmp = entry->offset; |
| 1919 | } |
| 1920 | |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1921 | if (entry->bytes < *bytes + align_off) { |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 1922 | *max_extent_size = max(get_max_extent_size(entry), |
| 1923 | *max_extent_size); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1924 | continue; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1925 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1926 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1927 | if (entry->bitmap) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1928 | u64 size = *bytes; |
| 1929 | |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 1930 | ret = search_bitmap(ctl, entry, &tmp, &size, true); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1931 | if (!ret) { |
| 1932 | *offset = tmp; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1933 | *bytes = size; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1934 | return entry; |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 1935 | } else { |
| 1936 | *max_extent_size = |
| 1937 | max(get_max_extent_size(entry), |
| 1938 | *max_extent_size); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1939 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1940 | continue; |
| 1941 | } |
| 1942 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1943 | *offset = tmp; |
| 1944 | *bytes = entry->bytes - align_off; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1945 | return entry; |
| 1946 | } |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 1947 | out: |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1948 | return NULL; |
| 1949 | } |
| 1950 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1951 | static void add_new_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1952 | struct btrfs_free_space *info, u64 offset) |
| 1953 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1954 | info->offset = offset_to_bitmap(ctl, offset); |
Josef Bacik | f019f42 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 1955 | info->bytes = 0; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 1956 | info->bitmap_extents = 0; |
Alexandre Oliva | f2d0f67 | 2011-11-28 12:04:43 -0200 | [diff] [blame] | 1957 | INIT_LIST_HEAD(&info->list); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1958 | link_free_space(ctl, info); |
| 1959 | ctl->total_bitmaps++; |
David Sterba | fa598b0 | 2020-12-03 17:18:38 +0100 | [diff] [blame] | 1960 | recalculate_thresholds(ctl); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1961 | } |
| 1962 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1963 | static void free_bitmap(struct btrfs_free_space_ctl *ctl, |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 1964 | struct btrfs_free_space *bitmap_info) |
| 1965 | { |
Dennis Zhou | 27f0afc | 2020-01-02 16:26:45 -0500 | [diff] [blame] | 1966 | /* |
| 1967 | * Normally when this is called, the bitmap is completely empty. However, |
| 1968 | * if we are blowing up the free space cache for one reason or another |
| 1969 | * via __btrfs_remove_free_space_cache(), then it may not be freed and |
| 1970 | * we may leave stats on the table. |
| 1971 | */ |
| 1972 | if (bitmap_info->bytes && !btrfs_free_space_trimmed(bitmap_info)) { |
| 1973 | ctl->discardable_extents[BTRFS_STAT_CURR] -= |
| 1974 | bitmap_info->bitmap_extents; |
| 1975 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= bitmap_info->bytes; |
| 1976 | |
| 1977 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1978 | unlink_free_space(ctl, bitmap_info); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 1979 | kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1980 | kmem_cache_free(btrfs_free_space_cachep, bitmap_info); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1981 | ctl->total_bitmaps--; |
David Sterba | fa598b0 | 2020-12-03 17:18:38 +0100 | [diff] [blame] | 1982 | recalculate_thresholds(ctl); |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 1983 | } |
| 1984 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1985 | static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1986 | struct btrfs_free_space *bitmap_info, |
| 1987 | u64 *offset, u64 *bytes) |
| 1988 | { |
| 1989 | u64 end; |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1990 | u64 search_start, search_bytes; |
| 1991 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1992 | |
| 1993 | again: |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 1994 | end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1995 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1996 | /* |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 1997 | * We need to search for bits in this bitmap. We could only cover some |
| 1998 | * of the extent in this bitmap thanks to how we add space, so we need |
| 1999 | * to search for as much as it as we can and clear that amount, and then |
| 2000 | * go searching for the next bit. |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2001 | */ |
| 2002 | search_start = *offset; |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2003 | search_bytes = ctl->unit; |
Josef Bacik | 13dbc08 | 2011-02-03 02:39:52 +0000 | [diff] [blame] | 2004 | search_bytes = min(search_bytes, end - search_start + 1); |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 2005 | ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes, |
| 2006 | false); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2007 | if (ret < 0 || search_start != *offset) |
| 2008 | return -EINVAL; |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2009 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2010 | /* We may have found more bits than what we need */ |
| 2011 | search_bytes = min(search_bytes, *bytes); |
| 2012 | |
| 2013 | /* Cannot clear past the end of the bitmap */ |
| 2014 | search_bytes = min(search_bytes, end - search_start + 1); |
| 2015 | |
| 2016 | bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes); |
| 2017 | *offset += search_bytes; |
| 2018 | *bytes -= search_bytes; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2019 | |
| 2020 | if (*bytes) { |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2021 | struct rb_node *next = rb_next(&bitmap_info->offset_index); |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 2022 | if (!bitmap_info->bytes) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2023 | free_bitmap(ctl, bitmap_info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2024 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2025 | /* |
| 2026 | * no entry after this bitmap, but we still have bytes to |
| 2027 | * remove, so something has gone wrong. |
| 2028 | */ |
| 2029 | if (!next) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2030 | return -EINVAL; |
| 2031 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2032 | bitmap_info = rb_entry(next, struct btrfs_free_space, |
| 2033 | offset_index); |
| 2034 | |
| 2035 | /* |
| 2036 | * if the next entry isn't a bitmap we need to return to let the |
| 2037 | * extent stuff do its work. |
| 2038 | */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2039 | if (!bitmap_info->bitmap) |
| 2040 | return -EAGAIN; |
| 2041 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2042 | /* |
| 2043 | * Ok the next item is a bitmap, but it may not actually hold |
| 2044 | * the information for the rest of this free space stuff, so |
| 2045 | * look for it, and if we don't find it return so we can try |
| 2046 | * everything over again. |
| 2047 | */ |
| 2048 | search_start = *offset; |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2049 | search_bytes = ctl->unit; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2050 | ret = search_bitmap(ctl, bitmap_info, &search_start, |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 2051 | &search_bytes, false); |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2052 | if (ret < 0 || search_start != *offset) |
| 2053 | return -EAGAIN; |
| 2054 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2055 | goto again; |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 2056 | } else if (!bitmap_info->bytes) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2057 | free_bitmap(ctl, bitmap_info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2058 | |
| 2059 | return 0; |
| 2060 | } |
| 2061 | |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2062 | static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl, |
| 2063 | struct btrfs_free_space *info, u64 offset, |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2064 | u64 bytes, enum btrfs_trim_state trim_state) |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2065 | { |
| 2066 | u64 bytes_to_set = 0; |
| 2067 | u64 end; |
| 2068 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2069 | /* |
| 2070 | * This is a tradeoff to make bitmap trim state minimal. We mark the |
| 2071 | * whole bitmap untrimmed if at any point we add untrimmed regions. |
| 2072 | */ |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2073 | if (trim_state == BTRFS_TRIM_STATE_UNTRIMMED) { |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2074 | if (btrfs_free_space_trimmed(info)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2075 | ctl->discardable_extents[BTRFS_STAT_CURR] += |
| 2076 | info->bitmap_extents; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2077 | ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes; |
| 2078 | } |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2079 | info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2080 | } |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2081 | |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2082 | end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit); |
| 2083 | |
| 2084 | bytes_to_set = min(end - offset, bytes); |
| 2085 | |
| 2086 | bitmap_set_bits(ctl, info, offset, bytes_to_set); |
| 2087 | |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 2088 | /* |
| 2089 | * We set some bytes, we have no idea what the max extent size is |
| 2090 | * anymore. |
| 2091 | */ |
| 2092 | info->max_extent_size = 0; |
| 2093 | |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2094 | return bytes_to_set; |
| 2095 | |
| 2096 | } |
| 2097 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2098 | static bool use_bitmap(struct btrfs_free_space_ctl *ctl, |
| 2099 | struct btrfs_free_space *info) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2100 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2101 | struct btrfs_block_group *block_group = ctl->private; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2102 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 2103 | bool forced = false; |
| 2104 | |
| 2105 | #ifdef CONFIG_BTRFS_DEBUG |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2106 | if (btrfs_should_fragment_free_space(block_group)) |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 2107 | forced = true; |
| 2108 | #endif |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2109 | |
Dennis Zhou | 5d90c5c | 2020-01-02 16:26:43 -0500 | [diff] [blame] | 2110 | /* This is a way to reclaim large regions from the bitmaps. */ |
| 2111 | if (!forced && info->bytes >= FORCE_EXTENT_THRESHOLD) |
| 2112 | return false; |
| 2113 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2114 | /* |
| 2115 | * If we are below the extents threshold then we can add this as an |
| 2116 | * extent, and don't have to deal with the bitmap |
| 2117 | */ |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 2118 | if (!forced && ctl->free_extents < ctl->extents_thresh) { |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 2119 | /* |
| 2120 | * If this block group has some small extents we don't want to |
| 2121 | * use up all of our free slots in the cache with them, we want |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2122 | * to reserve them to larger extents, however if we have plenty |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 2123 | * of cache left then go ahead an dadd them, no sense in adding |
| 2124 | * the overhead of a bitmap if we don't have to. |
| 2125 | */ |
Dennis Zhou | f9bb615 | 2020-01-02 16:26:44 -0500 | [diff] [blame] | 2126 | if (info->bytes <= fs_info->sectorsize * 8) { |
| 2127 | if (ctl->free_extents * 3 <= ctl->extents_thresh) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2128 | return false; |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 2129 | } else { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2130 | return false; |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 2131 | } |
| 2132 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2133 | |
| 2134 | /* |
Josef Bacik | dde5740 | 2013-02-12 14:07:51 -0500 | [diff] [blame] | 2135 | * The original block groups from mkfs can be really small, like 8 |
| 2136 | * megabytes, so don't bother with a bitmap for those entries. However |
| 2137 | * some block groups can be smaller than what a bitmap would cover but |
| 2138 | * are still large enough that they could overflow the 32k memory limit, |
| 2139 | * so allow those block groups to still be allowed to have a bitmap |
| 2140 | * entry. |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2141 | */ |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2142 | if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2143 | return false; |
| 2144 | |
| 2145 | return true; |
| 2146 | } |
| 2147 | |
David Sterba | 20e5506 | 2015-11-19 11:42:28 +0100 | [diff] [blame] | 2148 | static const struct btrfs_free_space_op free_space_op = { |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2149 | .use_bitmap = use_bitmap, |
| 2150 | }; |
| 2151 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2152 | static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl, |
| 2153 | struct btrfs_free_space *info) |
| 2154 | { |
| 2155 | struct btrfs_free_space *bitmap_info; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2156 | struct btrfs_block_group *block_group = NULL; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2157 | int added = 0; |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2158 | u64 bytes, offset, bytes_added; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2159 | enum btrfs_trim_state trim_state; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2160 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2161 | |
| 2162 | bytes = info->bytes; |
| 2163 | offset = info->offset; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2164 | trim_state = info->trim_state; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2165 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2166 | if (!ctl->op->use_bitmap(ctl, info)) |
| 2167 | return 0; |
| 2168 | |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2169 | if (ctl->op == &free_space_op) |
| 2170 | block_group = ctl->private; |
Chris Mason | 38e8788 | 2011-06-10 16:36:57 -0400 | [diff] [blame] | 2171 | again: |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2172 | /* |
| 2173 | * Since we link bitmaps right into the cluster we need to see if we |
| 2174 | * have a cluster here, and if so and it has our bitmap we need to add |
| 2175 | * the free space to that bitmap. |
| 2176 | */ |
| 2177 | if (block_group && !list_empty(&block_group->cluster_list)) { |
| 2178 | struct btrfs_free_cluster *cluster; |
| 2179 | struct rb_node *node; |
| 2180 | struct btrfs_free_space *entry; |
| 2181 | |
| 2182 | cluster = list_entry(block_group->cluster_list.next, |
| 2183 | struct btrfs_free_cluster, |
| 2184 | block_group_list); |
| 2185 | spin_lock(&cluster->lock); |
| 2186 | node = rb_first(&cluster->root); |
| 2187 | if (!node) { |
| 2188 | spin_unlock(&cluster->lock); |
Chris Mason | 38e8788 | 2011-06-10 16:36:57 -0400 | [diff] [blame] | 2189 | goto no_cluster_bitmap; |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2193 | if (!entry->bitmap) { |
| 2194 | spin_unlock(&cluster->lock); |
Chris Mason | 38e8788 | 2011-06-10 16:36:57 -0400 | [diff] [blame] | 2195 | goto no_cluster_bitmap; |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | if (entry->offset == offset_to_bitmap(ctl, offset)) { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2199 | bytes_added = add_bytes_to_bitmap(ctl, entry, offset, |
| 2200 | bytes, trim_state); |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2201 | bytes -= bytes_added; |
| 2202 | offset += bytes_added; |
| 2203 | } |
| 2204 | spin_unlock(&cluster->lock); |
| 2205 | if (!bytes) { |
| 2206 | ret = 1; |
| 2207 | goto out; |
| 2208 | } |
| 2209 | } |
Chris Mason | 38e8788 | 2011-06-10 16:36:57 -0400 | [diff] [blame] | 2210 | |
| 2211 | no_cluster_bitmap: |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2212 | bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2213 | 1, 0); |
| 2214 | if (!bitmap_info) { |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 2215 | ASSERT(added == 0); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2216 | goto new_bitmap; |
| 2217 | } |
| 2218 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2219 | bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes, |
| 2220 | trim_state); |
Josef Bacik | 2cdc342 | 2011-05-27 14:07:49 -0400 | [diff] [blame] | 2221 | bytes -= bytes_added; |
| 2222 | offset += bytes_added; |
| 2223 | added = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2224 | |
| 2225 | if (!bytes) { |
| 2226 | ret = 1; |
| 2227 | goto out; |
| 2228 | } else |
| 2229 | goto again; |
| 2230 | |
| 2231 | new_bitmap: |
| 2232 | if (info && info->bitmap) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2233 | add_new_bitmap(ctl, info, offset); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2234 | added = 1; |
| 2235 | info = NULL; |
| 2236 | goto again; |
| 2237 | } else { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2238 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2239 | |
| 2240 | /* no pre-allocated info, allocate a new one */ |
| 2241 | if (!info) { |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2242 | info = kmem_cache_zalloc(btrfs_free_space_cachep, |
| 2243 | GFP_NOFS); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2244 | if (!info) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2245 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2246 | ret = -ENOMEM; |
| 2247 | goto out; |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | /* allocate the bitmap */ |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 2252 | info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, |
| 2253 | GFP_NOFS); |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 2254 | info->trim_state = BTRFS_TRIM_STATE_TRIMMED; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2255 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2256 | if (!info->bitmap) { |
| 2257 | ret = -ENOMEM; |
| 2258 | goto out; |
| 2259 | } |
| 2260 | goto again; |
| 2261 | } |
| 2262 | |
| 2263 | out: |
| 2264 | if (info) { |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 2265 | if (info->bitmap) |
| 2266 | kmem_cache_free(btrfs_free_space_bitmap_cachep, |
| 2267 | info->bitmap); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2268 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2269 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2270 | |
| 2271 | return ret; |
| 2272 | } |
| 2273 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2274 | /* |
| 2275 | * Free space merging rules: |
| 2276 | * 1) Merge trimmed areas together |
| 2277 | * 2) Let untrimmed areas coalesce with trimmed areas |
| 2278 | * 3) Always pull neighboring regions from bitmaps |
| 2279 | * |
| 2280 | * The above rules are for when we merge free space based on btrfs_trim_state. |
| 2281 | * Rules 2 and 3 are subtle because they are suboptimal, but are done for the |
| 2282 | * same reason: to promote larger extent regions which makes life easier for |
| 2283 | * find_free_extent(). Rule 2 enables coalescing based on the common path |
| 2284 | * being returning free space from btrfs_finish_extent_commit(). So when free |
| 2285 | * space is trimmed, it will prevent aggregating trimmed new region and |
| 2286 | * untrimmed regions in the rb_tree. Rule 3 is purely to obtain larger extents |
| 2287 | * and provide find_free_extent() with the largest extents possible hoping for |
| 2288 | * the reuse path. |
| 2289 | */ |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 2290 | static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl, |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 2291 | struct btrfs_free_space *info, bool update_stat) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2292 | { |
Josef Bacik | bf53d46 | 2020-07-27 10:28:05 -0400 | [diff] [blame] | 2293 | struct btrfs_free_space *left_info = NULL; |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2294 | struct btrfs_free_space *right_info; |
| 2295 | bool merged = false; |
| 2296 | u64 offset = info->offset; |
| 2297 | u64 bytes = info->bytes; |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2298 | const bool is_trimmed = btrfs_free_space_trimmed(info); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2299 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2300 | /* |
| 2301 | * first we want to see if there is free space adjacent to the range we |
| 2302 | * are adding, if there is remove that struct and add a new one to |
| 2303 | * cover the entire range |
| 2304 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2305 | right_info = tree_search_offset(ctl, offset + bytes, 0, 0); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2306 | if (right_info && rb_prev(&right_info->offset_index)) |
| 2307 | left_info = rb_entry(rb_prev(&right_info->offset_index), |
| 2308 | struct btrfs_free_space, offset_index); |
Josef Bacik | bf53d46 | 2020-07-27 10:28:05 -0400 | [diff] [blame] | 2309 | else if (!right_info) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2310 | left_info = tree_search_offset(ctl, offset - 1, 0, 0); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2311 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2312 | /* See try_merge_free_space() comment. */ |
| 2313 | if (right_info && !right_info->bitmap && |
| 2314 | (!is_trimmed || btrfs_free_space_trimmed(right_info))) { |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 2315 | if (update_stat) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2316 | unlink_free_space(ctl, right_info); |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 2317 | else |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2318 | __unlink_free_space(ctl, right_info); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2319 | info->bytes += right_info->bytes; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2320 | kmem_cache_free(btrfs_free_space_cachep, right_info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2321 | merged = true; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2322 | } |
| 2323 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2324 | /* See try_merge_free_space() comment. */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2325 | if (left_info && !left_info->bitmap && |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2326 | left_info->offset + left_info->bytes == offset && |
| 2327 | (!is_trimmed || btrfs_free_space_trimmed(left_info))) { |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 2328 | if (update_stat) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2329 | unlink_free_space(ctl, left_info); |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 2330 | else |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2331 | __unlink_free_space(ctl, left_info); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2332 | info->offset = left_info->offset; |
| 2333 | info->bytes += left_info->bytes; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2334 | kmem_cache_free(btrfs_free_space_cachep, left_info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2335 | merged = true; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2336 | } |
| 2337 | |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2338 | return merged; |
| 2339 | } |
| 2340 | |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2341 | static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl, |
| 2342 | struct btrfs_free_space *info, |
| 2343 | bool update_stat) |
| 2344 | { |
| 2345 | struct btrfs_free_space *bitmap; |
| 2346 | unsigned long i; |
| 2347 | unsigned long j; |
| 2348 | const u64 end = info->offset + info->bytes; |
| 2349 | const u64 bitmap_offset = offset_to_bitmap(ctl, end); |
| 2350 | u64 bytes; |
| 2351 | |
| 2352 | bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0); |
| 2353 | if (!bitmap) |
| 2354 | return false; |
| 2355 | |
| 2356 | i = offset_to_bit(bitmap->offset, ctl->unit, end); |
| 2357 | j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i); |
| 2358 | if (j == i) |
| 2359 | return false; |
| 2360 | bytes = (j - i) * ctl->unit; |
| 2361 | info->bytes += bytes; |
| 2362 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2363 | /* See try_merge_free_space() comment. */ |
| 2364 | if (!btrfs_free_space_trimmed(bitmap)) |
| 2365 | info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
| 2366 | |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2367 | if (update_stat) |
| 2368 | bitmap_clear_bits(ctl, bitmap, end, bytes); |
| 2369 | else |
| 2370 | __bitmap_clear_bits(ctl, bitmap, end, bytes); |
| 2371 | |
| 2372 | if (!bitmap->bytes) |
| 2373 | free_bitmap(ctl, bitmap); |
| 2374 | |
| 2375 | return true; |
| 2376 | } |
| 2377 | |
| 2378 | static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl, |
| 2379 | struct btrfs_free_space *info, |
| 2380 | bool update_stat) |
| 2381 | { |
| 2382 | struct btrfs_free_space *bitmap; |
| 2383 | u64 bitmap_offset; |
| 2384 | unsigned long i; |
| 2385 | unsigned long j; |
| 2386 | unsigned long prev_j; |
| 2387 | u64 bytes; |
| 2388 | |
| 2389 | bitmap_offset = offset_to_bitmap(ctl, info->offset); |
| 2390 | /* If we're on a boundary, try the previous logical bitmap. */ |
| 2391 | if (bitmap_offset == info->offset) { |
| 2392 | if (info->offset == 0) |
| 2393 | return false; |
| 2394 | bitmap_offset = offset_to_bitmap(ctl, info->offset - 1); |
| 2395 | } |
| 2396 | |
| 2397 | bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0); |
| 2398 | if (!bitmap) |
| 2399 | return false; |
| 2400 | |
| 2401 | i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1; |
| 2402 | j = 0; |
| 2403 | prev_j = (unsigned long)-1; |
| 2404 | for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) { |
| 2405 | if (j > i) |
| 2406 | break; |
| 2407 | prev_j = j; |
| 2408 | } |
| 2409 | if (prev_j == i) |
| 2410 | return false; |
| 2411 | |
| 2412 | if (prev_j == (unsigned long)-1) |
| 2413 | bytes = (i + 1) * ctl->unit; |
| 2414 | else |
| 2415 | bytes = (i - prev_j) * ctl->unit; |
| 2416 | |
| 2417 | info->offset -= bytes; |
| 2418 | info->bytes += bytes; |
| 2419 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2420 | /* See try_merge_free_space() comment. */ |
| 2421 | if (!btrfs_free_space_trimmed(bitmap)) |
| 2422 | info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
| 2423 | |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2424 | if (update_stat) |
| 2425 | bitmap_clear_bits(ctl, bitmap, info->offset, bytes); |
| 2426 | else |
| 2427 | __bitmap_clear_bits(ctl, bitmap, info->offset, bytes); |
| 2428 | |
| 2429 | if (!bitmap->bytes) |
| 2430 | free_bitmap(ctl, bitmap); |
| 2431 | |
| 2432 | return true; |
| 2433 | } |
| 2434 | |
| 2435 | /* |
| 2436 | * We prefer always to allocate from extent entries, both for clustered and |
| 2437 | * non-clustered allocation requests. So when attempting to add a new extent |
| 2438 | * entry, try to see if there's adjacent free space in bitmap entries, and if |
| 2439 | * there is, migrate that space from the bitmaps to the extent. |
| 2440 | * Like this we get better chances of satisfying space allocation requests |
| 2441 | * because we attempt to satisfy them based on a single cache entry, and never |
| 2442 | * on 2 or more entries - even if the entries represent a contiguous free space |
| 2443 | * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry |
| 2444 | * ends). |
| 2445 | */ |
| 2446 | static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl, |
| 2447 | struct btrfs_free_space *info, |
| 2448 | bool update_stat) |
| 2449 | { |
| 2450 | /* |
| 2451 | * Only work with disconnected entries, as we can change their offset, |
| 2452 | * and must be extent entries. |
| 2453 | */ |
| 2454 | ASSERT(!info->bitmap); |
| 2455 | ASSERT(RB_EMPTY_NODE(&info->offset_index)); |
| 2456 | |
| 2457 | if (ctl->total_bitmaps > 0) { |
| 2458 | bool stole_end; |
| 2459 | bool stole_front = false; |
| 2460 | |
| 2461 | stole_end = steal_from_bitmap_to_end(ctl, info, update_stat); |
| 2462 | if (ctl->total_bitmaps > 0) |
| 2463 | stole_front = steal_from_bitmap_to_front(ctl, info, |
| 2464 | update_stat); |
| 2465 | |
| 2466 | if (stole_end || stole_front) |
| 2467 | try_merge_free_space(ctl, info, update_stat); |
| 2468 | } |
| 2469 | } |
| 2470 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2471 | int __btrfs_add_free_space(struct btrfs_fs_info *fs_info, |
| 2472 | struct btrfs_free_space_ctl *ctl, |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2473 | u64 offset, u64 bytes, |
| 2474 | enum btrfs_trim_state trim_state) |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2475 | { |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2476 | struct btrfs_block_group *block_group = ctl->private; |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2477 | struct btrfs_free_space *info; |
| 2478 | int ret = 0; |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 2479 | u64 filter_bytes = bytes; |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2480 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2481 | ASSERT(!btrfs_is_zoned(fs_info)); |
| 2482 | |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2483 | info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2484 | if (!info) |
| 2485 | return -ENOMEM; |
| 2486 | |
| 2487 | info->offset = offset; |
| 2488 | info->bytes = bytes; |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2489 | info->trim_state = trim_state; |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2490 | RB_CLEAR_NODE(&info->offset_index); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2491 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2492 | spin_lock(&ctl->tree_lock); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2493 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2494 | if (try_merge_free_space(ctl, info, true)) |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2495 | goto link; |
| 2496 | |
| 2497 | /* |
| 2498 | * There was no extent directly to the left or right of this new |
| 2499 | * extent then we know we're going to have to allocate a new extent, so |
| 2500 | * before we do that see if we need to drop this into a bitmap |
| 2501 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2502 | ret = insert_into_bitmap(ctl, info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 2503 | if (ret < 0) { |
| 2504 | goto out; |
| 2505 | } else if (ret) { |
| 2506 | ret = 0; |
| 2507 | goto out; |
| 2508 | } |
| 2509 | link: |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2510 | /* |
| 2511 | * Only steal free space from adjacent bitmaps if we're sure we're not |
| 2512 | * going to add the new free space to existing bitmap entries - because |
| 2513 | * that would mean unnecessary work that would be reverted. Therefore |
| 2514 | * attempt to steal space from bitmaps if we're adding an extent entry. |
| 2515 | */ |
| 2516 | steal_from_bitmap(ctl, info, true); |
| 2517 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 2518 | filter_bytes = max(filter_bytes, info->bytes); |
| 2519 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2520 | ret = link_free_space(ctl, info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2521 | if (ret) |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2522 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2523 | out: |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 2524 | btrfs_discard_update_discardable(block_group); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2525 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2526 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2527 | if (ret) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2528 | btrfs_crit(fs_info, "unable to add free space :%d", ret); |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 2529 | ASSERT(ret != -EEXIST); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2530 | } |
| 2531 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 2532 | if (trim_state != BTRFS_TRIM_STATE_TRIMMED) { |
| 2533 | btrfs_discard_check_filter(block_group, filter_bytes); |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2534 | btrfs_discard_queue_work(&fs_info->discard_ctl, block_group); |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 2535 | } |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2536 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2537 | return ret; |
| 2538 | } |
| 2539 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2540 | static int __btrfs_add_free_space_zoned(struct btrfs_block_group *block_group, |
| 2541 | u64 bytenr, u64 size, bool used) |
| 2542 | { |
Johannes Thumshirn | 18bb8bb | 2021-04-19 16:41:02 +0900 | [diff] [blame] | 2543 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2544 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
| 2545 | u64 offset = bytenr - block_group->start; |
| 2546 | u64 to_free, to_unusable; |
| 2547 | |
| 2548 | spin_lock(&ctl->tree_lock); |
| 2549 | if (!used) |
| 2550 | to_free = size; |
| 2551 | else if (offset >= block_group->alloc_offset) |
| 2552 | to_free = size; |
| 2553 | else if (offset + size <= block_group->alloc_offset) |
| 2554 | to_free = 0; |
| 2555 | else |
| 2556 | to_free = offset + size - block_group->alloc_offset; |
| 2557 | to_unusable = size - to_free; |
| 2558 | |
| 2559 | ctl->free_space += to_free; |
Naohiro Aota | badae9c | 2021-03-03 17:55:48 +0900 | [diff] [blame] | 2560 | /* |
| 2561 | * If the block group is read-only, we should account freed space into |
| 2562 | * bytes_readonly. |
| 2563 | */ |
| 2564 | if (!block_group->ro) |
| 2565 | block_group->zone_unusable += to_unusable; |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2566 | spin_unlock(&ctl->tree_lock); |
| 2567 | if (!used) { |
| 2568 | spin_lock(&block_group->lock); |
| 2569 | block_group->alloc_offset -= size; |
| 2570 | spin_unlock(&block_group->lock); |
| 2571 | } |
| 2572 | |
| 2573 | /* All the region is now unusable. Mark it as unused and reclaim */ |
Johannes Thumshirn | 18bb8bb | 2021-04-19 16:41:02 +0900 | [diff] [blame] | 2574 | if (block_group->zone_unusable == block_group->length) { |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2575 | btrfs_mark_bg_unused(block_group); |
Johannes Thumshirn | 18bb8bb | 2021-04-19 16:41:02 +0900 | [diff] [blame] | 2576 | } else if (block_group->zone_unusable >= |
| 2577 | div_factor_fine(block_group->length, |
| 2578 | fs_info->bg_reclaim_threshold)) { |
| 2579 | btrfs_mark_bg_to_reclaim(block_group); |
| 2580 | } |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2581 | |
| 2582 | return 0; |
| 2583 | } |
| 2584 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2585 | int btrfs_add_free_space(struct btrfs_block_group *block_group, |
Josef Bacik | 478b4d9 | 2019-06-20 15:37:43 -0400 | [diff] [blame] | 2586 | u64 bytenr, u64 size) |
| 2587 | { |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2588 | enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
| 2589 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2590 | if (btrfs_is_zoned(block_group->fs_info)) |
| 2591 | return __btrfs_add_free_space_zoned(block_group, bytenr, size, |
| 2592 | true); |
| 2593 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2594 | if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC)) |
| 2595 | trim_state = BTRFS_TRIM_STATE_TRIMMED; |
| 2596 | |
Josef Bacik | 478b4d9 | 2019-06-20 15:37:43 -0400 | [diff] [blame] | 2597 | return __btrfs_add_free_space(block_group->fs_info, |
| 2598 | block_group->free_space_ctl, |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2599 | bytenr, size, trim_state); |
Josef Bacik | 478b4d9 | 2019-06-20 15:37:43 -0400 | [diff] [blame] | 2600 | } |
| 2601 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2602 | int btrfs_add_free_space_unused(struct btrfs_block_group *block_group, |
| 2603 | u64 bytenr, u64 size) |
| 2604 | { |
| 2605 | if (btrfs_is_zoned(block_group->fs_info)) |
| 2606 | return __btrfs_add_free_space_zoned(block_group, bytenr, size, |
| 2607 | false); |
| 2608 | |
| 2609 | return btrfs_add_free_space(block_group, bytenr, size); |
| 2610 | } |
| 2611 | |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2612 | /* |
| 2613 | * This is a subtle distinction because when adding free space back in general, |
| 2614 | * we want it to be added as untrimmed for async. But in the case where we add |
| 2615 | * it on loading of a block group, we want to consider it trimmed. |
| 2616 | */ |
| 2617 | int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group, |
| 2618 | u64 bytenr, u64 size) |
| 2619 | { |
| 2620 | enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
| 2621 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2622 | if (btrfs_is_zoned(block_group->fs_info)) |
| 2623 | return __btrfs_add_free_space_zoned(block_group, bytenr, size, |
| 2624 | true); |
| 2625 | |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2626 | if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC) || |
| 2627 | btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC)) |
| 2628 | trim_state = BTRFS_TRIM_STATE_TRIMMED; |
| 2629 | |
| 2630 | return __btrfs_add_free_space(block_group->fs_info, |
| 2631 | block_group->free_space_ctl, |
| 2632 | bytenr, size, trim_state); |
| 2633 | } |
| 2634 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2635 | int btrfs_remove_free_space(struct btrfs_block_group *block_group, |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2636 | u64 offset, u64 bytes) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2637 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2638 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2639 | struct btrfs_free_space *info; |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2640 | int ret; |
| 2641 | bool re_search = false; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2642 | |
Naohiro Aota | 011b41b | 2021-02-04 19:21:55 +0900 | [diff] [blame] | 2643 | if (btrfs_is_zoned(block_group->fs_info)) { |
| 2644 | /* |
| 2645 | * This can happen with conventional zones when replaying log. |
| 2646 | * Since the allocation info of tree-log nodes are not recorded |
| 2647 | * to the extent-tree, calculate_alloc_pointer() failed to |
| 2648 | * advance the allocation pointer after last allocated tree log |
| 2649 | * node blocks. |
| 2650 | * |
| 2651 | * This function is called from |
| 2652 | * btrfs_pin_extent_for_log_replay() when replaying the log. |
| 2653 | * Advance the pointer not to overwrite the tree-log nodes. |
| 2654 | */ |
| 2655 | if (block_group->alloc_offset < offset + bytes) |
| 2656 | block_group->alloc_offset = offset + bytes; |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2657 | return 0; |
Naohiro Aota | 011b41b | 2021-02-04 19:21:55 +0900 | [diff] [blame] | 2658 | } |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2659 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2660 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2661 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2662 | again: |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2663 | ret = 0; |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2664 | if (!bytes) |
| 2665 | goto out_lock; |
| 2666 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2667 | info = tree_search_offset(ctl, offset, 0, 0); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2668 | if (!info) { |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2669 | /* |
| 2670 | * oops didn't find an extent that matched the space we wanted |
| 2671 | * to remove, look for a bitmap instead |
| 2672 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2673 | info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2674 | 1, 0); |
| 2675 | if (!info) { |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2676 | /* |
| 2677 | * If we found a partial bit of our free space in a |
| 2678 | * bitmap but then couldn't find the other part this may |
| 2679 | * be a problem, so WARN about it. |
Chris Mason | 24a7031 | 2011-11-21 09:39:11 -0500 | [diff] [blame] | 2680 | */ |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2681 | WARN_ON(re_search); |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 2682 | goto out_lock; |
| 2683 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2684 | } |
| 2685 | |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2686 | re_search = false; |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2687 | if (!info->bitmap) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2688 | unlink_free_space(ctl, info); |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2689 | if (offset == info->offset) { |
| 2690 | u64 to_free = min(bytes, info->bytes); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2691 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2692 | info->bytes -= to_free; |
| 2693 | info->offset += to_free; |
| 2694 | if (info->bytes) { |
| 2695 | ret = link_free_space(ctl, info); |
| 2696 | WARN_ON(ret); |
| 2697 | } else { |
| 2698 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 2699 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2700 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2701 | offset += to_free; |
| 2702 | bytes -= to_free; |
| 2703 | goto again; |
| 2704 | } else { |
| 2705 | u64 old_end = info->bytes + info->offset; |
Chris Mason | 9b49c9b | 2008-09-24 11:23:25 -0400 | [diff] [blame] | 2706 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2707 | info->bytes = offset - info->offset; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2708 | ret = link_free_space(ctl, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2709 | WARN_ON(ret); |
| 2710 | if (ret) |
| 2711 | goto out_lock; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2712 | |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2713 | /* Not enough bytes in this entry to satisfy us */ |
| 2714 | if (old_end < offset + bytes) { |
| 2715 | bytes -= old_end - offset; |
| 2716 | offset = old_end; |
| 2717 | goto again; |
| 2718 | } else if (old_end == offset + bytes) { |
| 2719 | /* all done */ |
| 2720 | goto out_lock; |
| 2721 | } |
| 2722 | spin_unlock(&ctl->tree_lock); |
| 2723 | |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2724 | ret = __btrfs_add_free_space(block_group->fs_info, ctl, |
| 2725 | offset + bytes, |
| 2726 | old_end - (offset + bytes), |
| 2727 | info->trim_state); |
Josef Bacik | bdb7d30 | 2012-06-27 15:10:56 -0400 | [diff] [blame] | 2728 | WARN_ON(ret); |
| 2729 | goto out; |
| 2730 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2731 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2732 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2733 | ret = remove_from_bitmap(ctl, info, &offset, &bytes); |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2734 | if (ret == -EAGAIN) { |
| 2735 | re_search = true; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2736 | goto again; |
Josef Bacik | b017511 | 2012-12-18 11:39:19 -0500 | [diff] [blame] | 2737 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2738 | out_lock: |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 2739 | btrfs_discard_update_discardable(block_group); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2740 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2741 | out: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2742 | return ret; |
| 2743 | } |
| 2744 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2745 | void btrfs_dump_free_space(struct btrfs_block_group *block_group, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2746 | u64 bytes) |
| 2747 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2748 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2749 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2750 | struct btrfs_free_space *info; |
| 2751 | struct rb_node *n; |
| 2752 | int count = 0; |
| 2753 | |
Naohiro Aota | 169e0da | 2021-02-04 19:21:52 +0900 | [diff] [blame] | 2754 | /* |
| 2755 | * Zoned btrfs does not use free space tree and cluster. Just print |
| 2756 | * out the free space after the allocation offset. |
| 2757 | */ |
| 2758 | if (btrfs_is_zoned(fs_info)) { |
| 2759 | btrfs_info(fs_info, "free space %llu", |
| 2760 | block_group->length - block_group->alloc_offset); |
| 2761 | return; |
| 2762 | } |
| 2763 | |
Filipe Manana | 9084cb6 | 2018-10-22 10:43:06 +0100 | [diff] [blame] | 2764 | spin_lock(&ctl->tree_lock); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2765 | for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2766 | info = rb_entry(n, struct btrfs_free_space, offset_index); |
Liu Bo | f6175ef | 2012-07-06 03:31:36 -0600 | [diff] [blame] | 2767 | if (info->bytes >= bytes && !block_group->ro) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2768 | count++; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2769 | btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2770 | info->offset, info->bytes, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2771 | (info->bitmap) ? "yes" : "no"); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2772 | } |
Filipe Manana | 9084cb6 | 2018-10-22 10:43:06 +0100 | [diff] [blame] | 2773 | spin_unlock(&ctl->tree_lock); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2774 | btrfs_info(fs_info, "block group has cluster?: %s", |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2775 | list_empty(&block_group->cluster_list) ? "no" : "yes"); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2776 | btrfs_info(fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2777 | "%d blocks of free space at or bigger than bytes is", count); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2778 | } |
| 2779 | |
Josef Bacik | cd79909 | 2020-10-23 09:58:08 -0400 | [diff] [blame] | 2780 | void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group, |
| 2781 | struct btrfs_free_space_ctl *ctl) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2782 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2783 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2784 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2785 | spin_lock_init(&ctl->tree_lock); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2786 | ctl->unit = fs_info->sectorsize; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2787 | ctl->start = block_group->start; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2788 | ctl->private = block_group; |
| 2789 | ctl->op = &free_space_op; |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 2790 | INIT_LIST_HEAD(&ctl->trimming_ranges); |
| 2791 | mutex_init(&ctl->cache_writeout_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2792 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2793 | /* |
| 2794 | * we only want to have 32k of ram per block group for keeping |
| 2795 | * track of free space, and if we pass 1/2 of that we want to |
| 2796 | * start converting things over to using bitmaps |
| 2797 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 2798 | ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2799 | } |
| 2800 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2801 | /* |
| 2802 | * for a given cluster, put all of its extents back into the free |
| 2803 | * space cache. If the block group passed doesn't match the block group |
| 2804 | * pointed to by the cluster, someone else raced in and freed the |
| 2805 | * cluster already. In that case, we just return without changing anything |
| 2806 | */ |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 2807 | static void __btrfs_return_cluster_to_free_space( |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2808 | struct btrfs_block_group *block_group, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2809 | struct btrfs_free_cluster *cluster) |
| 2810 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2811 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2812 | struct btrfs_free_space *entry; |
| 2813 | struct rb_node *node; |
| 2814 | |
| 2815 | spin_lock(&cluster->lock); |
Josef Bacik | 95c85fb | 2021-01-25 16:42:35 -0500 | [diff] [blame] | 2816 | if (cluster->block_group != block_group) { |
| 2817 | spin_unlock(&cluster->lock); |
| 2818 | return; |
| 2819 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2820 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2821 | cluster->block_group = NULL; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2822 | cluster->window_start = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2823 | list_del_init(&cluster->block_group_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2824 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2825 | node = rb_first(&cluster->root); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2826 | while (node) { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2827 | bool bitmap; |
| 2828 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2829 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2830 | node = rb_next(&entry->offset_index); |
| 2831 | rb_erase(&entry->offset_index, &cluster->root); |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2832 | RB_CLEAR_NODE(&entry->offset_index); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2833 | |
| 2834 | bitmap = (entry->bitmap != NULL); |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2835 | if (!bitmap) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2836 | /* Merging treats extents as if they were new */ |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2837 | if (!btrfs_free_space_trimmed(entry)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2838 | ctl->discardable_extents[BTRFS_STAT_CURR]--; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2839 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= |
| 2840 | entry->bytes; |
| 2841 | } |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2842 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2843 | try_merge_free_space(ctl, entry, false); |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2844 | steal_from_bitmap(ctl, entry, false); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2845 | |
| 2846 | /* As we insert directly, update these statistics */ |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2847 | if (!btrfs_free_space_trimmed(entry)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 2848 | ctl->discardable_extents[BTRFS_STAT_CURR]++; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 2849 | ctl->discardable_bytes[BTRFS_STAT_CURR] += |
| 2850 | entry->bytes; |
| 2851 | } |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 2852 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2853 | tree_insert_offset(&ctl->free_space_offset, |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2854 | entry->offset, &entry->offset_index, bitmap); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2855 | } |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 2856 | cluster->root = RB_ROOT; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2857 | spin_unlock(&cluster->lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2858 | btrfs_put_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2859 | } |
| 2860 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2861 | static void __btrfs_remove_free_space_cache_locked( |
| 2862 | struct btrfs_free_space_ctl *ctl) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2863 | { |
| 2864 | struct btrfs_free_space *info; |
| 2865 | struct rb_node *node; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 2866 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 2867 | while ((node = rb_last(&ctl->free_space_offset)) != NULL) { |
| 2868 | info = rb_entry(node, struct btrfs_free_space, offset_index); |
Josef Bacik | 9b90f51 | 2011-06-24 16:02:51 +0000 | [diff] [blame] | 2869 | if (!info->bitmap) { |
| 2870 | unlink_free_space(ctl, info); |
| 2871 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 2872 | } else { |
| 2873 | free_bitmap(ctl, info); |
| 2874 | } |
David Sterba | 351810c | 2015-01-08 15:20:54 +0100 | [diff] [blame] | 2875 | |
| 2876 | cond_resched_lock(&ctl->tree_lock); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 2877 | } |
Chris Mason | 0965537 | 2011-05-21 09:27:38 -0400 | [diff] [blame] | 2878 | } |
| 2879 | |
| 2880 | void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl) |
| 2881 | { |
| 2882 | spin_lock(&ctl->tree_lock); |
| 2883 | __btrfs_remove_free_space_cache_locked(ctl); |
Dennis Zhou | 27f0afc | 2020-01-02 16:26:45 -0500 | [diff] [blame] | 2884 | if (ctl->private) |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 2885 | btrfs_discard_update_discardable(ctl->private); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 2886 | spin_unlock(&ctl->tree_lock); |
| 2887 | } |
| 2888 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2889 | void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2890 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2891 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2892 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2893 | struct list_head *head; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2894 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2895 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2896 | while ((head = block_group->cluster_list.next) != |
| 2897 | &block_group->cluster_list) { |
| 2898 | cluster = list_entry(head, struct btrfs_free_cluster, |
| 2899 | block_group_list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2900 | |
| 2901 | WARN_ON(cluster->block_group != block_group); |
| 2902 | __btrfs_return_cluster_to_free_space(block_group, cluster); |
David Sterba | 351810c | 2015-01-08 15:20:54 +0100 | [diff] [blame] | 2903 | |
| 2904 | cond_resched_lock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2905 | } |
Chris Mason | 0965537 | 2011-05-21 09:27:38 -0400 | [diff] [blame] | 2906 | __btrfs_remove_free_space_cache_locked(ctl); |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 2907 | btrfs_discard_update_discardable(block_group); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2908 | spin_unlock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2909 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2910 | } |
| 2911 | |
Dennis Zhou | 6e80d4f | 2019-12-13 16:22:15 -0800 | [diff] [blame] | 2912 | /** |
| 2913 | * btrfs_is_free_space_trimmed - see if everything is trimmed |
| 2914 | * @block_group: block_group of interest |
| 2915 | * |
| 2916 | * Walk @block_group's free space rb_tree to determine if everything is trimmed. |
| 2917 | */ |
| 2918 | bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group) |
| 2919 | { |
| 2920 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
| 2921 | struct btrfs_free_space *info; |
| 2922 | struct rb_node *node; |
| 2923 | bool ret = true; |
| 2924 | |
| 2925 | spin_lock(&ctl->tree_lock); |
| 2926 | node = rb_first(&ctl->free_space_offset); |
| 2927 | |
| 2928 | while (node) { |
| 2929 | info = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2930 | |
| 2931 | if (!btrfs_free_space_trimmed(info)) { |
| 2932 | ret = false; |
| 2933 | break; |
| 2934 | } |
| 2935 | |
| 2936 | node = rb_next(node); |
| 2937 | } |
| 2938 | |
| 2939 | spin_unlock(&ctl->tree_lock); |
| 2940 | return ret; |
| 2941 | } |
| 2942 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2943 | u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 2944 | u64 offset, u64 bytes, u64 empty_size, |
| 2945 | u64 *max_extent_size) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2946 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2947 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 2948 | struct btrfs_discard_ctl *discard_ctl = |
| 2949 | &block_group->fs_info->discard_ctl; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2950 | struct btrfs_free_space *entry = NULL; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2951 | u64 bytes_search = bytes + empty_size; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2952 | u64 ret = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2953 | u64 align_gap = 0; |
| 2954 | u64 align_gap_len = 0; |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2955 | enum btrfs_trim_state align_gap_trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2956 | |
Naohiro Aota | 2eda570 | 2021-02-04 19:21:53 +0900 | [diff] [blame] | 2957 | ASSERT(!btrfs_is_zoned(block_group->fs_info)); |
| 2958 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2959 | spin_lock(&ctl->tree_lock); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2960 | entry = find_free_space(ctl, &offset, &bytes_search, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 2961 | block_group->full_stripe_len, max_extent_size); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2962 | if (!entry) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2963 | goto out; |
| 2964 | |
| 2965 | ret = offset; |
| 2966 | if (entry->bitmap) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2967 | bitmap_clear_bits(ctl, entry, offset, bytes); |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 2968 | |
| 2969 | if (!btrfs_free_space_trimmed(entry)) |
| 2970 | atomic64_add(bytes, &discard_ctl->discard_bytes_saved); |
| 2971 | |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 2972 | if (!entry->bytes) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2973 | free_bitmap(ctl, entry); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2974 | } else { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2975 | unlink_free_space(ctl, entry); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2976 | align_gap_len = offset - entry->offset; |
| 2977 | align_gap = entry->offset; |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2978 | align_gap_trim_state = entry->trim_state; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2979 | |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 2980 | if (!btrfs_free_space_trimmed(entry)) |
| 2981 | atomic64_add(bytes, &discard_ctl->discard_bytes_saved); |
| 2982 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2983 | entry->offset = offset + bytes; |
| 2984 | WARN_ON(entry->bytes < bytes + align_gap_len); |
| 2985 | |
| 2986 | entry->bytes -= bytes + align_gap_len; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2987 | if (!entry->bytes) |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 2988 | kmem_cache_free(btrfs_free_space_cachep, entry); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2989 | else |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2990 | link_free_space(ctl, entry); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2991 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2992 | out: |
Josef Bacik | 66b53ba | 2020-10-23 09:58:07 -0400 | [diff] [blame] | 2993 | btrfs_discard_update_discardable(block_group); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 2994 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2995 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2996 | if (align_gap_len) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2997 | __btrfs_add_free_space(block_group->fs_info, ctl, |
Dennis Zhou | a7ccb25 | 2019-12-13 16:22:12 -0800 | [diff] [blame] | 2998 | align_gap, align_gap_len, |
| 2999 | align_gap_trim_state); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3000 | return ret; |
| 3001 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3002 | |
| 3003 | /* |
| 3004 | * given a cluster, put all of its extents back into the free space |
| 3005 | * cache. If a block group is passed, this function will only free |
| 3006 | * a cluster that belongs to the passed block group. |
| 3007 | * |
| 3008 | * Otherwise, it'll get a reference on the block group pointed to by the |
| 3009 | * cluster and remove the cluster from it. |
| 3010 | */ |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 3011 | void btrfs_return_cluster_to_free_space( |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3012 | struct btrfs_block_group *block_group, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3013 | struct btrfs_free_cluster *cluster) |
| 3014 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3015 | struct btrfs_free_space_ctl *ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3016 | |
| 3017 | /* first, get a safe pointer to the block group */ |
| 3018 | spin_lock(&cluster->lock); |
| 3019 | if (!block_group) { |
| 3020 | block_group = cluster->block_group; |
| 3021 | if (!block_group) { |
| 3022 | spin_unlock(&cluster->lock); |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 3023 | return; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3024 | } |
| 3025 | } else if (cluster->block_group != block_group) { |
| 3026 | /* someone else has already freed it don't redo their work */ |
| 3027 | spin_unlock(&cluster->lock); |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 3028 | return; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3029 | } |
Anand Jain | b5790d5 | 2020-06-03 18:10:20 +0800 | [diff] [blame] | 3030 | btrfs_get_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3031 | spin_unlock(&cluster->lock); |
| 3032 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3033 | ctl = block_group->free_space_ctl; |
| 3034 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3035 | /* now return any extents the cluster had on it */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3036 | spin_lock(&ctl->tree_lock); |
Anand Jain | 69b0e09 | 2020-06-03 18:10:18 +0800 | [diff] [blame] | 3037 | __btrfs_return_cluster_to_free_space(block_group, cluster); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3038 | spin_unlock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3039 | |
Dennis Zhou | 6e80d4f | 2019-12-13 16:22:15 -0800 | [diff] [blame] | 3040 | btrfs_discard_queue_work(&block_group->fs_info->discard_ctl, block_group); |
| 3041 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3042 | /* finally drop our ref */ |
| 3043 | btrfs_put_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3044 | } |
| 3045 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3046 | static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group *block_group, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3047 | struct btrfs_free_cluster *cluster, |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3048 | struct btrfs_free_space *entry, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3049 | u64 bytes, u64 min_start, |
| 3050 | u64 *max_extent_size) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3051 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3052 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3053 | int err; |
| 3054 | u64 search_start = cluster->window_start; |
| 3055 | u64 search_bytes = bytes; |
| 3056 | u64 ret = 0; |
| 3057 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3058 | search_start = min_start; |
| 3059 | search_bytes = bytes; |
| 3060 | |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 3061 | err = search_bitmap(ctl, entry, &search_start, &search_bytes, true); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3062 | if (err) { |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 3063 | *max_extent_size = max(get_max_extent_size(entry), |
| 3064 | *max_extent_size); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3065 | return 0; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3066 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3067 | |
| 3068 | ret = search_start; |
Miao Xie | bb3ac5a | 2011-08-05 09:32:35 +0000 | [diff] [blame] | 3069 | __bitmap_clear_bits(ctl, entry, ret, bytes); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3070 | |
| 3071 | return ret; |
| 3072 | } |
| 3073 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3074 | /* |
| 3075 | * given a cluster, try to allocate 'bytes' from it, returns 0 |
| 3076 | * if it couldn't find anything suitably large, or a logical disk offset |
| 3077 | * if things worked out |
| 3078 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3079 | u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3080 | struct btrfs_free_cluster *cluster, u64 bytes, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3081 | u64 min_start, u64 *max_extent_size) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3082 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3083 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 3084 | struct btrfs_discard_ctl *discard_ctl = |
| 3085 | &block_group->fs_info->discard_ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3086 | struct btrfs_free_space *entry = NULL; |
| 3087 | struct rb_node *node; |
| 3088 | u64 ret = 0; |
| 3089 | |
Naohiro Aota | 2eda570 | 2021-02-04 19:21:53 +0900 | [diff] [blame] | 3090 | ASSERT(!btrfs_is_zoned(block_group->fs_info)); |
| 3091 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3092 | spin_lock(&cluster->lock); |
| 3093 | if (bytes > cluster->max_size) |
| 3094 | goto out; |
| 3095 | |
| 3096 | if (cluster->block_group != block_group) |
| 3097 | goto out; |
| 3098 | |
| 3099 | node = rb_first(&cluster->root); |
| 3100 | if (!node) |
| 3101 | goto out; |
| 3102 | |
| 3103 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 3104 | while (1) { |
Josef Bacik | ad22cf6 | 2018-10-12 15:32:33 -0400 | [diff] [blame] | 3105 | if (entry->bytes < bytes) |
| 3106 | *max_extent_size = max(get_max_extent_size(entry), |
| 3107 | *max_extent_size); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3108 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3109 | if (entry->bytes < bytes || |
| 3110 | (!entry->bitmap && entry->offset < min_start)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3111 | node = rb_next(&entry->offset_index); |
| 3112 | if (!node) |
| 3113 | break; |
| 3114 | entry = rb_entry(node, struct btrfs_free_space, |
| 3115 | offset_index); |
| 3116 | continue; |
| 3117 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3118 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3119 | if (entry->bitmap) { |
| 3120 | ret = btrfs_alloc_from_bitmap(block_group, |
| 3121 | cluster, entry, bytes, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3122 | cluster->window_start, |
| 3123 | max_extent_size); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3124 | if (ret == 0) { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3125 | node = rb_next(&entry->offset_index); |
| 3126 | if (!node) |
| 3127 | break; |
| 3128 | entry = rb_entry(node, struct btrfs_free_space, |
| 3129 | offset_index); |
| 3130 | continue; |
| 3131 | } |
Josef Bacik | 9b23062 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 3132 | cluster->window_start += bytes; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3133 | } else { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3134 | ret = entry->offset; |
| 3135 | |
| 3136 | entry->offset += bytes; |
| 3137 | entry->bytes -= bytes; |
| 3138 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3139 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3140 | break; |
| 3141 | } |
| 3142 | out: |
| 3143 | spin_unlock(&cluster->lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3144 | |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3145 | if (!ret) |
| 3146 | return 0; |
| 3147 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3148 | spin_lock(&ctl->tree_lock); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3149 | |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 3150 | if (!btrfs_free_space_trimmed(entry)) |
| 3151 | atomic64_add(bytes, &discard_ctl->discard_bytes_saved); |
| 3152 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3153 | ctl->free_space -= bytes; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 3154 | if (!entry->bitmap && !btrfs_free_space_trimmed(entry)) |
| 3155 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes; |
Nikolay Borisov | 3c17916 | 2021-02-08 10:26:54 +0200 | [diff] [blame] | 3156 | |
| 3157 | spin_lock(&cluster->lock); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3158 | if (entry->bytes == 0) { |
Nikolay Borisov | 3c17916 | 2021-02-08 10:26:54 +0200 | [diff] [blame] | 3159 | rb_erase(&entry->offset_index, &cluster->root); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3160 | ctl->free_extents--; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3161 | if (entry->bitmap) { |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 3162 | kmem_cache_free(btrfs_free_space_bitmap_cachep, |
| 3163 | entry->bitmap); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3164 | ctl->total_bitmaps--; |
David Sterba | fa598b0 | 2020-12-03 17:18:38 +0100 | [diff] [blame] | 3165 | recalculate_thresholds(ctl); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3166 | } else if (!btrfs_free_space_trimmed(entry)) { |
| 3167 | ctl->discardable_extents[BTRFS_STAT_CURR]--; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3168 | } |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 3169 | kmem_cache_free(btrfs_free_space_cachep, entry); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3170 | } |
| 3171 | |
Nikolay Borisov | 3c17916 | 2021-02-08 10:26:54 +0200 | [diff] [blame] | 3172 | spin_unlock(&cluster->lock); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3173 | spin_unlock(&ctl->tree_lock); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 3174 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3175 | return ret; |
| 3176 | } |
| 3177 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3178 | static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3179 | struct btrfs_free_space *entry, |
| 3180 | struct btrfs_free_cluster *cluster, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3181 | u64 offset, u64 bytes, |
| 3182 | u64 cont1_bytes, u64 min_bytes) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3183 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3184 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3185 | unsigned long next_zero; |
| 3186 | unsigned long i; |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3187 | unsigned long want_bits; |
| 3188 | unsigned long min_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3189 | unsigned long found_bits; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3190 | unsigned long max_bits = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3191 | unsigned long start = 0; |
| 3192 | unsigned long total_found = 0; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3193 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3194 | |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3195 | i = offset_to_bit(entry->offset, ctl->unit, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3196 | max_t(u64, offset, entry->offset)); |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3197 | want_bits = bytes_to_bits(bytes, ctl->unit); |
| 3198 | min_bits = bytes_to_bits(min_bytes, ctl->unit); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3199 | |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3200 | /* |
| 3201 | * Don't bother looking for a cluster in this bitmap if it's heavily |
| 3202 | * fragmented. |
| 3203 | */ |
| 3204 | if (entry->max_extent_size && |
| 3205 | entry->max_extent_size < cont1_bytes) |
| 3206 | return -ENOSPC; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3207 | again: |
| 3208 | found_bits = 0; |
Wei Yongjun | ebb3dad | 2012-09-13 20:29:02 -0600 | [diff] [blame] | 3209 | for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3210 | next_zero = find_next_zero_bit(entry->bitmap, |
| 3211 | BITS_PER_BITMAP, i); |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3212 | if (next_zero - i >= min_bits) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3213 | found_bits = next_zero - i; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3214 | if (found_bits > max_bits) |
| 3215 | max_bits = found_bits; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3216 | break; |
| 3217 | } |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3218 | if (next_zero - i > max_bits) |
| 3219 | max_bits = next_zero - i; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3220 | i = next_zero; |
| 3221 | } |
| 3222 | |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3223 | if (!found_bits) { |
| 3224 | entry->max_extent_size = (u64)max_bits * ctl->unit; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3225 | return -ENOSPC; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 3226 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3227 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3228 | if (!total_found) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3229 | start = i; |
Alexandre Oliva | b78d09b | 2011-11-30 13:43:00 -0500 | [diff] [blame] | 3230 | cluster->max_size = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | total_found += found_bits; |
| 3234 | |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3235 | if (cluster->max_size < found_bits * ctl->unit) |
| 3236 | cluster->max_size = found_bits * ctl->unit; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3237 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3238 | if (total_found < want_bits || cluster->max_size < cont1_bytes) { |
| 3239 | i = next_zero + 1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3240 | goto again; |
| 3241 | } |
| 3242 | |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3243 | cluster->window_start = start * ctl->unit + entry->offset; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3244 | rb_erase(&entry->offset_index, &ctl->free_space_offset); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3245 | ret = tree_insert_offset(&cluster->root, entry->offset, |
| 3246 | &entry->offset_index, 1); |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 3247 | ASSERT(!ret); /* -EEXIST; Logic error */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3248 | |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3249 | trace_btrfs_setup_cluster(block_group, cluster, |
Wang Sheng-Hui | 9600976 | 2012-11-30 06:30:14 +0000 | [diff] [blame] | 3250 | total_found * ctl->unit, 1); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3251 | return 0; |
| 3252 | } |
| 3253 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3254 | /* |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3255 | * This searches the block group for just extents to fill the cluster with. |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3256 | * Try to find a cluster with at least bytes total bytes, at least one |
| 3257 | * extent of cont1_bytes, and other clusters of at least min_bytes. |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3258 | */ |
Josef Bacik | 3de85bb | 2011-05-25 13:07:37 -0400 | [diff] [blame] | 3259 | static noinline int |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3260 | setup_cluster_no_bitmap(struct btrfs_block_group *block_group, |
Josef Bacik | 3de85bb | 2011-05-25 13:07:37 -0400 | [diff] [blame] | 3261 | struct btrfs_free_cluster *cluster, |
| 3262 | struct list_head *bitmaps, u64 offset, u64 bytes, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3263 | u64 cont1_bytes, u64 min_bytes) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3264 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3265 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3266 | struct btrfs_free_space *first = NULL; |
| 3267 | struct btrfs_free_space *entry = NULL; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3268 | struct btrfs_free_space *last; |
| 3269 | struct rb_node *node; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3270 | u64 window_free; |
| 3271 | u64 max_extent; |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3272 | u64 total_size = 0; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3273 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3274 | entry = tree_search_offset(ctl, offset, 0, 1); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3275 | if (!entry) |
| 3276 | return -ENOSPC; |
| 3277 | |
| 3278 | /* |
| 3279 | * We don't want bitmaps, so just move along until we find a normal |
| 3280 | * extent entry. |
| 3281 | */ |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3282 | while (entry->bitmap || entry->bytes < min_bytes) { |
| 3283 | if (entry->bitmap && list_empty(&entry->list)) |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3284 | list_add_tail(&entry->list, bitmaps); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3285 | node = rb_next(&entry->offset_index); |
| 3286 | if (!node) |
| 3287 | return -ENOSPC; |
| 3288 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 3289 | } |
| 3290 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3291 | window_free = entry->bytes; |
| 3292 | max_extent = entry->bytes; |
| 3293 | first = entry; |
| 3294 | last = entry; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3295 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3296 | for (node = rb_next(&entry->offset_index); node; |
| 3297 | node = rb_next(&entry->offset_index)) { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3298 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 3299 | |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3300 | if (entry->bitmap) { |
| 3301 | if (list_empty(&entry->list)) |
| 3302 | list_add_tail(&entry->list, bitmaps); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3303 | continue; |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3304 | } |
| 3305 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3306 | if (entry->bytes < min_bytes) |
| 3307 | continue; |
| 3308 | |
| 3309 | last = entry; |
| 3310 | window_free += entry->bytes; |
| 3311 | if (entry->bytes > max_extent) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3312 | max_extent = entry->bytes; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3313 | } |
| 3314 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3315 | if (window_free < bytes || max_extent < cont1_bytes) |
| 3316 | return -ENOSPC; |
| 3317 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3318 | cluster->window_start = first->offset; |
| 3319 | |
| 3320 | node = &first->offset_index; |
| 3321 | |
| 3322 | /* |
| 3323 | * now we've found our entries, pull them out of the free space |
| 3324 | * cache and put them into the cluster rbtree |
| 3325 | */ |
| 3326 | do { |
| 3327 | int ret; |
| 3328 | |
| 3329 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 3330 | node = rb_next(&entry->offset_index); |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3331 | if (entry->bitmap || entry->bytes < min_bytes) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3332 | continue; |
| 3333 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3334 | rb_erase(&entry->offset_index, &ctl->free_space_offset); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3335 | ret = tree_insert_offset(&cluster->root, entry->offset, |
| 3336 | &entry->offset_index, 0); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3337 | total_size += entry->bytes; |
Josef Bacik | b12d686 | 2013-08-26 17:14:08 -0400 | [diff] [blame] | 3338 | ASSERT(!ret); /* -EEXIST; Logic error */ |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3339 | } while (node && entry != last); |
| 3340 | |
| 3341 | cluster->max_size = max_extent; |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3342 | trace_btrfs_setup_cluster(block_group, cluster, total_size, 0); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3343 | return 0; |
| 3344 | } |
| 3345 | |
| 3346 | /* |
| 3347 | * This specifically looks for bitmaps that may work in the cluster, we assume |
| 3348 | * that we have already failed to find extents that will work. |
| 3349 | */ |
Josef Bacik | 3de85bb | 2011-05-25 13:07:37 -0400 | [diff] [blame] | 3350 | static noinline int |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3351 | setup_cluster_bitmap(struct btrfs_block_group *block_group, |
Josef Bacik | 3de85bb | 2011-05-25 13:07:37 -0400 | [diff] [blame] | 3352 | struct btrfs_free_cluster *cluster, |
| 3353 | struct list_head *bitmaps, u64 offset, u64 bytes, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3354 | u64 cont1_bytes, u64 min_bytes) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3355 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3356 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Chris Mason | 1b9b922 | 2015-12-15 07:15:32 -0800 | [diff] [blame] | 3357 | struct btrfs_free_space *entry = NULL; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3358 | int ret = -ENOSPC; |
Li Zefan | 0f0fbf1 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3359 | u64 bitmap_offset = offset_to_bitmap(ctl, offset); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3360 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3361 | if (ctl->total_bitmaps == 0) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3362 | return -ENOSPC; |
| 3363 | |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3364 | /* |
Li Zefan | 0f0fbf1 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3365 | * The bitmap that covers offset won't be in the list unless offset |
| 3366 | * is just its start offset. |
| 3367 | */ |
Chris Mason | 1b9b922 | 2015-12-15 07:15:32 -0800 | [diff] [blame] | 3368 | if (!list_empty(bitmaps)) |
| 3369 | entry = list_first_entry(bitmaps, struct btrfs_free_space, list); |
| 3370 | |
| 3371 | if (!entry || entry->offset != bitmap_offset) { |
Li Zefan | 0f0fbf1 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3372 | entry = tree_search_offset(ctl, bitmap_offset, 1, 0); |
| 3373 | if (entry && list_empty(&entry->list)) |
| 3374 | list_add(&entry->list, bitmaps); |
| 3375 | } |
| 3376 | |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3377 | list_for_each_entry(entry, bitmaps, list) { |
Josef Bacik | 357b978 | 2012-01-26 15:01:11 -0500 | [diff] [blame] | 3378 | if (entry->bytes < bytes) |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3379 | continue; |
| 3380 | ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3381 | bytes, cont1_bytes, min_bytes); |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3382 | if (!ret) |
| 3383 | return 0; |
| 3384 | } |
| 3385 | |
| 3386 | /* |
Li Zefan | 52621cb | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3387 | * The bitmaps list has all the bitmaps that record free space |
| 3388 | * starting after offset, so no more search is required. |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3389 | */ |
Li Zefan | 52621cb | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3390 | return -ENOSPC; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | /* |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3394 | * here we try to find a cluster of blocks in a block group. The goal |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3395 | * is to find at least bytes+empty_size. |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3396 | * We might not find them all in one contiguous area. |
| 3397 | * |
| 3398 | * returns zero and sets up cluster if things worked out, otherwise |
| 3399 | * it returns -enospc |
| 3400 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3401 | int btrfs_find_space_cluster(struct btrfs_block_group *block_group, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3402 | struct btrfs_free_cluster *cluster, |
| 3403 | u64 offset, u64 bytes, u64 empty_size) |
| 3404 | { |
David Sterba | 2ceeae2 | 2019-03-20 13:53:49 +0100 | [diff] [blame] | 3405 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3406 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3407 | struct btrfs_free_space *entry, *tmp; |
Li Zefan | 52621cb | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 3408 | LIST_HEAD(bitmaps); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3409 | u64 min_bytes; |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3410 | u64 cont1_bytes; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3411 | int ret; |
| 3412 | |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3413 | /* |
| 3414 | * Choose the minimum extent size we'll require for this |
| 3415 | * cluster. For SSD_SPREAD, don't allow any fragmentation. |
| 3416 | * For metadata, allow allocates with smaller extents. For |
| 3417 | * data, keep it dense. |
| 3418 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3419 | if (btrfs_test_opt(fs_info, SSD_SPREAD)) { |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3420 | cont1_bytes = min_bytes = bytes + empty_size; |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 3421 | } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3422 | cont1_bytes = bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3423 | min_bytes = fs_info->sectorsize; |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3424 | } else { |
| 3425 | cont1_bytes = max(bytes, (bytes + empty_size) >> 2); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3426 | min_bytes = fs_info->sectorsize; |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3427 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3428 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3429 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 7d0d2e8 | 2011-03-18 15:13:42 -0400 | [diff] [blame] | 3430 | |
| 3431 | /* |
| 3432 | * If we know we don't have enough space to make a cluster don't even |
| 3433 | * bother doing all the work to try and find one. |
| 3434 | */ |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3435 | if (ctl->free_space < bytes) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3436 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 7d0d2e8 | 2011-03-18 15:13:42 -0400 | [diff] [blame] | 3437 | return -ENOSPC; |
| 3438 | } |
| 3439 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3440 | spin_lock(&cluster->lock); |
| 3441 | |
| 3442 | /* someone already found a cluster, hooray */ |
| 3443 | if (cluster->block_group) { |
| 3444 | ret = 0; |
| 3445 | goto out; |
| 3446 | } |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3447 | |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3448 | trace_btrfs_find_cluster(block_group, offset, bytes, empty_size, |
| 3449 | min_bytes); |
| 3450 | |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3451 | ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3452 | bytes + empty_size, |
| 3453 | cont1_bytes, min_bytes); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3454 | if (ret) |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3455 | ret = setup_cluster_bitmap(block_group, cluster, &bitmaps, |
Alexandre Oliva | 1bb9190 | 2011-10-14 12:10:36 -0300 | [diff] [blame] | 3456 | offset, bytes + empty_size, |
| 3457 | cont1_bytes, min_bytes); |
Josef Bacik | 86d4a77 | 2011-05-25 13:03:16 -0400 | [diff] [blame] | 3458 | |
| 3459 | /* Clear our temporary list */ |
| 3460 | list_for_each_entry_safe(entry, tmp, &bitmaps, list) |
| 3461 | list_del_init(&entry->list); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3462 | |
| 3463 | if (!ret) { |
Anand Jain | b5790d5 | 2020-06-03 18:10:20 +0800 | [diff] [blame] | 3464 | btrfs_get_block_group(block_group); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 3465 | list_add_tail(&cluster->block_group_list, |
| 3466 | &block_group->cluster_list); |
| 3467 | cluster->block_group = block_group; |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3468 | } else { |
| 3469 | trace_btrfs_failed_cluster_setup(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3470 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3471 | out: |
| 3472 | spin_unlock(&cluster->lock); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3473 | spin_unlock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3474 | |
| 3475 | return ret; |
| 3476 | } |
| 3477 | |
| 3478 | /* |
| 3479 | * simple code to zero out a cluster |
| 3480 | */ |
| 3481 | void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster) |
| 3482 | { |
| 3483 | spin_lock_init(&cluster->lock); |
| 3484 | spin_lock_init(&cluster->refill_lock); |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 3485 | cluster->root = RB_ROOT; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3486 | cluster->max_size = 0; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 3487 | cluster->fragmented = false; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3488 | INIT_LIST_HEAD(&cluster->block_group_list); |
| 3489 | cluster->block_group = NULL; |
| 3490 | } |
| 3491 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3492 | static int do_trimming(struct btrfs_block_group *block_group, |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3493 | u64 *total_trimmed, u64 start, u64 bytes, |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3494 | u64 reserved_start, u64 reserved_bytes, |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3495 | enum btrfs_trim_state reserved_trim_state, |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3496 | struct btrfs_trim_range *trim_entry) |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3497 | { |
| 3498 | struct btrfs_space_info *space_info = block_group->space_info; |
| 3499 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3500 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3501 | int ret; |
| 3502 | int update = 0; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3503 | const u64 end = start + bytes; |
| 3504 | const u64 reserved_end = reserved_start + reserved_bytes; |
| 3505 | enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3506 | u64 trimmed = 0; |
| 3507 | |
| 3508 | spin_lock(&space_info->lock); |
| 3509 | spin_lock(&block_group->lock); |
| 3510 | if (!block_group->ro) { |
| 3511 | block_group->reserved += reserved_bytes; |
| 3512 | space_info->bytes_reserved += reserved_bytes; |
| 3513 | update = 1; |
| 3514 | } |
| 3515 | spin_unlock(&block_group->lock); |
| 3516 | spin_unlock(&space_info->lock); |
| 3517 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3518 | ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed); |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3519 | if (!ret) { |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3520 | *total_trimmed += trimmed; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3521 | trim_state = BTRFS_TRIM_STATE_TRIMMED; |
| 3522 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3523 | |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3524 | mutex_lock(&ctl->cache_writeout_mutex); |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3525 | if (reserved_start < start) |
| 3526 | __btrfs_add_free_space(fs_info, ctl, reserved_start, |
| 3527 | start - reserved_start, |
| 3528 | reserved_trim_state); |
| 3529 | if (start + bytes < reserved_start + reserved_bytes) |
| 3530 | __btrfs_add_free_space(fs_info, ctl, end, reserved_end - end, |
| 3531 | reserved_trim_state); |
| 3532 | __btrfs_add_free_space(fs_info, ctl, start, bytes, trim_state); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3533 | list_del(&trim_entry->list); |
| 3534 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3535 | |
| 3536 | if (update) { |
| 3537 | spin_lock(&space_info->lock); |
| 3538 | spin_lock(&block_group->lock); |
| 3539 | if (block_group->ro) |
| 3540 | space_info->bytes_readonly += reserved_bytes; |
| 3541 | block_group->reserved -= reserved_bytes; |
| 3542 | space_info->bytes_reserved -= reserved_bytes; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3543 | spin_unlock(&block_group->lock); |
Su Yue | 8f63a84 | 2018-11-28 11:21:12 +0800 | [diff] [blame] | 3544 | spin_unlock(&space_info->lock); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | return ret; |
| 3548 | } |
| 3549 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3550 | /* |
| 3551 | * If @async is set, then we will trim 1 region and return. |
| 3552 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3553 | static int trim_no_bitmap(struct btrfs_block_group *block_group, |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3554 | u64 *total_trimmed, u64 start, u64 end, u64 minlen, |
| 3555 | bool async) |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3556 | { |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3557 | struct btrfs_discard_ctl *discard_ctl = |
| 3558 | &block_group->fs_info->discard_ctl; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3559 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3560 | struct btrfs_free_space *entry; |
| 3561 | struct rb_node *node; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3562 | int ret = 0; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3563 | u64 extent_start; |
| 3564 | u64 extent_bytes; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3565 | enum btrfs_trim_state extent_trim_state; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3566 | u64 bytes; |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3567 | const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3568 | |
| 3569 | while (start < end) { |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3570 | struct btrfs_trim_range trim_entry; |
| 3571 | |
| 3572 | mutex_lock(&ctl->cache_writeout_mutex); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3573 | spin_lock(&ctl->tree_lock); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3574 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3575 | if (ctl->free_space < minlen) |
| 3576 | goto out_unlock; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3577 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3578 | entry = tree_search_offset(ctl, start, 0, 1); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3579 | if (!entry) |
| 3580 | goto out_unlock; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3581 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3582 | /* Skip bitmaps and if async, already trimmed entries */ |
| 3583 | while (entry->bitmap || |
| 3584 | (async && btrfs_free_space_trimmed(entry))) { |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3585 | node = rb_next(&entry->offset_index); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3586 | if (!node) |
| 3587 | goto out_unlock; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3588 | entry = rb_entry(node, struct btrfs_free_space, |
| 3589 | offset_index); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3592 | if (entry->offset >= end) |
| 3593 | goto out_unlock; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3594 | |
| 3595 | extent_start = entry->offset; |
| 3596 | extent_bytes = entry->bytes; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3597 | extent_trim_state = entry->trim_state; |
Dennis Zhou | 4aa9ad5 | 2020-01-02 16:26:37 -0500 | [diff] [blame] | 3598 | if (async) { |
| 3599 | start = entry->offset; |
| 3600 | bytes = entry->bytes; |
| 3601 | if (bytes < minlen) { |
| 3602 | spin_unlock(&ctl->tree_lock); |
| 3603 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 3604 | goto next; |
| 3605 | } |
| 3606 | unlink_free_space(ctl, entry); |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3607 | /* |
| 3608 | * Let bytes = BTRFS_MAX_DISCARD_SIZE + X. |
| 3609 | * If X < BTRFS_ASYNC_DISCARD_MIN_FILTER, we won't trim |
| 3610 | * X when we come back around. So trim it now. |
| 3611 | */ |
| 3612 | if (max_discard_size && |
| 3613 | bytes >= (max_discard_size + |
| 3614 | BTRFS_ASYNC_DISCARD_MIN_FILTER)) { |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3615 | bytes = max_discard_size; |
| 3616 | extent_bytes = max_discard_size; |
| 3617 | entry->offset += max_discard_size; |
| 3618 | entry->bytes -= max_discard_size; |
Dennis Zhou | 4aa9ad5 | 2020-01-02 16:26:37 -0500 | [diff] [blame] | 3619 | link_free_space(ctl, entry); |
| 3620 | } else { |
| 3621 | kmem_cache_free(btrfs_free_space_cachep, entry); |
| 3622 | } |
| 3623 | } else { |
| 3624 | start = max(start, extent_start); |
| 3625 | bytes = min(extent_start + extent_bytes, end) - start; |
| 3626 | if (bytes < minlen) { |
| 3627 | spin_unlock(&ctl->tree_lock); |
| 3628 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 3629 | goto next; |
| 3630 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3631 | |
Dennis Zhou | 4aa9ad5 | 2020-01-02 16:26:37 -0500 | [diff] [blame] | 3632 | unlink_free_space(ctl, entry); |
| 3633 | kmem_cache_free(btrfs_free_space_cachep, entry); |
| 3634 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3635 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 3636 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3637 | trim_entry.start = extent_start; |
| 3638 | trim_entry.bytes = extent_bytes; |
| 3639 | list_add_tail(&trim_entry.list, &ctl->trimming_ranges); |
| 3640 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3641 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3642 | ret = do_trimming(block_group, total_trimmed, start, bytes, |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3643 | extent_start, extent_bytes, extent_trim_state, |
| 3644 | &trim_entry); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3645 | if (ret) { |
| 3646 | block_group->discard_cursor = start + bytes; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3647 | break; |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3648 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3649 | next: |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3650 | start += bytes; |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3651 | block_group->discard_cursor = start; |
| 3652 | if (async && *total_trimmed) |
| 3653 | break; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3654 | |
| 3655 | if (fatal_signal_pending(current)) { |
| 3656 | ret = -ERESTARTSYS; |
| 3657 | break; |
| 3658 | } |
| 3659 | |
| 3660 | cond_resched(); |
| 3661 | } |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3662 | |
| 3663 | return ret; |
| 3664 | |
| 3665 | out_unlock: |
| 3666 | block_group->discard_cursor = btrfs_block_group_end(block_group); |
| 3667 | spin_unlock(&ctl->tree_lock); |
| 3668 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 3669 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3670 | return ret; |
| 3671 | } |
| 3672 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3673 | /* |
| 3674 | * If we break out of trimming a bitmap prematurely, we should reset the |
| 3675 | * trimming bit. In a rather contrieved case, it's possible to race here so |
| 3676 | * reset the state to BTRFS_TRIM_STATE_UNTRIMMED. |
| 3677 | * |
| 3678 | * start = start of bitmap |
| 3679 | * end = near end of bitmap |
| 3680 | * |
| 3681 | * Thread 1: Thread 2: |
| 3682 | * trim_bitmaps(start) |
| 3683 | * trim_bitmaps(end) |
| 3684 | * end_trimming_bitmap() |
| 3685 | * reset_trimming_bitmap() |
| 3686 | */ |
| 3687 | static void reset_trimming_bitmap(struct btrfs_free_space_ctl *ctl, u64 offset) |
| 3688 | { |
| 3689 | struct btrfs_free_space *entry; |
| 3690 | |
| 3691 | spin_lock(&ctl->tree_lock); |
| 3692 | entry = tree_search_offset(ctl, offset, 1, 0); |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3693 | if (entry) { |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 3694 | if (btrfs_free_space_trimmed(entry)) { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3695 | ctl->discardable_extents[BTRFS_STAT_CURR] += |
| 3696 | entry->bitmap_extents; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 3697 | ctl->discardable_bytes[BTRFS_STAT_CURR] += entry->bytes; |
| 3698 | } |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3699 | entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3700 | } |
| 3701 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3702 | spin_unlock(&ctl->tree_lock); |
| 3703 | } |
| 3704 | |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3705 | static void end_trimming_bitmap(struct btrfs_free_space_ctl *ctl, |
| 3706 | struct btrfs_free_space *entry) |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3707 | { |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3708 | if (btrfs_free_space_trimming_bitmap(entry)) { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3709 | entry->trim_state = BTRFS_TRIM_STATE_TRIMMED; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3710 | ctl->discardable_extents[BTRFS_STAT_CURR] -= |
| 3711 | entry->bitmap_extents; |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 3712 | ctl->discardable_bytes[BTRFS_STAT_CURR] -= entry->bytes; |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3713 | } |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3714 | } |
| 3715 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3716 | /* |
| 3717 | * If @async is set, then we will trim 1 region and return. |
| 3718 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3719 | static int trim_bitmaps(struct btrfs_block_group *block_group, |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3720 | u64 *total_trimmed, u64 start, u64 end, u64 minlen, |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3721 | u64 maxlen, bool async) |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3722 | { |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3723 | struct btrfs_discard_ctl *discard_ctl = |
| 3724 | &block_group->fs_info->discard_ctl; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3725 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
| 3726 | struct btrfs_free_space *entry; |
| 3727 | int ret = 0; |
| 3728 | int ret2; |
| 3729 | u64 bytes; |
| 3730 | u64 offset = offset_to_bitmap(ctl, start); |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3731 | const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3732 | |
| 3733 | while (offset < end) { |
| 3734 | bool next_bitmap = false; |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3735 | struct btrfs_trim_range trim_entry; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3736 | |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3737 | mutex_lock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3738 | spin_lock(&ctl->tree_lock); |
| 3739 | |
| 3740 | if (ctl->free_space < minlen) { |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3741 | block_group->discard_cursor = |
| 3742 | btrfs_block_group_end(block_group); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3743 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3744 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3745 | break; |
| 3746 | } |
| 3747 | |
| 3748 | entry = tree_search_offset(ctl, offset, 1, 0); |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3749 | /* |
| 3750 | * Bitmaps are marked trimmed lossily now to prevent constant |
| 3751 | * discarding of the same bitmap (the reason why we are bound |
| 3752 | * by the filters). So, retrim the block group bitmaps when we |
| 3753 | * are preparing to punt to the unused_bgs list. This uses |
| 3754 | * @minlen to determine if we are in BTRFS_DISCARD_INDEX_UNUSED |
| 3755 | * which is the only discard index which sets minlen to 0. |
| 3756 | */ |
| 3757 | if (!entry || (async && minlen && start == offset && |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3758 | btrfs_free_space_trimmed(entry))) { |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3759 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3760 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3761 | next_bitmap = true; |
| 3762 | goto next; |
| 3763 | } |
| 3764 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3765 | /* |
| 3766 | * Async discard bitmap trimming begins at by setting the start |
| 3767 | * to be key.objectid and the offset_to_bitmap() aligns to the |
| 3768 | * start of the bitmap. This lets us know we are fully |
| 3769 | * scanning the bitmap rather than only some portion of it. |
| 3770 | */ |
| 3771 | if (start == offset) |
| 3772 | entry->trim_state = BTRFS_TRIM_STATE_TRIMMING; |
| 3773 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3774 | bytes = minlen; |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 3775 | ret2 = search_bitmap(ctl, entry, &start, &bytes, false); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3776 | if (ret2 || start >= end) { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3777 | /* |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3778 | * We lossily consider a bitmap trimmed if we only skip |
| 3779 | * over regions <= BTRFS_ASYNC_DISCARD_MIN_FILTER. |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3780 | */ |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3781 | if (ret2 && minlen <= BTRFS_ASYNC_DISCARD_MIN_FILTER) |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 3782 | end_trimming_bitmap(ctl, entry); |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3783 | else |
| 3784 | entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3785 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3786 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3787 | next_bitmap = true; |
| 3788 | goto next; |
| 3789 | } |
| 3790 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3791 | /* |
| 3792 | * We already trimmed a region, but are using the locking above |
| 3793 | * to reset the trim_state. |
| 3794 | */ |
| 3795 | if (async && *total_trimmed) { |
| 3796 | spin_unlock(&ctl->tree_lock); |
| 3797 | mutex_unlock(&ctl->cache_writeout_mutex); |
| 3798 | goto out; |
| 3799 | } |
| 3800 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3801 | bytes = min(bytes, end - start); |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3802 | if (bytes < minlen || (async && maxlen && bytes > maxlen)) { |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3803 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3804 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3805 | goto next; |
| 3806 | } |
| 3807 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3808 | /* |
| 3809 | * Let bytes = BTRFS_MAX_DISCARD_SIZE + X. |
| 3810 | * If X < @minlen, we won't trim X when we come back around. |
| 3811 | * So trim it now. We differ here from trimming extents as we |
| 3812 | * don't keep individual state per bit. |
| 3813 | */ |
| 3814 | if (async && |
| 3815 | max_discard_size && |
| 3816 | bytes > (max_discard_size + minlen)) |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 3817 | bytes = max_discard_size; |
Dennis Zhou | 4aa9ad5 | 2020-01-02 16:26:37 -0500 | [diff] [blame] | 3818 | |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3819 | bitmap_clear_bits(ctl, entry, start, bytes); |
| 3820 | if (entry->bytes == 0) |
| 3821 | free_bitmap(ctl, entry); |
| 3822 | |
| 3823 | spin_unlock(&ctl->tree_lock); |
Filipe Manana | 55507ce | 2014-12-01 17:04:09 +0000 | [diff] [blame] | 3824 | trim_entry.start = start; |
| 3825 | trim_entry.bytes = bytes; |
| 3826 | list_add_tail(&trim_entry.list, &ctl->trimming_ranges); |
| 3827 | mutex_unlock(&ctl->cache_writeout_mutex); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3828 | |
| 3829 | ret = do_trimming(block_group, total_trimmed, start, bytes, |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3830 | start, bytes, 0, &trim_entry); |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3831 | if (ret) { |
| 3832 | reset_trimming_bitmap(ctl, offset); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3833 | block_group->discard_cursor = |
| 3834 | btrfs_block_group_end(block_group); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3835 | break; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3836 | } |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3837 | next: |
| 3838 | if (next_bitmap) { |
| 3839 | offset += BITS_PER_BITMAP * ctl->unit; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3840 | start = offset; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3841 | } else { |
| 3842 | start += bytes; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3843 | } |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3844 | block_group->discard_cursor = start; |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3845 | |
| 3846 | if (fatal_signal_pending(current)) { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3847 | if (start != offset) |
| 3848 | reset_trimming_bitmap(ctl, offset); |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3849 | ret = -ERESTARTSYS; |
| 3850 | break; |
| 3851 | } |
| 3852 | |
| 3853 | cond_resched(); |
| 3854 | } |
| 3855 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3856 | if (offset >= end) |
| 3857 | block_group->discard_cursor = end; |
| 3858 | |
| 3859 | out: |
Li Zefan | 7fe1e64 | 2011-12-29 14:47:27 +0800 | [diff] [blame] | 3860 | return ret; |
| 3861 | } |
| 3862 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3863 | int btrfs_trim_block_group(struct btrfs_block_group *block_group, |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3864 | u64 *trimmed, u64 start, u64 end, u64 minlen) |
| 3865 | { |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3866 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3867 | int ret; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3868 | u64 rem = 0; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3869 | |
Naohiro Aota | 2eda570 | 2021-02-04 19:21:53 +0900 | [diff] [blame] | 3870 | ASSERT(!btrfs_is_zoned(block_group->fs_info)); |
| 3871 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3872 | *trimmed = 0; |
| 3873 | |
| 3874 | spin_lock(&block_group->lock); |
| 3875 | if (block_group->removed) { |
| 3876 | spin_unlock(&block_group->lock); |
| 3877 | return 0; |
| 3878 | } |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 3879 | btrfs_freeze_block_group(block_group); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3880 | spin_unlock(&block_group->lock); |
| 3881 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3882 | ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, false); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3883 | if (ret) |
| 3884 | goto out; |
| 3885 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3886 | ret = trim_bitmaps(block_group, trimmed, start, end, minlen, 0, false); |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 3887 | div64_u64_rem(end, BITS_PER_BITMAP * ctl->unit, &rem); |
| 3888 | /* If we ended in the middle of a bitmap, reset the trimming flag */ |
| 3889 | if (rem) |
| 3890 | reset_trimming_bitmap(ctl, offset_to_bitmap(ctl, end)); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 3891 | out: |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 3892 | btrfs_unfreeze_block_group(block_group); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 3893 | return ret; |
| 3894 | } |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 3895 | |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3896 | int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group, |
| 3897 | u64 *trimmed, u64 start, u64 end, u64 minlen, |
| 3898 | bool async) |
| 3899 | { |
| 3900 | int ret; |
| 3901 | |
| 3902 | *trimmed = 0; |
| 3903 | |
| 3904 | spin_lock(&block_group->lock); |
| 3905 | if (block_group->removed) { |
| 3906 | spin_unlock(&block_group->lock); |
| 3907 | return 0; |
| 3908 | } |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 3909 | btrfs_freeze_block_group(block_group); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3910 | spin_unlock(&block_group->lock); |
| 3911 | |
| 3912 | ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, async); |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 3913 | btrfs_unfreeze_block_group(block_group); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3914 | |
| 3915 | return ret; |
| 3916 | } |
| 3917 | |
| 3918 | int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group, |
| 3919 | u64 *trimmed, u64 start, u64 end, u64 minlen, |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3920 | u64 maxlen, bool async) |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3921 | { |
| 3922 | int ret; |
| 3923 | |
| 3924 | *trimmed = 0; |
| 3925 | |
| 3926 | spin_lock(&block_group->lock); |
| 3927 | if (block_group->removed) { |
| 3928 | spin_unlock(&block_group->lock); |
| 3929 | return 0; |
| 3930 | } |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 3931 | btrfs_freeze_block_group(block_group); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3932 | spin_unlock(&block_group->lock); |
| 3933 | |
Dennis Zhou | 7fe6d45 | 2020-01-02 16:26:39 -0500 | [diff] [blame] | 3934 | ret = trim_bitmaps(block_group, trimmed, start, end, minlen, maxlen, |
| 3935 | async); |
| 3936 | |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 3937 | btrfs_unfreeze_block_group(block_group); |
Dennis Zhou | 2bee7eb | 2019-12-13 16:22:16 -0800 | [diff] [blame] | 3938 | |
| 3939 | return ret; |
| 3940 | } |
| 3941 | |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 3942 | bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info) |
| 3943 | { |
| 3944 | return btrfs_super_cache_generation(fs_info->super_copy); |
| 3945 | } |
| 3946 | |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 3947 | static int cleanup_free_space_cache_v1(struct btrfs_fs_info *fs_info, |
| 3948 | struct btrfs_trans_handle *trans) |
| 3949 | { |
| 3950 | struct btrfs_block_group *block_group; |
| 3951 | struct rb_node *node; |
Tom Rix | 77364fa | 2021-04-30 11:06:55 -0700 | [diff] [blame] | 3952 | int ret = 0; |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 3953 | |
| 3954 | btrfs_info(fs_info, "cleaning free space cache v1"); |
| 3955 | |
| 3956 | node = rb_first(&fs_info->block_group_cache_tree); |
| 3957 | while (node) { |
| 3958 | block_group = rb_entry(node, struct btrfs_block_group, cache_node); |
| 3959 | ret = btrfs_remove_free_space_inode(trans, NULL, block_group); |
| 3960 | if (ret) |
| 3961 | goto out; |
| 3962 | node = rb_next(node); |
| 3963 | } |
| 3964 | out: |
| 3965 | return ret; |
| 3966 | } |
| 3967 | |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 3968 | int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info *fs_info, bool active) |
| 3969 | { |
| 3970 | struct btrfs_trans_handle *trans; |
| 3971 | int ret; |
| 3972 | |
| 3973 | /* |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 3974 | * update_super_roots will appropriately set or unset |
| 3975 | * super_copy->cache_generation based on SPACE_CACHE and |
| 3976 | * BTRFS_FS_CLEANUP_SPACE_CACHE_V1. For this reason, we need a |
| 3977 | * transaction commit whether we are enabling space cache v1 and don't |
| 3978 | * have any other work to do, or are disabling it and removing free |
| 3979 | * space inodes. |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 3980 | */ |
| 3981 | trans = btrfs_start_transaction(fs_info->tree_root, 0); |
| 3982 | if (IS_ERR(trans)) |
| 3983 | return PTR_ERR(trans); |
| 3984 | |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 3985 | if (!active) { |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 3986 | set_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags); |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 3987 | ret = cleanup_free_space_cache_v1(fs_info, trans); |
| 3988 | if (ret) { |
| 3989 | btrfs_abort_transaction(trans, ret); |
| 3990 | btrfs_end_transaction(trans); |
| 3991 | goto out; |
| 3992 | } |
| 3993 | } |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 3994 | |
| 3995 | ret = btrfs_commit_transaction(trans); |
Boris Burkov | 36b216c | 2020-11-18 15:06:25 -0800 | [diff] [blame] | 3996 | out: |
Boris Burkov | 9484622 | 2020-11-18 15:06:22 -0800 | [diff] [blame] | 3997 | clear_bit(BTRFS_FS_CLEANUP_SPACE_CACHE_V1, &fs_info->flags); |
| 3998 | |
| 3999 | return ret; |
| 4000 | } |
| 4001 | |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4002 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4003 | /* |
| 4004 | * Use this if you need to make a bitmap or extent entry specifically, it |
| 4005 | * doesn't do any of the merging that add_free_space does, this acts a lot like |
| 4006 | * how the free space cache loading stuff works, so you can get really weird |
| 4007 | * configurations. |
| 4008 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 4009 | int test_add_free_space_entry(struct btrfs_block_group *cache, |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4010 | u64 offset, u64 bytes, bool bitmap) |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4011 | { |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4012 | struct btrfs_free_space_ctl *ctl = cache->free_space_ctl; |
| 4013 | struct btrfs_free_space *info = NULL, *bitmap_info; |
| 4014 | void *map = NULL; |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 4015 | enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_TRIMMED; |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4016 | u64 bytes_added; |
| 4017 | int ret; |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4018 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4019 | again: |
| 4020 | if (!info) { |
| 4021 | info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS); |
| 4022 | if (!info) |
| 4023 | return -ENOMEM; |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4024 | } |
| 4025 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4026 | if (!bitmap) { |
| 4027 | spin_lock(&ctl->tree_lock); |
| 4028 | info->offset = offset; |
| 4029 | info->bytes = bytes; |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 4030 | info->max_extent_size = 0; |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4031 | ret = link_free_space(ctl, info); |
| 4032 | spin_unlock(&ctl->tree_lock); |
| 4033 | if (ret) |
| 4034 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 4035 | return ret; |
| 4036 | } |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4037 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4038 | if (!map) { |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 4039 | map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS); |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4040 | if (!map) { |
| 4041 | kmem_cache_free(btrfs_free_space_cachep, info); |
| 4042 | return -ENOMEM; |
| 4043 | } |
| 4044 | } |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4045 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4046 | spin_lock(&ctl->tree_lock); |
| 4047 | bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
| 4048 | 1, 0); |
| 4049 | if (!bitmap_info) { |
| 4050 | info->bitmap = map; |
| 4051 | map = NULL; |
| 4052 | add_new_bitmap(ctl, info, offset); |
| 4053 | bitmap_info = info; |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 4054 | info = NULL; |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4055 | } |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4056 | |
Dennis Zhou | da080fe | 2019-12-13 16:22:13 -0800 | [diff] [blame] | 4057 | bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes, |
| 4058 | trim_state); |
Josef Bacik | cef4048 | 2015-10-02 16:09:42 -0400 | [diff] [blame] | 4059 | |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4060 | bytes -= bytes_added; |
| 4061 | offset += bytes_added; |
| 4062 | spin_unlock(&ctl->tree_lock); |
| 4063 | |
| 4064 | if (bytes) |
| 4065 | goto again; |
| 4066 | |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 4067 | if (info) |
| 4068 | kmem_cache_free(btrfs_free_space_cachep, info); |
Christophe Leroy | 3acd485 | 2019-08-21 15:05:55 +0000 | [diff] [blame] | 4069 | if (map) |
| 4070 | kmem_cache_free(btrfs_free_space_bitmap_cachep, map); |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4071 | return 0; |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | /* |
| 4075 | * Checks to see if the given range is in the free space cache. This is really |
| 4076 | * just used to check the absence of space, so if there is free space in the |
| 4077 | * range at all we will return 1. |
| 4078 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 4079 | int test_check_exists(struct btrfs_block_group *cache, |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4080 | u64 offset, u64 bytes) |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4081 | { |
| 4082 | struct btrfs_free_space_ctl *ctl = cache->free_space_ctl; |
| 4083 | struct btrfs_free_space *info; |
| 4084 | int ret = 0; |
| 4085 | |
| 4086 | spin_lock(&ctl->tree_lock); |
| 4087 | info = tree_search_offset(ctl, offset, 0, 0); |
| 4088 | if (!info) { |
| 4089 | info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
| 4090 | 1, 0); |
| 4091 | if (!info) |
| 4092 | goto out; |
| 4093 | } |
| 4094 | |
| 4095 | have_info: |
| 4096 | if (info->bitmap) { |
| 4097 | u64 bit_off, bit_bytes; |
| 4098 | struct rb_node *n; |
| 4099 | struct btrfs_free_space *tmp; |
| 4100 | |
| 4101 | bit_off = offset; |
| 4102 | bit_bytes = ctl->unit; |
Josef Bacik | 0584f71 | 2015-10-02 16:12:23 -0400 | [diff] [blame] | 4103 | ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false); |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4104 | if (!ret) { |
| 4105 | if (bit_off == offset) { |
| 4106 | ret = 1; |
| 4107 | goto out; |
| 4108 | } else if (bit_off > offset && |
| 4109 | offset + bytes > bit_off) { |
| 4110 | ret = 1; |
| 4111 | goto out; |
| 4112 | } |
| 4113 | } |
| 4114 | |
| 4115 | n = rb_prev(&info->offset_index); |
| 4116 | while (n) { |
| 4117 | tmp = rb_entry(n, struct btrfs_free_space, |
| 4118 | offset_index); |
| 4119 | if (tmp->offset + tmp->bytes < offset) |
| 4120 | break; |
| 4121 | if (offset + bytes < tmp->offset) { |
Feifei Xu | 5473e0c4 | 2016-06-01 19:18:23 +0800 | [diff] [blame] | 4122 | n = rb_prev(&tmp->offset_index); |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4123 | continue; |
| 4124 | } |
| 4125 | info = tmp; |
| 4126 | goto have_info; |
| 4127 | } |
| 4128 | |
| 4129 | n = rb_next(&info->offset_index); |
| 4130 | while (n) { |
| 4131 | tmp = rb_entry(n, struct btrfs_free_space, |
| 4132 | offset_index); |
| 4133 | if (offset + bytes < tmp->offset) |
| 4134 | break; |
| 4135 | if (tmp->offset + tmp->bytes < offset) { |
Feifei Xu | 5473e0c4 | 2016-06-01 19:18:23 +0800 | [diff] [blame] | 4136 | n = rb_next(&tmp->offset_index); |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4137 | continue; |
| 4138 | } |
| 4139 | info = tmp; |
| 4140 | goto have_info; |
| 4141 | } |
| 4142 | |
Filipe Manana | 2000552 | 2014-08-29 13:35:13 +0100 | [diff] [blame] | 4143 | ret = 0; |
Josef Bacik | 74255aa | 2013-03-15 09:47:08 -0400 | [diff] [blame] | 4144 | goto out; |
| 4145 | } |
| 4146 | |
| 4147 | if (info->offset == offset) { |
| 4148 | ret = 1; |
| 4149 | goto out; |
| 4150 | } |
| 4151 | |
| 4152 | if (offset > info->offset && offset < info->offset + info->bytes) |
| 4153 | ret = 1; |
| 4154 | out: |
| 4155 | spin_unlock(&ctl->tree_lock); |
| 4156 | return ret; |
| 4157 | } |
Josef Bacik | dc11dd5 | 2013-08-14 15:05:12 -0400 | [diff] [blame] | 4158 | #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */ |