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
;
584 bool release
= false;
586 src_rsv
= trans
->block_rsv
;
587 dst_rsv
= &fs_info
->delayed_block_rsv
;
589 num_bytes
= btrfs_calc_trans_metadata_size(fs_info
, 1);
592 * If our block_rsv is the delalloc block reserve then check and see if
593 * we have our extra reservation for updating the inode. If not fall
594 * through and try to reserve space quickly.
596 * We used to try and steal from the delalloc block rsv or the global
597 * reserve, but we'd steal a full reservation, which isn't kind. We are
598 * here through delalloc which means we've likely just cowed down close
599 * to the leaf that contains the inode, so we would steal less just
600 * doing the fallback inode update, so if we do end up having to steal
601 * from the global block rsv we hopefully only steal one or two blocks
602 * worth which is less likely to hurt us.
604 if (src_rsv
&& src_rsv
->type
== BTRFS_BLOCK_RSV_DELALLOC
) {
605 spin_lock(&inode
->lock
);
606 if (test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED
,
607 &inode
->runtime_flags
))
611 spin_unlock(&inode
->lock
);
615 * btrfs_dirty_inode will update the inode under btrfs_join_transaction
616 * which doesn't reserve space for speed. This is a problem since we
617 * still need to reserve space for this update, so try to reserve the
620 * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
621 * we're accounted for.
623 if (!src_rsv
|| (!trans
->bytes_reserved
&&
624 src_rsv
->type
!= BTRFS_BLOCK_RSV_DELALLOC
)) {
625 ret
= btrfs_block_rsv_add(root
, dst_rsv
, num_bytes
,
626 BTRFS_RESERVE_NO_FLUSH
);
628 * Since we're under a transaction reserve_metadata_bytes could
629 * try to commit the transaction which will make it return
630 * EAGAIN to make us stop the transaction we have, so return
631 * ENOSPC instead so that btrfs_dirty_inode knows what to do.
636 node
->bytes_reserved
= num_bytes
;
637 trace_btrfs_space_reservation(fs_info
,
645 ret
= btrfs_block_rsv_migrate(src_rsv
, dst_rsv
, num_bytes
, 1);
648 * Migrate only takes a reservation, it doesn't touch the size of the
649 * block_rsv. This is to simplify people who don't normally have things
650 * migrated from their block rsv. If they go to release their
651 * reservation, that will decrease the size as well, so if migrate
652 * reduced size we'd end up with a negative size. But for the
653 * delalloc_meta_reserved stuff we will only know to drop 1 reservation,
654 * but we could in fact do this reserve/migrate dance several times
655 * between the time we did the original reservation and we'd clean it
656 * up. So to take care of this, release the space for the meta
657 * reservation here. I think it may be time for a documentation page on
658 * how block rsvs. work.
661 trace_btrfs_space_reservation(fs_info
, "delayed_inode",
662 btrfs_ino(inode
), num_bytes
, 1);
663 node
->bytes_reserved
= num_bytes
;
667 trace_btrfs_space_reservation(fs_info
, "delalloc",
668 btrfs_ino(inode
), num_bytes
, 0);
669 btrfs_block_rsv_release(fs_info
, src_rsv
, num_bytes
);
675 static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info
*fs_info
,
676 struct btrfs_delayed_node
*node
)
678 struct btrfs_block_rsv
*rsv
;
680 if (!node
->bytes_reserved
)
683 rsv
= &fs_info
->delayed_block_rsv
;
684 trace_btrfs_space_reservation(fs_info
, "delayed_inode",
685 node
->inode_id
, node
->bytes_reserved
, 0);
686 btrfs_block_rsv_release(fs_info
, rsv
,
687 node
->bytes_reserved
);
688 node
->bytes_reserved
= 0;
692 * This helper will insert some continuous items into the same leaf according
693 * to the free space of the leaf.
695 static int btrfs_batch_insert_items(struct btrfs_root
*root
,
696 struct btrfs_path
*path
,
697 struct btrfs_delayed_item
*item
)
699 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
700 struct btrfs_delayed_item
*curr
, *next
;
702 int total_data_size
= 0, total_size
= 0;
703 struct extent_buffer
*leaf
;
705 struct btrfs_key
*keys
;
707 struct list_head head
;
713 BUG_ON(!path
->nodes
[0]);
715 leaf
= path
->nodes
[0];
716 free_space
= btrfs_leaf_free_space(fs_info
, leaf
);
717 INIT_LIST_HEAD(&head
);
723 * count the number of the continuous items that we can insert in batch
725 while (total_size
+ next
->data_len
+ sizeof(struct btrfs_item
) <=
727 total_data_size
+= next
->data_len
;
728 total_size
+= next
->data_len
+ sizeof(struct btrfs_item
);
729 list_add_tail(&next
->tree_list
, &head
);
733 next
= __btrfs_next_delayed_item(curr
);
737 if (!btrfs_is_continuous_delayed_item(curr
, next
))
747 * we need allocate some memory space, but it might cause the task
748 * to sleep, so we set all locked nodes in the path to blocking locks
751 btrfs_set_path_blocking(path
);
753 keys
= kmalloc_array(nitems
, sizeof(struct btrfs_key
), GFP_NOFS
);
759 data_size
= kmalloc_array(nitems
, sizeof(u32
), GFP_NOFS
);
765 /* get keys of all the delayed items */
767 list_for_each_entry(next
, &head
, tree_list
) {
769 data_size
[i
] = next
->data_len
;
773 /* reset all the locked nodes in the patch to spinning locks. */
774 btrfs_clear_path_blocking(path
, NULL
, 0);
776 /* insert the keys of the items */
777 setup_items_for_insert(root
, path
, keys
, data_size
,
778 total_data_size
, total_size
, nitems
);
780 /* insert the dir index items */
781 slot
= path
->slots
[0];
782 list_for_each_entry_safe(curr
, next
, &head
, tree_list
) {
783 data_ptr
= btrfs_item_ptr(leaf
, slot
, char);
784 write_extent_buffer(leaf
, &curr
->data
,
785 (unsigned long)data_ptr
,
789 btrfs_delayed_item_release_metadata(fs_info
, curr
);
791 list_del(&curr
->tree_list
);
792 btrfs_release_delayed_item(curr
);
803 * This helper can just do simple insertion that needn't extend item for new
804 * data, such as directory name index insertion, inode insertion.
806 static int btrfs_insert_delayed_item(struct btrfs_trans_handle
*trans
,
807 struct btrfs_root
*root
,
808 struct btrfs_path
*path
,
809 struct btrfs_delayed_item
*delayed_item
)
811 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
812 struct extent_buffer
*leaf
;
816 ret
= btrfs_insert_empty_item(trans
, root
, path
, &delayed_item
->key
,
817 delayed_item
->data_len
);
818 if (ret
< 0 && ret
!= -EEXIST
)
821 leaf
= path
->nodes
[0];
823 ptr
= btrfs_item_ptr(leaf
, path
->slots
[0], char);
825 write_extent_buffer(leaf
, delayed_item
->data
, (unsigned long)ptr
,
826 delayed_item
->data_len
);
827 btrfs_mark_buffer_dirty(leaf
);
829 btrfs_delayed_item_release_metadata(fs_info
, delayed_item
);
834 * we insert an item first, then if there are some continuous items, we try
835 * to insert those items into the same leaf.
837 static int btrfs_insert_delayed_items(struct btrfs_trans_handle
*trans
,
838 struct btrfs_path
*path
,
839 struct btrfs_root
*root
,
840 struct btrfs_delayed_node
*node
)
842 struct btrfs_delayed_item
*curr
, *prev
;
846 mutex_lock(&node
->mutex
);
847 curr
= __btrfs_first_delayed_insertion_item(node
);
851 ret
= btrfs_insert_delayed_item(trans
, root
, path
, curr
);
853 btrfs_release_path(path
);
858 curr
= __btrfs_next_delayed_item(prev
);
859 if (curr
&& btrfs_is_continuous_delayed_item(prev
, curr
)) {
860 /* insert the continuous items into the same leaf */
862 btrfs_batch_insert_items(root
, path
, curr
);
864 btrfs_release_delayed_item(prev
);
865 btrfs_mark_buffer_dirty(path
->nodes
[0]);
867 btrfs_release_path(path
);
868 mutex_unlock(&node
->mutex
);
872 mutex_unlock(&node
->mutex
);
876 static int btrfs_batch_delete_items(struct btrfs_trans_handle
*trans
,
877 struct btrfs_root
*root
,
878 struct btrfs_path
*path
,
879 struct btrfs_delayed_item
*item
)
881 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
882 struct btrfs_delayed_item
*curr
, *next
;
883 struct extent_buffer
*leaf
;
884 struct btrfs_key key
;
885 struct list_head head
;
886 int nitems
, i
, last_item
;
889 BUG_ON(!path
->nodes
[0]);
891 leaf
= path
->nodes
[0];
894 last_item
= btrfs_header_nritems(leaf
) - 1;
896 return -ENOENT
; /* FIXME: Is errno suitable? */
899 INIT_LIST_HEAD(&head
);
900 btrfs_item_key_to_cpu(leaf
, &key
, i
);
903 * count the number of the dir index items that we can delete in batch
905 while (btrfs_comp_cpu_keys(&next
->key
, &key
) == 0) {
906 list_add_tail(&next
->tree_list
, &head
);
910 next
= __btrfs_next_delayed_item(curr
);
914 if (!btrfs_is_continuous_delayed_item(curr
, next
))
920 btrfs_item_key_to_cpu(leaf
, &key
, i
);
926 ret
= btrfs_del_items(trans
, root
, path
, path
->slots
[0], nitems
);
930 list_for_each_entry_safe(curr
, next
, &head
, tree_list
) {
931 btrfs_delayed_item_release_metadata(fs_info
, curr
);
932 list_del(&curr
->tree_list
);
933 btrfs_release_delayed_item(curr
);
940 static int btrfs_delete_delayed_items(struct btrfs_trans_handle
*trans
,
941 struct btrfs_path
*path
,
942 struct btrfs_root
*root
,
943 struct btrfs_delayed_node
*node
)
945 struct btrfs_delayed_item
*curr
, *prev
;
949 mutex_lock(&node
->mutex
);
950 curr
= __btrfs_first_delayed_deletion_item(node
);
954 ret
= btrfs_search_slot(trans
, root
, &curr
->key
, path
, -1, 1);
959 * can't find the item which the node points to, so this node
960 * is invalid, just drop it.
963 curr
= __btrfs_next_delayed_item(prev
);
964 btrfs_release_delayed_item(prev
);
966 btrfs_release_path(path
);
968 mutex_unlock(&node
->mutex
);
974 btrfs_batch_delete_items(trans
, root
, path
, curr
);
975 btrfs_release_path(path
);
976 mutex_unlock(&node
->mutex
);
980 btrfs_release_path(path
);
981 mutex_unlock(&node
->mutex
);
985 static void btrfs_release_delayed_inode(struct btrfs_delayed_node
*delayed_node
)
987 struct btrfs_delayed_root
*delayed_root
;
990 test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
)) {
991 BUG_ON(!delayed_node
->root
);
992 clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
);
993 delayed_node
->count
--;
995 delayed_root
= delayed_node
->root
->fs_info
->delayed_root
;
996 finish_one_item(delayed_root
);
1000 static void btrfs_release_delayed_iref(struct btrfs_delayed_node
*delayed_node
)
1002 struct btrfs_delayed_root
*delayed_root
;
1004 ASSERT(delayed_node
->root
);
1005 clear_bit(BTRFS_DELAYED_NODE_DEL_IREF
, &delayed_node
->flags
);
1006 delayed_node
->count
--;
1008 delayed_root
= delayed_node
->root
->fs_info
->delayed_root
;
1009 finish_one_item(delayed_root
);
1012 static int __btrfs_update_delayed_inode(struct btrfs_trans_handle
*trans
,
1013 struct btrfs_root
*root
,
1014 struct btrfs_path
*path
,
1015 struct btrfs_delayed_node
*node
)
1017 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1018 struct btrfs_key key
;
1019 struct btrfs_inode_item
*inode_item
;
1020 struct extent_buffer
*leaf
;
1024 key
.objectid
= node
->inode_id
;
1025 key
.type
= BTRFS_INODE_ITEM_KEY
;
1028 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF
, &node
->flags
))
1033 ret
= btrfs_lookup_inode(trans
, root
, path
, &key
, mod
);
1035 btrfs_release_path(path
);
1037 } else if (ret
< 0) {
1041 leaf
= path
->nodes
[0];
1042 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1043 struct btrfs_inode_item
);
1044 write_extent_buffer(leaf
, &node
->inode_item
, (unsigned long)inode_item
,
1045 sizeof(struct btrfs_inode_item
));
1046 btrfs_mark_buffer_dirty(leaf
);
1048 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF
, &node
->flags
))
1052 if (path
->slots
[0] >= btrfs_header_nritems(leaf
))
1055 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1056 if (key
.objectid
!= node
->inode_id
)
1059 if (key
.type
!= BTRFS_INODE_REF_KEY
&&
1060 key
.type
!= BTRFS_INODE_EXTREF_KEY
)
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.
1068 btrfs_del_item(trans
, root
, path
);
1070 btrfs_release_delayed_iref(node
);
1072 btrfs_release_path(path
);
1074 btrfs_delayed_inode_release_metadata(fs_info
, node
);
1075 btrfs_release_delayed_inode(node
);
1080 btrfs_release_path(path
);
1082 key
.type
= BTRFS_INODE_EXTREF_KEY
;
1084 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1090 leaf
= path
->nodes
[0];
1095 static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle
*trans
,
1096 struct btrfs_root
*root
,
1097 struct btrfs_path
*path
,
1098 struct btrfs_delayed_node
*node
)
1102 mutex_lock(&node
->mutex
);
1103 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &node
->flags
)) {
1104 mutex_unlock(&node
->mutex
);
1108 ret
= __btrfs_update_delayed_inode(trans
, root
, path
, node
);
1109 mutex_unlock(&node
->mutex
);
1114 __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle
*trans
,
1115 struct btrfs_path
*path
,
1116 struct btrfs_delayed_node
*node
)
1120 ret
= btrfs_insert_delayed_items(trans
, path
, node
->root
, node
);
1124 ret
= btrfs_delete_delayed_items(trans
, path
, node
->root
, node
);
1128 ret
= btrfs_update_delayed_inode(trans
, node
->root
, path
, node
);
1133 * Called when committing the transaction.
1134 * Returns 0 on success.
1135 * Returns < 0 on error and returns with an aborted transaction with any
1136 * outstanding delayed items cleaned up.
1138 static int __btrfs_run_delayed_items(struct btrfs_trans_handle
*trans
,
1139 struct btrfs_fs_info
*fs_info
, int nr
)
1141 struct btrfs_delayed_root
*delayed_root
;
1142 struct btrfs_delayed_node
*curr_node
, *prev_node
;
1143 struct btrfs_path
*path
;
1144 struct btrfs_block_rsv
*block_rsv
;
1146 bool count
= (nr
> 0);
1151 path
= btrfs_alloc_path();
1154 path
->leave_spinning
= 1;
1156 block_rsv
= trans
->block_rsv
;
1157 trans
->block_rsv
= &fs_info
->delayed_block_rsv
;
1159 delayed_root
= fs_info
->delayed_root
;
1161 curr_node
= btrfs_first_delayed_node(delayed_root
);
1162 while (curr_node
&& (!count
|| (count
&& nr
--))) {
1163 ret
= __btrfs_commit_inode_delayed_items(trans
, path
,
1166 btrfs_release_delayed_node(curr_node
);
1168 btrfs_abort_transaction(trans
, ret
);
1172 prev_node
= curr_node
;
1173 curr_node
= btrfs_next_delayed_node(curr_node
);
1174 btrfs_release_delayed_node(prev_node
);
1178 btrfs_release_delayed_node(curr_node
);
1179 btrfs_free_path(path
);
1180 trans
->block_rsv
= block_rsv
;
1185 int btrfs_run_delayed_items(struct btrfs_trans_handle
*trans
,
1186 struct btrfs_fs_info
*fs_info
)
1188 return __btrfs_run_delayed_items(trans
, fs_info
, -1);
1191 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle
*trans
,
1192 struct btrfs_fs_info
*fs_info
, int nr
)
1194 return __btrfs_run_delayed_items(trans
, fs_info
, nr
);
1197 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle
*trans
,
1198 struct btrfs_inode
*inode
)
1200 struct btrfs_delayed_node
*delayed_node
= btrfs_get_delayed_node(inode
);
1201 struct btrfs_path
*path
;
1202 struct btrfs_block_rsv
*block_rsv
;
1208 mutex_lock(&delayed_node
->mutex
);
1209 if (!delayed_node
->count
) {
1210 mutex_unlock(&delayed_node
->mutex
);
1211 btrfs_release_delayed_node(delayed_node
);
1214 mutex_unlock(&delayed_node
->mutex
);
1216 path
= btrfs_alloc_path();
1218 btrfs_release_delayed_node(delayed_node
);
1221 path
->leave_spinning
= 1;
1223 block_rsv
= trans
->block_rsv
;
1224 trans
->block_rsv
= &delayed_node
->root
->fs_info
->delayed_block_rsv
;
1226 ret
= __btrfs_commit_inode_delayed_items(trans
, path
, delayed_node
);
1228 btrfs_release_delayed_node(delayed_node
);
1229 btrfs_free_path(path
);
1230 trans
->block_rsv
= block_rsv
;
1235 int btrfs_commit_inode_delayed_inode(struct btrfs_inode
*inode
)
1237 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->vfs_inode
.i_sb
);
1238 struct btrfs_trans_handle
*trans
;
1239 struct btrfs_delayed_node
*delayed_node
= btrfs_get_delayed_node(inode
);
1240 struct btrfs_path
*path
;
1241 struct btrfs_block_rsv
*block_rsv
;
1247 mutex_lock(&delayed_node
->mutex
);
1248 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
)) {
1249 mutex_unlock(&delayed_node
->mutex
);
1250 btrfs_release_delayed_node(delayed_node
);
1253 mutex_unlock(&delayed_node
->mutex
);
1255 trans
= btrfs_join_transaction(delayed_node
->root
);
1256 if (IS_ERR(trans
)) {
1257 ret
= PTR_ERR(trans
);
1261 path
= btrfs_alloc_path();
1266 path
->leave_spinning
= 1;
1268 block_rsv
= trans
->block_rsv
;
1269 trans
->block_rsv
= &fs_info
->delayed_block_rsv
;
1271 mutex_lock(&delayed_node
->mutex
);
1272 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
))
1273 ret
= __btrfs_update_delayed_inode(trans
, delayed_node
->root
,
1274 path
, delayed_node
);
1277 mutex_unlock(&delayed_node
->mutex
);
1279 btrfs_free_path(path
);
1280 trans
->block_rsv
= block_rsv
;
1282 btrfs_end_transaction(trans
);
1283 btrfs_btree_balance_dirty(fs_info
);
1285 btrfs_release_delayed_node(delayed_node
);
1290 void btrfs_remove_delayed_node(struct btrfs_inode
*inode
)
1292 struct btrfs_delayed_node
*delayed_node
;
1294 delayed_node
= READ_ONCE(inode
->delayed_node
);
1298 inode
->delayed_node
= NULL
;
1299 btrfs_release_delayed_node(delayed_node
);
1302 struct btrfs_async_delayed_work
{
1303 struct btrfs_delayed_root
*delayed_root
;
1305 struct btrfs_work work
;
1308 static void btrfs_async_run_delayed_root(struct btrfs_work
*work
)
1310 struct btrfs_async_delayed_work
*async_work
;
1311 struct btrfs_delayed_root
*delayed_root
;
1312 struct btrfs_trans_handle
*trans
;
1313 struct btrfs_path
*path
;
1314 struct btrfs_delayed_node
*delayed_node
= NULL
;
1315 struct btrfs_root
*root
;
1316 struct btrfs_block_rsv
*block_rsv
;
1319 async_work
= container_of(work
, struct btrfs_async_delayed_work
, work
);
1320 delayed_root
= async_work
->delayed_root
;
1322 path
= btrfs_alloc_path();
1327 if (atomic_read(&delayed_root
->items
) < BTRFS_DELAYED_BACKGROUND
/ 2)
1330 delayed_node
= btrfs_first_prepared_delayed_node(delayed_root
);
1334 path
->leave_spinning
= 1;
1335 root
= delayed_node
->root
;
1337 trans
= btrfs_join_transaction(root
);
1341 block_rsv
= trans
->block_rsv
;
1342 trans
->block_rsv
= &root
->fs_info
->delayed_block_rsv
;
1344 __btrfs_commit_inode_delayed_items(trans
, path
, delayed_node
);
1346 trans
->block_rsv
= block_rsv
;
1347 btrfs_end_transaction(trans
);
1348 btrfs_btree_balance_dirty_nodelay(root
->fs_info
);
1351 btrfs_release_path(path
);
1354 btrfs_release_prepared_delayed_node(delayed_node
);
1355 if ((async_work
->nr
== 0 && total_done
< BTRFS_DELAYED_WRITEBACK
) ||
1356 total_done
< async_work
->nr
)
1360 btrfs_free_path(path
);
1362 wake_up(&delayed_root
->wait
);
1367 static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root
*delayed_root
,
1368 struct btrfs_fs_info
*fs_info
, int nr
)
1370 struct btrfs_async_delayed_work
*async_work
;
1372 if (atomic_read(&delayed_root
->items
) < BTRFS_DELAYED_BACKGROUND
||
1373 btrfs_workqueue_normal_congested(fs_info
->delayed_workers
))
1376 async_work
= kmalloc(sizeof(*async_work
), GFP_NOFS
);
1380 async_work
->delayed_root
= delayed_root
;
1381 btrfs_init_work(&async_work
->work
, btrfs_delayed_meta_helper
,
1382 btrfs_async_run_delayed_root
, NULL
, NULL
);
1383 async_work
->nr
= nr
;
1385 btrfs_queue_work(fs_info
->delayed_workers
, &async_work
->work
);
1389 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info
*fs_info
)
1391 WARN_ON(btrfs_first_delayed_node(fs_info
->delayed_root
));
1394 static int could_end_wait(struct btrfs_delayed_root
*delayed_root
, int seq
)
1396 int val
= atomic_read(&delayed_root
->items_seq
);
1398 if (val
< seq
|| val
>= seq
+ BTRFS_DELAYED_BATCH
)
1401 if (atomic_read(&delayed_root
->items
) < BTRFS_DELAYED_BACKGROUND
)
1407 void btrfs_balance_delayed_items(struct btrfs_fs_info
*fs_info
)
1409 struct btrfs_delayed_root
*delayed_root
= fs_info
->delayed_root
;
1411 if (atomic_read(&delayed_root
->items
) < BTRFS_DELAYED_BACKGROUND
)
1414 if (atomic_read(&delayed_root
->items
) >= BTRFS_DELAYED_WRITEBACK
) {
1418 seq
= atomic_read(&delayed_root
->items_seq
);
1420 ret
= btrfs_wq_run_delayed_node(delayed_root
, fs_info
, 0);
1424 wait_event_interruptible(delayed_root
->wait
,
1425 could_end_wait(delayed_root
, seq
));
1429 btrfs_wq_run_delayed_node(delayed_root
, fs_info
, BTRFS_DELAYED_BATCH
);
1432 /* Will return 0 or -ENOMEM */
1433 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle
*trans
,
1434 struct btrfs_fs_info
*fs_info
,
1435 const char *name
, int name_len
,
1436 struct btrfs_inode
*dir
,
1437 struct btrfs_disk_key
*disk_key
, u8 type
,
1440 struct btrfs_delayed_node
*delayed_node
;
1441 struct btrfs_delayed_item
*delayed_item
;
1442 struct btrfs_dir_item
*dir_item
;
1445 delayed_node
= btrfs_get_or_create_delayed_node(dir
);
1446 if (IS_ERR(delayed_node
))
1447 return PTR_ERR(delayed_node
);
1449 delayed_item
= btrfs_alloc_delayed_item(sizeof(*dir_item
) + name_len
);
1450 if (!delayed_item
) {
1455 delayed_item
->key
.objectid
= btrfs_ino(dir
);
1456 delayed_item
->key
.type
= BTRFS_DIR_INDEX_KEY
;
1457 delayed_item
->key
.offset
= index
;
1459 dir_item
= (struct btrfs_dir_item
*)delayed_item
->data
;
1460 dir_item
->location
= *disk_key
;
1461 btrfs_set_stack_dir_transid(dir_item
, trans
->transid
);
1462 btrfs_set_stack_dir_data_len(dir_item
, 0);
1463 btrfs_set_stack_dir_name_len(dir_item
, name_len
);
1464 btrfs_set_stack_dir_type(dir_item
, type
);
1465 memcpy((char *)(dir_item
+ 1), name
, name_len
);
1467 ret
= btrfs_delayed_item_reserve_metadata(trans
, fs_info
, delayed_item
);
1469 * we have reserved enough space when we start a new transaction,
1470 * so reserving metadata failure is impossible
1475 mutex_lock(&delayed_node
->mutex
);
1476 ret
= __btrfs_add_delayed_insertion_item(delayed_node
, delayed_item
);
1477 if (unlikely(ret
)) {
1479 "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1480 name_len
, name
, delayed_node
->root
->objectid
,
1481 delayed_node
->inode_id
, ret
);
1484 mutex_unlock(&delayed_node
->mutex
);
1487 btrfs_release_delayed_node(delayed_node
);
1491 static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info
*fs_info
,
1492 struct btrfs_delayed_node
*node
,
1493 struct btrfs_key
*key
)
1495 struct btrfs_delayed_item
*item
;
1497 mutex_lock(&node
->mutex
);
1498 item
= __btrfs_lookup_delayed_insertion_item(node
, key
);
1500 mutex_unlock(&node
->mutex
);
1504 btrfs_delayed_item_release_metadata(fs_info
, item
);
1505 btrfs_release_delayed_item(item
);
1506 mutex_unlock(&node
->mutex
);
1510 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle
*trans
,
1511 struct btrfs_fs_info
*fs_info
,
1512 struct btrfs_inode
*dir
, u64 index
)
1514 struct btrfs_delayed_node
*node
;
1515 struct btrfs_delayed_item
*item
;
1516 struct btrfs_key item_key
;
1519 node
= btrfs_get_or_create_delayed_node(dir
);
1521 return PTR_ERR(node
);
1523 item_key
.objectid
= btrfs_ino(dir
);
1524 item_key
.type
= BTRFS_DIR_INDEX_KEY
;
1525 item_key
.offset
= index
;
1527 ret
= btrfs_delete_delayed_insertion_item(fs_info
, node
, &item_key
);
1531 item
= btrfs_alloc_delayed_item(0);
1537 item
->key
= item_key
;
1539 ret
= btrfs_delayed_item_reserve_metadata(trans
, fs_info
, item
);
1541 * we have reserved enough space when we start a new transaction,
1542 * so reserving metadata failure is impossible.
1546 mutex_lock(&node
->mutex
);
1547 ret
= __btrfs_add_delayed_deletion_item(node
, item
);
1548 if (unlikely(ret
)) {
1550 "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1551 index
, node
->root
->objectid
, node
->inode_id
, ret
);
1554 mutex_unlock(&node
->mutex
);
1556 btrfs_release_delayed_node(node
);
1560 int btrfs_inode_delayed_dir_index_count(struct btrfs_inode
*inode
)
1562 struct btrfs_delayed_node
*delayed_node
= btrfs_get_delayed_node(inode
);
1568 * Since we have held i_mutex of this directory, it is impossible that
1569 * a new directory index is added into the delayed node and index_cnt
1570 * is updated now. So we needn't lock the delayed node.
1572 if (!delayed_node
->index_cnt
) {
1573 btrfs_release_delayed_node(delayed_node
);
1577 inode
->index_cnt
= delayed_node
->index_cnt
;
1578 btrfs_release_delayed_node(delayed_node
);
1582 bool btrfs_readdir_get_delayed_items(struct inode
*inode
,
1583 struct list_head
*ins_list
,
1584 struct list_head
*del_list
)
1586 struct btrfs_delayed_node
*delayed_node
;
1587 struct btrfs_delayed_item
*item
;
1589 delayed_node
= btrfs_get_delayed_node(BTRFS_I(inode
));
1594 * We can only do one readdir with delayed items at a time because of
1595 * item->readdir_list.
1597 inode_unlock_shared(inode
);
1600 mutex_lock(&delayed_node
->mutex
);
1601 item
= __btrfs_first_delayed_insertion_item(delayed_node
);
1603 refcount_inc(&item
->refs
);
1604 list_add_tail(&item
->readdir_list
, ins_list
);
1605 item
= __btrfs_next_delayed_item(item
);
1608 item
= __btrfs_first_delayed_deletion_item(delayed_node
);
1610 refcount_inc(&item
->refs
);
1611 list_add_tail(&item
->readdir_list
, del_list
);
1612 item
= __btrfs_next_delayed_item(item
);
1614 mutex_unlock(&delayed_node
->mutex
);
1616 * This delayed node is still cached in the btrfs inode, so refs
1617 * must be > 1 now, and we needn't check it is going to be freed
1620 * Besides that, this function is used to read dir, we do not
1621 * insert/delete delayed items in this period. So we also needn't
1622 * requeue or dequeue this delayed node.
1624 refcount_dec(&delayed_node
->refs
);
1629 void btrfs_readdir_put_delayed_items(struct inode
*inode
,
1630 struct list_head
*ins_list
,
1631 struct list_head
*del_list
)
1633 struct btrfs_delayed_item
*curr
, *next
;
1635 list_for_each_entry_safe(curr
, next
, ins_list
, readdir_list
) {
1636 list_del(&curr
->readdir_list
);
1637 if (refcount_dec_and_test(&curr
->refs
))
1641 list_for_each_entry_safe(curr
, next
, del_list
, readdir_list
) {
1642 list_del(&curr
->readdir_list
);
1643 if (refcount_dec_and_test(&curr
->refs
))
1648 * The VFS is going to do up_read(), so we need to downgrade back to a
1651 downgrade_write(&inode
->i_rwsem
);
1654 int btrfs_should_delete_dir_index(struct list_head
*del_list
,
1657 struct btrfs_delayed_item
*curr
, *next
;
1660 if (list_empty(del_list
))
1663 list_for_each_entry_safe(curr
, next
, del_list
, readdir_list
) {
1664 if (curr
->key
.offset
> index
)
1667 list_del(&curr
->readdir_list
);
1668 ret
= (curr
->key
.offset
== index
);
1670 if (refcount_dec_and_test(&curr
->refs
))
1682 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1685 int btrfs_readdir_delayed_dir_index(struct dir_context
*ctx
,
1686 struct list_head
*ins_list
)
1688 struct btrfs_dir_item
*di
;
1689 struct btrfs_delayed_item
*curr
, *next
;
1690 struct btrfs_key location
;
1694 unsigned char d_type
;
1696 if (list_empty(ins_list
))
1700 * Changing the data of the delayed item is impossible. So
1701 * we needn't lock them. And we have held i_mutex of the
1702 * directory, nobody can delete any directory indexes now.
1704 list_for_each_entry_safe(curr
, next
, ins_list
, readdir_list
) {
1705 list_del(&curr
->readdir_list
);
1707 if (curr
->key
.offset
< ctx
->pos
) {
1708 if (refcount_dec_and_test(&curr
->refs
))
1713 ctx
->pos
= curr
->key
.offset
;
1715 di
= (struct btrfs_dir_item
*)curr
->data
;
1716 name
= (char *)(di
+ 1);
1717 name_len
= btrfs_stack_dir_name_len(di
);
1719 d_type
= btrfs_filetype_table
[di
->type
];
1720 btrfs_disk_key_to_cpu(&location
, &di
->location
);
1722 over
= !dir_emit(ctx
, name
, name_len
,
1723 location
.objectid
, d_type
);
1725 if (refcount_dec_and_test(&curr
->refs
))
1734 static void fill_stack_inode_item(struct btrfs_trans_handle
*trans
,
1735 struct btrfs_inode_item
*inode_item
,
1736 struct inode
*inode
)
1738 btrfs_set_stack_inode_uid(inode_item
, i_uid_read(inode
));
1739 btrfs_set_stack_inode_gid(inode_item
, i_gid_read(inode
));
1740 btrfs_set_stack_inode_size(inode_item
, BTRFS_I(inode
)->disk_i_size
);
1741 btrfs_set_stack_inode_mode(inode_item
, inode
->i_mode
);
1742 btrfs_set_stack_inode_nlink(inode_item
, inode
->i_nlink
);
1743 btrfs_set_stack_inode_nbytes(inode_item
, inode_get_bytes(inode
));
1744 btrfs_set_stack_inode_generation(inode_item
,
1745 BTRFS_I(inode
)->generation
);
1746 btrfs_set_stack_inode_sequence(inode_item
, inode
->i_version
);
1747 btrfs_set_stack_inode_transid(inode_item
, trans
->transid
);
1748 btrfs_set_stack_inode_rdev(inode_item
, inode
->i_rdev
);
1749 btrfs_set_stack_inode_flags(inode_item
, BTRFS_I(inode
)->flags
);
1750 btrfs_set_stack_inode_block_group(inode_item
, 0);
1752 btrfs_set_stack_timespec_sec(&inode_item
->atime
,
1753 inode
->i_atime
.tv_sec
);
1754 btrfs_set_stack_timespec_nsec(&inode_item
->atime
,
1755 inode
->i_atime
.tv_nsec
);
1757 btrfs_set_stack_timespec_sec(&inode_item
->mtime
,
1758 inode
->i_mtime
.tv_sec
);
1759 btrfs_set_stack_timespec_nsec(&inode_item
->mtime
,
1760 inode
->i_mtime
.tv_nsec
);
1762 btrfs_set_stack_timespec_sec(&inode_item
->ctime
,
1763 inode
->i_ctime
.tv_sec
);
1764 btrfs_set_stack_timespec_nsec(&inode_item
->ctime
,
1765 inode
->i_ctime
.tv_nsec
);
1767 btrfs_set_stack_timespec_sec(&inode_item
->otime
,
1768 BTRFS_I(inode
)->i_otime
.tv_sec
);
1769 btrfs_set_stack_timespec_nsec(&inode_item
->otime
,
1770 BTRFS_I(inode
)->i_otime
.tv_nsec
);
1773 int btrfs_fill_inode(struct inode
*inode
, u32
*rdev
)
1775 struct btrfs_delayed_node
*delayed_node
;
1776 struct btrfs_inode_item
*inode_item
;
1778 delayed_node
= btrfs_get_delayed_node(BTRFS_I(inode
));
1782 mutex_lock(&delayed_node
->mutex
);
1783 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
)) {
1784 mutex_unlock(&delayed_node
->mutex
);
1785 btrfs_release_delayed_node(delayed_node
);
1789 inode_item
= &delayed_node
->inode_item
;
1791 i_uid_write(inode
, btrfs_stack_inode_uid(inode_item
));
1792 i_gid_write(inode
, btrfs_stack_inode_gid(inode_item
));
1793 btrfs_i_size_write(BTRFS_I(inode
), btrfs_stack_inode_size(inode_item
));
1794 inode
->i_mode
= btrfs_stack_inode_mode(inode_item
);
1795 set_nlink(inode
, btrfs_stack_inode_nlink(inode_item
));
1796 inode_set_bytes(inode
, btrfs_stack_inode_nbytes(inode_item
));
1797 BTRFS_I(inode
)->generation
= btrfs_stack_inode_generation(inode_item
);
1798 BTRFS_I(inode
)->last_trans
= btrfs_stack_inode_transid(inode_item
);
1800 inode
->i_version
= btrfs_stack_inode_sequence(inode_item
);
1802 *rdev
= btrfs_stack_inode_rdev(inode_item
);
1803 BTRFS_I(inode
)->flags
= btrfs_stack_inode_flags(inode_item
);
1805 inode
->i_atime
.tv_sec
= btrfs_stack_timespec_sec(&inode_item
->atime
);
1806 inode
->i_atime
.tv_nsec
= btrfs_stack_timespec_nsec(&inode_item
->atime
);
1808 inode
->i_mtime
.tv_sec
= btrfs_stack_timespec_sec(&inode_item
->mtime
);
1809 inode
->i_mtime
.tv_nsec
= btrfs_stack_timespec_nsec(&inode_item
->mtime
);
1811 inode
->i_ctime
.tv_sec
= btrfs_stack_timespec_sec(&inode_item
->ctime
);
1812 inode
->i_ctime
.tv_nsec
= btrfs_stack_timespec_nsec(&inode_item
->ctime
);
1814 BTRFS_I(inode
)->i_otime
.tv_sec
=
1815 btrfs_stack_timespec_sec(&inode_item
->otime
);
1816 BTRFS_I(inode
)->i_otime
.tv_nsec
=
1817 btrfs_stack_timespec_nsec(&inode_item
->otime
);
1819 inode
->i_generation
= BTRFS_I(inode
)->generation
;
1820 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1822 mutex_unlock(&delayed_node
->mutex
);
1823 btrfs_release_delayed_node(delayed_node
);
1827 int btrfs_delayed_update_inode(struct btrfs_trans_handle
*trans
,
1828 struct btrfs_root
*root
, struct inode
*inode
)
1830 struct btrfs_delayed_node
*delayed_node
;
1833 delayed_node
= btrfs_get_or_create_delayed_node(BTRFS_I(inode
));
1834 if (IS_ERR(delayed_node
))
1835 return PTR_ERR(delayed_node
);
1837 mutex_lock(&delayed_node
->mutex
);
1838 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
)) {
1839 fill_stack_inode_item(trans
, &delayed_node
->inode_item
, inode
);
1843 ret
= btrfs_delayed_inode_reserve_metadata(trans
, root
, BTRFS_I(inode
),
1848 fill_stack_inode_item(trans
, &delayed_node
->inode_item
, inode
);
1849 set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
);
1850 delayed_node
->count
++;
1851 atomic_inc(&root
->fs_info
->delayed_root
->items
);
1853 mutex_unlock(&delayed_node
->mutex
);
1854 btrfs_release_delayed_node(delayed_node
);
1858 int btrfs_delayed_delete_inode_ref(struct btrfs_inode
*inode
)
1860 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->vfs_inode
.i_sb
);
1861 struct btrfs_delayed_node
*delayed_node
;
1864 * we don't do delayed inode updates during log recovery because it
1865 * leads to enospc problems. This means we also can't do
1866 * delayed inode refs
1868 if (test_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
))
1871 delayed_node
= btrfs_get_or_create_delayed_node(inode
);
1872 if (IS_ERR(delayed_node
))
1873 return PTR_ERR(delayed_node
);
1876 * We don't reserve space for inode ref deletion is because:
1877 * - We ONLY do async inode ref deletion for the inode who has only
1878 * one link(i_nlink == 1), it means there is only one inode ref.
1879 * And in most case, the inode ref and the inode item are in the
1880 * same leaf, and we will deal with them at the same time.
1881 * Since we are sure we will reserve the space for the inode item,
1882 * it is unnecessary to reserve space for inode ref deletion.
1883 * - If the inode ref and the inode item are not in the same leaf,
1884 * We also needn't worry about enospc problem, because we reserve
1885 * much more space for the inode update than it needs.
1886 * - At the worst, we can steal some space from the global reservation.
1889 mutex_lock(&delayed_node
->mutex
);
1890 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF
, &delayed_node
->flags
))
1893 set_bit(BTRFS_DELAYED_NODE_DEL_IREF
, &delayed_node
->flags
);
1894 delayed_node
->count
++;
1895 atomic_inc(&fs_info
->delayed_root
->items
);
1897 mutex_unlock(&delayed_node
->mutex
);
1898 btrfs_release_delayed_node(delayed_node
);
1902 static void __btrfs_kill_delayed_node(struct btrfs_delayed_node
*delayed_node
)
1904 struct btrfs_root
*root
= delayed_node
->root
;
1905 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1906 struct btrfs_delayed_item
*curr_item
, *prev_item
;
1908 mutex_lock(&delayed_node
->mutex
);
1909 curr_item
= __btrfs_first_delayed_insertion_item(delayed_node
);
1911 btrfs_delayed_item_release_metadata(fs_info
, curr_item
);
1912 prev_item
= curr_item
;
1913 curr_item
= __btrfs_next_delayed_item(prev_item
);
1914 btrfs_release_delayed_item(prev_item
);
1917 curr_item
= __btrfs_first_delayed_deletion_item(delayed_node
);
1919 btrfs_delayed_item_release_metadata(fs_info
, curr_item
);
1920 prev_item
= curr_item
;
1921 curr_item
= __btrfs_next_delayed_item(prev_item
);
1922 btrfs_release_delayed_item(prev_item
);
1925 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF
, &delayed_node
->flags
))
1926 btrfs_release_delayed_iref(delayed_node
);
1928 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY
, &delayed_node
->flags
)) {
1929 btrfs_delayed_inode_release_metadata(fs_info
, delayed_node
);
1930 btrfs_release_delayed_inode(delayed_node
);
1932 mutex_unlock(&delayed_node
->mutex
);
1935 void btrfs_kill_delayed_inode_items(struct btrfs_inode
*inode
)
1937 struct btrfs_delayed_node
*delayed_node
;
1939 delayed_node
= btrfs_get_delayed_node(inode
);
1943 __btrfs_kill_delayed_node(delayed_node
);
1944 btrfs_release_delayed_node(delayed_node
);
1947 void btrfs_kill_all_delayed_nodes(struct btrfs_root
*root
)
1950 struct btrfs_delayed_node
*delayed_nodes
[8];
1954 spin_lock(&root
->inode_lock
);
1955 n
= radix_tree_gang_lookup(&root
->delayed_nodes_tree
,
1956 (void **)delayed_nodes
, inode_id
,
1957 ARRAY_SIZE(delayed_nodes
));
1959 spin_unlock(&root
->inode_lock
);
1963 inode_id
= delayed_nodes
[n
- 1]->inode_id
+ 1;
1965 for (i
= 0; i
< n
; i
++)
1966 refcount_inc(&delayed_nodes
[i
]->refs
);
1967 spin_unlock(&root
->inode_lock
);
1969 for (i
= 0; i
< n
; i
++) {
1970 __btrfs_kill_delayed_node(delayed_nodes
[i
]);
1971 btrfs_release_delayed_node(delayed_nodes
[i
]);
1976 void btrfs_destroy_delayed_inodes(struct btrfs_fs_info
*fs_info
)
1978 struct btrfs_delayed_node
*curr_node
, *prev_node
;
1980 curr_node
= btrfs_first_delayed_node(fs_info
->delayed_root
);
1982 __btrfs_kill_delayed_node(curr_node
);
1984 prev_node
= curr_node
;
1985 curr_node
= btrfs_next_delayed_node(curr_node
);
1986 btrfs_release_delayed_node(prev_node
);