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