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