David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Fujitsu. All rights reserved. |
| 4 | * Written by Miao Xie <miaox@cn.fujitsu.com> |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/slab.h> |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 8 | #include <linux/iversion.h> |
David Sterba | 602cbe9 | 2019-08-21 18:48:25 +0200 | [diff] [blame] | 9 | #include "misc.h" |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 10 | #include "delayed-inode.h" |
| 11 | #include "disk-io.h" |
| 12 | #include "transaction.h" |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 13 | #include "ctree.h" |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 14 | #include "qgroup.h" |
David Sterba | 1f95ec0 | 2019-09-24 19:17:17 +0200 | [diff] [blame] | 15 | #include "locking.h" |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 16 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 17 | #define BTRFS_DELAYED_WRITEBACK 512 |
| 18 | #define BTRFS_DELAYED_BACKGROUND 128 |
| 19 | #define BTRFS_DELAYED_BATCH 16 |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 20 | |
| 21 | static struct kmem_cache *delayed_node_cache; |
| 22 | |
| 23 | int __init btrfs_delayed_inode_init(void) |
| 24 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 25 | delayed_node_cache = kmem_cache_create("btrfs_delayed_node", |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 26 | sizeof(struct btrfs_delayed_node), |
| 27 | 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 28 | SLAB_MEM_SPREAD, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 29 | NULL); |
| 30 | if (!delayed_node_cache) |
| 31 | return -ENOMEM; |
| 32 | return 0; |
| 33 | } |
| 34 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 35 | void __cold btrfs_delayed_inode_exit(void) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 36 | { |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 37 | kmem_cache_destroy(delayed_node_cache); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static inline void btrfs_init_delayed_node( |
| 41 | struct btrfs_delayed_node *delayed_node, |
| 42 | struct btrfs_root *root, u64 inode_id) |
| 43 | { |
| 44 | delayed_node->root = root; |
| 45 | delayed_node->inode_id = inode_id; |
Elena Reshetova | 6de5f18 | 2017-03-03 10:55:16 +0200 | [diff] [blame] | 46 | refcount_set(&delayed_node->refs, 0); |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 47 | delayed_node->ins_root = RB_ROOT_CACHED; |
| 48 | delayed_node->del_root = RB_ROOT_CACHED; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 49 | mutex_init(&delayed_node->mutex); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 50 | INIT_LIST_HEAD(&delayed_node->n_list); |
| 51 | INIT_LIST_HEAD(&delayed_node->p_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static inline int btrfs_is_continuous_delayed_item( |
| 55 | struct btrfs_delayed_item *item1, |
| 56 | struct btrfs_delayed_item *item2) |
| 57 | { |
| 58 | if (item1->key.type == BTRFS_DIR_INDEX_KEY && |
| 59 | item1->key.objectid == item2->key.objectid && |
| 60 | item1->key.type == item2->key.type && |
| 61 | item1->key.offset + 1 == item2->key.offset) |
| 62 | return 1; |
| 63 | return 0; |
| 64 | } |
| 65 | |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 66 | static struct btrfs_delayed_node *btrfs_get_delayed_node( |
| 67 | struct btrfs_inode *btrfs_inode) |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 68 | { |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 69 | struct btrfs_root *root = btrfs_inode->root; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 70 | u64 ino = btrfs_ino(btrfs_inode); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 71 | struct btrfs_delayed_node *node; |
| 72 | |
Seraphime Kirkovski | 20c7bce | 2016-12-15 14:38:16 +0100 | [diff] [blame] | 73 | node = READ_ONCE(btrfs_inode->delayed_node); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 74 | if (node) { |
Elena Reshetova | 6de5f18 | 2017-03-03 10:55:16 +0200 | [diff] [blame] | 75 | refcount_inc(&node->refs); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 76 | return node; |
| 77 | } |
| 78 | |
| 79 | spin_lock(&root->inode_lock); |
| 80 | node = radix_tree_lookup(&root->delayed_nodes_tree, ino); |
Chris Mason | ec35e48 | 2017-12-15 11:58:27 -0800 | [diff] [blame] | 81 | |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 82 | if (node) { |
| 83 | if (btrfs_inode->delayed_node) { |
Elena Reshetova | 6de5f18 | 2017-03-03 10:55:16 +0200 | [diff] [blame] | 84 | refcount_inc(&node->refs); /* can be accessed */ |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 85 | BUG_ON(btrfs_inode->delayed_node != node); |
| 86 | spin_unlock(&root->inode_lock); |
| 87 | return node; |
| 88 | } |
Chris Mason | ec35e48 | 2017-12-15 11:58:27 -0800 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * It's possible that we're racing into the middle of removing |
| 92 | * this node from the radix tree. In this case, the refcount |
| 93 | * was zero and it should never go back to one. Just return |
| 94 | * NULL like it was never in the radix at all; our release |
| 95 | * function is in the process of removing it. |
| 96 | * |
| 97 | * Some implementations of refcount_inc refuse to bump the |
| 98 | * refcount once it has hit zero. If we don't do this dance |
| 99 | * here, refcount_inc() may decide to just WARN_ONCE() instead |
| 100 | * of actually bumping the refcount. |
| 101 | * |
| 102 | * If this node is properly in the radix, we want to bump the |
| 103 | * refcount twice, once for the inode and once for this get |
| 104 | * operation. |
| 105 | */ |
| 106 | if (refcount_inc_not_zero(&node->refs)) { |
| 107 | refcount_inc(&node->refs); |
| 108 | btrfs_inode->delayed_node = node; |
| 109 | } else { |
| 110 | node = NULL; |
| 111 | } |
| 112 | |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 113 | spin_unlock(&root->inode_lock); |
| 114 | return node; |
| 115 | } |
| 116 | spin_unlock(&root->inode_lock); |
| 117 | |
| 118 | return NULL; |
| 119 | } |
| 120 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 121 | /* Will return either the node or PTR_ERR(-ENOMEM) */ |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 122 | static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node( |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 123 | struct btrfs_inode *btrfs_inode) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 124 | { |
| 125 | struct btrfs_delayed_node *node; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 126 | struct btrfs_root *root = btrfs_inode->root; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 127 | u64 ino = btrfs_ino(btrfs_inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 128 | int ret; |
| 129 | |
| 130 | again: |
Nikolay Borisov | 340c6ca | 2017-01-10 20:35:32 +0200 | [diff] [blame] | 131 | node = btrfs_get_delayed_node(btrfs_inode); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 132 | if (node) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 133 | return node; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 134 | |
Alexandru Moise | 352dd9c | 2015-10-25 20:15:06 +0000 | [diff] [blame] | 135 | node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 136 | if (!node) |
| 137 | return ERR_PTR(-ENOMEM); |
Chris Mason | 0d0ca30 | 2011-05-22 07:11:22 -0400 | [diff] [blame] | 138 | btrfs_init_delayed_node(node, root, ino); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 139 | |
Rashika | 95e94d1 | 2013-10-31 03:12:42 +0530 | [diff] [blame] | 140 | /* cached in the btrfs inode and can be accessed */ |
Elena Reshetova | 6de5f18 | 2017-03-03 10:55:16 +0200 | [diff] [blame] | 141 | refcount_set(&node->refs, 2); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 142 | |
David Sterba | e1860a7 | 2016-05-09 14:11:38 +0200 | [diff] [blame] | 143 | ret = radix_tree_preload(GFP_NOFS); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 144 | if (ret) { |
| 145 | kmem_cache_free(delayed_node_cache, node); |
| 146 | return ERR_PTR(ret); |
| 147 | } |
| 148 | |
| 149 | spin_lock(&root->inode_lock); |
Chris Mason | 0d0ca30 | 2011-05-22 07:11:22 -0400 | [diff] [blame] | 150 | ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 151 | if (ret == -EEXIST) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 152 | spin_unlock(&root->inode_lock); |
Jeff Mahoney | 9649303 | 2014-05-27 13:53:20 -0400 | [diff] [blame] | 153 | kmem_cache_free(delayed_node_cache, node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 154 | radix_tree_preload_end(); |
| 155 | goto again; |
| 156 | } |
| 157 | btrfs_inode->delayed_node = node; |
| 158 | spin_unlock(&root->inode_lock); |
| 159 | radix_tree_preload_end(); |
| 160 | |
| 161 | return node; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Call it when holding delayed_node->mutex |
| 166 | * |
| 167 | * If mod = 1, add this node into the prepared list. |
| 168 | */ |
| 169 | static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root, |
| 170 | struct btrfs_delayed_node *node, |
| 171 | int mod) |
| 172 | { |
| 173 | spin_lock(&root->lock); |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 174 | if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 175 | if (!list_empty(&node->p_list)) |
| 176 | list_move_tail(&node->p_list, &root->prepare_list); |
| 177 | else if (mod) |
| 178 | list_add_tail(&node->p_list, &root->prepare_list); |
| 179 | } else { |
| 180 | list_add_tail(&node->n_list, &root->node_list); |
| 181 | list_add_tail(&node->p_list, &root->prepare_list); |
Elena Reshetova | 6de5f18 | 2017-03-03 10:55:16 +0200 | [diff] [blame] | 182 | refcount_inc(&node->refs); /* inserted into list */ |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 183 | root->nodes++; |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 184 | set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 185 | } |
| 186 | spin_unlock(&root->lock); |
| 187 | } |
| 188 | |
| 189 | /* Call it when holding delayed_node->mutex */ |
| 190 | static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root, |
| 191 | struct btrfs_delayed_node *node) |
| 192 | { |
| 193 | spin_lock(&root->lock); |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 194 | if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 195 | root->nodes--; |
Elena Reshetova | 6de5f18 | 2017-03-03 10:55:16 +0200 | [diff] [blame] | 196 | refcount_dec(&node->refs); /* not in the list */ |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 197 | list_del_init(&node->n_list); |
| 198 | if (!list_empty(&node->p_list)) |
| 199 | list_del_init(&node->p_list); |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 200 | clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 201 | } |
| 202 | spin_unlock(&root->lock); |
| 203 | } |
| 204 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 205 | static struct btrfs_delayed_node *btrfs_first_delayed_node( |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 206 | struct btrfs_delayed_root *delayed_root) |
| 207 | { |
| 208 | struct list_head *p; |
| 209 | struct btrfs_delayed_node *node = NULL; |
| 210 | |
| 211 | spin_lock(&delayed_root->lock); |
| 212 | if (list_empty(&delayed_root->node_list)) |
| 213 | goto out; |
| 214 | |
| 215 | p = delayed_root->node_list.next; |
| 216 | node = list_entry(p, struct btrfs_delayed_node, n_list); |
Elena Reshetova | 6de5f18 | 2017-03-03 10:55:16 +0200 | [diff] [blame] | 217 | refcount_inc(&node->refs); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 218 | out: |
| 219 | spin_unlock(&delayed_root->lock); |
| 220 | |
| 221 | return node; |
| 222 | } |
| 223 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 224 | static struct btrfs_delayed_node *btrfs_next_delayed_node( |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 225 | struct btrfs_delayed_node *node) |
| 226 | { |
| 227 | struct btrfs_delayed_root *delayed_root; |
| 228 | struct list_head *p; |
| 229 | struct btrfs_delayed_node *next = NULL; |
| 230 | |
| 231 | delayed_root = node->root->fs_info->delayed_root; |
| 232 | spin_lock(&delayed_root->lock); |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 233 | if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) { |
| 234 | /* not in the list */ |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 235 | if (list_empty(&delayed_root->node_list)) |
| 236 | goto out; |
| 237 | p = delayed_root->node_list.next; |
| 238 | } else if (list_is_last(&node->n_list, &delayed_root->node_list)) |
| 239 | goto out; |
| 240 | else |
| 241 | p = node->n_list.next; |
| 242 | |
| 243 | next = list_entry(p, struct btrfs_delayed_node, n_list); |
Elena Reshetova | 6de5f18 | 2017-03-03 10:55:16 +0200 | [diff] [blame] | 244 | refcount_inc(&next->refs); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 245 | out: |
| 246 | spin_unlock(&delayed_root->lock); |
| 247 | |
| 248 | return next; |
| 249 | } |
| 250 | |
| 251 | static void __btrfs_release_delayed_node( |
| 252 | struct btrfs_delayed_node *delayed_node, |
| 253 | int mod) |
| 254 | { |
| 255 | struct btrfs_delayed_root *delayed_root; |
| 256 | |
| 257 | if (!delayed_node) |
| 258 | return; |
| 259 | |
| 260 | delayed_root = delayed_node->root->fs_info->delayed_root; |
| 261 | |
| 262 | mutex_lock(&delayed_node->mutex); |
| 263 | if (delayed_node->count) |
| 264 | btrfs_queue_delayed_node(delayed_root, delayed_node, mod); |
| 265 | else |
| 266 | btrfs_dequeue_delayed_node(delayed_root, delayed_node); |
| 267 | mutex_unlock(&delayed_node->mutex); |
| 268 | |
Elena Reshetova | 6de5f18 | 2017-03-03 10:55:16 +0200 | [diff] [blame] | 269 | if (refcount_dec_and_test(&delayed_node->refs)) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 270 | struct btrfs_root *root = delayed_node->root; |
Chris Mason | ec35e48 | 2017-12-15 11:58:27 -0800 | [diff] [blame] | 271 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 272 | spin_lock(&root->inode_lock); |
Chris Mason | ec35e48 | 2017-12-15 11:58:27 -0800 | [diff] [blame] | 273 | /* |
| 274 | * Once our refcount goes to zero, nobody is allowed to bump it |
| 275 | * back up. We can delete it now. |
| 276 | */ |
| 277 | ASSERT(refcount_read(&delayed_node->refs) == 0); |
| 278 | radix_tree_delete(&root->delayed_nodes_tree, |
| 279 | delayed_node->inode_id); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 280 | spin_unlock(&root->inode_lock); |
Chris Mason | ec35e48 | 2017-12-15 11:58:27 -0800 | [diff] [blame] | 281 | kmem_cache_free(delayed_node_cache, delayed_node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node) |
| 286 | { |
| 287 | __btrfs_release_delayed_node(node, 0); |
| 288 | } |
| 289 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 290 | static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node( |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 291 | struct btrfs_delayed_root *delayed_root) |
| 292 | { |
| 293 | struct list_head *p; |
| 294 | struct btrfs_delayed_node *node = NULL; |
| 295 | |
| 296 | spin_lock(&delayed_root->lock); |
| 297 | if (list_empty(&delayed_root->prepare_list)) |
| 298 | goto out; |
| 299 | |
| 300 | p = delayed_root->prepare_list.next; |
| 301 | list_del_init(p); |
| 302 | node = list_entry(p, struct btrfs_delayed_node, p_list); |
Elena Reshetova | 6de5f18 | 2017-03-03 10:55:16 +0200 | [diff] [blame] | 303 | refcount_inc(&node->refs); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 304 | out: |
| 305 | spin_unlock(&delayed_root->lock); |
| 306 | |
| 307 | return node; |
| 308 | } |
| 309 | |
| 310 | static inline void btrfs_release_prepared_delayed_node( |
| 311 | struct btrfs_delayed_node *node) |
| 312 | { |
| 313 | __btrfs_release_delayed_node(node, 1); |
| 314 | } |
| 315 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 316 | static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 317 | { |
| 318 | struct btrfs_delayed_item *item; |
| 319 | item = kmalloc(sizeof(*item) + data_len, GFP_NOFS); |
| 320 | if (item) { |
| 321 | item->data_len = data_len; |
| 322 | item->ins_or_del = 0; |
| 323 | item->bytes_reserved = 0; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 324 | item->delayed_node = NULL; |
Elena Reshetova | 089e77e | 2017-03-03 10:55:17 +0200 | [diff] [blame] | 325 | refcount_set(&item->refs, 1); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 326 | } |
| 327 | return item; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * __btrfs_lookup_delayed_item - look up the delayed item by key |
| 332 | * @delayed_node: pointer to the delayed node |
| 333 | * @key: the key to look up |
| 334 | * @prev: used to store the prev item if the right item isn't found |
| 335 | * @next: used to store the next item if the right item isn't found |
| 336 | * |
| 337 | * Note: if we don't find the right item, we will return the prev item and |
| 338 | * the next item. |
| 339 | */ |
| 340 | static struct btrfs_delayed_item *__btrfs_lookup_delayed_item( |
| 341 | struct rb_root *root, |
| 342 | struct btrfs_key *key, |
| 343 | struct btrfs_delayed_item **prev, |
| 344 | struct btrfs_delayed_item **next) |
| 345 | { |
| 346 | struct rb_node *node, *prev_node = NULL; |
| 347 | struct btrfs_delayed_item *delayed_item = NULL; |
| 348 | int ret = 0; |
| 349 | |
| 350 | node = root->rb_node; |
| 351 | |
| 352 | while (node) { |
| 353 | delayed_item = rb_entry(node, struct btrfs_delayed_item, |
| 354 | rb_node); |
| 355 | prev_node = node; |
| 356 | ret = btrfs_comp_cpu_keys(&delayed_item->key, key); |
| 357 | if (ret < 0) |
| 358 | node = node->rb_right; |
| 359 | else if (ret > 0) |
| 360 | node = node->rb_left; |
| 361 | else |
| 362 | return delayed_item; |
| 363 | } |
| 364 | |
| 365 | if (prev) { |
| 366 | if (!prev_node) |
| 367 | *prev = NULL; |
| 368 | else if (ret < 0) |
| 369 | *prev = delayed_item; |
| 370 | else if ((node = rb_prev(prev_node)) != NULL) { |
| 371 | *prev = rb_entry(node, struct btrfs_delayed_item, |
| 372 | rb_node); |
| 373 | } else |
| 374 | *prev = NULL; |
| 375 | } |
| 376 | |
| 377 | if (next) { |
| 378 | if (!prev_node) |
| 379 | *next = NULL; |
| 380 | else if (ret > 0) |
| 381 | *next = delayed_item; |
| 382 | else if ((node = rb_next(prev_node)) != NULL) { |
| 383 | *next = rb_entry(node, struct btrfs_delayed_item, |
| 384 | rb_node); |
| 385 | } else |
| 386 | *next = NULL; |
| 387 | } |
| 388 | return NULL; |
| 389 | } |
| 390 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 391 | static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item( |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 392 | struct btrfs_delayed_node *delayed_node, |
| 393 | struct btrfs_key *key) |
| 394 | { |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 395 | return __btrfs_lookup_delayed_item(&delayed_node->ins_root.rb_root, key, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 396 | NULL, NULL); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 397 | } |
| 398 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 399 | static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node, |
| 400 | struct btrfs_delayed_item *ins, |
| 401 | int action) |
| 402 | { |
| 403 | struct rb_node **p, *node; |
| 404 | struct rb_node *parent_node = NULL; |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 405 | struct rb_root_cached *root; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 406 | struct btrfs_delayed_item *item; |
| 407 | int cmp; |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 408 | bool leftmost = true; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 409 | |
| 410 | if (action == BTRFS_DELAYED_INSERTION_ITEM) |
| 411 | root = &delayed_node->ins_root; |
| 412 | else if (action == BTRFS_DELAYED_DELETION_ITEM) |
| 413 | root = &delayed_node->del_root; |
| 414 | else |
| 415 | BUG(); |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 416 | p = &root->rb_root.rb_node; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 417 | node = &ins->rb_node; |
| 418 | |
| 419 | while (*p) { |
| 420 | parent_node = *p; |
| 421 | item = rb_entry(parent_node, struct btrfs_delayed_item, |
| 422 | rb_node); |
| 423 | |
| 424 | cmp = btrfs_comp_cpu_keys(&item->key, &ins->key); |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 425 | if (cmp < 0) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 426 | p = &(*p)->rb_right; |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 427 | leftmost = false; |
| 428 | } else if (cmp > 0) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 429 | p = &(*p)->rb_left; |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 430 | } else { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 431 | return -EEXIST; |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 432 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | rb_link_node(node, parent_node, p); |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 436 | rb_insert_color_cached(node, root, leftmost); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 437 | ins->delayed_node = delayed_node; |
| 438 | ins->ins_or_del = action; |
| 439 | |
| 440 | if (ins->key.type == BTRFS_DIR_INDEX_KEY && |
| 441 | action == BTRFS_DELAYED_INSERTION_ITEM && |
| 442 | ins->key.offset >= delayed_node->index_cnt) |
| 443 | delayed_node->index_cnt = ins->key.offset + 1; |
| 444 | |
| 445 | delayed_node->count++; |
| 446 | atomic_inc(&delayed_node->root->fs_info->delayed_root->items); |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node, |
| 451 | struct btrfs_delayed_item *item) |
| 452 | { |
| 453 | return __btrfs_add_delayed_item(node, item, |
| 454 | BTRFS_DELAYED_INSERTION_ITEM); |
| 455 | } |
| 456 | |
| 457 | static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node, |
| 458 | struct btrfs_delayed_item *item) |
| 459 | { |
| 460 | return __btrfs_add_delayed_item(node, item, |
| 461 | BTRFS_DELAYED_DELETION_ITEM); |
| 462 | } |
| 463 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 464 | static void finish_one_item(struct btrfs_delayed_root *delayed_root) |
| 465 | { |
| 466 | int seq = atomic_inc_return(&delayed_root->items_seq); |
David Sterba | ee86395 | 2015-02-16 19:41:40 +0100 | [diff] [blame] | 467 | |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 468 | /* atomic_dec_return implies a barrier */ |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 469 | if ((atomic_dec_return(&delayed_root->items) < |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 470 | BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0)) |
| 471 | cond_wake_up_nomb(&delayed_root->wait); |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 472 | } |
| 473 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 474 | static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item) |
| 475 | { |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 476 | struct rb_root_cached *root; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 477 | struct btrfs_delayed_root *delayed_root; |
| 478 | |
Qu Wenruo | 933c22a | 2019-07-16 17:00:32 +0800 | [diff] [blame] | 479 | /* Not associated with any delayed_node */ |
| 480 | if (!delayed_item->delayed_node) |
| 481 | return; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 482 | delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root; |
| 483 | |
| 484 | BUG_ON(!delayed_root); |
| 485 | BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM && |
| 486 | delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM); |
| 487 | |
| 488 | if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM) |
| 489 | root = &delayed_item->delayed_node->ins_root; |
| 490 | else |
| 491 | root = &delayed_item->delayed_node->del_root; |
| 492 | |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 493 | rb_erase_cached(&delayed_item->rb_node, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 494 | delayed_item->delayed_node->count--; |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 495 | |
| 496 | finish_one_item(delayed_root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static void btrfs_release_delayed_item(struct btrfs_delayed_item *item) |
| 500 | { |
| 501 | if (item) { |
| 502 | __btrfs_remove_delayed_item(item); |
Elena Reshetova | 089e77e | 2017-03-03 10:55:17 +0200 | [diff] [blame] | 503 | if (refcount_dec_and_test(&item->refs)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 504 | kfree(item); |
| 505 | } |
| 506 | } |
| 507 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 508 | static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item( |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 509 | struct btrfs_delayed_node *delayed_node) |
| 510 | { |
| 511 | struct rb_node *p; |
| 512 | struct btrfs_delayed_item *item = NULL; |
| 513 | |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 514 | p = rb_first_cached(&delayed_node->ins_root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 515 | if (p) |
| 516 | item = rb_entry(p, struct btrfs_delayed_item, rb_node); |
| 517 | |
| 518 | return item; |
| 519 | } |
| 520 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 521 | static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item( |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 522 | struct btrfs_delayed_node *delayed_node) |
| 523 | { |
| 524 | struct rb_node *p; |
| 525 | struct btrfs_delayed_item *item = NULL; |
| 526 | |
Liu Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 527 | p = rb_first_cached(&delayed_node->del_root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 528 | if (p) |
| 529 | item = rb_entry(p, struct btrfs_delayed_item, rb_node); |
| 530 | |
| 531 | return item; |
| 532 | } |
| 533 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 534 | static struct btrfs_delayed_item *__btrfs_next_delayed_item( |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 535 | struct btrfs_delayed_item *item) |
| 536 | { |
| 537 | struct rb_node *p; |
| 538 | struct btrfs_delayed_item *next = NULL; |
| 539 | |
| 540 | p = rb_next(&item->rb_node); |
| 541 | if (p) |
| 542 | next = rb_entry(p, struct btrfs_delayed_item, rb_node); |
| 543 | |
| 544 | return next; |
| 545 | } |
| 546 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 547 | static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans, |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 548 | struct btrfs_root *root, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 549 | struct btrfs_delayed_item *item) |
| 550 | { |
| 551 | struct btrfs_block_rsv *src_rsv; |
| 552 | struct btrfs_block_rsv *dst_rsv; |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 553 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 554 | u64 num_bytes; |
| 555 | int ret; |
| 556 | |
| 557 | if (!trans->bytes_reserved) |
| 558 | return 0; |
| 559 | |
| 560 | src_rsv = trans->block_rsv; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 561 | dst_rsv = &fs_info->delayed_block_rsv; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 562 | |
Josef Bacik | 2bd36e7 | 2019-08-22 15:14:33 -0400 | [diff] [blame] | 563 | num_bytes = btrfs_calc_insert_metadata_size(fs_info, 1); |
Qu Wenruo | f218ea6 | 2018-04-17 16:52:45 +0800 | [diff] [blame] | 564 | |
| 565 | /* |
| 566 | * Here we migrate space rsv from transaction rsv, since have already |
| 567 | * reserved space when starting a transaction. So no need to reserve |
| 568 | * qgroup space here. |
| 569 | */ |
Lu Fengqi | 3a58417 | 2018-08-04 21:10:55 +0800 | [diff] [blame] | 570 | ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 571 | if (!ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 572 | trace_btrfs_space_reservation(fs_info, "delayed_item", |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 573 | item->key.objectid, |
| 574 | num_bytes, 1); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 575 | item->bytes_reserved = num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 576 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 577 | |
| 578 | return ret; |
| 579 | } |
| 580 | |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 581 | static void btrfs_delayed_item_release_metadata(struct btrfs_root *root, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 582 | struct btrfs_delayed_item *item) |
| 583 | { |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 584 | struct btrfs_block_rsv *rsv; |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 585 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 586 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 587 | if (!item->bytes_reserved) |
| 588 | return; |
| 589 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 590 | rsv = &fs_info->delayed_block_rsv; |
Qu Wenruo | f218ea6 | 2018-04-17 16:52:45 +0800 | [diff] [blame] | 591 | /* |
| 592 | * Check btrfs_delayed_item_reserve_metadata() to see why we don't need |
| 593 | * to release/reserve qgroup space. |
| 594 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 595 | trace_btrfs_space_reservation(fs_info, "delayed_item", |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 596 | item->key.objectid, item->bytes_reserved, |
| 597 | 0); |
Nikolay Borisov | 63f018b | 2020-03-10 10:59:31 +0200 | [diff] [blame] | 598 | btrfs_block_rsv_release(fs_info, rsv, item->bytes_reserved, NULL); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | static int btrfs_delayed_inode_reserve_metadata( |
| 602 | struct btrfs_trans_handle *trans, |
| 603 | struct btrfs_root *root, |
| 604 | struct btrfs_delayed_node *node) |
| 605 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 606 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 607 | struct btrfs_block_rsv *src_rsv; |
| 608 | struct btrfs_block_rsv *dst_rsv; |
| 609 | u64 num_bytes; |
| 610 | int ret; |
| 611 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 612 | src_rsv = trans->block_rsv; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 613 | dst_rsv = &fs_info->delayed_block_rsv; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 614 | |
Josef Bacik | bcacf5f3 | 2019-08-22 15:14:34 -0400 | [diff] [blame] | 615 | num_bytes = btrfs_calc_metadata_size(fs_info, 1); |
Josef Bacik | c06a0e1 | 2011-11-04 19:56:02 -0400 | [diff] [blame] | 616 | |
| 617 | /* |
| 618 | * btrfs_dirty_inode will update the inode under btrfs_join_transaction |
| 619 | * which doesn't reserve space for speed. This is a problem since we |
| 620 | * still need to reserve space for this update, so try to reserve the |
| 621 | * space. |
| 622 | * |
| 623 | * Now if src_rsv == delalloc_block_rsv we'll let it just steal since |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 624 | * we always reserve enough to update the inode item. |
Josef Bacik | c06a0e1 | 2011-11-04 19:56:02 -0400 | [diff] [blame] | 625 | */ |
Chris Mason | e755d9a | 2011-12-15 13:36:29 -0500 | [diff] [blame] | 626 | if (!src_rsv || (!trans->bytes_reserved && |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 627 | src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) { |
Nikolay Borisov | 4d14c5c | 2021-02-22 18:40:44 +0200 | [diff] [blame] | 628 | ret = btrfs_qgroup_reserve_meta(root, num_bytes, |
| 629 | BTRFS_QGROUP_RSV_META_PREALLOC, true); |
Qu Wenruo | f218ea6 | 2018-04-17 16:52:45 +0800 | [diff] [blame] | 630 | if (ret < 0) |
| 631 | return ret; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 632 | ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes, |
| 633 | BTRFS_RESERVE_NO_FLUSH); |
Nikolay Borisov | 98686ff | 2021-02-22 18:40:47 +0200 | [diff] [blame] | 634 | /* NO_FLUSH could only fail with -ENOSPC */ |
| 635 | ASSERT(ret == 0 || ret == -ENOSPC); |
| 636 | if (ret) |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 637 | btrfs_qgroup_free_meta_prealloc(root, num_bytes); |
Nikolay Borisov | 98686ff | 2021-02-22 18:40:47 +0200 | [diff] [blame] | 638 | } else { |
| 639 | ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true); |
Josef Bacik | c06a0e1 | 2011-11-04 19:56:02 -0400 | [diff] [blame] | 640 | } |
| 641 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 642 | if (!ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 643 | trace_btrfs_space_reservation(fs_info, "delayed_inode", |
Nikolay Borisov | 8e3c9d3 | 2021-02-22 18:40:46 +0200 | [diff] [blame] | 644 | node->inode_id, num_bytes, 1); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 645 | node->bytes_reserved = num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 646 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 647 | |
| 648 | return ret; |
| 649 | } |
| 650 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 651 | static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info *fs_info, |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 652 | struct btrfs_delayed_node *node, |
| 653 | bool qgroup_free) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 654 | { |
| 655 | struct btrfs_block_rsv *rsv; |
| 656 | |
| 657 | if (!node->bytes_reserved) |
| 658 | return; |
| 659 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 660 | rsv = &fs_info->delayed_block_rsv; |
| 661 | trace_btrfs_space_reservation(fs_info, "delayed_inode", |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 662 | node->inode_id, node->bytes_reserved, 0); |
Nikolay Borisov | 63f018b | 2020-03-10 10:59:31 +0200 | [diff] [blame] | 663 | btrfs_block_rsv_release(fs_info, rsv, node->bytes_reserved, NULL); |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 664 | if (qgroup_free) |
| 665 | btrfs_qgroup_free_meta_prealloc(node->root, |
| 666 | node->bytes_reserved); |
| 667 | else |
| 668 | btrfs_qgroup_convert_reserved_meta(node->root, |
| 669 | node->bytes_reserved); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 670 | node->bytes_reserved = 0; |
| 671 | } |
| 672 | |
| 673 | /* |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 674 | * Insert a single delayed item or a batch of delayed items that have consecutive |
| 675 | * keys if they exist. |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 676 | */ |
| 677 | static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans, |
| 678 | struct btrfs_root *root, |
| 679 | struct btrfs_path *path, |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 680 | struct btrfs_delayed_item *first_item) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 681 | { |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 682 | LIST_HEAD(item_list); |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 683 | struct btrfs_delayed_item *curr; |
| 684 | struct btrfs_delayed_item *next; |
| 685 | const int max_size = BTRFS_LEAF_DATA_SIZE(root->fs_info); |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 686 | struct btrfs_item_batch batch; |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 687 | int total_size; |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 688 | char *ins_data = NULL; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 689 | int ret; |
| 690 | |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 691 | list_add_tail(&first_item->tree_list, &item_list); |
| 692 | batch.total_data_size = first_item->data_len; |
| 693 | batch.nr = 1; |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 694 | total_size = first_item->data_len + sizeof(struct btrfs_item); |
| 695 | curr = first_item; |
| 696 | |
| 697 | while (true) { |
| 698 | int next_size; |
| 699 | |
| 700 | next = __btrfs_next_delayed_item(curr); |
| 701 | if (!next || !btrfs_is_continuous_delayed_item(curr, next)) |
| 702 | break; |
| 703 | |
| 704 | next_size = next->data_len + sizeof(struct btrfs_item); |
| 705 | if (total_size + next_size > max_size) |
| 706 | break; |
| 707 | |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 708 | list_add_tail(&next->tree_list, &item_list); |
| 709 | batch.nr++; |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 710 | total_size += next_size; |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 711 | batch.total_data_size += next->data_len; |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 712 | curr = next; |
| 713 | } |
| 714 | |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 715 | if (batch.nr == 1) { |
| 716 | batch.keys = &first_item->key; |
| 717 | batch.data_sizes = &first_item->data_len; |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 718 | } else { |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 719 | struct btrfs_key *ins_keys; |
| 720 | u32 *ins_sizes; |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 721 | int i = 0; |
| 722 | |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 723 | ins_data = kmalloc(batch.nr * sizeof(u32) + |
| 724 | batch.nr * sizeof(struct btrfs_key), GFP_NOFS); |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 725 | if (!ins_data) { |
| 726 | ret = -ENOMEM; |
| 727 | goto out; |
| 728 | } |
| 729 | ins_sizes = (u32 *)ins_data; |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 730 | ins_keys = (struct btrfs_key *)(ins_data + batch.nr * sizeof(u32)); |
| 731 | batch.keys = ins_keys; |
| 732 | batch.data_sizes = ins_sizes; |
| 733 | list_for_each_entry(curr, &item_list, tree_list) { |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 734 | ins_keys[i] = curr->key; |
| 735 | ins_sizes[i] = curr->data_len; |
| 736 | i++; |
| 737 | } |
| 738 | } |
| 739 | |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 740 | ret = btrfs_insert_empty_items(trans, root, path, &batch); |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 741 | if (ret) |
| 742 | goto out; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 743 | |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 744 | list_for_each_entry(curr, &item_list, tree_list) { |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 745 | char *data_ptr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 746 | |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 747 | data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char); |
| 748 | write_extent_buffer(path->nodes[0], &curr->data, |
| 749 | (unsigned long)data_ptr, curr->data_len); |
| 750 | path->slots[0]++; |
| 751 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 752 | |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 753 | /* |
| 754 | * Now release our path before releasing the delayed items and their |
| 755 | * metadata reservations, so that we don't block other tasks for more |
| 756 | * time than needed. |
| 757 | */ |
| 758 | btrfs_release_path(path); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 759 | |
Filipe Manana | b7ef5f3 | 2021-09-24 12:28:13 +0100 | [diff] [blame] | 760 | list_for_each_entry_safe(curr, next, &item_list, tree_list) { |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 761 | list_del(&curr->tree_list); |
| 762 | btrfs_delayed_item_release_metadata(root, curr); |
| 763 | btrfs_release_delayed_item(curr); |
| 764 | } |
| 765 | out: |
| 766 | kfree(ins_data); |
| 767 | return ret; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 768 | } |
| 769 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 770 | static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans, |
| 771 | struct btrfs_path *path, |
| 772 | struct btrfs_root *root, |
| 773 | struct btrfs_delayed_node *node) |
| 774 | { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 775 | int ret = 0; |
| 776 | |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 777 | while (ret == 0) { |
| 778 | struct btrfs_delayed_item *curr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 779 | |
Filipe Manana | 506650d | 2021-07-20 16:05:22 +0100 | [diff] [blame] | 780 | mutex_lock(&node->mutex); |
| 781 | curr = __btrfs_first_delayed_insertion_item(node); |
| 782 | if (!curr) { |
| 783 | mutex_unlock(&node->mutex); |
| 784 | break; |
| 785 | } |
| 786 | ret = btrfs_insert_delayed_item(trans, root, path, curr); |
| 787 | mutex_unlock(&node->mutex); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 788 | } |
| 789 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans, |
| 794 | struct btrfs_root *root, |
| 795 | struct btrfs_path *path, |
| 796 | struct btrfs_delayed_item *item) |
| 797 | { |
| 798 | struct btrfs_delayed_item *curr, *next; |
| 799 | struct extent_buffer *leaf; |
| 800 | struct btrfs_key key; |
| 801 | struct list_head head; |
| 802 | int nitems, i, last_item; |
| 803 | int ret = 0; |
| 804 | |
| 805 | BUG_ON(!path->nodes[0]); |
| 806 | |
| 807 | leaf = path->nodes[0]; |
| 808 | |
| 809 | i = path->slots[0]; |
| 810 | last_item = btrfs_header_nritems(leaf) - 1; |
| 811 | if (i > last_item) |
| 812 | return -ENOENT; /* FIXME: Is errno suitable? */ |
| 813 | |
| 814 | next = item; |
| 815 | INIT_LIST_HEAD(&head); |
| 816 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 817 | nitems = 0; |
| 818 | /* |
| 819 | * count the number of the dir index items that we can delete in batch |
| 820 | */ |
| 821 | while (btrfs_comp_cpu_keys(&next->key, &key) == 0) { |
| 822 | list_add_tail(&next->tree_list, &head); |
| 823 | nitems++; |
| 824 | |
| 825 | curr = next; |
| 826 | next = __btrfs_next_delayed_item(curr); |
| 827 | if (!next) |
| 828 | break; |
| 829 | |
| 830 | if (!btrfs_is_continuous_delayed_item(curr, next)) |
| 831 | break; |
| 832 | |
| 833 | i++; |
| 834 | if (i > last_item) |
| 835 | break; |
| 836 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 837 | } |
| 838 | |
| 839 | if (!nitems) |
| 840 | return 0; |
| 841 | |
| 842 | ret = btrfs_del_items(trans, root, path, path->slots[0], nitems); |
| 843 | if (ret) |
| 844 | goto out; |
| 845 | |
| 846 | list_for_each_entry_safe(curr, next, &head, tree_list) { |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 847 | btrfs_delayed_item_release_metadata(root, curr); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 848 | list_del(&curr->tree_list); |
| 849 | btrfs_release_delayed_item(curr); |
| 850 | } |
| 851 | |
| 852 | out: |
| 853 | return ret; |
| 854 | } |
| 855 | |
| 856 | static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans, |
| 857 | struct btrfs_path *path, |
| 858 | struct btrfs_root *root, |
| 859 | struct btrfs_delayed_node *node) |
| 860 | { |
| 861 | struct btrfs_delayed_item *curr, *prev; |
| 862 | int ret = 0; |
| 863 | |
| 864 | do_again: |
| 865 | mutex_lock(&node->mutex); |
| 866 | curr = __btrfs_first_delayed_deletion_item(node); |
| 867 | if (!curr) |
| 868 | goto delete_fail; |
| 869 | |
| 870 | ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1); |
| 871 | if (ret < 0) |
| 872 | goto delete_fail; |
| 873 | else if (ret > 0) { |
| 874 | /* |
| 875 | * can't find the item which the node points to, so this node |
| 876 | * is invalid, just drop it. |
| 877 | */ |
| 878 | prev = curr; |
| 879 | curr = __btrfs_next_delayed_item(prev); |
| 880 | btrfs_release_delayed_item(prev); |
| 881 | ret = 0; |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 882 | btrfs_release_path(path); |
Fengguang Wu | 6209526 | 2012-08-04 01:45:02 -0600 | [diff] [blame] | 883 | if (curr) { |
| 884 | mutex_unlock(&node->mutex); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 885 | goto do_again; |
Fengguang Wu | 6209526 | 2012-08-04 01:45:02 -0600 | [diff] [blame] | 886 | } else |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 887 | goto delete_fail; |
| 888 | } |
| 889 | |
| 890 | btrfs_batch_delete_items(trans, root, path, curr); |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 891 | btrfs_release_path(path); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 892 | mutex_unlock(&node->mutex); |
| 893 | goto do_again; |
| 894 | |
| 895 | delete_fail: |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 896 | btrfs_release_path(path); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 897 | mutex_unlock(&node->mutex); |
| 898 | return ret; |
| 899 | } |
| 900 | |
| 901 | static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node) |
| 902 | { |
| 903 | struct btrfs_delayed_root *delayed_root; |
| 904 | |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 905 | if (delayed_node && |
| 906 | test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 907 | BUG_ON(!delayed_node->root); |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 908 | clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 909 | delayed_node->count--; |
| 910 | |
| 911 | delayed_root = delayed_node->root->fs_info->delayed_root; |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 912 | finish_one_item(delayed_root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 916 | static void btrfs_release_delayed_iref(struct btrfs_delayed_node *delayed_node) |
| 917 | { |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 918 | |
Josef Bacik | a4cb90d | 2021-05-21 16:44:07 -0400 | [diff] [blame] | 919 | if (test_and_clear_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags)) { |
| 920 | struct btrfs_delayed_root *delayed_root; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 921 | |
Josef Bacik | a4cb90d | 2021-05-21 16:44:07 -0400 | [diff] [blame] | 922 | ASSERT(delayed_node->root); |
| 923 | delayed_node->count--; |
| 924 | |
| 925 | delayed_root = delayed_node->root->fs_info->delayed_root; |
| 926 | finish_one_item(delayed_root); |
| 927 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 928 | } |
| 929 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 930 | static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans, |
| 931 | struct btrfs_root *root, |
| 932 | struct btrfs_path *path, |
| 933 | struct btrfs_delayed_node *node) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 934 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 935 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 936 | struct btrfs_key key; |
| 937 | struct btrfs_inode_item *inode_item; |
| 938 | struct extent_buffer *leaf; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 939 | int mod; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 940 | int ret; |
| 941 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 942 | key.objectid = node->inode_id; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 943 | key.type = BTRFS_INODE_ITEM_KEY; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 944 | key.offset = 0; |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 945 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 946 | if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags)) |
| 947 | mod = -1; |
| 948 | else |
| 949 | mod = 1; |
| 950 | |
| 951 | ret = btrfs_lookup_inode(trans, root, path, &key, mod); |
Josef Bacik | bb385be | 2021-05-21 16:44:08 -0400 | [diff] [blame] | 952 | if (ret > 0) |
| 953 | ret = -ENOENT; |
| 954 | if (ret < 0) |
| 955 | goto out; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 956 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 957 | leaf = path->nodes[0]; |
| 958 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 959 | struct btrfs_inode_item); |
| 960 | write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item, |
| 961 | sizeof(struct btrfs_inode_item)); |
| 962 | btrfs_mark_buffer_dirty(leaf); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 963 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 964 | if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags)) |
Josef Bacik | a4cb90d | 2021-05-21 16:44:07 -0400 | [diff] [blame] | 965 | goto out; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 966 | |
| 967 | path->slots[0]++; |
| 968 | if (path->slots[0] >= btrfs_header_nritems(leaf)) |
| 969 | goto search; |
| 970 | again: |
| 971 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 972 | if (key.objectid != node->inode_id) |
| 973 | goto out; |
| 974 | |
| 975 | if (key.type != BTRFS_INODE_REF_KEY && |
| 976 | key.type != BTRFS_INODE_EXTREF_KEY) |
| 977 | goto out; |
| 978 | |
| 979 | /* |
| 980 | * Delayed iref deletion is for the inode who has only one link, |
| 981 | * so there is only one iref. The case that several irefs are |
| 982 | * in the same item doesn't exist. |
| 983 | */ |
| 984 | btrfs_del_item(trans, root, path); |
| 985 | out: |
| 986 | btrfs_release_delayed_iref(node); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 987 | btrfs_release_path(path); |
| 988 | err_out: |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 989 | btrfs_delayed_inode_release_metadata(fs_info, node, (ret < 0)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 990 | btrfs_release_delayed_inode(node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 991 | |
Josef Bacik | 04587ad | 2021-05-21 16:44:09 -0400 | [diff] [blame] | 992 | /* |
| 993 | * If we fail to update the delayed inode we need to abort the |
| 994 | * transaction, because we could leave the inode with the improper |
| 995 | * counts behind. |
| 996 | */ |
| 997 | if (ret && ret != -ENOENT) |
| 998 | btrfs_abort_transaction(trans, ret); |
| 999 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 1000 | return ret; |
| 1001 | |
| 1002 | search: |
| 1003 | btrfs_release_path(path); |
| 1004 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1005 | key.type = BTRFS_INODE_EXTREF_KEY; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 1006 | key.offset = -1; |
Josef Bacik | 351cbf6 | 2020-03-19 10:11:32 -0400 | [diff] [blame] | 1007 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 1008 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1009 | if (ret < 0) |
| 1010 | goto err_out; |
| 1011 | ASSERT(ret); |
| 1012 | |
| 1013 | ret = 0; |
| 1014 | leaf = path->nodes[0]; |
| 1015 | path->slots[0]--; |
| 1016 | goto again; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1017 | } |
| 1018 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1019 | static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans, |
| 1020 | struct btrfs_root *root, |
| 1021 | struct btrfs_path *path, |
| 1022 | struct btrfs_delayed_node *node) |
| 1023 | { |
| 1024 | int ret; |
| 1025 | |
| 1026 | mutex_lock(&node->mutex); |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 1027 | if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &node->flags)) { |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1028 | mutex_unlock(&node->mutex); |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | ret = __btrfs_update_delayed_inode(trans, root, path, node); |
| 1033 | mutex_unlock(&node->mutex); |
| 1034 | return ret; |
| 1035 | } |
| 1036 | |
Miao Xie | 4ea41ce | 2012-12-19 06:59:03 +0000 | [diff] [blame] | 1037 | static inline int |
| 1038 | __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans, |
| 1039 | struct btrfs_path *path, |
| 1040 | struct btrfs_delayed_node *node) |
| 1041 | { |
| 1042 | int ret; |
| 1043 | |
| 1044 | ret = btrfs_insert_delayed_items(trans, path, node->root, node); |
| 1045 | if (ret) |
| 1046 | return ret; |
| 1047 | |
| 1048 | ret = btrfs_delete_delayed_items(trans, path, node->root, node); |
| 1049 | if (ret) |
| 1050 | return ret; |
| 1051 | |
| 1052 | ret = btrfs_update_delayed_inode(trans, node->root, path, node); |
| 1053 | return ret; |
| 1054 | } |
| 1055 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1056 | /* |
| 1057 | * Called when committing the transaction. |
| 1058 | * Returns 0 on success. |
| 1059 | * Returns < 0 on error and returns with an aborted transaction with any |
| 1060 | * outstanding delayed items cleaned up. |
| 1061 | */ |
Nikolay Borisov | b84acab | 2018-02-07 17:55:42 +0200 | [diff] [blame] | 1062 | static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, int nr) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1063 | { |
Nikolay Borisov | b84acab | 2018-02-07 17:55:42 +0200 | [diff] [blame] | 1064 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1065 | struct btrfs_delayed_root *delayed_root; |
| 1066 | struct btrfs_delayed_node *curr_node, *prev_node; |
| 1067 | struct btrfs_path *path; |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1068 | struct btrfs_block_rsv *block_rsv; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1069 | int ret = 0; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 1070 | bool count = (nr > 0); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1071 | |
David Sterba | bf31f87 | 2020-02-05 17:34:34 +0100 | [diff] [blame] | 1072 | if (TRANS_ABORTED(trans)) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1073 | return -EIO; |
| 1074 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1075 | path = btrfs_alloc_path(); |
| 1076 | if (!path) |
| 1077 | return -ENOMEM; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1078 | |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1079 | block_rsv = trans->block_rsv; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1080 | trans->block_rsv = &fs_info->delayed_block_rsv; |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1081 | |
Jeff Mahoney | ccdf9b3 | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1082 | delayed_root = fs_info->delayed_root; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1083 | |
| 1084 | curr_node = btrfs_first_delayed_node(delayed_root); |
Abaci Team | a4559e6 | 2021-01-27 16:11:37 +0800 | [diff] [blame] | 1085 | while (curr_node && (!count || nr--)) { |
Miao Xie | 4ea41ce | 2012-12-19 06:59:03 +0000 | [diff] [blame] | 1086 | ret = __btrfs_commit_inode_delayed_items(trans, path, |
| 1087 | curr_node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1088 | if (ret) { |
| 1089 | btrfs_release_delayed_node(curr_node); |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 1090 | curr_node = NULL; |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1091 | btrfs_abort_transaction(trans, ret); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1092 | break; |
| 1093 | } |
| 1094 | |
| 1095 | prev_node = curr_node; |
| 1096 | curr_node = btrfs_next_delayed_node(curr_node); |
| 1097 | btrfs_release_delayed_node(prev_node); |
| 1098 | } |
| 1099 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 1100 | if (curr_node) |
| 1101 | btrfs_release_delayed_node(curr_node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1102 | btrfs_free_path(path); |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1103 | trans->block_rsv = block_rsv; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1104 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1105 | return ret; |
| 1106 | } |
| 1107 | |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 1108 | int btrfs_run_delayed_items(struct btrfs_trans_handle *trans) |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 1109 | { |
Nikolay Borisov | b84acab | 2018-02-07 17:55:42 +0200 | [diff] [blame] | 1110 | return __btrfs_run_delayed_items(trans, -1); |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 1111 | } |
| 1112 | |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 1113 | int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr) |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 1114 | { |
Nikolay Borisov | b84acab | 2018-02-07 17:55:42 +0200 | [diff] [blame] | 1115 | return __btrfs_run_delayed_items(trans, nr); |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 1116 | } |
| 1117 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1118 | int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 5f4b32e | 2017-01-10 20:35:41 +0200 | [diff] [blame] | 1119 | struct btrfs_inode *inode) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1120 | { |
Nikolay Borisov | 5f4b32e | 2017-01-10 20:35:41 +0200 | [diff] [blame] | 1121 | struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode); |
Miao Xie | 4ea41ce | 2012-12-19 06:59:03 +0000 | [diff] [blame] | 1122 | struct btrfs_path *path; |
| 1123 | struct btrfs_block_rsv *block_rsv; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1124 | int ret; |
| 1125 | |
| 1126 | if (!delayed_node) |
| 1127 | return 0; |
| 1128 | |
| 1129 | mutex_lock(&delayed_node->mutex); |
| 1130 | if (!delayed_node->count) { |
| 1131 | mutex_unlock(&delayed_node->mutex); |
| 1132 | btrfs_release_delayed_node(delayed_node); |
| 1133 | return 0; |
| 1134 | } |
| 1135 | mutex_unlock(&delayed_node->mutex); |
| 1136 | |
Miao Xie | 4ea41ce | 2012-12-19 06:59:03 +0000 | [diff] [blame] | 1137 | path = btrfs_alloc_path(); |
Filipe David Borba Manana | 3c77bd9 | 2013-10-12 20:32:59 +0100 | [diff] [blame] | 1138 | if (!path) { |
| 1139 | btrfs_release_delayed_node(delayed_node); |
Miao Xie | 4ea41ce | 2012-12-19 06:59:03 +0000 | [diff] [blame] | 1140 | return -ENOMEM; |
Filipe David Borba Manana | 3c77bd9 | 2013-10-12 20:32:59 +0100 | [diff] [blame] | 1141 | } |
Miao Xie | 4ea41ce | 2012-12-19 06:59:03 +0000 | [diff] [blame] | 1142 | |
| 1143 | block_rsv = trans->block_rsv; |
| 1144 | trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv; |
| 1145 | |
| 1146 | ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node); |
| 1147 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1148 | btrfs_release_delayed_node(delayed_node); |
Miao Xie | 4ea41ce | 2012-12-19 06:59:03 +0000 | [diff] [blame] | 1149 | btrfs_free_path(path); |
| 1150 | trans->block_rsv = block_rsv; |
| 1151 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1152 | return ret; |
| 1153 | } |
| 1154 | |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 1155 | int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode) |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1156 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 1157 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1158 | struct btrfs_trans_handle *trans; |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 1159 | struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode); |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1160 | struct btrfs_path *path; |
| 1161 | struct btrfs_block_rsv *block_rsv; |
| 1162 | int ret; |
| 1163 | |
| 1164 | if (!delayed_node) |
| 1165 | return 0; |
| 1166 | |
| 1167 | mutex_lock(&delayed_node->mutex); |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 1168 | if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1169 | mutex_unlock(&delayed_node->mutex); |
| 1170 | btrfs_release_delayed_node(delayed_node); |
| 1171 | return 0; |
| 1172 | } |
| 1173 | mutex_unlock(&delayed_node->mutex); |
| 1174 | |
| 1175 | trans = btrfs_join_transaction(delayed_node->root); |
| 1176 | if (IS_ERR(trans)) { |
| 1177 | ret = PTR_ERR(trans); |
| 1178 | goto out; |
| 1179 | } |
| 1180 | |
| 1181 | path = btrfs_alloc_path(); |
| 1182 | if (!path) { |
| 1183 | ret = -ENOMEM; |
| 1184 | goto trans_out; |
| 1185 | } |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1186 | |
| 1187 | block_rsv = trans->block_rsv; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1188 | trans->block_rsv = &fs_info->delayed_block_rsv; |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1189 | |
| 1190 | mutex_lock(&delayed_node->mutex); |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 1191 | if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1192 | ret = __btrfs_update_delayed_inode(trans, delayed_node->root, |
| 1193 | path, delayed_node); |
| 1194 | else |
| 1195 | ret = 0; |
| 1196 | mutex_unlock(&delayed_node->mutex); |
| 1197 | |
| 1198 | btrfs_free_path(path); |
| 1199 | trans->block_rsv = block_rsv; |
| 1200 | trans_out: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 1201 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1202 | btrfs_btree_balance_dirty(fs_info); |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 1203 | out: |
| 1204 | btrfs_release_delayed_node(delayed_node); |
| 1205 | |
| 1206 | return ret; |
| 1207 | } |
| 1208 | |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 1209 | void btrfs_remove_delayed_node(struct btrfs_inode *inode) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1210 | { |
| 1211 | struct btrfs_delayed_node *delayed_node; |
| 1212 | |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 1213 | delayed_node = READ_ONCE(inode->delayed_node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1214 | if (!delayed_node) |
| 1215 | return; |
| 1216 | |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 1217 | inode->delayed_node = NULL; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1218 | btrfs_release_delayed_node(delayed_node); |
| 1219 | } |
| 1220 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1221 | struct btrfs_async_delayed_work { |
| 1222 | struct btrfs_delayed_root *delayed_root; |
| 1223 | int nr; |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 1224 | struct btrfs_work work; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1225 | }; |
| 1226 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 1227 | static void btrfs_async_run_delayed_root(struct btrfs_work *work) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1228 | { |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1229 | struct btrfs_async_delayed_work *async_work; |
| 1230 | struct btrfs_delayed_root *delayed_root; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1231 | struct btrfs_trans_handle *trans; |
| 1232 | struct btrfs_path *path; |
| 1233 | struct btrfs_delayed_node *delayed_node = NULL; |
| 1234 | struct btrfs_root *root; |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1235 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1236 | int total_done = 0; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1237 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1238 | async_work = container_of(work, struct btrfs_async_delayed_work, work); |
| 1239 | delayed_root = async_work->delayed_root; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1240 | |
| 1241 | path = btrfs_alloc_path(); |
| 1242 | if (!path) |
| 1243 | goto out; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1244 | |
Nikolay Borisov | 617c54a | 2017-10-23 13:51:48 +0300 | [diff] [blame] | 1245 | do { |
| 1246 | if (atomic_read(&delayed_root->items) < |
| 1247 | BTRFS_DELAYED_BACKGROUND / 2) |
| 1248 | break; |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1249 | |
Nikolay Borisov | 617c54a | 2017-10-23 13:51:48 +0300 | [diff] [blame] | 1250 | delayed_node = btrfs_first_prepared_delayed_node(delayed_root); |
| 1251 | if (!delayed_node) |
| 1252 | break; |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1253 | |
Nikolay Borisov | 617c54a | 2017-10-23 13:51:48 +0300 | [diff] [blame] | 1254 | root = delayed_node->root; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1255 | |
Nikolay Borisov | 617c54a | 2017-10-23 13:51:48 +0300 | [diff] [blame] | 1256 | trans = btrfs_join_transaction(root); |
| 1257 | if (IS_ERR(trans)) { |
| 1258 | btrfs_release_path(path); |
| 1259 | btrfs_release_prepared_delayed_node(delayed_node); |
| 1260 | total_done++; |
| 1261 | continue; |
| 1262 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1263 | |
Nikolay Borisov | 617c54a | 2017-10-23 13:51:48 +0300 | [diff] [blame] | 1264 | block_rsv = trans->block_rsv; |
| 1265 | trans->block_rsv = &root->fs_info->delayed_block_rsv; |
Miao Xie | 19fd294 | 2011-06-15 10:47:30 +0000 | [diff] [blame] | 1266 | |
Nikolay Borisov | 617c54a | 2017-10-23 13:51:48 +0300 | [diff] [blame] | 1267 | __btrfs_commit_inode_delayed_items(trans, path, delayed_node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1268 | |
Nikolay Borisov | 617c54a | 2017-10-23 13:51:48 +0300 | [diff] [blame] | 1269 | trans->block_rsv = block_rsv; |
| 1270 | btrfs_end_transaction(trans); |
| 1271 | btrfs_btree_balance_dirty_nodelay(root->fs_info); |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1272 | |
Nikolay Borisov | 617c54a | 2017-10-23 13:51:48 +0300 | [diff] [blame] | 1273 | btrfs_release_path(path); |
| 1274 | btrfs_release_prepared_delayed_node(delayed_node); |
| 1275 | total_done++; |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1276 | |
Nikolay Borisov | 617c54a | 2017-10-23 13:51:48 +0300 | [diff] [blame] | 1277 | } while ((async_work->nr == 0 && total_done < BTRFS_DELAYED_WRITEBACK) |
| 1278 | || total_done < async_work->nr); |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1279 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1280 | btrfs_free_path(path); |
| 1281 | out: |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1282 | wake_up(&delayed_root->wait); |
| 1283 | kfree(async_work); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1284 | } |
| 1285 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1286 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1287 | static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root, |
Daniel Dressler | a585e94 | 2014-11-17 22:05:02 +0900 | [diff] [blame] | 1288 | struct btrfs_fs_info *fs_info, int nr) |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1289 | { |
| 1290 | struct btrfs_async_delayed_work *async_work; |
| 1291 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1292 | async_work = kmalloc(sizeof(*async_work), GFP_NOFS); |
| 1293 | if (!async_work) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1294 | return -ENOMEM; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1295 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1296 | async_work->delayed_root = delayed_root; |
Omar Sandoval | a0cac0e | 2019-09-16 11:30:57 -0700 | [diff] [blame] | 1297 | btrfs_init_work(&async_work->work, btrfs_async_run_delayed_root, NULL, |
| 1298 | NULL); |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1299 | async_work->nr = nr; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1300 | |
Daniel Dressler | a585e94 | 2014-11-17 22:05:02 +0900 | [diff] [blame] | 1301 | btrfs_queue_work(fs_info->delayed_workers, &async_work->work); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1302 | return 0; |
| 1303 | } |
| 1304 | |
Jeff Mahoney | ccdf9b3 | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1305 | void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info) |
Chris Mason | e999376 | 2011-06-17 16:14:09 -0400 | [diff] [blame] | 1306 | { |
Jeff Mahoney | ccdf9b3 | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1307 | WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root)); |
Chris Mason | e999376 | 2011-06-17 16:14:09 -0400 | [diff] [blame] | 1308 | } |
| 1309 | |
Miao Xie | 0353808 | 2013-12-26 13:07:03 +0800 | [diff] [blame] | 1310 | static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq) |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1311 | { |
| 1312 | int val = atomic_read(&delayed_root->items_seq); |
| 1313 | |
Miao Xie | 0353808 | 2013-12-26 13:07:03 +0800 | [diff] [blame] | 1314 | if (val < seq || val >= seq + BTRFS_DELAYED_BATCH) |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1315 | return 1; |
Miao Xie | 0353808 | 2013-12-26 13:07:03 +0800 | [diff] [blame] | 1316 | |
| 1317 | if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) |
| 1318 | return 1; |
| 1319 | |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1320 | return 0; |
| 1321 | } |
| 1322 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1323 | void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1324 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1325 | struct btrfs_delayed_root *delayed_root = fs_info->delayed_root; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1326 | |
Nikolay Borisov | 8577787 | 2017-10-23 13:51:49 +0300 | [diff] [blame] | 1327 | if ((atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) || |
| 1328 | btrfs_workqueue_normal_congested(fs_info->delayed_workers)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1329 | return; |
| 1330 | |
| 1331 | if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) { |
Miao Xie | 0353808 | 2013-12-26 13:07:03 +0800 | [diff] [blame] | 1332 | int seq; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1333 | int ret; |
Miao Xie | 0353808 | 2013-12-26 13:07:03 +0800 | [diff] [blame] | 1334 | |
| 1335 | seq = atomic_read(&delayed_root->items_seq); |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 1336 | |
Daniel Dressler | a585e94 | 2014-11-17 22:05:02 +0900 | [diff] [blame] | 1337 | ret = btrfs_wq_run_delayed_node(delayed_root, fs_info, 0); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1338 | if (ret) |
| 1339 | return; |
| 1340 | |
Miao Xie | 0353808 | 2013-12-26 13:07:03 +0800 | [diff] [blame] | 1341 | wait_event_interruptible(delayed_root->wait, |
| 1342 | could_end_wait(delayed_root, seq)); |
Miao Xie | 4dd466d | 2013-12-26 13:07:02 +0800 | [diff] [blame] | 1343 | return; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1344 | } |
| 1345 | |
Daniel Dressler | a585e94 | 2014-11-17 22:05:02 +0900 | [diff] [blame] | 1346 | btrfs_wq_run_delayed_node(delayed_root, fs_info, BTRFS_DELAYED_BATCH); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1347 | } |
| 1348 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1349 | /* Will return 0 or -ENOMEM */ |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1350 | int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1351 | const char *name, int name_len, |
Nikolay Borisov | 6f45d18 | 2017-01-10 20:35:35 +0200 | [diff] [blame] | 1352 | struct btrfs_inode *dir, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1353 | struct btrfs_disk_key *disk_key, u8 type, |
| 1354 | u64 index) |
| 1355 | { |
| 1356 | struct btrfs_delayed_node *delayed_node; |
| 1357 | struct btrfs_delayed_item *delayed_item; |
| 1358 | struct btrfs_dir_item *dir_item; |
| 1359 | int ret; |
| 1360 | |
Nikolay Borisov | 6f45d18 | 2017-01-10 20:35:35 +0200 | [diff] [blame] | 1361 | delayed_node = btrfs_get_or_create_delayed_node(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1362 | if (IS_ERR(delayed_node)) |
| 1363 | return PTR_ERR(delayed_node); |
| 1364 | |
| 1365 | delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len); |
| 1366 | if (!delayed_item) { |
| 1367 | ret = -ENOMEM; |
| 1368 | goto release_node; |
| 1369 | } |
| 1370 | |
Nikolay Borisov | 6f45d18 | 2017-01-10 20:35:35 +0200 | [diff] [blame] | 1371 | delayed_item->key.objectid = btrfs_ino(dir); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1372 | delayed_item->key.type = BTRFS_DIR_INDEX_KEY; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1373 | delayed_item->key.offset = index; |
| 1374 | |
| 1375 | dir_item = (struct btrfs_dir_item *)delayed_item->data; |
| 1376 | dir_item->location = *disk_key; |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1377 | btrfs_set_stack_dir_transid(dir_item, trans->transid); |
| 1378 | btrfs_set_stack_dir_data_len(dir_item, 0); |
| 1379 | btrfs_set_stack_dir_name_len(dir_item, name_len); |
| 1380 | btrfs_set_stack_dir_type(dir_item, type); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1381 | memcpy((char *)(dir_item + 1), name, name_len); |
| 1382 | |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 1383 | ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, delayed_item); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 1384 | /* |
| 1385 | * we have reserved enough space when we start a new transaction, |
| 1386 | * so reserving metadata failure is impossible |
| 1387 | */ |
| 1388 | BUG_ON(ret); |
| 1389 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1390 | mutex_lock(&delayed_node->mutex); |
| 1391 | ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item); |
| 1392 | if (unlikely(ret)) { |
Lu Fengqi | 4465c8b | 2018-08-01 11:32:25 +0800 | [diff] [blame] | 1393 | btrfs_err(trans->fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 1394 | "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)", |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 1395 | name_len, name, delayed_node->root->root_key.objectid, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 1396 | delayed_node->inode_id, ret); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1397 | BUG(); |
| 1398 | } |
| 1399 | mutex_unlock(&delayed_node->mutex); |
| 1400 | |
| 1401 | release_node: |
| 1402 | btrfs_release_delayed_node(delayed_node); |
| 1403 | return ret; |
| 1404 | } |
| 1405 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1406 | static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info *fs_info, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1407 | struct btrfs_delayed_node *node, |
| 1408 | struct btrfs_key *key) |
| 1409 | { |
| 1410 | struct btrfs_delayed_item *item; |
| 1411 | |
| 1412 | mutex_lock(&node->mutex); |
| 1413 | item = __btrfs_lookup_delayed_insertion_item(node, key); |
| 1414 | if (!item) { |
| 1415 | mutex_unlock(&node->mutex); |
| 1416 | return 1; |
| 1417 | } |
| 1418 | |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 1419 | btrfs_delayed_item_release_metadata(node->root, item); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1420 | btrfs_release_delayed_item(item); |
| 1421 | mutex_unlock(&node->mutex); |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
| 1425 | int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e67bbbb | 2017-01-10 20:35:36 +0200 | [diff] [blame] | 1426 | struct btrfs_inode *dir, u64 index) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1427 | { |
| 1428 | struct btrfs_delayed_node *node; |
| 1429 | struct btrfs_delayed_item *item; |
| 1430 | struct btrfs_key item_key; |
| 1431 | int ret; |
| 1432 | |
Nikolay Borisov | e67bbbb | 2017-01-10 20:35:36 +0200 | [diff] [blame] | 1433 | node = btrfs_get_or_create_delayed_node(dir); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1434 | if (IS_ERR(node)) |
| 1435 | return PTR_ERR(node); |
| 1436 | |
Nikolay Borisov | e67bbbb | 2017-01-10 20:35:36 +0200 | [diff] [blame] | 1437 | item_key.objectid = btrfs_ino(dir); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1438 | item_key.type = BTRFS_DIR_INDEX_KEY; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1439 | item_key.offset = index; |
| 1440 | |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 1441 | ret = btrfs_delete_delayed_insertion_item(trans->fs_info, node, |
| 1442 | &item_key); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1443 | if (!ret) |
| 1444 | goto end; |
| 1445 | |
| 1446 | item = btrfs_alloc_delayed_item(0); |
| 1447 | if (!item) { |
| 1448 | ret = -ENOMEM; |
| 1449 | goto end; |
| 1450 | } |
| 1451 | |
| 1452 | item->key = item_key; |
| 1453 | |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 1454 | ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, item); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1455 | /* |
| 1456 | * we have reserved enough space when we start a new transaction, |
| 1457 | * so reserving metadata failure is impossible. |
| 1458 | */ |
Qu Wenruo | 933c22a | 2019-07-16 17:00:32 +0800 | [diff] [blame] | 1459 | if (ret < 0) { |
| 1460 | btrfs_err(trans->fs_info, |
| 1461 | "metadata reservation failed for delayed dir item deltiona, should have been reserved"); |
| 1462 | btrfs_release_delayed_item(item); |
| 1463 | goto end; |
| 1464 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1465 | |
| 1466 | mutex_lock(&node->mutex); |
| 1467 | ret = __btrfs_add_delayed_deletion_item(node, item); |
| 1468 | if (unlikely(ret)) { |
Lu Fengqi | 9add294 | 2018-08-01 11:32:26 +0800 | [diff] [blame] | 1469 | btrfs_err(trans->fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 1470 | "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)", |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 1471 | index, node->root->root_key.objectid, |
| 1472 | node->inode_id, ret); |
Qu Wenruo | 933c22a | 2019-07-16 17:00:32 +0800 | [diff] [blame] | 1473 | btrfs_delayed_item_release_metadata(dir->root, item); |
| 1474 | btrfs_release_delayed_item(item); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1475 | } |
| 1476 | mutex_unlock(&node->mutex); |
| 1477 | end: |
| 1478 | btrfs_release_delayed_node(node); |
| 1479 | return ret; |
| 1480 | } |
| 1481 | |
Nikolay Borisov | f5cc7b8 | 2017-01-10 20:35:42 +0200 | [diff] [blame] | 1482 | int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1483 | { |
Nikolay Borisov | f5cc7b8 | 2017-01-10 20:35:42 +0200 | [diff] [blame] | 1484 | struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1485 | |
| 1486 | if (!delayed_node) |
| 1487 | return -ENOENT; |
| 1488 | |
| 1489 | /* |
| 1490 | * Since we have held i_mutex of this directory, it is impossible that |
| 1491 | * a new directory index is added into the delayed node and index_cnt |
| 1492 | * is updated now. So we needn't lock the delayed node. |
| 1493 | */ |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1494 | if (!delayed_node->index_cnt) { |
| 1495 | btrfs_release_delayed_node(delayed_node); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1496 | return -EINVAL; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1497 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1498 | |
Nikolay Borisov | f5cc7b8 | 2017-01-10 20:35:42 +0200 | [diff] [blame] | 1499 | inode->index_cnt = delayed_node->index_cnt; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1500 | btrfs_release_delayed_node(delayed_node); |
| 1501 | return 0; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1502 | } |
| 1503 | |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 1504 | bool btrfs_readdir_get_delayed_items(struct inode *inode, |
| 1505 | struct list_head *ins_list, |
| 1506 | struct list_head *del_list) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1507 | { |
| 1508 | struct btrfs_delayed_node *delayed_node; |
| 1509 | struct btrfs_delayed_item *item; |
| 1510 | |
Nikolay Borisov | 340c6ca | 2017-01-10 20:35:32 +0200 | [diff] [blame] | 1511 | delayed_node = btrfs_get_delayed_node(BTRFS_I(inode)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1512 | if (!delayed_node) |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 1513 | return false; |
| 1514 | |
| 1515 | /* |
| 1516 | * We can only do one readdir with delayed items at a time because of |
| 1517 | * item->readdir_list. |
| 1518 | */ |
Josef Bacik | 6470853 | 2021-02-10 17:14:34 -0500 | [diff] [blame] | 1519 | btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); |
| 1520 | btrfs_inode_lock(inode, 0); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1521 | |
| 1522 | mutex_lock(&delayed_node->mutex); |
| 1523 | item = __btrfs_first_delayed_insertion_item(delayed_node); |
| 1524 | while (item) { |
Elena Reshetova | 089e77e | 2017-03-03 10:55:17 +0200 | [diff] [blame] | 1525 | refcount_inc(&item->refs); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1526 | list_add_tail(&item->readdir_list, ins_list); |
| 1527 | item = __btrfs_next_delayed_item(item); |
| 1528 | } |
| 1529 | |
| 1530 | item = __btrfs_first_delayed_deletion_item(delayed_node); |
| 1531 | while (item) { |
Elena Reshetova | 089e77e | 2017-03-03 10:55:17 +0200 | [diff] [blame] | 1532 | refcount_inc(&item->refs); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1533 | list_add_tail(&item->readdir_list, del_list); |
| 1534 | item = __btrfs_next_delayed_item(item); |
| 1535 | } |
| 1536 | mutex_unlock(&delayed_node->mutex); |
| 1537 | /* |
| 1538 | * This delayed node is still cached in the btrfs inode, so refs |
| 1539 | * must be > 1 now, and we needn't check it is going to be freed |
| 1540 | * or not. |
| 1541 | * |
| 1542 | * Besides that, this function is used to read dir, we do not |
| 1543 | * insert/delete delayed items in this period. So we also needn't |
| 1544 | * requeue or dequeue this delayed node. |
| 1545 | */ |
Elena Reshetova | 6de5f18 | 2017-03-03 10:55:16 +0200 | [diff] [blame] | 1546 | refcount_dec(&delayed_node->refs); |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 1547 | |
| 1548 | return true; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1549 | } |
| 1550 | |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 1551 | void btrfs_readdir_put_delayed_items(struct inode *inode, |
| 1552 | struct list_head *ins_list, |
| 1553 | struct list_head *del_list) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1554 | { |
| 1555 | struct btrfs_delayed_item *curr, *next; |
| 1556 | |
| 1557 | list_for_each_entry_safe(curr, next, ins_list, readdir_list) { |
| 1558 | list_del(&curr->readdir_list); |
Elena Reshetova | 089e77e | 2017-03-03 10:55:17 +0200 | [diff] [blame] | 1559 | if (refcount_dec_and_test(&curr->refs)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1560 | kfree(curr); |
| 1561 | } |
| 1562 | |
| 1563 | list_for_each_entry_safe(curr, next, del_list, readdir_list) { |
| 1564 | list_del(&curr->readdir_list); |
Elena Reshetova | 089e77e | 2017-03-03 10:55:17 +0200 | [diff] [blame] | 1565 | if (refcount_dec_and_test(&curr->refs)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1566 | kfree(curr); |
| 1567 | } |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 1568 | |
| 1569 | /* |
| 1570 | * The VFS is going to do up_read(), so we need to downgrade back to a |
| 1571 | * read lock. |
| 1572 | */ |
| 1573 | downgrade_write(&inode->i_rwsem); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | int btrfs_should_delete_dir_index(struct list_head *del_list, |
| 1577 | u64 index) |
| 1578 | { |
Josef Bacik | e4fd493 | 2018-01-23 15:17:05 -0500 | [diff] [blame] | 1579 | struct btrfs_delayed_item *curr; |
| 1580 | int ret = 0; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1581 | |
Josef Bacik | e4fd493 | 2018-01-23 15:17:05 -0500 | [diff] [blame] | 1582 | list_for_each_entry(curr, del_list, readdir_list) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1583 | if (curr->key.offset > index) |
| 1584 | break; |
Josef Bacik | e4fd493 | 2018-01-23 15:17:05 -0500 | [diff] [blame] | 1585 | if (curr->key.offset == index) { |
| 1586 | ret = 1; |
| 1587 | break; |
| 1588 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1589 | } |
Josef Bacik | e4fd493 | 2018-01-23 15:17:05 -0500 | [diff] [blame] | 1590 | return ret; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | /* |
| 1594 | * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree |
| 1595 | * |
| 1596 | */ |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 1597 | int btrfs_readdir_delayed_dir_index(struct dir_context *ctx, |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 1598 | struct list_head *ins_list) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1599 | { |
| 1600 | struct btrfs_dir_item *di; |
| 1601 | struct btrfs_delayed_item *curr, *next; |
| 1602 | struct btrfs_key location; |
| 1603 | char *name; |
| 1604 | int name_len; |
| 1605 | int over = 0; |
| 1606 | unsigned char d_type; |
| 1607 | |
| 1608 | if (list_empty(ins_list)) |
| 1609 | return 0; |
| 1610 | |
| 1611 | /* |
| 1612 | * Changing the data of the delayed item is impossible. So |
| 1613 | * we needn't lock them. And we have held i_mutex of the |
| 1614 | * directory, nobody can delete any directory indexes now. |
| 1615 | */ |
| 1616 | list_for_each_entry_safe(curr, next, ins_list, readdir_list) { |
| 1617 | list_del(&curr->readdir_list); |
| 1618 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 1619 | if (curr->key.offset < ctx->pos) { |
Elena Reshetova | 089e77e | 2017-03-03 10:55:17 +0200 | [diff] [blame] | 1620 | if (refcount_dec_and_test(&curr->refs)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1621 | kfree(curr); |
| 1622 | continue; |
| 1623 | } |
| 1624 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 1625 | ctx->pos = curr->key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1626 | |
| 1627 | di = (struct btrfs_dir_item *)curr->data; |
| 1628 | name = (char *)(di + 1); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1629 | name_len = btrfs_stack_dir_name_len(di); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1630 | |
Phillip Potter | 7d157c3 | 2019-03-26 21:39:34 +0000 | [diff] [blame] | 1631 | d_type = fs_ftype_to_dtype(di->type); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1632 | btrfs_disk_key_to_cpu(&location, &di->location); |
| 1633 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 1634 | over = !dir_emit(ctx, name, name_len, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1635 | location.objectid, d_type); |
| 1636 | |
Elena Reshetova | 089e77e | 2017-03-03 10:55:17 +0200 | [diff] [blame] | 1637 | if (refcount_dec_and_test(&curr->refs)) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1638 | kfree(curr); |
| 1639 | |
| 1640 | if (over) |
| 1641 | return 1; |
Josef Bacik | 42e9cc4 | 2017-07-24 15:14:26 -0400 | [diff] [blame] | 1642 | ctx->pos++; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1643 | } |
| 1644 | return 0; |
| 1645 | } |
| 1646 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1647 | static void fill_stack_inode_item(struct btrfs_trans_handle *trans, |
| 1648 | struct btrfs_inode_item *inode_item, |
| 1649 | struct inode *inode) |
| 1650 | { |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 1651 | u64 flags; |
| 1652 | |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 1653 | btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode)); |
| 1654 | btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1655 | btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size); |
| 1656 | btrfs_set_stack_inode_mode(inode_item, inode->i_mode); |
| 1657 | btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink); |
| 1658 | btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode)); |
| 1659 | btrfs_set_stack_inode_generation(inode_item, |
| 1660 | BTRFS_I(inode)->generation); |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 1661 | btrfs_set_stack_inode_sequence(inode_item, |
| 1662 | inode_peek_iversion(inode)); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1663 | btrfs_set_stack_inode_transid(inode_item, trans->transid); |
| 1664 | btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev); |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 1665 | flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags, |
| 1666 | BTRFS_I(inode)->ro_flags); |
| 1667 | btrfs_set_stack_inode_flags(inode_item, flags); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1668 | btrfs_set_stack_inode_block_group(inode_item, 0); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1669 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 1670 | btrfs_set_stack_timespec_sec(&inode_item->atime, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1671 | inode->i_atime.tv_sec); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 1672 | btrfs_set_stack_timespec_nsec(&inode_item->atime, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1673 | inode->i_atime.tv_nsec); |
| 1674 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 1675 | btrfs_set_stack_timespec_sec(&inode_item->mtime, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1676 | inode->i_mtime.tv_sec); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 1677 | btrfs_set_stack_timespec_nsec(&inode_item->mtime, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1678 | inode->i_mtime.tv_nsec); |
| 1679 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 1680 | btrfs_set_stack_timespec_sec(&inode_item->ctime, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1681 | inode->i_ctime.tv_sec); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 1682 | btrfs_set_stack_timespec_nsec(&inode_item->ctime, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1683 | inode->i_ctime.tv_nsec); |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 1684 | |
| 1685 | btrfs_set_stack_timespec_sec(&inode_item->otime, |
| 1686 | BTRFS_I(inode)->i_otime.tv_sec); |
| 1687 | btrfs_set_stack_timespec_nsec(&inode_item->otime, |
| 1688 | BTRFS_I(inode)->i_otime.tv_nsec); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1689 | } |
| 1690 | |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1691 | int btrfs_fill_inode(struct inode *inode, u32 *rdev) |
| 1692 | { |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 1693 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1694 | struct btrfs_delayed_node *delayed_node; |
| 1695 | struct btrfs_inode_item *inode_item; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1696 | |
Nikolay Borisov | 340c6ca | 2017-01-10 20:35:32 +0200 | [diff] [blame] | 1697 | delayed_node = btrfs_get_delayed_node(BTRFS_I(inode)); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1698 | if (!delayed_node) |
| 1699 | return -ENOENT; |
| 1700 | |
| 1701 | mutex_lock(&delayed_node->mutex); |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 1702 | if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1703 | mutex_unlock(&delayed_node->mutex); |
| 1704 | btrfs_release_delayed_node(delayed_node); |
| 1705 | return -ENOENT; |
| 1706 | } |
| 1707 | |
| 1708 | inode_item = &delayed_node->inode_item; |
| 1709 | |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 1710 | i_uid_write(inode, btrfs_stack_inode_uid(inode_item)); |
| 1711 | i_gid_write(inode, btrfs_stack_inode_gid(inode_item)); |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 1712 | btrfs_i_size_write(BTRFS_I(inode), btrfs_stack_inode_size(inode_item)); |
Josef Bacik | 9ddc959 | 2020-01-17 09:02:22 -0500 | [diff] [blame] | 1713 | btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0, |
| 1714 | round_up(i_size_read(inode), fs_info->sectorsize)); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1715 | inode->i_mode = btrfs_stack_inode_mode(inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 1716 | set_nlink(inode, btrfs_stack_inode_nlink(inode_item)); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1717 | inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item)); |
| 1718 | BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item); |
Yang Dongsheng | 6e17d30 | 2015-04-09 12:08:43 +0800 | [diff] [blame] | 1719 | BTRFS_I(inode)->last_trans = btrfs_stack_inode_transid(inode_item); |
| 1720 | |
Jeff Layton | c7f88c4 | 2017-12-11 06:35:12 -0500 | [diff] [blame] | 1721 | inode_set_iversion_queried(inode, |
| 1722 | btrfs_stack_inode_sequence(inode_item)); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1723 | inode->i_rdev = 0; |
| 1724 | *rdev = btrfs_stack_inode_rdev(inode_item); |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 1725 | btrfs_inode_split_flags(btrfs_stack_inode_flags(inode_item), |
| 1726 | &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1727 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 1728 | inode->i_atime.tv_sec = btrfs_stack_timespec_sec(&inode_item->atime); |
| 1729 | inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->atime); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1730 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 1731 | inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(&inode_item->mtime); |
| 1732 | inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->mtime); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1733 | |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 1734 | inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(&inode_item->ctime); |
| 1735 | inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->ctime); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1736 | |
chandan r | 9cc97d6 | 2012-07-04 12:48:07 +0530 | [diff] [blame] | 1737 | BTRFS_I(inode)->i_otime.tv_sec = |
| 1738 | btrfs_stack_timespec_sec(&inode_item->otime); |
| 1739 | BTRFS_I(inode)->i_otime.tv_nsec = |
| 1740 | btrfs_stack_timespec_nsec(&inode_item->otime); |
| 1741 | |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 1742 | inode->i_generation = BTRFS_I(inode)->generation; |
| 1743 | BTRFS_I(inode)->index_cnt = (u64)-1; |
| 1744 | |
| 1745 | mutex_unlock(&delayed_node->mutex); |
| 1746 | btrfs_release_delayed_node(delayed_node); |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1750 | int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans, |
Nikolay Borisov | f3fbcae | 2020-11-02 16:48:57 +0200 | [diff] [blame] | 1751 | struct btrfs_root *root, |
| 1752 | struct btrfs_inode *inode) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1753 | { |
| 1754 | struct btrfs_delayed_node *delayed_node; |
David Sterba | aa0467d | 2011-06-03 16:29:08 +0200 | [diff] [blame] | 1755 | int ret = 0; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1756 | |
Nikolay Borisov | f3fbcae | 2020-11-02 16:48:57 +0200 | [diff] [blame] | 1757 | delayed_node = btrfs_get_or_create_delayed_node(inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1758 | if (IS_ERR(delayed_node)) |
| 1759 | return PTR_ERR(delayed_node); |
| 1760 | |
| 1761 | mutex_lock(&delayed_node->mutex); |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 1762 | if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { |
Nikolay Borisov | f3fbcae | 2020-11-02 16:48:57 +0200 | [diff] [blame] | 1763 | fill_stack_inode_item(trans, &delayed_node->inode_item, |
| 1764 | &inode->vfs_inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1765 | goto release_node; |
| 1766 | } |
| 1767 | |
Nikolay Borisov | 8e3c9d3 | 2021-02-22 18:40:46 +0200 | [diff] [blame] | 1768 | ret = btrfs_delayed_inode_reserve_metadata(trans, root, delayed_node); |
Josef Bacik | c06a0e1 | 2011-11-04 19:56:02 -0400 | [diff] [blame] | 1769 | if (ret) |
| 1770 | goto release_node; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1771 | |
Nikolay Borisov | f3fbcae | 2020-11-02 16:48:57 +0200 | [diff] [blame] | 1772 | fill_stack_inode_item(trans, &delayed_node->inode_item, &inode->vfs_inode); |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 1773 | set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1774 | delayed_node->count++; |
| 1775 | atomic_inc(&root->fs_info->delayed_root->items); |
| 1776 | release_node: |
| 1777 | mutex_unlock(&delayed_node->mutex); |
| 1778 | btrfs_release_delayed_node(delayed_node); |
| 1779 | return ret; |
| 1780 | } |
| 1781 | |
Nikolay Borisov | e07222c | 2017-01-10 20:35:37 +0200 | [diff] [blame] | 1782 | int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 1783 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 1784 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 1785 | struct btrfs_delayed_node *delayed_node; |
| 1786 | |
Chris Mason | 6f89605 | 2014-12-31 12:18:29 -0500 | [diff] [blame] | 1787 | /* |
| 1788 | * we don't do delayed inode updates during log recovery because it |
| 1789 | * leads to enospc problems. This means we also can't do |
| 1790 | * delayed inode refs |
| 1791 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1792 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
Chris Mason | 6f89605 | 2014-12-31 12:18:29 -0500 | [diff] [blame] | 1793 | return -EAGAIN; |
| 1794 | |
Nikolay Borisov | e07222c | 2017-01-10 20:35:37 +0200 | [diff] [blame] | 1795 | delayed_node = btrfs_get_or_create_delayed_node(inode); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 1796 | if (IS_ERR(delayed_node)) |
| 1797 | return PTR_ERR(delayed_node); |
| 1798 | |
| 1799 | /* |
| 1800 | * We don't reserve space for inode ref deletion is because: |
| 1801 | * - We ONLY do async inode ref deletion for the inode who has only |
| 1802 | * one link(i_nlink == 1), it means there is only one inode ref. |
| 1803 | * And in most case, the inode ref and the inode item are in the |
| 1804 | * same leaf, and we will deal with them at the same time. |
| 1805 | * Since we are sure we will reserve the space for the inode item, |
| 1806 | * it is unnecessary to reserve space for inode ref deletion. |
| 1807 | * - If the inode ref and the inode item are not in the same leaf, |
| 1808 | * We also needn't worry about enospc problem, because we reserve |
| 1809 | * much more space for the inode update than it needs. |
| 1810 | * - At the worst, we can steal some space from the global reservation. |
| 1811 | * It is very rare. |
| 1812 | */ |
| 1813 | mutex_lock(&delayed_node->mutex); |
| 1814 | if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags)) |
| 1815 | goto release_node; |
| 1816 | |
| 1817 | set_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags); |
| 1818 | delayed_node->count++; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1819 | atomic_inc(&fs_info->delayed_root->items); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 1820 | release_node: |
| 1821 | mutex_unlock(&delayed_node->mutex); |
| 1822 | btrfs_release_delayed_node(delayed_node); |
| 1823 | return 0; |
| 1824 | } |
| 1825 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1826 | static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node) |
| 1827 | { |
| 1828 | struct btrfs_root *root = delayed_node->root; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1829 | struct btrfs_fs_info *fs_info = root->fs_info; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1830 | struct btrfs_delayed_item *curr_item, *prev_item; |
| 1831 | |
| 1832 | mutex_lock(&delayed_node->mutex); |
| 1833 | curr_item = __btrfs_first_delayed_insertion_item(delayed_node); |
| 1834 | while (curr_item) { |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 1835 | btrfs_delayed_item_release_metadata(root, curr_item); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1836 | prev_item = curr_item; |
| 1837 | curr_item = __btrfs_next_delayed_item(prev_item); |
| 1838 | btrfs_release_delayed_item(prev_item); |
| 1839 | } |
| 1840 | |
| 1841 | curr_item = __btrfs_first_delayed_deletion_item(delayed_node); |
| 1842 | while (curr_item) { |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 1843 | btrfs_delayed_item_release_metadata(root, curr_item); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1844 | prev_item = curr_item; |
| 1845 | curr_item = __btrfs_next_delayed_item(prev_item); |
| 1846 | btrfs_release_delayed_item(prev_item); |
| 1847 | } |
| 1848 | |
Josef Bacik | a4cb90d | 2021-05-21 16:44:07 -0400 | [diff] [blame] | 1849 | btrfs_release_delayed_iref(delayed_node); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 1850 | |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 1851 | if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { |
Qu Wenruo | 4f5427c | 2017-12-12 15:34:33 +0800 | [diff] [blame] | 1852 | btrfs_delayed_inode_release_metadata(fs_info, delayed_node, false); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1853 | btrfs_release_delayed_inode(delayed_node); |
| 1854 | } |
| 1855 | mutex_unlock(&delayed_node->mutex); |
| 1856 | } |
| 1857 | |
Nikolay Borisov | 4ccb5c7 | 2017-01-10 20:35:38 +0200 | [diff] [blame] | 1858 | void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1859 | { |
| 1860 | struct btrfs_delayed_node *delayed_node; |
| 1861 | |
Nikolay Borisov | 4ccb5c7 | 2017-01-10 20:35:38 +0200 | [diff] [blame] | 1862 | delayed_node = btrfs_get_delayed_node(inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1863 | if (!delayed_node) |
| 1864 | return; |
| 1865 | |
| 1866 | __btrfs_kill_delayed_node(delayed_node); |
| 1867 | btrfs_release_delayed_node(delayed_node); |
| 1868 | } |
| 1869 | |
| 1870 | void btrfs_kill_all_delayed_nodes(struct btrfs_root *root) |
| 1871 | { |
| 1872 | u64 inode_id = 0; |
| 1873 | struct btrfs_delayed_node *delayed_nodes[8]; |
| 1874 | int i, n; |
| 1875 | |
| 1876 | while (1) { |
| 1877 | spin_lock(&root->inode_lock); |
| 1878 | n = radix_tree_gang_lookup(&root->delayed_nodes_tree, |
| 1879 | (void **)delayed_nodes, inode_id, |
| 1880 | ARRAY_SIZE(delayed_nodes)); |
| 1881 | if (!n) { |
| 1882 | spin_unlock(&root->inode_lock); |
| 1883 | break; |
| 1884 | } |
| 1885 | |
| 1886 | inode_id = delayed_nodes[n - 1]->inode_id + 1; |
Josef Bacik | baf320b | 2019-09-26 08:29:32 -0400 | [diff] [blame] | 1887 | for (i = 0; i < n; i++) { |
| 1888 | /* |
| 1889 | * Don't increase refs in case the node is dead and |
| 1890 | * about to be removed from the tree in the loop below |
| 1891 | */ |
| 1892 | if (!refcount_inc_not_zero(&delayed_nodes[i]->refs)) |
| 1893 | delayed_nodes[i] = NULL; |
| 1894 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1895 | spin_unlock(&root->inode_lock); |
| 1896 | |
| 1897 | for (i = 0; i < n; i++) { |
Josef Bacik | baf320b | 2019-09-26 08:29:32 -0400 | [diff] [blame] | 1898 | if (!delayed_nodes[i]) |
| 1899 | continue; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 1900 | __btrfs_kill_delayed_node(delayed_nodes[i]); |
| 1901 | btrfs_release_delayed_node(delayed_nodes[i]); |
| 1902 | } |
| 1903 | } |
| 1904 | } |
Miao Xie | 67cde34 | 2012-06-14 02:23:22 -0600 | [diff] [blame] | 1905 | |
Jeff Mahoney | ccdf9b3 | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1906 | void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info) |
Miao Xie | 67cde34 | 2012-06-14 02:23:22 -0600 | [diff] [blame] | 1907 | { |
Miao Xie | 67cde34 | 2012-06-14 02:23:22 -0600 | [diff] [blame] | 1908 | struct btrfs_delayed_node *curr_node, *prev_node; |
| 1909 | |
Jeff Mahoney | ccdf9b3 | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1910 | curr_node = btrfs_first_delayed_node(fs_info->delayed_root); |
Miao Xie | 67cde34 | 2012-06-14 02:23:22 -0600 | [diff] [blame] | 1911 | while (curr_node) { |
| 1912 | __btrfs_kill_delayed_node(curr_node); |
| 1913 | |
| 1914 | prev_node = curr_node; |
| 1915 | curr_node = btrfs_next_delayed_node(curr_node); |
| 1916 | btrfs_release_delayed_node(prev_node); |
| 1917 | } |
| 1918 | } |
| 1919 | |