2 * Copyright (C) 2011 Fujitsu. All rights reserved.
3 * Written by Miao Xie <miaox@cn.fujitsu.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public
7 * License v2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 021110-1307, USA.
20 #include <linux/slab.h>
21 #include "delayed-inode.h"
23 #include "transaction.h"
26 #define BTRFS_DELAYED_WRITEBACK 512
27 #define BTRFS_DELAYED_BACKGROUND 128
28 #define BTRFS_DELAYED_BATCH 16
30 static struct kmem_cache
*delayed_node_cache
;
32 int __init
btrfs_delayed_inode_init(void)
34 delayed_node_cache
= kmem_cache_create("btrfs_delayed_node",
35 sizeof(struct btrfs_delayed_node
),
39 if (!delayed_node_cache
)
44 void btrfs_delayed_inode_exit(void)
46 kmem_cache_destroy(delayed_node_cache
);
49 static inline void btrfs_init_delayed_node(
50 struct btrfs_delayed_node
*delayed_node
,
51 struct btrfs_root
*root
, u64 inode_id
)
53 delayed_node
->root
= root
;
54 delayed_node
->inode_id
= inode_id
;
55 refcount_set(&delayed_node
->refs
, 0);
56 delayed_node
->ins_root
= RB_ROOT
;
57 delayed_node
->del_root
= RB_ROOT
;
58 mutex_init(&delayed_node
->mutex
);
59 INIT_LIST_HEAD(&delayed_node
->n_list
);
60 INIT_LIST_HEAD(&delayed_node
->p_list
);
63 static inline int btrfs_is_continuous_delayed_item(
64 struct btrfs_delayed_item
*item1
,
65 struct btrfs_delayed_item
*item2
)
67 if (item1
->key
.type
== BTRFS_DIR_INDEX_KEY
&&
68 item1
->key
.objectid
== item2
->key
.objectid
&&
69 item1
->key
.type
== item2
->key
.type
&&
70 item1
->key
.offset
+ 1 == item2
->key
.offset
)
75 static struct btrfs_delayed_node
*btrfs_get_delayed_node(
76 struct btrfs_inode
*btrfs_inode
)
78 struct btrfs_root
*root
= btrfs_inode
->root
;
79 u64 ino
= btrfs_ino(btrfs_inode
);
80 struct btrfs_delayed_node
*node
;
82 node
= READ_ONCE(btrfs_inode
->delayed_node
);
84 refcount_inc(&node
->refs
);
88 spin_lock(&root
->inode_lock
);
89 node
= radix_tree_lookup(&root
->delayed_nodes_tree
, ino
);
91 if (btrfs_inode
->delayed_node
) {
92 refcount_inc(&node
->refs
); /* can be accessed */
93 BUG_ON(btrfs_inode
->delayed_node
!= node
);
94 spin_unlock(&root
->inode_lock
);
97 btrfs_inode
->delayed_node
= node
;
98 /* can be accessed and cached in the inode */
99 refcount_add(2, &node
->refs
);
100 spin_unlock(&root
->inode_lock
);
103 spin_unlock(&root
->inode_lock
);
108 /* Will return either the node or PTR_ERR(-ENOMEM) */
109 static struct btrfs_delayed_node
*btrfs_get_or_create_delayed_node(
110 struct btrfs_inode
*btrfs_inode
)
112 struct btrfs_delayed_node
*node
;
113 struct btrfs_root
*root
= btrfs_inode
->root
;
114 u64 ino
= btrfs_ino(btrfs_inode
);
118 node
= btrfs_get_delayed_node(btrfs_inode
);
122 node
= kmem_cache_zalloc(delayed_node_cache
, GFP_NOFS
);
124 return ERR_PTR(-ENOMEM
);
125 btrfs_init_delayed_node(node
, root
, ino
);
127 /* cached in the btrfs inode and can be accessed */
128 refcount_set(&node
->refs
, 2);
130 ret
= radix_tree_preload(GFP_NOFS
);
132 kmem_cache_free(delayed_node_cache
, node
);
136 spin_lock(&root
->inode_lock
);
137 ret
= radix_tree_insert(&root
->delayed_nodes_tree
, ino
, node
);
138 if (ret
== -EEXIST
) {
139 spin_unlock(&root
->inode_lock
);
140 kmem_cache_free(delayed_node_cache
, node
);
141 radix_tree_preload_end();
144 btrfs_inode
->delayed_node
= node
;
145 spin_unlock(&root
->inode_lock
);
146 radix_tree_preload_end();
152 * Call it when holding delayed_node->mutex
154 * If mod = 1, add this node into the prepared list.
156 static void btrfs_queue_delayed_node(struct btrfs_delayed_root
*root
,
157 struct btrfs_delayed_node
*node
,
160 spin_lock(&root
->lock
);
161 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST
, &node
->flags
)) {
162 if (!list_empty(&node
->p_list
))
163 list_move_tail(&node
->p_list
, &root
->prepare_list
);
165 list_add_tail(&node
->p_list
, &root
->prepare_list
);
167 list_add_tail(&node
->n_list
, &root
->node_list
);
168 list_add_tail(&node
->p_list
, &root
->prepare_list
);
169 refcount_inc(&node
->refs
); /* inserted into list */
171 set_bit(BTRFS_DELAYED_NODE_IN_LIST
, &node
->flags
);
173 spin_unlock(&root
->lock
);
176 /* Call it when holding delayed_node->mutex */
177 static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root
*root
,
178 struct btrfs_delayed_node
*node
)
180 spin_lock(&root
->lock
);
181 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST
, &node
->flags
)) {
183 refcount_dec(&node
->refs
); /* not in the list */
184 list_del_init(&node
->n_list
);
185 if (!list_empty(&node
->p_list
))
186 list_del_init(&node
->p_list
);
187 clear_bit(BTRFS_DELAYED_NODE_IN_LIST
, &node
->flags
);
189 spin_unlock(&root
->lock
);
192 static struct btrfs_delayed_node
*btrfs_first_delayed_node(
193 struct btrfs_delayed_root
*delayed_root
)
196 struct btrfs_delayed_node
*node
= NULL
;
198 spin_lock(&delayed_root
->lock
);
199 if (list_empty(&delayed_root
->node_list
))
202 p
= delayed_root
->node_list
.next
;
203 node
= list_entry(p
, struct btrfs_delayed_node
, n_list
);
204 refcount_inc(&node
->refs
);
206 spin_unlock(&delayed_root
->lock
);
211 static struct btrfs_delayed_node
*btrfs_next_delayed_node(
212 struct btrfs_delayed_node
*node
)
214 struct btrfs_delayed_root
*delayed_root
;
216 struct btrfs_delayed_node
*next
= NULL
;
218 delayed_root
= node
->root
->fs_info
->delayed_root
;
219 spin_lock(&delayed_root
->lock
);
220 if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST
, &node
->flags
)) {
221 /* not in the list */
222 if (list_empty(&delayed_root
->node_list
))
224 p
= delayed_root
->node_list
.next
;
225 } else if (list_is_last(&node
->n_list
, &delayed_root
->node_list
))
228 p
= node
->n_list
.next
;
230 next
= list_entry(p
, struct btrfs_delayed_node
, n_list
);
231 refcount_inc(&next
->refs
);
233 spin_unlock(&delayed_root
->lock
);
238 static void __btrfs_release_delayed_node(
239 struct btrfs_delayed_node
*delayed_node
,
242 struct btrfs_delayed_root
*delayed_root
;
247 delayed_root
= delayed_node
->root
->fs_info
->delayed_root
;
249 mutex_lock(&delayed_node
->mutex
);
250 if (delayed_node
->count
)
251 btrfs_queue_delayed_node(delayed_root
, delayed_node
, mod
);
253 btrfs_dequeue_delayed_node(delayed_root
, delayed_node
);
254 mutex_unlock(&delayed_node
->mutex
);
256 if (refcount_dec_and_test(&delayed_node
->refs
)) {
258 struct btrfs_root
*root
= delayed_node
->root
;
259 spin_lock(&root
->inode_lock
);
260 if (refcount_read(&delayed_node
->refs
) == 0) {
261 radix_tree_delete(&root
->delayed_nodes_tree
,
262 delayed_node
->inode_id
);
265 spin_unlock(&root
->inode_lock
);
267 kmem_cache_free(delayed_node_cache
, delayed_node
);
271 static inline void btrfs_release_delayed_node(struct btrfs_delayed_node
*node
)
273 __btrfs_release_delayed_node(node
, 0);
276 static struct btrfs_delayed_node
*btrfs_first_prepared_delayed_node(
277 struct btrfs_delayed_root
*delayed_root
)
280 struct btrfs_delayed_node
*node
= NULL
;
282 spin_lock(&delayed_root
->lock
);
283 if (list_empty(&delayed_root
->prepare_list
))
286 p
= delayed_root
->prepare_list
.next
;
288 node
= list_entry(p
, struct btrfs_delayed_node
, p_list
);
289 refcount_inc(&node
->refs
);
291 spin_unlock(&delayed_root
->lock
);
296 static inline void btrfs_release_prepared_delayed_node(
297 struct btrfs_delayed_node
*node
)
299 __btrfs_release_delayed_node(node
, 1);
302 static struct btrfs_delayed_item
*btrfs_alloc_delayed_item(u32 data_len
)
304 struct btrfs_delayed_item
*item
;
305 item
= kmalloc(sizeof(*item
) + data_len
, GFP_NOFS
);
307 item
->data_len
= data_len
;
308 item
->ins_or_del
= 0;
309 item
->bytes_reserved
= 0;
310 item
->delayed_node
= NULL
;
311 refcount_set(&item
->refs
, 1);
317 * __btrfs_lookup_delayed_item - look up the delayed item by key
318 * @delayed_node: pointer to the delayed node
319 * @key: the key to look up
320 * @prev: used to store the prev item if the right item isn't found
321 * @next: used to store the next item if the right item isn't found
323 * Note: if we don't find the right item, we will return the prev item and
326 static struct btrfs_delayed_item
*__btrfs_lookup_delayed_item(
327 struct rb_root
*root
,
328 struct btrfs_key
*key
,
329 struct btrfs_delayed_item
**prev
,
330 struct btrfs_delayed_item
**next
)
332 struct rb_node
*node
, *prev_node
= NULL
;
333 struct btrfs_delayed_item
*delayed_item
= NULL
;
336 node
= root
->rb_node
;
339 delayed_item
= rb_entry(node
, struct btrfs_delayed_item
,
342 ret
= btrfs_comp_cpu_keys(&delayed_item
->key
, key
);
344 node
= node
->rb_right
;
346 node
= node
->rb_left
;
355 *prev
= delayed_item
;
356 else if ((node
= rb_prev(prev_node
)) != NULL
) {
357 *prev
= rb_entry(node
, struct btrfs_delayed_item
,
367 *next
= delayed_item
;
368 else if ((node
= rb_next(prev_node
)) != NULL
) {
369 *next
= rb_entry(node
, struct btrfs_delayed_item
,
377 static struct btrfs_delayed_item
*__btrfs_lookup_delayed_insertion_item(
378 struct btrfs_delayed_node
*delayed_node
,
379 struct btrfs_key
*key
)
381 return __btrfs_lookup_delayed_item(&delayed_node
->ins_root
, key
,
385 static int __btrfs_add_delayed_item(struct btrfs_delayed_node
*delayed_node
,
386 struct btrfs_delayed_item
*ins
,
389 struct rb_node
**p
, *node
;
390 struct rb_node
*parent_node
= NULL
;
391 struct rb_root
*root
;
392 struct btrfs_delayed_item
*item
;
395 if (action
== BTRFS_DELAYED_INSERTION_ITEM
)
396 root
= &delayed_node
->ins_root
;
397 else if (action
== BTRFS_DELAYED_DELETION_ITEM
)
398 root
= &delayed_node
->del_root
;
402 node
= &ins
->rb_node
;
406 item
= rb_entry(parent_node
, struct btrfs_delayed_item
,
409 cmp
= btrfs_comp_cpu_keys(&item
->key
, &ins
->key
);
418 rb_link_node(node
, parent_node
, p
);
419 rb_insert_color(node
, root
);
420 ins
->delayed_node
= delayed_node
;
421 ins
->ins_or_del
= action
;
423 if (ins
->key
.type
== BTRFS_DIR_INDEX_KEY
&&
424 action
== BTRFS_DELAYED_INSERTION_ITEM
&&
425 ins
->key
.offset
>= delayed_node
->index_cnt
)
426 delayed_node
->index_cnt
= ins
->key
.offset
+ 1;
428 delayed_node
->count
++;
429 atomic_inc(&delayed_node
->root
->fs_info
->delayed_root
->items
);
433 static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node
*node
,
434 struct btrfs_delayed_item
*item
)
436 return __btrfs_add_delayed_item(node
, item
,
437 BTRFS_DELAYED_INSERTION_ITEM
);
440 static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node
*node
,
441 struct btrfs_delayed_item
*item
)
443 return __btrfs_add_delayed_item(node
, item
,
444 BTRFS_DELAYED_DELETION_ITEM
);
447 static void finish_one_item(struct btrfs_delayed_root
*delayed_root
)
449 int seq
= atomic_inc_return(&delayed_root
->items_seq
);
452 * atomic_dec_return implies a barrier for waitqueue_active
454 if ((atomic_dec_return(&delayed_root
->items
) <
455 BTRFS_DELAYED_BACKGROUND
|| seq
% BTRFS_DELAYED_BATCH
== 0) &&
456 waitqueue_active(&delayed_root
->wait
))
457 wake_up(&delayed_root
->wait
);
460 static void __btrfs_remove_delayed_item(struct btrfs_delayed_item
*delayed_item
)
462 struct rb_root
*root
;
463 struct btrfs_delayed_root
*delayed_root
;
465 delayed_root
= delayed_item
->delayed_node
->root
->fs_info
->delayed_root
;
467 BUG_ON(!delayed_root
);
468 BUG_ON(delayed_item
->ins_or_del
!= BTRFS_DELAYED_DELETION_ITEM
&&
469 delayed_item
->ins_or_del
!= BTRFS_DELAYED_INSERTION_ITEM
);
471 if (delayed_item
->ins_or_del
== BTRFS_DELAYED_INSERTION_ITEM
)
472 root
= &delayed_item
->delayed_node
->ins_root
;
474 root
= &delayed_item
->delayed_node
->del_root
;
476 rb_erase(&delayed_item
->rb_node
, root
);
477 delayed_item
->delayed_node
->count
--;
479 finish_one_item(delayed_root
);
482 static void btrfs_release_delayed_item(struct btrfs_delayed_item
*item
)
485 __btrfs_remove_delayed_item(item
);
486 if (refcount_dec_and_test(&item
->refs
))
491 static struct btrfs_delayed_item
*__btrfs_first_delayed_insertion_item(
492 struct btrfs_delayed_node
*delayed_node
)
495 struct btrfs_delayed_item
*item
= NULL
;
497 p
= rb_first(&delayed_node
->ins_root
);
499 item
= rb_entry(p
, struct btrfs_delayed_item
, rb_node
);
504 static struct btrfs_delayed_item
*__btrfs_first_delayed_deletion_item(
505 struct btrfs_delayed_node
*delayed_node
)
508 struct btrfs_delayed_item
*item
= NULL
;
510 p
= rb_first(&delayed_node
->del_root
);
512 item
= rb_entry(p
, struct btrfs_delayed_item
, rb_node
);
517 static struct btrfs_delayed_item
*__btrfs_next_delayed_item(
518 struct btrfs_delayed_item
*item
)
521 struct btrfs_delayed_item
*next
= NULL
;
523 p
= rb_next(&item
->rb_node
);
525 next
= rb_entry(p
, struct btrfs_delayed_item
, rb_node
);
530 static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle
*trans
,
531 struct btrfs_fs_info
*fs_info
,
532 struct btrfs_delayed_item
*item
)
534 struct btrfs_block_rsv
*src_rsv
;
535 struct btrfs_block_rsv
*dst_rsv
;
539 if (!trans
->bytes_reserved
)
542 src_rsv
= trans
->block_rsv
;
543 dst_rsv
= &fs_info
->delayed_block_rsv
;
545 num_bytes
= btrfs_calc_trans_metadata_size(fs_info
, 1);
546 ret
= btrfs_block_rsv_migrate(src_rsv
, dst_rsv
, num_bytes
, 1);
548 trace_btrfs_space_reservation(fs_info
, "delayed_item",
551 item
->bytes_reserved
= num_bytes
;
557 static void btrfs_delayed_item_release_metadata(struct btrfs_fs_info
*fs_info
,
558 struct btrfs_delayed_item
*item
)
560 struct btrfs_block_rsv
*rsv
;
562 if (!item
->bytes_reserved
)
565 rsv
= &fs_info
->delayed_block_rsv
;
566 trace_btrfs_space_reservation(fs_info
, "delayed_item",
567 item
->key
.objectid
, item
->bytes_reserved
,
569 btrfs_block_rsv_release(fs_info
, rsv
,
570 item
->bytes_reserved
);
573 static int btrfs_delayed_inode_reserve_metadata(
574 struct btrfs_trans_handle
*trans
,
575 struct btrfs_root
*root
,
576 struct btrfs_inode
*inode
,
577 struct btrfs_delayed_node
*node
)
579 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
580 struct btrfs_block_rsv
*src_rsv
;
581 struct btrfs_block_rsv
*dst_rsv
;
585 src_rsv
= trans
->block_rsv
;
586 dst_rsv
= &fs_info
->delayed_block_rsv
;
588 num_bytes
= btrfs_calc_trans_metadata_size(fs_info
, 1);
591 * btrfs_dirty_inode will update the inode under btrfs_join_transaction
592 * which doesn't reserve space for speed. This is a problem since we
593 * still need to reserve space for this update, so try to reserve the
596 * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
597 * we always reserve enough to update the inode item.
599 if (!src_rsv
|| (!trans
->bytes_reserved
&&
600 src_rsv
->type
!= BTRFS_BLOCK_RSV_DELALLOC
)) {
601 ret
= btrfs_block_rsv_add(root
, dst_rsv
, num_bytes
,
602 BTRFS_RESERVE_NO_FLUSH
);
604 * Since we're under a transaction reserve_metadata_bytes could
605 * try to commit the transaction which will make it return
606 * EAGAIN to make us stop the transaction we have, so return
607 * ENOSPC instead so that btrfs_dirty_inode knows what to do.
612 node
->bytes_reserved
= num_bytes
;
613 trace_btrfs_space_reservation(fs_info
,
621 ret
= btrfs_block_rsv_migrate(src_rsv
, dst_rsv
, num_bytes
, 1);
623 trace_btrfs_space_reservation(fs_info
, "delayed_inode",
624 btrfs_ino(inode
), num_bytes
, 1);
625 node
->bytes_reserved
= num_bytes
;
631 static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info
*fs_info
,
632 struct btrfs_delayed_node
*node
)
634 struct btrfs_block_rsv
*rsv
;
636 if (!node
->bytes_reserved
)
639 rsv
= &fs_info
->delayed_block_rsv
;
640 trace_btrfs_space_reservation(fs_info
, "delayed_inode",
641 node
->inode_id
, node
->bytes_reserved
, 0);
642 btrfs_block_rsv_release(fs_info
, rsv
,
643 node
->bytes_reserved
);
644 node
->bytes_reserved
= 0;
648 * This helper will insert some continuous items into the same leaf according
649 * to the free space of the leaf.
651 static int btrfs_batch_insert_items(struct btrfs_root
*root
,
652 struct btrfs_path
*path
,
653 struct btrfs_delayed_item
*item
)
655 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
656 struct btrfs_delayed_item
*curr
, *next
;
658 int total_data_size
= 0, total_size
= 0;
659 struct extent_buffer
*leaf
;
661 struct btrfs_key
*keys
;
663 struct list_head head
;
669 BUG_ON(!path
->nodes
[0]);
671 leaf
= path
->nodes
[0];
672 free_space
= btrfs_leaf_free_space(fs_info
, leaf
);
673 INIT_LIST_HEAD(&head
);
679 * count the number of the continuous items that we can insert in batch
681 while (total_size
+ next
->data_len
+ sizeof(struct btrfs_item
) <=
683 total_data_size
+= next
->data_len
;
684 total_size
+= next
->data_len
+ sizeof(struct btrfs_item
);
685 list_add_tail(&next
->tree_list
, &head
);
689 next
= __btrfs_next_delayed_item(curr
);
693 if (!btrfs_is_continuous_delayed_item(curr
, next
))
703 * we need allocate some memory space, but it might cause the task
704 * to sleep, so we set all locked nodes in the path to blocking locks
707 btrfs_set_path_blocking(path
);
709 keys
= kmalloc_array(nitems
, sizeof(struct btrfs_key
), GFP_NOFS
);
715 data_size
= kmalloc_array(nitems
, sizeof(u32
), GFP_NOFS
);
721 /* get keys of all the delayed items */
723 list_for_each_entry(next
, &head
, tree_list
) {
725 data_size
[i
] = next
->data_len
;
729 /* reset all the locked nodes in the patch to spinning locks. */
730 btrfs_clear_path_blocking(path
, NULL
, 0);
732 /* insert the keys of the items */
733 setup_items_for_insert(root
, path
, keys
, data_size
,
734 total_data_size
, total_size
, nitems
);
736 /* insert the dir index items */
737 slot
= path
->slots
[0];
738 list_for_each_entry_safe(curr
, next
, &head
, tree_list
) {
739 data_ptr
= btrfs_item_ptr(leaf
, slot
, char);
740 write_extent_buffer(leaf
, &curr
->data
,
741 (unsigned long)data_ptr
,
745 btrfs_delayed_item_release_metadata(fs_info
, curr
);
747 list_del(&curr
->tree_list
);
748 btrfs_release_delayed_item(curr
);
759 * This helper can just do simple insertion that needn't extend item for new
760 * data, such as directory name index insertion, inode insertion.
762 static int btrfs_insert_delayed_item(struct btrfs_trans_handle
*trans
,
763 struct btrfs_root
*root
,
764 struct btrfs_path
*path
,
765 struct btrfs_delayed_item
*delayed_item
)
767 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
768 struct extent_buffer
*leaf
;
772 ret
= btrfs_insert_empty_item(trans
, root
, path
, &delayed_item
->key
,
773 delayed_item
->data_len
);
774 if (ret
< 0 && ret
!= -EEXIST
)
777 leaf
= path
->nodes
[0];
779 ptr
= btrfs_item_ptr(leaf
, path
->slots
[0], char);
781 write_extent_buffer(leaf
, delayed_item
->data
, (unsigned long)ptr
,
782 delayed_item
->data_len
);
783 btrfs_mark_buffer_dirty(leaf
);
785 btrfs_delayed_item_release_metadata(fs_info
, delayed_item
);
790 * we insert an item first, then if there are some continuous items, we try
791 * to insert those items into the same leaf.
793 static int btrfs_insert_delayed_items(struct btrfs_trans_handle
*trans
,
794 struct btrfs_path
*path
,
795 struct btrfs_root
*root
,
796 struct btrfs_delayed_node
*node
)
798 struct btrfs_delayed_item
*curr
, *prev
;
802 mutex_lock(&node
->mutex
);
803 curr
= __btrfs_first_delayed_insertion_item(node
);
807 ret
= btrfs_insert_delayed_item(trans
, root
, path
, curr
);
809 btrfs_release_path(path
);
814 curr
= __btrfs_next_delayed_item(prev
);
815 if (curr
&& btrfs_is_continuous_delayed_item(prev
, curr
)) {
816 /* insert the continuous items into the same leaf */
818 btrfs_batch_insert_items(root
, path
, curr
);
820 btrfs_release_delayed_item(prev
);
821 btrfs_mark_buffer_dirty(path
->nodes
[0]);
823 btrfs_release_path(path
);
824 mutex_unlock(&node
->mutex
);
828 mutex_unlock(&node
->mutex
);
832 static int btrfs_batch_delete_items(struct btrfs_trans_handle
*trans
,
833 struct btrfs_root
*root
,
834 struct btrfs_path
*path
,
835 struct btrfs_delayed_item
*item
)
837 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
838 struct btrfs_delayed_item
*curr
, *next
;
839 struct extent_buffer
*leaf
;
840 struct btrfs_key key
;
841 struct list_head head
;
842 int nitems
, i
, last_item
;
845 BUG_ON(!path
->nodes
[0]);
847 leaf
= path
->nodes
[0];
850 last_item
= btrfs_header_nritems(leaf
) - 1;
852 return -ENOENT
; /* FIXME: Is errno suitable? */
855 INIT_LIST_HEAD(&head
);
856 btrfs_item_key_to_cpu(leaf
, &key
, i
);
859 * count the number of the dir index items that we can delete in batch
861 while (btrfs_comp_cpu_keys(&next
->key
, &key
) == 0) {
862 list_add_tail(&next
->tree_list
, &head
);
866 next
= __btrfs_next_delayed_item(curr
);
870 if (!btrfs_is_continuous_delayed_item(curr
, next
))
876 btrfs_item_key_to_cpu(leaf
, &key
, i
);
882 ret
= btrfs_del_items(trans
, root
, path
, path
->slots
[0], nitems
);
886 list_for_each_entry_safe(curr
, next
, &head
, tree_list
) {
887 btrfs_delayed_item_release_metadata(fs_info
, curr
);
888 list_del(&curr
->tree_list
);
889 btrfs_release_delayed_item(curr
);
896 static int btrfs_delete_delayed_items(struct btrfs_trans_handle
*trans
,
897 struct btrfs_path
*path
,
898 struct btrfs_root
*root
,
899 struct btrfs_delayed_node
*node
)
901 struct btrfs_delayed_item
*curr
, *prev
;
905 mutex_lock(&node
->mutex
);
906 curr
= __btrfs_first_delayed_deletion_item(node
);
910 ret
= btrfs_search_slot(trans
, root
, &curr
->key
, path
, -1, 1);
915 * can't find the item which the node points to, so this node
916 * is invalid, just drop it.
919 curr
= __btrfs_next_delayed_item(prev
);
920 btrfs_release_delayed_item(prev
);
922 btrfs_release_path(path
);
924 mutex_unlock(&node
->mutex
);
930 btrfs_batch_delete_items(trans
, root
, path
, curr
);
931 btrfs_release_path(path
);
932 mutex_unlock(&node
->mutex
);
936 btrfs_release_path(path
);
937 mutex_unlock(&node
->mutex
);
941 static void btrfs_release_delayed_inode(struct btrfs_delayed_node
*delayed_node
)
943 struct btrfs_delayed_root
*delayed_root
;
946 test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
)) {
947 BUG_ON(!delayed_node
->root
);
948 clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
);
949 delayed_node
->count
--;
951 delayed_root
= delayed_node
->root
->fs_info
->delayed_root
;
952 finish_one_item(delayed_root
);
956 static void btrfs_release_delayed_iref(struct btrfs_delayed_node
*delayed_node
)
958 struct btrfs_delayed_root
*delayed_root
;
960 ASSERT(delayed_node
->root
);
961 clear_bit(BTRFS_DELAYED_NODE_DEL_IREF
, &delayed_node
->flags
);
962 delayed_node
->count
--;
964 delayed_root
= delayed_node
->root
->fs_info
->delayed_root
;
965 finish_one_item(delayed_root
);
968 static int __btrfs_update_delayed_inode(struct btrfs_trans_handle
*trans
,
969 struct btrfs_root
*root
,
970 struct btrfs_path
*path
,
971 struct btrfs_delayed_node
*node
)
973 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
974 struct btrfs_key key
;
975 struct btrfs_inode_item
*inode_item
;
976 struct extent_buffer
*leaf
;
980 key
.objectid
= node
->inode_id
;
981 key
.type
= BTRFS_INODE_ITEM_KEY
;
984 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF
, &node
->flags
))
989 ret
= btrfs_lookup_inode(trans
, root
, path
, &key
, mod
);
991 btrfs_release_path(path
);
993 } else if (ret
< 0) {
997 leaf
= path
->nodes
[0];
998 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
999 struct btrfs_inode_item
);
1000 write_extent_buffer(leaf
, &node
->inode_item
, (unsigned long)inode_item
,
1001 sizeof(struct btrfs_inode_item
));
1002 btrfs_mark_buffer_dirty(leaf
);
1004 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF
, &node
->flags
))
1008 if (path
->slots
[0] >= btrfs_header_nritems(leaf
))
1011 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1012 if (key
.objectid
!= node
->inode_id
)
1015 if (key
.type
!= BTRFS_INODE_REF_KEY
&&
1016 key
.type
!= BTRFS_INODE_EXTREF_KEY
)
1020 * Delayed iref deletion is for the inode who has only one link,
1021 * so there is only one iref. The case that several irefs are
1022 * in the same item doesn't exist.
1024 btrfs_del_item(trans
, root
, path
);
1026 btrfs_release_delayed_iref(node
);
1028 btrfs_release_path(path
);
1030 btrfs_delayed_inode_release_metadata(fs_info
, node
);
1031 btrfs_release_delayed_inode(node
);
1036 btrfs_release_path(path
);
1038 key
.type
= BTRFS_INODE_EXTREF_KEY
;
1040 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1046 leaf
= path
->nodes
[0];
1051 static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle
*trans
,
1052 struct btrfs_root
*root
,
1053 struct btrfs_path
*path
,
1054 struct btrfs_delayed_node
*node
)
1058 mutex_lock(&node
->mutex
);
1059 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &node
->flags
)) {
1060 mutex_unlock(&node
->mutex
);
1064 ret
= __btrfs_update_delayed_inode(trans
, root
, path
, node
);
1065 mutex_unlock(&node
->mutex
);
1070 __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle
*trans
,
1071 struct btrfs_path
*path
,
1072 struct btrfs_delayed_node
*node
)
1076 ret
= btrfs_insert_delayed_items(trans
, path
, node
->root
, node
);
1080 ret
= btrfs_delete_delayed_items(trans
, path
, node
->root
, node
);
1084 ret
= btrfs_update_delayed_inode(trans
, node
->root
, path
, node
);
1089 * Called when committing the transaction.
1090 * Returns 0 on success.
1091 * Returns < 0 on error and returns with an aborted transaction with any
1092 * outstanding delayed items cleaned up.
1094 static int __btrfs_run_delayed_items(struct btrfs_trans_handle
*trans
,
1095 struct btrfs_fs_info
*fs_info
, int nr
)
1097 struct btrfs_delayed_root
*delayed_root
;
1098 struct btrfs_delayed_node
*curr_node
, *prev_node
;
1099 struct btrfs_path
*path
;
1100 struct btrfs_block_rsv
*block_rsv
;
1102 bool count
= (nr
> 0);
1107 path
= btrfs_alloc_path();
1110 path
->leave_spinning
= 1;
1112 block_rsv
= trans
->block_rsv
;
1113 trans
->block_rsv
= &fs_info
->delayed_block_rsv
;
1115 delayed_root
= fs_info
->delayed_root
;
1117 curr_node
= btrfs_first_delayed_node(delayed_root
);
1118 while (curr_node
&& (!count
|| (count
&& nr
--))) {
1119 ret
= __btrfs_commit_inode_delayed_items(trans
, path
,
1122 btrfs_release_delayed_node(curr_node
);
1124 btrfs_abort_transaction(trans
, ret
);
1128 prev_node
= curr_node
;
1129 curr_node
= btrfs_next_delayed_node(curr_node
);
1130 btrfs_release_delayed_node(prev_node
);
1134 btrfs_release_delayed_node(curr_node
);
1135 btrfs_free_path(path
);
1136 trans
->block_rsv
= block_rsv
;
1141 int btrfs_run_delayed_items(struct btrfs_trans_handle
*trans
,
1142 struct btrfs_fs_info
*fs_info
)
1144 return __btrfs_run_delayed_items(trans
, fs_info
, -1);
1147 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle
*trans
,
1148 struct btrfs_fs_info
*fs_info
, int nr
)
1150 return __btrfs_run_delayed_items(trans
, fs_info
, nr
);
1153 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle
*trans
,
1154 struct btrfs_inode
*inode
)
1156 struct btrfs_delayed_node
*delayed_node
= btrfs_get_delayed_node(inode
);
1157 struct btrfs_path
*path
;
1158 struct btrfs_block_rsv
*block_rsv
;
1164 mutex_lock(&delayed_node
->mutex
);
1165 if (!delayed_node
->count
) {
1166 mutex_unlock(&delayed_node
->mutex
);
1167 btrfs_release_delayed_node(delayed_node
);
1170 mutex_unlock(&delayed_node
->mutex
);
1172 path
= btrfs_alloc_path();
1174 btrfs_release_delayed_node(delayed_node
);
1177 path
->leave_spinning
= 1;
1179 block_rsv
= trans
->block_rsv
;
1180 trans
->block_rsv
= &delayed_node
->root
->fs_info
->delayed_block_rsv
;
1182 ret
= __btrfs_commit_inode_delayed_items(trans
, path
, delayed_node
);
1184 btrfs_release_delayed_node(delayed_node
);
1185 btrfs_free_path(path
);
1186 trans
->block_rsv
= block_rsv
;
1191 int btrfs_commit_inode_delayed_inode(struct btrfs_inode
*inode
)
1193 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->vfs_inode
.i_sb
);
1194 struct btrfs_trans_handle
*trans
;
1195 struct btrfs_delayed_node
*delayed_node
= btrfs_get_delayed_node(inode
);
1196 struct btrfs_path
*path
;
1197 struct btrfs_block_rsv
*block_rsv
;
1203 mutex_lock(&delayed_node
->mutex
);
1204 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
)) {
1205 mutex_unlock(&delayed_node
->mutex
);
1206 btrfs_release_delayed_node(delayed_node
);
1209 mutex_unlock(&delayed_node
->mutex
);
1211 trans
= btrfs_join_transaction(delayed_node
->root
);
1212 if (IS_ERR(trans
)) {
1213 ret
= PTR_ERR(trans
);
1217 path
= btrfs_alloc_path();
1222 path
->leave_spinning
= 1;
1224 block_rsv
= trans
->block_rsv
;
1225 trans
->block_rsv
= &fs_info
->delayed_block_rsv
;
1227 mutex_lock(&delayed_node
->mutex
);
1228 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
))
1229 ret
= __btrfs_update_delayed_inode(trans
, delayed_node
->root
,
1230 path
, delayed_node
);
1233 mutex_unlock(&delayed_node
->mutex
);
1235 btrfs_free_path(path
);
1236 trans
->block_rsv
= block_rsv
;
1238 btrfs_end_transaction(trans
);
1239 btrfs_btree_balance_dirty(fs_info
);
1241 btrfs_release_delayed_node(delayed_node
);
1246 void btrfs_remove_delayed_node(struct btrfs_inode
*inode
)
1248 struct btrfs_delayed_node
*delayed_node
;
1250 delayed_node
= READ_ONCE(inode
->delayed_node
);
1254 inode
->delayed_node
= NULL
;
1255 btrfs_release_delayed_node(delayed_node
);
1258 struct btrfs_async_delayed_work
{
1259 struct btrfs_delayed_root
*delayed_root
;
1261 struct btrfs_work work
;
1264 static void btrfs_async_run_delayed_root(struct btrfs_work
*work
)
1266 struct btrfs_async_delayed_work
*async_work
;
1267 struct btrfs_delayed_root
*delayed_root
;
1268 struct btrfs_trans_handle
*trans
;
1269 struct btrfs_path
*path
;
1270 struct btrfs_delayed_node
*delayed_node
= NULL
;
1271 struct btrfs_root
*root
;
1272 struct btrfs_block_rsv
*block_rsv
;
1275 async_work
= container_of(work
, struct btrfs_async_delayed_work
, work
);
1276 delayed_root
= async_work
->delayed_root
;
1278 path
= btrfs_alloc_path();
1283 if (atomic_read(&delayed_root
->items
) < BTRFS_DELAYED_BACKGROUND
/ 2)
1286 delayed_node
= btrfs_first_prepared_delayed_node(delayed_root
);
1290 path
->leave_spinning
= 1;
1291 root
= delayed_node
->root
;
1293 trans
= btrfs_join_transaction(root
);
1297 block_rsv
= trans
->block_rsv
;
1298 trans
->block_rsv
= &root
->fs_info
->delayed_block_rsv
;
1300 __btrfs_commit_inode_delayed_items(trans
, path
, delayed_node
);
1302 trans
->block_rsv
= block_rsv
;
1303 btrfs_end_transaction(trans
);
1304 btrfs_btree_balance_dirty_nodelay(root
->fs_info
);
1307 btrfs_release_path(path
);
1310 btrfs_release_prepared_delayed_node(delayed_node
);
1311 if ((async_work
->nr
== 0 && total_done
< BTRFS_DELAYED_WRITEBACK
) ||
1312 total_done
< async_work
->nr
)
1316 btrfs_free_path(path
);
1318 wake_up(&delayed_root
->wait
);
1323 static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root
*delayed_root
,
1324 struct btrfs_fs_info
*fs_info
, int nr
)
1326 struct btrfs_async_delayed_work
*async_work
;
1328 if (atomic_read(&delayed_root
->items
) < BTRFS_DELAYED_BACKGROUND
||
1329 btrfs_workqueue_normal_congested(fs_info
->delayed_workers
))
1332 async_work
= kmalloc(sizeof(*async_work
), GFP_NOFS
);
1336 async_work
->delayed_root
= delayed_root
;
1337 btrfs_init_work(&async_work
->work
, btrfs_delayed_meta_helper
,
1338 btrfs_async_run_delayed_root
, NULL
, NULL
);
1339 async_work
->nr
= nr
;
1341 btrfs_queue_work(fs_info
->delayed_workers
, &async_work
->work
);
1345 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info
*fs_info
)
1347 WARN_ON(btrfs_first_delayed_node(fs_info
->delayed_root
));
1350 static int could_end_wait(struct btrfs_delayed_root
*delayed_root
, int seq
)
1352 int val
= atomic_read(&delayed_root
->items_seq
);
1354 if (val
< seq
|| val
>= seq
+ BTRFS_DELAYED_BATCH
)
1357 if (atomic_read(&delayed_root
->items
) < BTRFS_DELAYED_BACKGROUND
)
1363 void btrfs_balance_delayed_items(struct btrfs_fs_info
*fs_info
)
1365 struct btrfs_delayed_root
*delayed_root
= fs_info
->delayed_root
;
1367 if (atomic_read(&delayed_root
->items
) < BTRFS_DELAYED_BACKGROUND
)
1370 if (atomic_read(&delayed_root
->items
) >= BTRFS_DELAYED_WRITEBACK
) {
1374 seq
= atomic_read(&delayed_root
->items_seq
);
1376 ret
= btrfs_wq_run_delayed_node(delayed_root
, fs_info
, 0);
1380 wait_event_interruptible(delayed_root
->wait
,
1381 could_end_wait(delayed_root
, seq
));
1385 btrfs_wq_run_delayed_node(delayed_root
, fs_info
, BTRFS_DELAYED_BATCH
);
1388 /* Will return 0 or -ENOMEM */
1389 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle
*trans
,
1390 struct btrfs_fs_info
*fs_info
,
1391 const char *name
, int name_len
,
1392 struct btrfs_inode
*dir
,
1393 struct btrfs_disk_key
*disk_key
, u8 type
,
1396 struct btrfs_delayed_node
*delayed_node
;
1397 struct btrfs_delayed_item
*delayed_item
;
1398 struct btrfs_dir_item
*dir_item
;
1401 delayed_node
= btrfs_get_or_create_delayed_node(dir
);
1402 if (IS_ERR(delayed_node
))
1403 return PTR_ERR(delayed_node
);
1405 delayed_item
= btrfs_alloc_delayed_item(sizeof(*dir_item
) + name_len
);
1406 if (!delayed_item
) {
1411 delayed_item
->key
.objectid
= btrfs_ino(dir
);
1412 delayed_item
->key
.type
= BTRFS_DIR_INDEX_KEY
;
1413 delayed_item
->key
.offset
= index
;
1415 dir_item
= (struct btrfs_dir_item
*)delayed_item
->data
;
1416 dir_item
->location
= *disk_key
;
1417 btrfs_set_stack_dir_transid(dir_item
, trans
->transid
);
1418 btrfs_set_stack_dir_data_len(dir_item
, 0);
1419 btrfs_set_stack_dir_name_len(dir_item
, name_len
);
1420 btrfs_set_stack_dir_type(dir_item
, type
);
1421 memcpy((char *)(dir_item
+ 1), name
, name_len
);
1423 ret
= btrfs_delayed_item_reserve_metadata(trans
, fs_info
, delayed_item
);
1425 * we have reserved enough space when we start a new transaction,
1426 * so reserving metadata failure is impossible
1431 mutex_lock(&delayed_node
->mutex
);
1432 ret
= __btrfs_add_delayed_insertion_item(delayed_node
, delayed_item
);
1433 if (unlikely(ret
)) {
1435 "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1436 name_len
, name
, delayed_node
->root
->objectid
,
1437 delayed_node
->inode_id
, ret
);
1440 mutex_unlock(&delayed_node
->mutex
);
1443 btrfs_release_delayed_node(delayed_node
);
1447 static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info
*fs_info
,
1448 struct btrfs_delayed_node
*node
,
1449 struct btrfs_key
*key
)
1451 struct btrfs_delayed_item
*item
;
1453 mutex_lock(&node
->mutex
);
1454 item
= __btrfs_lookup_delayed_insertion_item(node
, key
);
1456 mutex_unlock(&node
->mutex
);
1460 btrfs_delayed_item_release_metadata(fs_info
, item
);
1461 btrfs_release_delayed_item(item
);
1462 mutex_unlock(&node
->mutex
);
1466 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle
*trans
,
1467 struct btrfs_fs_info
*fs_info
,
1468 struct btrfs_inode
*dir
, u64 index
)
1470 struct btrfs_delayed_node
*node
;
1471 struct btrfs_delayed_item
*item
;
1472 struct btrfs_key item_key
;
1475 node
= btrfs_get_or_create_delayed_node(dir
);
1477 return PTR_ERR(node
);
1479 item_key
.objectid
= btrfs_ino(dir
);
1480 item_key
.type
= BTRFS_DIR_INDEX_KEY
;
1481 item_key
.offset
= index
;
1483 ret
= btrfs_delete_delayed_insertion_item(fs_info
, node
, &item_key
);
1487 item
= btrfs_alloc_delayed_item(0);
1493 item
->key
= item_key
;
1495 ret
= btrfs_delayed_item_reserve_metadata(trans
, fs_info
, item
);
1497 * we have reserved enough space when we start a new transaction,
1498 * so reserving metadata failure is impossible.
1502 mutex_lock(&node
->mutex
);
1503 ret
= __btrfs_add_delayed_deletion_item(node
, item
);
1504 if (unlikely(ret
)) {
1506 "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1507 index
, node
->root
->objectid
, node
->inode_id
, ret
);
1510 mutex_unlock(&node
->mutex
);
1512 btrfs_release_delayed_node(node
);
1516 int btrfs_inode_delayed_dir_index_count(struct btrfs_inode
*inode
)
1518 struct btrfs_delayed_node
*delayed_node
= btrfs_get_delayed_node(inode
);
1524 * Since we have held i_mutex of this directory, it is impossible that
1525 * a new directory index is added into the delayed node and index_cnt
1526 * is updated now. So we needn't lock the delayed node.
1528 if (!delayed_node
->index_cnt
) {
1529 btrfs_release_delayed_node(delayed_node
);
1533 inode
->index_cnt
= delayed_node
->index_cnt
;
1534 btrfs_release_delayed_node(delayed_node
);
1538 bool btrfs_readdir_get_delayed_items(struct inode
*inode
,
1539 struct list_head
*ins_list
,
1540 struct list_head
*del_list
)
1542 struct btrfs_delayed_node
*delayed_node
;
1543 struct btrfs_delayed_item
*item
;
1545 delayed_node
= btrfs_get_delayed_node(BTRFS_I(inode
));
1550 * We can only do one readdir with delayed items at a time because of
1551 * item->readdir_list.
1553 inode_unlock_shared(inode
);
1556 mutex_lock(&delayed_node
->mutex
);
1557 item
= __btrfs_first_delayed_insertion_item(delayed_node
);
1559 refcount_inc(&item
->refs
);
1560 list_add_tail(&item
->readdir_list
, ins_list
);
1561 item
= __btrfs_next_delayed_item(item
);
1564 item
= __btrfs_first_delayed_deletion_item(delayed_node
);
1566 refcount_inc(&item
->refs
);
1567 list_add_tail(&item
->readdir_list
, del_list
);
1568 item
= __btrfs_next_delayed_item(item
);
1570 mutex_unlock(&delayed_node
->mutex
);
1572 * This delayed node is still cached in the btrfs inode, so refs
1573 * must be > 1 now, and we needn't check it is going to be freed
1576 * Besides that, this function is used to read dir, we do not
1577 * insert/delete delayed items in this period. So we also needn't
1578 * requeue or dequeue this delayed node.
1580 refcount_dec(&delayed_node
->refs
);
1585 void btrfs_readdir_put_delayed_items(struct inode
*inode
,
1586 struct list_head
*ins_list
,
1587 struct list_head
*del_list
)
1589 struct btrfs_delayed_item
*curr
, *next
;
1591 list_for_each_entry_safe(curr
, next
, ins_list
, readdir_list
) {
1592 list_del(&curr
->readdir_list
);
1593 if (refcount_dec_and_test(&curr
->refs
))
1597 list_for_each_entry_safe(curr
, next
, del_list
, readdir_list
) {
1598 list_del(&curr
->readdir_list
);
1599 if (refcount_dec_and_test(&curr
->refs
))
1604 * The VFS is going to do up_read(), so we need to downgrade back to a
1607 downgrade_write(&inode
->i_rwsem
);
1610 int btrfs_should_delete_dir_index(struct list_head
*del_list
,
1613 struct btrfs_delayed_item
*curr
, *next
;
1616 if (list_empty(del_list
))
1619 list_for_each_entry_safe(curr
, next
, del_list
, readdir_list
) {
1620 if (curr
->key
.offset
> index
)
1623 list_del(&curr
->readdir_list
);
1624 ret
= (curr
->key
.offset
== index
);
1626 if (refcount_dec_and_test(&curr
->refs
))
1638 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1641 int btrfs_readdir_delayed_dir_index(struct dir_context
*ctx
,
1642 struct list_head
*ins_list
)
1644 struct btrfs_dir_item
*di
;
1645 struct btrfs_delayed_item
*curr
, *next
;
1646 struct btrfs_key location
;
1650 unsigned char d_type
;
1652 if (list_empty(ins_list
))
1656 * Changing the data of the delayed item is impossible. So
1657 * we needn't lock them. And we have held i_mutex of the
1658 * directory, nobody can delete any directory indexes now.
1660 list_for_each_entry_safe(curr
, next
, ins_list
, readdir_list
) {
1661 list_del(&curr
->readdir_list
);
1663 if (curr
->key
.offset
< ctx
->pos
) {
1664 if (refcount_dec_and_test(&curr
->refs
))
1669 ctx
->pos
= curr
->key
.offset
;
1671 di
= (struct btrfs_dir_item
*)curr
->data
;
1672 name
= (char *)(di
+ 1);
1673 name_len
= btrfs_stack_dir_name_len(di
);
1675 d_type
= btrfs_filetype_table
[di
->type
];
1676 btrfs_disk_key_to_cpu(&location
, &di
->location
);
1678 over
= !dir_emit(ctx
, name
, name_len
,
1679 location
.objectid
, d_type
);
1681 if (refcount_dec_and_test(&curr
->refs
))
1691 static void fill_stack_inode_item(struct btrfs_trans_handle
*trans
,
1692 struct btrfs_inode_item
*inode_item
,
1693 struct inode
*inode
)
1695 btrfs_set_stack_inode_uid(inode_item
, i_uid_read(inode
));
1696 btrfs_set_stack_inode_gid(inode_item
, i_gid_read(inode
));
1697 btrfs_set_stack_inode_size(inode_item
, BTRFS_I(inode
)->disk_i_size
);
1698 btrfs_set_stack_inode_mode(inode_item
, inode
->i_mode
);
1699 btrfs_set_stack_inode_nlink(inode_item
, inode
->i_nlink
);
1700 btrfs_set_stack_inode_nbytes(inode_item
, inode_get_bytes(inode
));
1701 btrfs_set_stack_inode_generation(inode_item
,
1702 BTRFS_I(inode
)->generation
);
1703 btrfs_set_stack_inode_sequence(inode_item
, inode
->i_version
);
1704 btrfs_set_stack_inode_transid(inode_item
, trans
->transid
);
1705 btrfs_set_stack_inode_rdev(inode_item
, inode
->i_rdev
);
1706 btrfs_set_stack_inode_flags(inode_item
, BTRFS_I(inode
)->flags
);
1707 btrfs_set_stack_inode_block_group(inode_item
, 0);
1709 btrfs_set_stack_timespec_sec(&inode_item
->atime
,
1710 inode
->i_atime
.tv_sec
);
1711 btrfs_set_stack_timespec_nsec(&inode_item
->atime
,
1712 inode
->i_atime
.tv_nsec
);
1714 btrfs_set_stack_timespec_sec(&inode_item
->mtime
,
1715 inode
->i_mtime
.tv_sec
);
1716 btrfs_set_stack_timespec_nsec(&inode_item
->mtime
,
1717 inode
->i_mtime
.tv_nsec
);
1719 btrfs_set_stack_timespec_sec(&inode_item
->ctime
,
1720 inode
->i_ctime
.tv_sec
);
1721 btrfs_set_stack_timespec_nsec(&inode_item
->ctime
,
1722 inode
->i_ctime
.tv_nsec
);
1724 btrfs_set_stack_timespec_sec(&inode_item
->otime
,
1725 BTRFS_I(inode
)->i_otime
.tv_sec
);
1726 btrfs_set_stack_timespec_nsec(&inode_item
->otime
,
1727 BTRFS_I(inode
)->i_otime
.tv_nsec
);
1730 int btrfs_fill_inode(struct inode
*inode
, u32
*rdev
)
1732 struct btrfs_delayed_node
*delayed_node
;
1733 struct btrfs_inode_item
*inode_item
;
1735 delayed_node
= btrfs_get_delayed_node(BTRFS_I(inode
));
1739 mutex_lock(&delayed_node
->mutex
);
1740 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
)) {
1741 mutex_unlock(&delayed_node
->mutex
);
1742 btrfs_release_delayed_node(delayed_node
);
1746 inode_item
= &delayed_node
->inode_item
;
1748 i_uid_write(inode
, btrfs_stack_inode_uid(inode_item
));
1749 i_gid_write(inode
, btrfs_stack_inode_gid(inode_item
));
1750 btrfs_i_size_write(BTRFS_I(inode
), btrfs_stack_inode_size(inode_item
));
1751 inode
->i_mode
= btrfs_stack_inode_mode(inode_item
);
1752 set_nlink(inode
, btrfs_stack_inode_nlink(inode_item
));
1753 inode_set_bytes(inode
, btrfs_stack_inode_nbytes(inode_item
));
1754 BTRFS_I(inode
)->generation
= btrfs_stack_inode_generation(inode_item
);
1755 BTRFS_I(inode
)->last_trans
= btrfs_stack_inode_transid(inode_item
);
1757 inode
->i_version
= btrfs_stack_inode_sequence(inode_item
);
1759 *rdev
= btrfs_stack_inode_rdev(inode_item
);
1760 BTRFS_I(inode
)->flags
= btrfs_stack_inode_flags(inode_item
);
1762 inode
->i_atime
.tv_sec
= btrfs_stack_timespec_sec(&inode_item
->atime
);
1763 inode
->i_atime
.tv_nsec
= btrfs_stack_timespec_nsec(&inode_item
->atime
);
1765 inode
->i_mtime
.tv_sec
= btrfs_stack_timespec_sec(&inode_item
->mtime
);
1766 inode
->i_mtime
.tv_nsec
= btrfs_stack_timespec_nsec(&inode_item
->mtime
);
1768 inode
->i_ctime
.tv_sec
= btrfs_stack_timespec_sec(&inode_item
->ctime
);
1769 inode
->i_ctime
.tv_nsec
= btrfs_stack_timespec_nsec(&inode_item
->ctime
);
1771 BTRFS_I(inode
)->i_otime
.tv_sec
=
1772 btrfs_stack_timespec_sec(&inode_item
->otime
);
1773 BTRFS_I(inode
)->i_otime
.tv_nsec
=
1774 btrfs_stack_timespec_nsec(&inode_item
->otime
);
1776 inode
->i_generation
= BTRFS_I(inode
)->generation
;
1777 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1779 mutex_unlock(&delayed_node
->mutex
);
1780 btrfs_release_delayed_node(delayed_node
);
1784 int btrfs_delayed_update_inode(struct btrfs_trans_handle
*trans
,
1785 struct btrfs_root
*root
, struct inode
*inode
)
1787 struct btrfs_delayed_node
*delayed_node
;
1790 delayed_node
= btrfs_get_or_create_delayed_node(BTRFS_I(inode
));
1791 if (IS_ERR(delayed_node
))
1792 return PTR_ERR(delayed_node
);
1794 mutex_lock(&delayed_node
->mutex
);
1795 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
)) {
1796 fill_stack_inode_item(trans
, &delayed_node
->inode_item
, inode
);
1800 ret
= btrfs_delayed_inode_reserve_metadata(trans
, root
, BTRFS_I(inode
),
1805 fill_stack_inode_item(trans
, &delayed_node
->inode_item
, inode
);
1806 set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
);
1807 delayed_node
->count
++;
1808 atomic_inc(&root
->fs_info
->delayed_root
->items
);
1810 mutex_unlock(&delayed_node
->mutex
);
1811 btrfs_release_delayed_node(delayed_node
);
1815 int btrfs_delayed_delete_inode_ref(struct btrfs_inode
*inode
)
1817 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->vfs_inode
.i_sb
);
1818 struct btrfs_delayed_node
*delayed_node
;
1821 * we don't do delayed inode updates during log recovery because it
1822 * leads to enospc problems. This means we also can't do
1823 * delayed inode refs
1825 if (test_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
))
1828 delayed_node
= btrfs_get_or_create_delayed_node(inode
);
1829 if (IS_ERR(delayed_node
))
1830 return PTR_ERR(delayed_node
);
1833 * We don't reserve space for inode ref deletion is because:
1834 * - We ONLY do async inode ref deletion for the inode who has only
1835 * one link(i_nlink == 1), it means there is only one inode ref.
1836 * And in most case, the inode ref and the inode item are in the
1837 * same leaf, and we will deal with them at the same time.
1838 * Since we are sure we will reserve the space for the inode item,
1839 * it is unnecessary to reserve space for inode ref deletion.
1840 * - If the inode ref and the inode item are not in the same leaf,
1841 * We also needn't worry about enospc problem, because we reserve
1842 * much more space for the inode update than it needs.
1843 * - At the worst, we can steal some space from the global reservation.
1846 mutex_lock(&delayed_node
->mutex
);
1847 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF
, &delayed_node
->flags
))
1850 set_bit(BTRFS_DELAYED_NODE_DEL_IREF
, &delayed_node
->flags
);
1851 delayed_node
->count
++;
1852 atomic_inc(&fs_info
->delayed_root
->items
);
1854 mutex_unlock(&delayed_node
->mutex
);
1855 btrfs_release_delayed_node(delayed_node
);
1859 static void __btrfs_kill_delayed_node(struct btrfs_delayed_node
*delayed_node
)
1861 struct btrfs_root
*root
= delayed_node
->root
;
1862 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1863 struct btrfs_delayed_item
*curr_item
, *prev_item
;
1865 mutex_lock(&delayed_node
->mutex
);
1866 curr_item
= __btrfs_first_delayed_insertion_item(delayed_node
);
1868 btrfs_delayed_item_release_metadata(fs_info
, curr_item
);
1869 prev_item
= curr_item
;
1870 curr_item
= __btrfs_next_delayed_item(prev_item
);
1871 btrfs_release_delayed_item(prev_item
);
1874 curr_item
= __btrfs_first_delayed_deletion_item(delayed_node
);
1876 btrfs_delayed_item_release_metadata(fs_info
, curr_item
);
1877 prev_item
= curr_item
;
1878 curr_item
= __btrfs_next_delayed_item(prev_item
);
1879 btrfs_release_delayed_item(prev_item
);
1882 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF
, &delayed_node
->flags
))
1883 btrfs_release_delayed_iref(delayed_node
);
1885 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
)) {
1886 btrfs_delayed_inode_release_metadata(fs_info
, delayed_node
);
1887 btrfs_release_delayed_inode(delayed_node
);
1889 mutex_unlock(&delayed_node
->mutex
);
1892 void btrfs_kill_delayed_inode_items(struct btrfs_inode
*inode
)
1894 struct btrfs_delayed_node
*delayed_node
;
1896 delayed_node
= btrfs_get_delayed_node(inode
);
1900 __btrfs_kill_delayed_node(delayed_node
);
1901 btrfs_release_delayed_node(delayed_node
);
1904 void btrfs_kill_all_delayed_nodes(struct btrfs_root
*root
)
1907 struct btrfs_delayed_node
*delayed_nodes
[8];
1911 spin_lock(&root
->inode_lock
);
1912 n
= radix_tree_gang_lookup(&root
->delayed_nodes_tree
,
1913 (void **)delayed_nodes
, inode_id
,
1914 ARRAY_SIZE(delayed_nodes
));
1916 spin_unlock(&root
->inode_lock
);
1920 inode_id
= delayed_nodes
[n
- 1]->inode_id
+ 1;
1922 for (i
= 0; i
< n
; i
++)
1923 refcount_inc(&delayed_nodes
[i
]->refs
);
1924 spin_unlock(&root
->inode_lock
);
1926 for (i
= 0; i
< n
; i
++) {
1927 __btrfs_kill_delayed_node(delayed_nodes
[i
]);
1928 btrfs_release_delayed_node(delayed_nodes
[i
]);
1933 void btrfs_destroy_delayed_inodes(struct btrfs_fs_info
*fs_info
)
1935 struct btrfs_delayed_node
*curr_node
, *prev_node
;
1937 curr_node
= btrfs_first_delayed_node(fs_info
->delayed_root
);
1939 __btrfs_kill_delayed_node(curr_node
);
1941 prev_node
= curr_node
;
1942 curr_node
= btrfs_next_delayed_node(curr_node
);
1943 btrfs_release_delayed_node(prev_node
);