blob: 7374fb23381ca1096a1c29c48961a29a0057fe6b [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Miao Xie16cdcec2011-04-22 18:12:22 +08002/*
3 * Copyright (C) 2011 Fujitsu. All rights reserved.
4 * Written by Miao Xie <miaox@cn.fujitsu.com>
Miao Xie16cdcec2011-04-22 18:12:22 +08005 */
6
7#include <linux/slab.h>
Jeff Laytonc7f88c42017-12-11 06:35:12 -05008#include <linux/iversion.h>
Josef Bacik45b9d992020-03-19 10:11:32 -04009#include <linux/sched/mm.h>
Miao Xie16cdcec2011-04-22 18:12:22 +080010#include "delayed-inode.h"
11#include "disk-io.h"
12#include "transaction.h"
Qu Wenruo3cae2102013-07-16 11:19:18 +080013#include "ctree.h"
Qu Wenruo4f5427c2017-12-12 15:34:33 +080014#include "qgroup.h"
Miao Xie16cdcec2011-04-22 18:12:22 +080015
Chris Masonde3cb942013-03-04 17:13:31 -050016#define BTRFS_DELAYED_WRITEBACK 512
17#define BTRFS_DELAYED_BACKGROUND 128
18#define BTRFS_DELAYED_BATCH 16
Miao Xie16cdcec2011-04-22 18:12:22 +080019
20static struct kmem_cache *delayed_node_cache;
21
22int __init btrfs_delayed_inode_init(void)
23{
David Sterba837e1972012-09-07 03:00:48 -060024 delayed_node_cache = kmem_cache_create("btrfs_delayed_node",
Miao Xie16cdcec2011-04-22 18:12:22 +080025 sizeof(struct btrfs_delayed_node),
26 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +030027 SLAB_MEM_SPREAD,
Miao Xie16cdcec2011-04-22 18:12:22 +080028 NULL);
29 if (!delayed_node_cache)
30 return -ENOMEM;
31 return 0;
32}
33
David Sterbae67c7182018-02-19 17:24:18 +010034void __cold btrfs_delayed_inode_exit(void)
Miao Xie16cdcec2011-04-22 18:12:22 +080035{
Kinglong Mee5598e902016-01-29 21:36:35 +080036 kmem_cache_destroy(delayed_node_cache);
Miao Xie16cdcec2011-04-22 18:12:22 +080037}
38
39static 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 Reshetova6de5f182017-03-03 10:55:16 +020045 refcount_set(&delayed_node->refs, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +080046 delayed_node->ins_root = RB_ROOT;
47 delayed_node->del_root = RB_ROOT;
48 mutex_init(&delayed_node->mutex);
Miao Xie16cdcec2011-04-22 18:12:22 +080049 INIT_LIST_HEAD(&delayed_node->n_list);
50 INIT_LIST_HEAD(&delayed_node->p_list);
Miao Xie16cdcec2011-04-22 18:12:22 +080051}
52
53static 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 Sterbaf85b7372017-01-20 14:54:07 +010065static struct btrfs_delayed_node *btrfs_get_delayed_node(
66 struct btrfs_inode *btrfs_inode)
Miao Xie2f7e33d2011-06-23 07:27:13 +000067{
Miao Xie2f7e33d2011-06-23 07:27:13 +000068 struct btrfs_root *root = btrfs_inode->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020069 u64 ino = btrfs_ino(btrfs_inode);
Miao Xie2f7e33d2011-06-23 07:27:13 +000070 struct btrfs_delayed_node *node;
71
Seraphime Kirkovski20c7bce2016-12-15 14:38:16 +010072 node = READ_ONCE(btrfs_inode->delayed_node);
Miao Xie2f7e33d2011-06-23 07:27:13 +000073 if (node) {
Elena Reshetova6de5f182017-03-03 10:55:16 +020074 refcount_inc(&node->refs);
Miao Xie2f7e33d2011-06-23 07:27:13 +000075 return node;
76 }
77
78 spin_lock(&root->inode_lock);
79 node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
Chris Masonec35e482017-12-15 11:58:27 -080080
Miao Xie2f7e33d2011-06-23 07:27:13 +000081 if (node) {
82 if (btrfs_inode->delayed_node) {
Elena Reshetova6de5f182017-03-03 10:55:16 +020083 refcount_inc(&node->refs); /* can be accessed */
Miao Xie2f7e33d2011-06-23 07:27:13 +000084 BUG_ON(btrfs_inode->delayed_node != node);
85 spin_unlock(&root->inode_lock);
86 return node;
87 }
Chris Masonec35e482017-12-15 11:58:27 -080088
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 Xie2f7e33d2011-06-23 07:27:13 +0000112 spin_unlock(&root->inode_lock);
113 return node;
114 }
115 spin_unlock(&root->inode_lock);
116
117 return NULL;
118}
119
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100120/* Will return either the node or PTR_ERR(-ENOMEM) */
Miao Xie16cdcec2011-04-22 18:12:22 +0800121static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
David Sterbaf85b7372017-01-20 14:54:07 +0100122 struct btrfs_inode *btrfs_inode)
Miao Xie16cdcec2011-04-22 18:12:22 +0800123{
124 struct btrfs_delayed_node *node;
Miao Xie16cdcec2011-04-22 18:12:22 +0800125 struct btrfs_root *root = btrfs_inode->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200126 u64 ino = btrfs_ino(btrfs_inode);
Miao Xie16cdcec2011-04-22 18:12:22 +0800127 int ret;
128
129again:
Nikolay Borisov340c6ca2017-01-10 20:35:32 +0200130 node = btrfs_get_delayed_node(btrfs_inode);
Miao Xie2f7e33d2011-06-23 07:27:13 +0000131 if (node)
Miao Xie16cdcec2011-04-22 18:12:22 +0800132 return node;
Miao Xie16cdcec2011-04-22 18:12:22 +0800133
Alexandru Moise352dd9c2015-10-25 20:15:06 +0000134 node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS);
Miao Xie16cdcec2011-04-22 18:12:22 +0800135 if (!node)
136 return ERR_PTR(-ENOMEM);
Chris Mason0d0ca302011-05-22 07:11:22 -0400137 btrfs_init_delayed_node(node, root, ino);
Miao Xie16cdcec2011-04-22 18:12:22 +0800138
Rashika95e94d12013-10-31 03:12:42 +0530139 /* cached in the btrfs inode and can be accessed */
Elena Reshetova6de5f182017-03-03 10:55:16 +0200140 refcount_set(&node->refs, 2);
Miao Xie16cdcec2011-04-22 18:12:22 +0800141
David Sterbae1860a72016-05-09 14:11:38 +0200142 ret = radix_tree_preload(GFP_NOFS);
Miao Xie16cdcec2011-04-22 18:12:22 +0800143 if (ret) {
144 kmem_cache_free(delayed_node_cache, node);
145 return ERR_PTR(ret);
146 }
147
148 spin_lock(&root->inode_lock);
Chris Mason0d0ca302011-05-22 07:11:22 -0400149 ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
Miao Xie16cdcec2011-04-22 18:12:22 +0800150 if (ret == -EEXIST) {
Miao Xie16cdcec2011-04-22 18:12:22 +0800151 spin_unlock(&root->inode_lock);
Jeff Mahoney96493032014-05-27 13:53:20 -0400152 kmem_cache_free(delayed_node_cache, node);
Miao Xie16cdcec2011-04-22 18:12:22 +0800153 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 */
168static 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 Xie7cf35d92013-12-26 13:07:05 +0800173 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
Miao Xie16cdcec2011-04-22 18:12:22 +0800174 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 Reshetova6de5f182017-03-03 10:55:16 +0200181 refcount_inc(&node->refs); /* inserted into list */
Miao Xie16cdcec2011-04-22 18:12:22 +0800182 root->nodes++;
Miao Xie7cf35d92013-12-26 13:07:05 +0800183 set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
Miao Xie16cdcec2011-04-22 18:12:22 +0800184 }
185 spin_unlock(&root->lock);
186}
187
188/* Call it when holding delayed_node->mutex */
189static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
190 struct btrfs_delayed_node *node)
191{
192 spin_lock(&root->lock);
Miao Xie7cf35d92013-12-26 13:07:05 +0800193 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
Miao Xie16cdcec2011-04-22 18:12:22 +0800194 root->nodes--;
Elena Reshetova6de5f182017-03-03 10:55:16 +0200195 refcount_dec(&node->refs); /* not in the list */
Miao Xie16cdcec2011-04-22 18:12:22 +0800196 list_del_init(&node->n_list);
197 if (!list_empty(&node->p_list))
198 list_del_init(&node->p_list);
Miao Xie7cf35d92013-12-26 13:07:05 +0800199 clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
Miao Xie16cdcec2011-04-22 18:12:22 +0800200 }
201 spin_unlock(&root->lock);
202}
203
Eric Sandeen48a3b632013-04-25 20:41:01 +0000204static struct btrfs_delayed_node *btrfs_first_delayed_node(
Miao Xie16cdcec2011-04-22 18:12:22 +0800205 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 Reshetova6de5f182017-03-03 10:55:16 +0200216 refcount_inc(&node->refs);
Miao Xie16cdcec2011-04-22 18:12:22 +0800217out:
218 spin_unlock(&delayed_root->lock);
219
220 return node;
221}
222
Eric Sandeen48a3b632013-04-25 20:41:01 +0000223static struct btrfs_delayed_node *btrfs_next_delayed_node(
Miao Xie16cdcec2011-04-22 18:12:22 +0800224 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 Xie7cf35d92013-12-26 13:07:05 +0800232 if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
233 /* not in the list */
Miao Xie16cdcec2011-04-22 18:12:22 +0800234 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 Reshetova6de5f182017-03-03 10:55:16 +0200243 refcount_inc(&next->refs);
Miao Xie16cdcec2011-04-22 18:12:22 +0800244out:
245 spin_unlock(&delayed_root->lock);
246
247 return next;
248}
249
250static 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 Reshetova6de5f182017-03-03 10:55:16 +0200268 if (refcount_dec_and_test(&delayed_node->refs)) {
Miao Xie16cdcec2011-04-22 18:12:22 +0800269 struct btrfs_root *root = delayed_node->root;
Chris Masonec35e482017-12-15 11:58:27 -0800270
Miao Xie16cdcec2011-04-22 18:12:22 +0800271 spin_lock(&root->inode_lock);
Chris Masonec35e482017-12-15 11:58:27 -0800272 /*
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 Xie16cdcec2011-04-22 18:12:22 +0800279 spin_unlock(&root->inode_lock);
Chris Masonec35e482017-12-15 11:58:27 -0800280 kmem_cache_free(delayed_node_cache, delayed_node);
Miao Xie16cdcec2011-04-22 18:12:22 +0800281 }
282}
283
284static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
285{
286 __btrfs_release_delayed_node(node, 0);
287}
288
Eric Sandeen48a3b632013-04-25 20:41:01 +0000289static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
Miao Xie16cdcec2011-04-22 18:12:22 +0800290 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 Reshetova6de5f182017-03-03 10:55:16 +0200302 refcount_inc(&node->refs);
Miao Xie16cdcec2011-04-22 18:12:22 +0800303out:
304 spin_unlock(&delayed_root->lock);
305
306 return node;
307}
308
309static inline void btrfs_release_prepared_delayed_node(
310 struct btrfs_delayed_node *node)
311{
312 __btrfs_release_delayed_node(node, 1);
313}
314
Eric Sandeen48a3b632013-04-25 20:41:01 +0000315static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len)
Miao Xie16cdcec2011-04-22 18:12:22 +0800316{
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 Xie16cdcec2011-04-22 18:12:22 +0800323 item->delayed_node = NULL;
Elena Reshetova089e77e2017-03-03 10:55:17 +0200324 refcount_set(&item->refs, 1);
Miao Xie16cdcec2011-04-22 18:12:22 +0800325 }
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 */
339static 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 Sandeen48a3b632013-04-25 20:41:01 +0000390static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item(
Miao Xie16cdcec2011-04-22 18:12:22 +0800391 struct btrfs_delayed_node *delayed_node,
392 struct btrfs_key *key)
393{
Masahiro Yamadae2c89902016-09-13 04:35:52 +0900394 return __btrfs_lookup_delayed_item(&delayed_node->ins_root, key,
Miao Xie16cdcec2011-04-22 18:12:22 +0800395 NULL, NULL);
Miao Xie16cdcec2011-04-22 18:12:22 +0800396}
397
Miao Xie16cdcec2011-04-22 18:12:22 +0800398static 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
446static 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
453static 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 Masonde3cb942013-03-04 17:13:31 -0500460static void finish_one_item(struct btrfs_delayed_root *delayed_root)
461{
462 int seq = atomic_inc_return(&delayed_root->items_seq);
David Sterbaee863952015-02-16 19:41:40 +0100463
David Sterba093258e2018-02-26 16:15:17 +0100464 /* atomic_dec_return implies a barrier */
Chris Masonde3cb942013-03-04 17:13:31 -0500465 if ((atomic_dec_return(&delayed_root->items) <
David Sterba093258e2018-02-26 16:15:17 +0100466 BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0))
467 cond_wake_up_nomb(&delayed_root->wait);
Chris Masonde3cb942013-03-04 17:13:31 -0500468}
469
Miao Xie16cdcec2011-04-22 18:12:22 +0800470static 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 Masonde3cb942013-03-04 17:13:31 -0500488
489 finish_one_item(delayed_root);
Miao Xie16cdcec2011-04-22 18:12:22 +0800490}
491
492static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
493{
494 if (item) {
495 __btrfs_remove_delayed_item(item);
Elena Reshetova089e77e2017-03-03 10:55:17 +0200496 if (refcount_dec_and_test(&item->refs))
Miao Xie16cdcec2011-04-22 18:12:22 +0800497 kfree(item);
498 }
499}
500
Eric Sandeen48a3b632013-04-25 20:41:01 +0000501static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
Miao Xie16cdcec2011-04-22 18:12:22 +0800502 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 Sandeen48a3b632013-04-25 20:41:01 +0000514static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
Miao Xie16cdcec2011-04-22 18:12:22 +0800515 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 Sandeen48a3b632013-04-25 20:41:01 +0000527static struct btrfs_delayed_item *__btrfs_next_delayed_item(
Miao Xie16cdcec2011-04-22 18:12:22 +0800528 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 Xie16cdcec2011-04-22 18:12:22 +0800540static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
Qu Wenruo4f5427c2017-12-12 15:34:33 +0800541 struct btrfs_root *root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800542 struct btrfs_delayed_item *item)
543{
544 struct btrfs_block_rsv *src_rsv;
545 struct btrfs_block_rsv *dst_rsv;
Qu Wenruo4f5427c2017-12-12 15:34:33 +0800546 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xie16cdcec2011-04-22 18:12:22 +0800547 u64 num_bytes;
548 int ret;
549
550 if (!trans->bytes_reserved)
551 return 0;
552
553 src_rsv = trans->block_rsv;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400554 dst_rsv = &fs_info->delayed_block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +0800555
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400556 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Qu Wenruof218ea62018-04-17 16:52:45 +0800557
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 Bacik25d609f2016-03-25 13:25:48 -0400563 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500564 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400565 trace_btrfs_space_reservation(fs_info, "delayed_item",
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500566 item->key.objectid,
567 num_bytes, 1);
Miao Xie16cdcec2011-04-22 18:12:22 +0800568 item->bytes_reserved = num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500569 }
Miao Xie16cdcec2011-04-22 18:12:22 +0800570
571 return ret;
572}
573
Qu Wenruo4f5427c2017-12-12 15:34:33 +0800574static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800575 struct btrfs_delayed_item *item)
576{
Miao Xie19fd2942011-06-15 10:47:30 +0000577 struct btrfs_block_rsv *rsv;
Qu Wenruo4f5427c2017-12-12 15:34:33 +0800578 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xie19fd2942011-06-15 10:47:30 +0000579
Miao Xie16cdcec2011-04-22 18:12:22 +0800580 if (!item->bytes_reserved)
581 return;
582
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400583 rsv = &fs_info->delayed_block_rsv;
Qu Wenruof218ea62018-04-17 16:52:45 +0800584 /*
585 * Check btrfs_delayed_item_reserve_metadata() to see why we don't need
586 * to release/reserve qgroup space.
587 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400588 trace_btrfs_space_reservation(fs_info, "delayed_item",
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500589 item->key.objectid, item->bytes_reserved,
590 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400591 btrfs_block_rsv_release(fs_info, rsv,
Miao Xie16cdcec2011-04-22 18:12:22 +0800592 item->bytes_reserved);
593}
594
595static int btrfs_delayed_inode_reserve_metadata(
596 struct btrfs_trans_handle *trans,
597 struct btrfs_root *root,
Nikolay Borisovfcabdd12017-01-10 20:35:34 +0200598 struct btrfs_inode *inode,
Miao Xie16cdcec2011-04-22 18:12:22 +0800599 struct btrfs_delayed_node *node)
600{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400601 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xie16cdcec2011-04-22 18:12:22 +0800602 struct btrfs_block_rsv *src_rsv;
603 struct btrfs_block_rsv *dst_rsv;
604 u64 num_bytes;
605 int ret;
606
Miao Xie16cdcec2011-04-22 18:12:22 +0800607 src_rsv = trans->block_rsv;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400608 dst_rsv = &fs_info->delayed_block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +0800609
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400610 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Josef Bacikc06a0e12011-11-04 19:56:02 -0400611
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 Bacik69fe2d72017-10-19 14:15:57 -0400619 * we always reserve enough to update the inode item.
Josef Bacikc06a0e12011-11-04 19:56:02 -0400620 */
Chris Masone755d9a2011-12-15 13:36:29 -0500621 if (!src_rsv || (!trans->bytes_reserved &&
Miao Xie66d8f3d2012-09-06 04:02:28 -0600622 src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
Qu Wenruof218ea62018-04-17 16:52:45 +0800623 ret = btrfs_qgroup_reserve_meta_prealloc(root,
624 fs_info->nodesize, true);
625 if (ret < 0)
626 return ret;
Miao Xie08e007d2012-10-16 11:33:38 +0000627 ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
628 BTRFS_RESERVE_NO_FLUSH);
Josef Bacikc06a0e12011-11-04 19:56:02 -0400629 /*
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 Wenruo4f5427c2017-12-12 15:34:33 +0800635 if (ret == -EAGAIN) {
Josef Bacikc06a0e12011-11-04 19:56:02 -0400636 ret = -ENOSPC;
Qu Wenruo4f5427c2017-12-12 15:34:33 +0800637 btrfs_qgroup_free_meta_prealloc(root, num_bytes);
638 }
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500639 if (!ret) {
Josef Bacikc06a0e12011-11-04 19:56:02 -0400640 node->bytes_reserved = num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400641 trace_btrfs_space_reservation(fs_info,
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500642 "delayed_inode",
Nikolay Borisovfcabdd12017-01-10 20:35:34 +0200643 btrfs_ino(inode),
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500644 num_bytes, 1);
Qu Wenruof218ea62018-04-17 16:52:45 +0800645 } else {
646 btrfs_qgroup_free_meta_prealloc(root, fs_info->nodesize);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500647 }
Josef Bacikc06a0e12011-11-04 19:56:02 -0400648 return ret;
649 }
650
Josef Bacik25d609f2016-03-25 13:25:48 -0400651 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500652 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400653 trace_btrfs_space_reservation(fs_info, "delayed_inode",
Nikolay Borisovfcabdd12017-01-10 20:35:34 +0200654 btrfs_ino(inode), num_bytes, 1);
Miao Xie16cdcec2011-04-22 18:12:22 +0800655 node->bytes_reserved = num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500656 }
Miao Xie16cdcec2011-04-22 18:12:22 +0800657
658 return ret;
659}
660
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400661static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info *fs_info,
Qu Wenruo4f5427c2017-12-12 15:34:33 +0800662 struct btrfs_delayed_node *node,
663 bool qgroup_free)
Miao Xie16cdcec2011-04-22 18:12:22 +0800664{
665 struct btrfs_block_rsv *rsv;
666
667 if (!node->bytes_reserved)
668 return;
669
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400670 rsv = &fs_info->delayed_block_rsv;
671 trace_btrfs_space_reservation(fs_info, "delayed_inode",
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500672 node->inode_id, node->bytes_reserved, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400673 btrfs_block_rsv_release(fs_info, rsv,
Miao Xie16cdcec2011-04-22 18:12:22 +0800674 node->bytes_reserved);
Qu Wenruo4f5427c2017-12-12 15:34:33 +0800675 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 Xie16cdcec2011-04-22 18:12:22 +0800681 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 Itohafe5fea2013-04-16 05:18:22 +0000688static int btrfs_batch_insert_items(struct btrfs_root *root,
689 struct btrfs_path *path,
690 struct btrfs_delayed_item *item)
Miao Xie16cdcec2011-04-22 18:12:22 +0800691{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400692 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xie16cdcec2011-04-22 18:12:22 +0800693 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 Mahoney2ff7e612016-06-22 18:54:24 -0400709 free_space = btrfs_leaf_free_space(fs_info, leaf);
Miao Xie16cdcec2011-04-22 18:12:22 +0800710 INIT_LIST_HEAD(&head);
711
712 next = item;
Chris Mason17aca1c2011-06-03 01:13:45 -0400713 nitems = 0;
Miao Xie16cdcec2011-04-22 18:12:22 +0800714
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 Gunawardhanad9b0d9b2013-10-31 10:32:18 +0530746 keys = kmalloc_array(nitems, sizeof(struct btrfs_key), GFP_NOFS);
Miao Xie16cdcec2011-04-22 18:12:22 +0800747 if (!keys) {
748 ret = -ENOMEM;
749 goto out;
750 }
751
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +0530752 data_size = kmalloc_array(nitems, sizeof(u32), GFP_NOFS);
Miao Xie16cdcec2011-04-22 18:12:22 +0800753 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 Masonbd681512011-07-16 15:23:14 -0400767 btrfs_clear_path_blocking(path, NULL, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +0800768
769 /* insert the keys of the items */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000770 setup_items_for_insert(root, path, keys, data_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100771 total_data_size, total_size, nitems);
Miao Xie16cdcec2011-04-22 18:12:22 +0800772
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 Wenruo4f5427c2017-12-12 15:34:33 +0800782 btrfs_delayed_item_release_metadata(root, curr);
Miao Xie16cdcec2011-04-22 18:12:22 +0800783
784 list_del(&curr->tree_list);
785 btrfs_release_delayed_item(curr);
786 }
787
788error:
789 kfree(data_size);
790 kfree(keys);
791out:
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 */
799static 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 Bacik45b9d992020-03-19 10:11:32 -0400805 unsigned int nofs_flag;
Miao Xie16cdcec2011-04-22 18:12:22 +0800806 char *ptr;
807 int ret;
808
Josef Bacik45b9d992020-03-19 10:11:32 -0400809 nofs_flag = memalloc_nofs_save();
Miao Xie16cdcec2011-04-22 18:12:22 +0800810 ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->key,
811 delayed_item->data_len);
Josef Bacik45b9d992020-03-19 10:11:32 -0400812 memalloc_nofs_restore(nofs_flag);
Miao Xie16cdcec2011-04-22 18:12:22 +0800813 if (ret < 0 && ret != -EEXIST)
814 return ret;
815
816 leaf = path->nodes[0];
817
Miao Xie16cdcec2011-04-22 18:12:22 +0800818 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 Wenruo4f5427c2017-12-12 15:34:33 +0800824 btrfs_delayed_item_release_metadata(root, delayed_item);
Miao Xie16cdcec2011-04-22 18:12:22 +0800825 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 */
832static 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
840do_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 Mason945d8962011-05-22 12:33:42 -0400848 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +0800849 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 Itohafe5fea2013-04-16 05:18:22 +0000857 btrfs_batch_insert_items(root, path, curr);
Miao Xie16cdcec2011-04-22 18:12:22 +0800858 }
859 btrfs_release_delayed_item(prev);
860 btrfs_mark_buffer_dirty(path->nodes[0]);
861
Chris Mason945d8962011-05-22 12:33:42 -0400862 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +0800863 mutex_unlock(&node->mutex);
864 goto do_again;
865
866insert_end:
867 mutex_unlock(&node->mutex);
868 return ret;
869}
870
871static 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 Wenruo4f5427c2017-12-12 15:34:33 +0800925 btrfs_delayed_item_release_metadata(root, curr);
Miao Xie16cdcec2011-04-22 18:12:22 +0800926 list_del(&curr->tree_list);
927 btrfs_release_delayed_item(curr);
928 }
929
930out:
931 return ret;
932}
933
934static 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 Bacik45b9d992020-03-19 10:11:32 -0400940 unsigned int nofs_flag;
Miao Xie16cdcec2011-04-22 18:12:22 +0800941 int ret = 0;
942
943do_again:
944 mutex_lock(&node->mutex);
945 curr = __btrfs_first_delayed_deletion_item(node);
946 if (!curr)
947 goto delete_fail;
948
Josef Bacik45b9d992020-03-19 10:11:32 -0400949 nofs_flag = memalloc_nofs_save();
Miao Xie16cdcec2011-04-22 18:12:22 +0800950 ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1);
Josef Bacik45b9d992020-03-19 10:11:32 -0400951 memalloc_nofs_restore(nofs_flag);
Miao Xie16cdcec2011-04-22 18:12:22 +0800952 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 Mason945d8962011-05-22 12:33:42 -0400963 btrfs_release_path(path);
Fengguang Wu62095262012-08-04 01:45:02 -0600964 if (curr) {
965 mutex_unlock(&node->mutex);
Miao Xie16cdcec2011-04-22 18:12:22 +0800966 goto do_again;
Fengguang Wu62095262012-08-04 01:45:02 -0600967 } else
Miao Xie16cdcec2011-04-22 18:12:22 +0800968 goto delete_fail;
969 }
970
971 btrfs_batch_delete_items(trans, root, path, curr);
Chris Mason945d8962011-05-22 12:33:42 -0400972 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +0800973 mutex_unlock(&node->mutex);
974 goto do_again;
975
976delete_fail:
Chris Mason945d8962011-05-22 12:33:42 -0400977 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +0800978 mutex_unlock(&node->mutex);
979 return ret;
980}
981
982static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
983{
984 struct btrfs_delayed_root *delayed_root;
985
Miao Xie7cf35d92013-12-26 13:07:05 +0800986 if (delayed_node &&
987 test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
Miao Xie16cdcec2011-04-22 18:12:22 +0800988 BUG_ON(!delayed_node->root);
Miao Xie7cf35d92013-12-26 13:07:05 +0800989 clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
Miao Xie16cdcec2011-04-22 18:12:22 +0800990 delayed_node->count--;
991
992 delayed_root = delayed_node->root->fs_info->delayed_root;
Chris Masonde3cb942013-03-04 17:13:31 -0500993 finish_one_item(delayed_root);
Miao Xie16cdcec2011-04-22 18:12:22 +0800994 }
995}
996
Miao Xie67de1172013-12-26 13:07:06 +0800997static 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 Xie0e8c36a2012-12-19 06:59:51 +00001009static 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 Xie16cdcec2011-04-22 18:12:22 +08001013{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001014 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xie16cdcec2011-04-22 18:12:22 +08001015 struct btrfs_key key;
1016 struct btrfs_inode_item *inode_item;
1017 struct extent_buffer *leaf;
Josef Bacik45b9d992020-03-19 10:11:32 -04001018 unsigned int nofs_flag;
Miao Xie67de1172013-12-26 13:07:06 +08001019 int mod;
Miao Xie16cdcec2011-04-22 18:12:22 +08001020 int ret;
1021
Miao Xie16cdcec2011-04-22 18:12:22 +08001022 key.objectid = node->inode_id;
David Sterba962a2982014-06-04 18:41:45 +02001023 key.type = BTRFS_INODE_ITEM_KEY;
Miao Xie16cdcec2011-04-22 18:12:22 +08001024 key.offset = 0;
Miao Xie0e8c36a2012-12-19 06:59:51 +00001025
Miao Xie67de1172013-12-26 13:07:06 +08001026 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
1027 mod = -1;
1028 else
1029 mod = 1;
1030
Josef Bacik45b9d992020-03-19 10:11:32 -04001031 nofs_flag = memalloc_nofs_save();
Miao Xie67de1172013-12-26 13:07:06 +08001032 ret = btrfs_lookup_inode(trans, root, path, &key, mod);
Josef Bacik45b9d992020-03-19 10:11:32 -04001033 memalloc_nofs_restore(nofs_flag);
Miao Xie16cdcec2011-04-22 18:12:22 +08001034 if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -04001035 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +08001036 return -ENOENT;
1037 } else if (ret < 0) {
Miao Xie16cdcec2011-04-22 18:12:22 +08001038 return ret;
1039 }
1040
Miao Xie16cdcec2011-04-22 18:12:22 +08001041 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 Xie16cdcec2011-04-22 18:12:22 +08001047
Miao Xie67de1172013-12-26 13:07:06 +08001048 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;
1054again:
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);
1069out:
1070 btrfs_release_delayed_iref(node);
1071no_iref:
1072 btrfs_release_path(path);
1073err_out:
Qu Wenruo4f5427c2017-12-12 15:34:33 +08001074 btrfs_delayed_inode_release_metadata(fs_info, node, (ret < 0));
Miao Xie16cdcec2011-04-22 18:12:22 +08001075 btrfs_release_delayed_inode(node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001076
Miao Xie67de1172013-12-26 13:07:06 +08001077 return ret;
1078
1079search:
1080 btrfs_release_path(path);
1081
David Sterba962a2982014-06-04 18:41:45 +02001082 key.type = BTRFS_INODE_EXTREF_KEY;
Miao Xie67de1172013-12-26 13:07:06 +08001083 key.offset = -1;
Josef Bacik45b9d992020-03-19 10:11:32 -04001084
1085 nofs_flag = memalloc_nofs_save();
Miao Xie67de1172013-12-26 13:07:06 +08001086 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Josef Bacik45b9d992020-03-19 10:11:32 -04001087 memalloc_nofs_restore(nofs_flag);
Miao Xie67de1172013-12-26 13:07:06 +08001088 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 Xie16cdcec2011-04-22 18:12:22 +08001096}
1097
Miao Xie0e8c36a2012-12-19 06:59:51 +00001098static 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 Xie7cf35d92013-12-26 13:07:05 +08001106 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &node->flags)) {
Miao Xie0e8c36a2012-12-19 06:59:51 +00001107 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 Xie4ea41ce2012-12-19 06:59:03 +00001116static 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 Mahoney79787ea2012-03-12 16:03:00 +01001135/*
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 Borisovb84acab2018-02-07 17:55:42 +02001141static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, int nr)
Miao Xie16cdcec2011-04-22 18:12:22 +08001142{
Nikolay Borisovb84acab2018-02-07 17:55:42 +02001143 struct btrfs_fs_info *fs_info = trans->fs_info;
Miao Xie16cdcec2011-04-22 18:12:22 +08001144 struct btrfs_delayed_root *delayed_root;
1145 struct btrfs_delayed_node *curr_node, *prev_node;
1146 struct btrfs_path *path;
Miao Xie19fd2942011-06-15 10:47:30 +00001147 struct btrfs_block_rsv *block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +08001148 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04001149 bool count = (nr > 0);
Miao Xie16cdcec2011-04-22 18:12:22 +08001150
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001151 if (trans->aborted)
1152 return -EIO;
1153
Miao Xie16cdcec2011-04-22 18:12:22 +08001154 path = btrfs_alloc_path();
1155 if (!path)
1156 return -ENOMEM;
1157 path->leave_spinning = 1;
1158
Miao Xie19fd2942011-06-15 10:47:30 +00001159 block_rsv = trans->block_rsv;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001160 trans->block_rsv = &fs_info->delayed_block_rsv;
Miao Xie19fd2942011-06-15 10:47:30 +00001161
Jeff Mahoneyccdf9b32016-06-22 18:54:23 -04001162 delayed_root = fs_info->delayed_root;
Miao Xie16cdcec2011-04-22 18:12:22 +08001163
1164 curr_node = btrfs_first_delayed_node(delayed_root);
Josef Bacik96c3f432012-06-21 14:05:49 -04001165 while (curr_node && (!count || (count && nr--))) {
Miao Xie4ea41ce2012-12-19 06:59:03 +00001166 ret = __btrfs_commit_inode_delayed_items(trans, path,
1167 curr_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001168 if (ret) {
1169 btrfs_release_delayed_node(curr_node);
Josef Bacik96c3f432012-06-21 14:05:49 -04001170 curr_node = NULL;
Jeff Mahoney66642832016-06-10 18:19:25 -04001171 btrfs_abort_transaction(trans, ret);
Miao Xie16cdcec2011-04-22 18:12:22 +08001172 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 Bacik96c3f432012-06-21 14:05:49 -04001180 if (curr_node)
1181 btrfs_release_delayed_node(curr_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001182 btrfs_free_path(path);
Miao Xie19fd2942011-06-15 10:47:30 +00001183 trans->block_rsv = block_rsv;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001184
Miao Xie16cdcec2011-04-22 18:12:22 +08001185 return ret;
1186}
1187
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001188int btrfs_run_delayed_items(struct btrfs_trans_handle *trans)
Josef Bacik96c3f432012-06-21 14:05:49 -04001189{
Nikolay Borisovb84acab2018-02-07 17:55:42 +02001190 return __btrfs_run_delayed_items(trans, -1);
Josef Bacik96c3f432012-06-21 14:05:49 -04001191}
1192
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001193int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr)
Josef Bacik96c3f432012-06-21 14:05:49 -04001194{
Nikolay Borisovb84acab2018-02-07 17:55:42 +02001195 return __btrfs_run_delayed_items(trans, nr);
Josef Bacik96c3f432012-06-21 14:05:49 -04001196}
1197
Miao Xie16cdcec2011-04-22 18:12:22 +08001198int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
Nikolay Borisov5f4b32e2017-01-10 20:35:41 +02001199 struct btrfs_inode *inode)
Miao Xie16cdcec2011-04-22 18:12:22 +08001200{
Nikolay Borisov5f4b32e2017-01-10 20:35:41 +02001201 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
Miao Xie4ea41ce2012-12-19 06:59:03 +00001202 struct btrfs_path *path;
1203 struct btrfs_block_rsv *block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +08001204 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 Xie4ea41ce2012-12-19 06:59:03 +00001217 path = btrfs_alloc_path();
Filipe David Borba Manana3c77bd92013-10-12 20:32:59 +01001218 if (!path) {
1219 btrfs_release_delayed_node(delayed_node);
Miao Xie4ea41ce2012-12-19 06:59:03 +00001220 return -ENOMEM;
Filipe David Borba Manana3c77bd92013-10-12 20:32:59 +01001221 }
Miao Xie4ea41ce2012-12-19 06:59:03 +00001222 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 Xie16cdcec2011-04-22 18:12:22 +08001229 btrfs_release_delayed_node(delayed_node);
Miao Xie4ea41ce2012-12-19 06:59:03 +00001230 btrfs_free_path(path);
1231 trans->block_rsv = block_rsv;
1232
Miao Xie16cdcec2011-04-22 18:12:22 +08001233 return ret;
1234}
1235
Nikolay Borisovaa790212017-01-10 20:35:40 +02001236int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode)
Miao Xie0e8c36a2012-12-19 06:59:51 +00001237{
David Sterba3ffbd682018-06-29 10:56:42 +02001238 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Miao Xie0e8c36a2012-12-19 06:59:51 +00001239 struct btrfs_trans_handle *trans;
Nikolay Borisovaa790212017-01-10 20:35:40 +02001240 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
Miao Xie0e8c36a2012-12-19 06:59:51 +00001241 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 Xie7cf35d92013-12-26 13:07:05 +08001249 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
Miao Xie0e8c36a2012-12-19 06:59:51 +00001250 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 Mahoney2ff7e612016-06-22 18:54:24 -04001270 trans->block_rsv = &fs_info->delayed_block_rsv;
Miao Xie0e8c36a2012-12-19 06:59:51 +00001271
1272 mutex_lock(&delayed_node->mutex);
Miao Xie7cf35d92013-12-26 13:07:05 +08001273 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags))
Miao Xie0e8c36a2012-12-19 06:59:51 +00001274 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;
1282trans_out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001283 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001284 btrfs_btree_balance_dirty(fs_info);
Miao Xie0e8c36a2012-12-19 06:59:51 +00001285out:
1286 btrfs_release_delayed_node(delayed_node);
1287
1288 return ret;
1289}
1290
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02001291void btrfs_remove_delayed_node(struct btrfs_inode *inode)
Miao Xie16cdcec2011-04-22 18:12:22 +08001292{
1293 struct btrfs_delayed_node *delayed_node;
1294
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02001295 delayed_node = READ_ONCE(inode->delayed_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001296 if (!delayed_node)
1297 return;
1298
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02001299 inode->delayed_node = NULL;
Miao Xie16cdcec2011-04-22 18:12:22 +08001300 btrfs_release_delayed_node(delayed_node);
1301}
1302
Chris Masonde3cb942013-03-04 17:13:31 -05001303struct btrfs_async_delayed_work {
1304 struct btrfs_delayed_root *delayed_root;
1305 int nr;
Qu Wenruod458b052014-02-28 10:46:19 +08001306 struct btrfs_work work;
Miao Xie16cdcec2011-04-22 18:12:22 +08001307};
1308
Qu Wenruod458b052014-02-28 10:46:19 +08001309static void btrfs_async_run_delayed_root(struct btrfs_work *work)
Miao Xie16cdcec2011-04-22 18:12:22 +08001310{
Chris Masonde3cb942013-03-04 17:13:31 -05001311 struct btrfs_async_delayed_work *async_work;
1312 struct btrfs_delayed_root *delayed_root;
Miao Xie16cdcec2011-04-22 18:12:22 +08001313 struct btrfs_trans_handle *trans;
1314 struct btrfs_path *path;
1315 struct btrfs_delayed_node *delayed_node = NULL;
1316 struct btrfs_root *root;
Miao Xie19fd2942011-06-15 10:47:30 +00001317 struct btrfs_block_rsv *block_rsv;
Chris Masonde3cb942013-03-04 17:13:31 -05001318 int total_done = 0;
Miao Xie16cdcec2011-04-22 18:12:22 +08001319
Chris Masonde3cb942013-03-04 17:13:31 -05001320 async_work = container_of(work, struct btrfs_async_delayed_work, work);
1321 delayed_root = async_work->delayed_root;
Miao Xie16cdcec2011-04-22 18:12:22 +08001322
1323 path = btrfs_alloc_path();
1324 if (!path)
1325 goto out;
Miao Xie16cdcec2011-04-22 18:12:22 +08001326
Nikolay Borisov617c54a2017-10-23 13:51:48 +03001327 do {
1328 if (atomic_read(&delayed_root->items) <
1329 BTRFS_DELAYED_BACKGROUND / 2)
1330 break;
Chris Masonde3cb942013-03-04 17:13:31 -05001331
Nikolay Borisov617c54a2017-10-23 13:51:48 +03001332 delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
1333 if (!delayed_node)
1334 break;
Chris Masonde3cb942013-03-04 17:13:31 -05001335
Nikolay Borisov617c54a2017-10-23 13:51:48 +03001336 path->leave_spinning = 1;
1337 root = delayed_node->root;
Miao Xie16cdcec2011-04-22 18:12:22 +08001338
Nikolay Borisov617c54a2017-10-23 13:51:48 +03001339 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 Xie16cdcec2011-04-22 18:12:22 +08001346
Nikolay Borisov617c54a2017-10-23 13:51:48 +03001347 block_rsv = trans->block_rsv;
1348 trans->block_rsv = &root->fs_info->delayed_block_rsv;
Miao Xie19fd2942011-06-15 10:47:30 +00001349
Nikolay Borisov617c54a2017-10-23 13:51:48 +03001350 __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001351
Nikolay Borisov617c54a2017-10-23 13:51:48 +03001352 trans->block_rsv = block_rsv;
1353 btrfs_end_transaction(trans);
1354 btrfs_btree_balance_dirty_nodelay(root->fs_info);
Chris Masonde3cb942013-03-04 17:13:31 -05001355
Nikolay Borisov617c54a2017-10-23 13:51:48 +03001356 btrfs_release_path(path);
1357 btrfs_release_prepared_delayed_node(delayed_node);
1358 total_done++;
Chris Masonde3cb942013-03-04 17:13:31 -05001359
Nikolay Borisov617c54a2017-10-23 13:51:48 +03001360 } while ((async_work->nr == 0 && total_done < BTRFS_DELAYED_WRITEBACK)
1361 || total_done < async_work->nr);
Chris Masonde3cb942013-03-04 17:13:31 -05001362
Miao Xie16cdcec2011-04-22 18:12:22 +08001363 btrfs_free_path(path);
1364out:
Chris Masonde3cb942013-03-04 17:13:31 -05001365 wake_up(&delayed_root->wait);
1366 kfree(async_work);
Miao Xie16cdcec2011-04-22 18:12:22 +08001367}
1368
Miao Xie16cdcec2011-04-22 18:12:22 +08001369
Chris Masonde3cb942013-03-04 17:13:31 -05001370static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
Daniel Dresslera585e942014-11-17 22:05:02 +09001371 struct btrfs_fs_info *fs_info, int nr)
Chris Masonde3cb942013-03-04 17:13:31 -05001372{
1373 struct btrfs_async_delayed_work *async_work;
1374
Chris Masonde3cb942013-03-04 17:13:31 -05001375 async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
1376 if (!async_work)
Miao Xie16cdcec2011-04-22 18:12:22 +08001377 return -ENOMEM;
Miao Xie16cdcec2011-04-22 18:12:22 +08001378
Chris Masonde3cb942013-03-04 17:13:31 -05001379 async_work->delayed_root = delayed_root;
Liu Bo9e0af232014-08-15 23:36:53 +08001380 btrfs_init_work(&async_work->work, btrfs_delayed_meta_helper,
1381 btrfs_async_run_delayed_root, NULL, NULL);
Chris Masonde3cb942013-03-04 17:13:31 -05001382 async_work->nr = nr;
Miao Xie16cdcec2011-04-22 18:12:22 +08001383
Daniel Dresslera585e942014-11-17 22:05:02 +09001384 btrfs_queue_work(fs_info->delayed_workers, &async_work->work);
Miao Xie16cdcec2011-04-22 18:12:22 +08001385 return 0;
1386}
1387
Jeff Mahoneyccdf9b32016-06-22 18:54:23 -04001388void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info)
Chris Masone9993762011-06-17 16:14:09 -04001389{
Jeff Mahoneyccdf9b32016-06-22 18:54:23 -04001390 WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root));
Chris Masone9993762011-06-17 16:14:09 -04001391}
1392
Miao Xie03538082013-12-26 13:07:03 +08001393static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
Chris Masonde3cb942013-03-04 17:13:31 -05001394{
1395 int val = atomic_read(&delayed_root->items_seq);
1396
Miao Xie03538082013-12-26 13:07:03 +08001397 if (val < seq || val >= seq + BTRFS_DELAYED_BATCH)
Chris Masonde3cb942013-03-04 17:13:31 -05001398 return 1;
Miao Xie03538082013-12-26 13:07:03 +08001399
1400 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1401 return 1;
1402
Chris Masonde3cb942013-03-04 17:13:31 -05001403 return 0;
1404}
1405
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001406void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info)
Miao Xie16cdcec2011-04-22 18:12:22 +08001407{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001408 struct btrfs_delayed_root *delayed_root = fs_info->delayed_root;
Miao Xie16cdcec2011-04-22 18:12:22 +08001409
Nikolay Borisov85777872017-10-23 13:51:49 +03001410 if ((atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) ||
1411 btrfs_workqueue_normal_congested(fs_info->delayed_workers))
Miao Xie16cdcec2011-04-22 18:12:22 +08001412 return;
1413
1414 if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
Miao Xie03538082013-12-26 13:07:03 +08001415 int seq;
Miao Xie16cdcec2011-04-22 18:12:22 +08001416 int ret;
Miao Xie03538082013-12-26 13:07:03 +08001417
1418 seq = atomic_read(&delayed_root->items_seq);
Chris Masonde3cb942013-03-04 17:13:31 -05001419
Daniel Dresslera585e942014-11-17 22:05:02 +09001420 ret = btrfs_wq_run_delayed_node(delayed_root, fs_info, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +08001421 if (ret)
1422 return;
1423
Miao Xie03538082013-12-26 13:07:03 +08001424 wait_event_interruptible(delayed_root->wait,
1425 could_end_wait(delayed_root, seq));
Miao Xie4dd466d2013-12-26 13:07:02 +08001426 return;
Miao Xie16cdcec2011-04-22 18:12:22 +08001427 }
1428
Daniel Dresslera585e942014-11-17 22:05:02 +09001429 btrfs_wq_run_delayed_node(delayed_root, fs_info, BTRFS_DELAYED_BATCH);
Miao Xie16cdcec2011-04-22 18:12:22 +08001430}
1431
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001432/* Will return 0 or -ENOMEM */
Miao Xie16cdcec2011-04-22 18:12:22 +08001433int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001434 const char *name, int name_len,
Nikolay Borisov6f45d182017-01-10 20:35:35 +02001435 struct btrfs_inode *dir,
Miao Xie16cdcec2011-04-22 18:12:22 +08001436 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 Borisov6f45d182017-01-10 20:35:35 +02001444 delayed_node = btrfs_get_or_create_delayed_node(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08001445 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 Borisov6f45d182017-01-10 20:35:35 +02001454 delayed_item->key.objectid = btrfs_ino(dir);
David Sterba962a2982014-06-04 18:41:45 +02001455 delayed_item->key.type = BTRFS_DIR_INDEX_KEY;
Miao Xie16cdcec2011-04-22 18:12:22 +08001456 delayed_item->key.offset = index;
1457
1458 dir_item = (struct btrfs_dir_item *)delayed_item->data;
1459 dir_item->location = *disk_key;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001460 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 Xie16cdcec2011-04-22 18:12:22 +08001464 memcpy((char *)(dir_item + 1), name, name_len);
1465
Qu Wenruo4f5427c2017-12-12 15:34:33 +08001466 ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, delayed_item);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05001467 /*
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 Xie16cdcec2011-04-22 18:12:22 +08001473 mutex_lock(&delayed_node->mutex);
1474 ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
1475 if (unlikely(ret)) {
Lu Fengqi4465c8b2018-08-01 11:32:25 +08001476 btrfs_err(trans->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001477 "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 Xie16cdcec2011-04-22 18:12:22 +08001480 BUG();
1481 }
1482 mutex_unlock(&delayed_node->mutex);
1483
1484release_node:
1485 btrfs_release_delayed_node(delayed_node);
1486 return ret;
1487}
1488
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001489static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info *fs_info,
Miao Xie16cdcec2011-04-22 18:12:22 +08001490 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 Wenruo4f5427c2017-12-12 15:34:33 +08001502 btrfs_delayed_item_release_metadata(node->root, item);
Miao Xie16cdcec2011-04-22 18:12:22 +08001503 btrfs_release_delayed_item(item);
1504 mutex_unlock(&node->mutex);
1505 return 0;
1506}
1507
1508int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02001509 struct btrfs_inode *dir, u64 index)
Miao Xie16cdcec2011-04-22 18:12:22 +08001510{
1511 struct btrfs_delayed_node *node;
1512 struct btrfs_delayed_item *item;
1513 struct btrfs_key item_key;
1514 int ret;
1515
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02001516 node = btrfs_get_or_create_delayed_node(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08001517 if (IS_ERR(node))
1518 return PTR_ERR(node);
1519
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02001520 item_key.objectid = btrfs_ino(dir);
David Sterba962a2982014-06-04 18:41:45 +02001521 item_key.type = BTRFS_DIR_INDEX_KEY;
Miao Xie16cdcec2011-04-22 18:12:22 +08001522 item_key.offset = index;
1523
Lu Fengqi9add2942018-08-01 11:32:26 +08001524 ret = btrfs_delete_delayed_insertion_item(trans->fs_info, node,
1525 &item_key);
Miao Xie16cdcec2011-04-22 18:12:22 +08001526 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 Wenruo4f5427c2017-12-12 15:34:33 +08001537 ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, item);
Miao Xie16cdcec2011-04-22 18:12:22 +08001538 /*
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 Fengqi9add2942018-08-01 11:32:26 +08001547 btrfs_err(trans->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001548 "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 Xie16cdcec2011-04-22 18:12:22 +08001550 BUG();
1551 }
1552 mutex_unlock(&node->mutex);
1553end:
1554 btrfs_release_delayed_node(node);
1555 return ret;
1556}
1557
Nikolay Borisovf5cc7b82017-01-10 20:35:42 +02001558int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode)
Miao Xie16cdcec2011-04-22 18:12:22 +08001559{
Nikolay Borisovf5cc7b82017-01-10 20:35:42 +02001560 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08001561
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 Xie2f7e33d2011-06-23 07:27:13 +00001570 if (!delayed_node->index_cnt) {
1571 btrfs_release_delayed_node(delayed_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001572 return -EINVAL;
Miao Xie2f7e33d2011-06-23 07:27:13 +00001573 }
Miao Xie16cdcec2011-04-22 18:12:22 +08001574
Nikolay Borisovf5cc7b82017-01-10 20:35:42 +02001575 inode->index_cnt = delayed_node->index_cnt;
Miao Xie2f7e33d2011-06-23 07:27:13 +00001576 btrfs_release_delayed_node(delayed_node);
1577 return 0;
Miao Xie16cdcec2011-04-22 18:12:22 +08001578}
1579
Omar Sandoval02dbfc92016-05-20 13:50:33 -07001580bool btrfs_readdir_get_delayed_items(struct inode *inode,
1581 struct list_head *ins_list,
1582 struct list_head *del_list)
Miao Xie16cdcec2011-04-22 18:12:22 +08001583{
1584 struct btrfs_delayed_node *delayed_node;
1585 struct btrfs_delayed_item *item;
1586
Nikolay Borisov340c6ca2017-01-10 20:35:32 +02001587 delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08001588 if (!delayed_node)
Omar Sandoval02dbfc92016-05-20 13:50:33 -07001589 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 Xie16cdcec2011-04-22 18:12:22 +08001597
1598 mutex_lock(&delayed_node->mutex);
1599 item = __btrfs_first_delayed_insertion_item(delayed_node);
1600 while (item) {
Elena Reshetova089e77e2017-03-03 10:55:17 +02001601 refcount_inc(&item->refs);
Miao Xie16cdcec2011-04-22 18:12:22 +08001602 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 Reshetova089e77e2017-03-03 10:55:17 +02001608 refcount_inc(&item->refs);
Miao Xie16cdcec2011-04-22 18:12:22 +08001609 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 Reshetova6de5f182017-03-03 10:55:16 +02001622 refcount_dec(&delayed_node->refs);
Omar Sandoval02dbfc92016-05-20 13:50:33 -07001623
1624 return true;
Miao Xie16cdcec2011-04-22 18:12:22 +08001625}
1626
Omar Sandoval02dbfc92016-05-20 13:50:33 -07001627void btrfs_readdir_put_delayed_items(struct inode *inode,
1628 struct list_head *ins_list,
1629 struct list_head *del_list)
Miao Xie16cdcec2011-04-22 18:12:22 +08001630{
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 Reshetova089e77e2017-03-03 10:55:17 +02001635 if (refcount_dec_and_test(&curr->refs))
Miao Xie16cdcec2011-04-22 18:12:22 +08001636 kfree(curr);
1637 }
1638
1639 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1640 list_del(&curr->readdir_list);
Elena Reshetova089e77e2017-03-03 10:55:17 +02001641 if (refcount_dec_and_test(&curr->refs))
Miao Xie16cdcec2011-04-22 18:12:22 +08001642 kfree(curr);
1643 }
Omar Sandoval02dbfc92016-05-20 13:50:33 -07001644
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 Xie16cdcec2011-04-22 18:12:22 +08001650}
1651
1652int btrfs_should_delete_dir_index(struct list_head *del_list,
1653 u64 index)
1654{
Josef Bacike4fd4932018-01-23 15:17:05 -05001655 struct btrfs_delayed_item *curr;
1656 int ret = 0;
Miao Xie16cdcec2011-04-22 18:12:22 +08001657
Josef Bacike4fd4932018-01-23 15:17:05 -05001658 list_for_each_entry(curr, del_list, readdir_list) {
Miao Xie16cdcec2011-04-22 18:12:22 +08001659 if (curr->key.offset > index)
1660 break;
Josef Bacike4fd4932018-01-23 15:17:05 -05001661 if (curr->key.offset == index) {
1662 ret = 1;
1663 break;
1664 }
Miao Xie16cdcec2011-04-22 18:12:22 +08001665 }
Josef Bacike4fd4932018-01-23 15:17:05 -05001666 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08001667}
1668
1669/*
1670 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1671 *
1672 */
Al Viro9cdda8d2013-05-22 16:48:09 -04001673int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04001674 struct list_head *ins_list)
Miao Xie16cdcec2011-04-22 18:12:22 +08001675{
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 Viro9cdda8d2013-05-22 16:48:09 -04001695 if (curr->key.offset < ctx->pos) {
Elena Reshetova089e77e2017-03-03 10:55:17 +02001696 if (refcount_dec_and_test(&curr->refs))
Miao Xie16cdcec2011-04-22 18:12:22 +08001697 kfree(curr);
1698 continue;
1699 }
1700
Al Viro9cdda8d2013-05-22 16:48:09 -04001701 ctx->pos = curr->key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08001702
1703 di = (struct btrfs_dir_item *)curr->data;
1704 name = (char *)(di + 1);
Qu Wenruo3cae2102013-07-16 11:19:18 +08001705 name_len = btrfs_stack_dir_name_len(di);
Miao Xie16cdcec2011-04-22 18:12:22 +08001706
1707 d_type = btrfs_filetype_table[di->type];
1708 btrfs_disk_key_to_cpu(&location, &di->location);
1709
Al Viro9cdda8d2013-05-22 16:48:09 -04001710 over = !dir_emit(ctx, name, name_len,
Miao Xie16cdcec2011-04-22 18:12:22 +08001711 location.objectid, d_type);
1712
Elena Reshetova089e77e2017-03-03 10:55:17 +02001713 if (refcount_dec_and_test(&curr->refs))
Miao Xie16cdcec2011-04-22 18:12:22 +08001714 kfree(curr);
1715
1716 if (over)
1717 return 1;
Josef Bacik42e9cc42017-07-24 15:14:26 -04001718 ctx->pos++;
Miao Xie16cdcec2011-04-22 18:12:22 +08001719 }
1720 return 0;
1721}
1722
Miao Xie16cdcec2011-04-22 18:12:22 +08001723static void fill_stack_inode_item(struct btrfs_trans_handle *trans,
1724 struct btrfs_inode_item *inode_item,
1725 struct inode *inode)
1726{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08001727 btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
1728 btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08001729 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 Laytonc7f88c42017-12-11 06:35:12 -05001735 btrfs_set_stack_inode_sequence(inode_item,
1736 inode_peek_iversion(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08001737 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 Masonff5714c2011-05-28 07:00:39 -04001740 btrfs_set_stack_inode_block_group(inode_item, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +08001741
David Sterbaa937b972014-12-12 17:39:12 +01001742 btrfs_set_stack_timespec_sec(&inode_item->atime,
Miao Xie16cdcec2011-04-22 18:12:22 +08001743 inode->i_atime.tv_sec);
David Sterbaa937b972014-12-12 17:39:12 +01001744 btrfs_set_stack_timespec_nsec(&inode_item->atime,
Miao Xie16cdcec2011-04-22 18:12:22 +08001745 inode->i_atime.tv_nsec);
1746
David Sterbaa937b972014-12-12 17:39:12 +01001747 btrfs_set_stack_timespec_sec(&inode_item->mtime,
Miao Xie16cdcec2011-04-22 18:12:22 +08001748 inode->i_mtime.tv_sec);
David Sterbaa937b972014-12-12 17:39:12 +01001749 btrfs_set_stack_timespec_nsec(&inode_item->mtime,
Miao Xie16cdcec2011-04-22 18:12:22 +08001750 inode->i_mtime.tv_nsec);
1751
David Sterbaa937b972014-12-12 17:39:12 +01001752 btrfs_set_stack_timespec_sec(&inode_item->ctime,
Miao Xie16cdcec2011-04-22 18:12:22 +08001753 inode->i_ctime.tv_sec);
David Sterbaa937b972014-12-12 17:39:12 +01001754 btrfs_set_stack_timespec_nsec(&inode_item->ctime,
Miao Xie16cdcec2011-04-22 18:12:22 +08001755 inode->i_ctime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05301756
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 Xie16cdcec2011-04-22 18:12:22 +08001761}
1762
Miao Xie2f7e33d2011-06-23 07:27:13 +00001763int btrfs_fill_inode(struct inode *inode, u32 *rdev)
1764{
1765 struct btrfs_delayed_node *delayed_node;
1766 struct btrfs_inode_item *inode_item;
Miao Xie2f7e33d2011-06-23 07:27:13 +00001767
Nikolay Borisov340c6ca2017-01-10 20:35:32 +02001768 delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
Miao Xie2f7e33d2011-06-23 07:27:13 +00001769 if (!delayed_node)
1770 return -ENOENT;
1771
1772 mutex_lock(&delayed_node->mutex);
Miao Xie7cf35d92013-12-26 13:07:05 +08001773 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
Miao Xie2f7e33d2011-06-23 07:27:13 +00001774 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. Biederman2f2f43d2012-02-10 11:05:07 -08001781 i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
1782 i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02001783 btrfs_i_size_write(BTRFS_I(inode), btrfs_stack_inode_size(inode_item));
Miao Xie2f7e33d2011-06-23 07:27:13 +00001784 inode->i_mode = btrfs_stack_inode_mode(inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001785 set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
Miao Xie2f7e33d2011-06-23 07:27:13 +00001786 inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item));
1787 BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
Yang Dongsheng6e17d302015-04-09 12:08:43 +08001788 BTRFS_I(inode)->last_trans = btrfs_stack_inode_transid(inode_item);
1789
Jeff Laytonc7f88c42017-12-11 06:35:12 -05001790 inode_set_iversion_queried(inode,
1791 btrfs_stack_inode_sequence(inode_item));
Miao Xie2f7e33d2011-06-23 07:27:13 +00001792 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 Sterbaa937b972014-12-12 17:39:12 +01001796 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 Xie2f7e33d2011-06-23 07:27:13 +00001798
David Sterbaa937b972014-12-12 17:39:12 +01001799 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 Xie2f7e33d2011-06-23 07:27:13 +00001801
David Sterbaa937b972014-12-12 17:39:12 +01001802 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 Xie2f7e33d2011-06-23 07:27:13 +00001804
chandan r9cc97d62012-07-04 12:48:07 +05301805 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 Xie2f7e33d2011-06-23 07:27:13 +00001810 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 Xie16cdcec2011-04-22 18:12:22 +08001818int 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 Sterbaaa0467d2011-06-03 16:29:08 +02001822 int ret = 0;
Miao Xie16cdcec2011-04-22 18:12:22 +08001823
Nikolay Borisove5517a72017-01-10 20:35:33 +02001824 delayed_node = btrfs_get_or_create_delayed_node(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08001825 if (IS_ERR(delayed_node))
1826 return PTR_ERR(delayed_node);
1827
1828 mutex_lock(&delayed_node->mutex);
Miao Xie7cf35d92013-12-26 13:07:05 +08001829 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
Miao Xie16cdcec2011-04-22 18:12:22 +08001830 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1831 goto release_node;
1832 }
1833
Nikolay Borisovfcabdd12017-01-10 20:35:34 +02001834 ret = btrfs_delayed_inode_reserve_metadata(trans, root, BTRFS_I(inode),
Josef Bacik7fd2ae22011-11-08 15:47:34 -05001835 delayed_node);
Josef Bacikc06a0e12011-11-04 19:56:02 -04001836 if (ret)
1837 goto release_node;
Miao Xie16cdcec2011-04-22 18:12:22 +08001838
1839 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
Miao Xie7cf35d92013-12-26 13:07:05 +08001840 set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
Miao Xie16cdcec2011-04-22 18:12:22 +08001841 delayed_node->count++;
1842 atomic_inc(&root->fs_info->delayed_root->items);
1843release_node:
1844 mutex_unlock(&delayed_node->mutex);
1845 btrfs_release_delayed_node(delayed_node);
1846 return ret;
1847}
1848
Nikolay Borisove07222c2017-01-10 20:35:37 +02001849int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode)
Miao Xie67de1172013-12-26 13:07:06 +08001850{
David Sterba3ffbd682018-06-29 10:56:42 +02001851 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Miao Xie67de1172013-12-26 13:07:06 +08001852 struct btrfs_delayed_node *delayed_node;
1853
Chris Mason6f896052014-12-31 12:18:29 -05001854 /*
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 Mahoney0b246af2016-06-22 18:54:23 -04001859 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Chris Mason6f896052014-12-31 12:18:29 -05001860 return -EAGAIN;
1861
Nikolay Borisove07222c2017-01-10 20:35:37 +02001862 delayed_node = btrfs_get_or_create_delayed_node(inode);
Miao Xie67de1172013-12-26 13:07:06 +08001863 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 Mahoney0b246af2016-06-22 18:54:23 -04001886 atomic_inc(&fs_info->delayed_root->items);
Miao Xie67de1172013-12-26 13:07:06 +08001887release_node:
1888 mutex_unlock(&delayed_node->mutex);
1889 btrfs_release_delayed_node(delayed_node);
1890 return 0;
1891}
1892
Miao Xie16cdcec2011-04-22 18:12:22 +08001893static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1894{
1895 struct btrfs_root *root = delayed_node->root;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001896 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xie16cdcec2011-04-22 18:12:22 +08001897 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 Wenruo4f5427c2017-12-12 15:34:33 +08001902 btrfs_delayed_item_release_metadata(root, curr_item);
Miao Xie16cdcec2011-04-22 18:12:22 +08001903 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 Wenruo4f5427c2017-12-12 15:34:33 +08001910 btrfs_delayed_item_release_metadata(root, curr_item);
Miao Xie16cdcec2011-04-22 18:12:22 +08001911 prev_item = curr_item;
1912 curr_item = __btrfs_next_delayed_item(prev_item);
1913 btrfs_release_delayed_item(prev_item);
1914 }
1915
Miao Xie67de1172013-12-26 13:07:06 +08001916 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags))
1917 btrfs_release_delayed_iref(delayed_node);
1918
Miao Xie7cf35d92013-12-26 13:07:05 +08001919 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
Qu Wenruo4f5427c2017-12-12 15:34:33 +08001920 btrfs_delayed_inode_release_metadata(fs_info, delayed_node, false);
Miao Xie16cdcec2011-04-22 18:12:22 +08001921 btrfs_release_delayed_inode(delayed_node);
1922 }
1923 mutex_unlock(&delayed_node->mutex);
1924}
1925
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02001926void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode)
Miao Xie16cdcec2011-04-22 18:12:22 +08001927{
1928 struct btrfs_delayed_node *delayed_node;
1929
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02001930 delayed_node = btrfs_get_delayed_node(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08001931 if (!delayed_node)
1932 return;
1933
1934 __btrfs_kill_delayed_node(delayed_node);
1935 btrfs_release_delayed_node(delayed_node);
1936}
1937
1938void 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 Bacikeda96b22019-09-26 08:29:32 -04001955 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 Xie16cdcec2011-04-22 18:12:22 +08001963 spin_unlock(&root->inode_lock);
1964
1965 for (i = 0; i < n; i++) {
Josef Bacikeda96b22019-09-26 08:29:32 -04001966 if (!delayed_nodes[i])
1967 continue;
Miao Xie16cdcec2011-04-22 18:12:22 +08001968 __btrfs_kill_delayed_node(delayed_nodes[i]);
1969 btrfs_release_delayed_node(delayed_nodes[i]);
1970 }
1971 }
1972}
Miao Xie67cde342012-06-14 02:23:22 -06001973
Jeff Mahoneyccdf9b32016-06-22 18:54:23 -04001974void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info)
Miao Xie67cde342012-06-14 02:23:22 -06001975{
Miao Xie67cde342012-06-14 02:23:22 -06001976 struct btrfs_delayed_node *curr_node, *prev_node;
1977
Jeff Mahoneyccdf9b32016-06-22 18:54:23 -04001978 curr_node = btrfs_first_delayed_node(fs_info->delayed_root);
Miao Xie67cde342012-06-14 02:23:22 -06001979 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