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"
25 #define BTRFS_DELAYED_WRITEBACK 512
26 #define BTRFS_DELAYED_BACKGROUND 128
27 #define BTRFS_DELAYED_BATCH 16
29 static struct kmem_cache *delayed_node_cache;
31 int __init btrfs_delayed_inode_init(void)
33 delayed_node_cache = kmem_cache_create("btrfs_delayed_node",
34 sizeof(struct btrfs_delayed_node),
36 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
38 if (!delayed_node_cache)
43 void btrfs_delayed_inode_exit(void)
45 if (delayed_node_cache)
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 atomic_set(&delayed_node->refs, 0);
56 delayed_node->count = 0;
57 delayed_node->in_list = 0;
58 delayed_node->inode_dirty = 0;
59 delayed_node->ins_root = RB_ROOT;
60 delayed_node->del_root = RB_ROOT;
61 mutex_init(&delayed_node->mutex);
62 delayed_node->index_cnt = 0;
63 INIT_LIST_HEAD(&delayed_node->n_list);
64 INIT_LIST_HEAD(&delayed_node->p_list);
65 delayed_node->bytes_reserved = 0;
66 memset(&delayed_node->inode_item, 0, sizeof(delayed_node->inode_item));
69 static inline int btrfs_is_continuous_delayed_item(
70 struct btrfs_delayed_item *item1,
71 struct btrfs_delayed_item *item2)
73 if (item1->key.type == BTRFS_DIR_INDEX_KEY &&
74 item1->key.objectid == item2->key.objectid &&
75 item1->key.type == item2->key.type &&
76 item1->key.offset + 1 == item2->key.offset)
81 static inline struct btrfs_delayed_root *btrfs_get_delayed_root(
82 struct btrfs_root *root)
84 return root->fs_info->delayed_root;
87 static struct btrfs_delayed_node *btrfs_get_delayed_node(struct inode *inode)
89 struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
90 struct btrfs_root *root = btrfs_inode->root;
91 u64 ino = btrfs_ino(inode);
92 struct btrfs_delayed_node *node;
94 node = ACCESS_ONCE(btrfs_inode->delayed_node);
96 atomic_inc(&node->refs);
100 spin_lock(&root->inode_lock);
101 node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
103 if (btrfs_inode->delayed_node) {
104 atomic_inc(&node->refs); /* can be accessed */
105 BUG_ON(btrfs_inode->delayed_node != node);
106 spin_unlock(&root->inode_lock);
109 btrfs_inode->delayed_node = node;
110 atomic_inc(&node->refs); /* can be accessed */
111 atomic_inc(&node->refs); /* cached in the inode */
112 spin_unlock(&root->inode_lock);
115 spin_unlock(&root->inode_lock);
120 /* Will return either the node or PTR_ERR(-ENOMEM) */
121 static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
124 struct btrfs_delayed_node *node;
125 struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
126 struct btrfs_root *root = btrfs_inode->root;
127 u64 ino = btrfs_ino(inode);
131 node = btrfs_get_delayed_node(inode);
135 node = kmem_cache_alloc(delayed_node_cache, GFP_NOFS);
137 return ERR_PTR(-ENOMEM);
138 btrfs_init_delayed_node(node, root, ino);
140 atomic_inc(&node->refs); /* cached in the btrfs inode */
141 atomic_inc(&node->refs); /* can be accessed */
143 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
145 kmem_cache_free(delayed_node_cache, node);
149 spin_lock(&root->inode_lock);
150 ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
151 if (ret == -EEXIST) {
152 kmem_cache_free(delayed_node_cache, node);
153 spin_unlock(&root->inode_lock);
154 radix_tree_preload_end();
157 btrfs_inode->delayed_node = node;
158 spin_unlock(&root->inode_lock);
159 radix_tree_preload_end();
165 * Call it when holding delayed_node->mutex
167 * If mod = 1, add this node into the prepared list.
169 static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root,
170 struct btrfs_delayed_node *node,
173 spin_lock(&root->lock);
175 if (!list_empty(&node->p_list))
176 list_move_tail(&node->p_list, &root->prepare_list);
178 list_add_tail(&node->p_list, &root->prepare_list);
180 list_add_tail(&node->n_list, &root->node_list);
181 list_add_tail(&node->p_list, &root->prepare_list);
182 atomic_inc(&node->refs); /* inserted into list */
186 spin_unlock(&root->lock);
189 /* Call it when holding delayed_node->mutex */
190 static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
191 struct btrfs_delayed_node *node)
193 spin_lock(&root->lock);
196 atomic_dec(&node->refs); /* not in the list */
197 list_del_init(&node->n_list);
198 if (!list_empty(&node->p_list))
199 list_del_init(&node->p_list);
202 spin_unlock(&root->lock);
205 static struct btrfs_delayed_node *btrfs_first_delayed_node(
206 struct btrfs_delayed_root *delayed_root)
209 struct btrfs_delayed_node *node = NULL;
211 spin_lock(&delayed_root->lock);
212 if (list_empty(&delayed_root->node_list))
215 p = delayed_root->node_list.next;
216 node = list_entry(p, struct btrfs_delayed_node, n_list);
217 atomic_inc(&node->refs);
219 spin_unlock(&delayed_root->lock);
224 static struct btrfs_delayed_node *btrfs_next_delayed_node(
225 struct btrfs_delayed_node *node)
227 struct btrfs_delayed_root *delayed_root;
229 struct btrfs_delayed_node *next = NULL;
231 delayed_root = node->root->fs_info->delayed_root;
232 spin_lock(&delayed_root->lock);
233 if (!node->in_list) { /* not in the list */
234 if (list_empty(&delayed_root->node_list))
236 p = delayed_root->node_list.next;
237 } else if (list_is_last(&node->n_list, &delayed_root->node_list))
240 p = node->n_list.next;
242 next = list_entry(p, struct btrfs_delayed_node, n_list);
243 atomic_inc(&next->refs);
245 spin_unlock(&delayed_root->lock);
250 static void __btrfs_release_delayed_node(
251 struct btrfs_delayed_node *delayed_node,
254 struct btrfs_delayed_root *delayed_root;
259 delayed_root = delayed_node->root->fs_info->delayed_root;
261 mutex_lock(&delayed_node->mutex);
262 if (delayed_node->count)
263 btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
265 btrfs_dequeue_delayed_node(delayed_root, delayed_node);
266 mutex_unlock(&delayed_node->mutex);
268 if (atomic_dec_and_test(&delayed_node->refs)) {
269 struct btrfs_root *root = delayed_node->root;
270 spin_lock(&root->inode_lock);
271 if (atomic_read(&delayed_node->refs) == 0) {
272 radix_tree_delete(&root->delayed_nodes_tree,
273 delayed_node->inode_id);
274 kmem_cache_free(delayed_node_cache, delayed_node);
276 spin_unlock(&root->inode_lock);
280 static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
282 __btrfs_release_delayed_node(node, 0);
285 static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
286 struct btrfs_delayed_root *delayed_root)
289 struct btrfs_delayed_node *node = NULL;
291 spin_lock(&delayed_root->lock);
292 if (list_empty(&delayed_root->prepare_list))
295 p = delayed_root->prepare_list.next;
297 node = list_entry(p, struct btrfs_delayed_node, p_list);
298 atomic_inc(&node->refs);
300 spin_unlock(&delayed_root->lock);
305 static inline void btrfs_release_prepared_delayed_node(
306 struct btrfs_delayed_node *node)
308 __btrfs_release_delayed_node(node, 1);
311 static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len)
313 struct btrfs_delayed_item *item;
314 item = kmalloc(sizeof(*item) + data_len, GFP_NOFS);
316 item->data_len = data_len;
317 item->ins_or_del = 0;
318 item->bytes_reserved = 0;
319 item->delayed_node = NULL;
320 atomic_set(&item->refs, 1);
326 * __btrfs_lookup_delayed_item - look up the delayed item by key
327 * @delayed_node: pointer to the delayed node
328 * @key: the key to look up
329 * @prev: used to store the prev item if the right item isn't found
330 * @next: used to store the next item if the right item isn't found
332 * Note: if we don't find the right item, we will return the prev item and
335 static struct btrfs_delayed_item *__btrfs_lookup_delayed_item(
336 struct rb_root *root,
337 struct btrfs_key *key,
338 struct btrfs_delayed_item **prev,
339 struct btrfs_delayed_item **next)
341 struct rb_node *node, *prev_node = NULL;
342 struct btrfs_delayed_item *delayed_item = NULL;
345 node = root->rb_node;
348 delayed_item = rb_entry(node, struct btrfs_delayed_item,
351 ret = btrfs_comp_cpu_keys(&delayed_item->key, key);
353 node = node->rb_right;
355 node = node->rb_left;
364 *prev = delayed_item;
365 else if ((node = rb_prev(prev_node)) != NULL) {
366 *prev = rb_entry(node, struct btrfs_delayed_item,
376 *next = delayed_item;
377 else if ((node = rb_next(prev_node)) != NULL) {
378 *next = rb_entry(node, struct btrfs_delayed_item,
386 static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item(
387 struct btrfs_delayed_node *delayed_node,
388 struct btrfs_key *key)
390 struct btrfs_delayed_item *item;
392 item = __btrfs_lookup_delayed_item(&delayed_node->ins_root, key,
397 static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node,
398 struct btrfs_delayed_item *ins,
401 struct rb_node **p, *node;
402 struct rb_node *parent_node = NULL;
403 struct rb_root *root;
404 struct btrfs_delayed_item *item;
407 if (action == BTRFS_DELAYED_INSERTION_ITEM)
408 root = &delayed_node->ins_root;
409 else if (action == BTRFS_DELAYED_DELETION_ITEM)
410 root = &delayed_node->del_root;
414 node = &ins->rb_node;
418 item = rb_entry(parent_node, struct btrfs_delayed_item,
421 cmp = btrfs_comp_cpu_keys(&item->key, &ins->key);
430 rb_link_node(node, parent_node, p);
431 rb_insert_color(node, root);
432 ins->delayed_node = delayed_node;
433 ins->ins_or_del = action;
435 if (ins->key.type == BTRFS_DIR_INDEX_KEY &&
436 action == BTRFS_DELAYED_INSERTION_ITEM &&
437 ins->key.offset >= delayed_node->index_cnt)
438 delayed_node->index_cnt = ins->key.offset + 1;
440 delayed_node->count++;
441 atomic_inc(&delayed_node->root->fs_info->delayed_root->items);
445 static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node,
446 struct btrfs_delayed_item *item)
448 return __btrfs_add_delayed_item(node, item,
449 BTRFS_DELAYED_INSERTION_ITEM);
452 static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node,
453 struct btrfs_delayed_item *item)
455 return __btrfs_add_delayed_item(node, item,
456 BTRFS_DELAYED_DELETION_ITEM);
459 static void finish_one_item(struct btrfs_delayed_root *delayed_root)
461 int seq = atomic_inc_return(&delayed_root->items_seq);
462 if ((atomic_dec_return(&delayed_root->items) <
463 BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0) &&
464 waitqueue_active(&delayed_root->wait))
465 wake_up(&delayed_root->wait);
468 static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
470 struct rb_root *root;
471 struct btrfs_delayed_root *delayed_root;
473 delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root;
475 BUG_ON(!delayed_root);
476 BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM &&
477 delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM);
479 if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM)
480 root = &delayed_item->delayed_node->ins_root;
482 root = &delayed_item->delayed_node->del_root;
484 rb_erase(&delayed_item->rb_node, root);
485 delayed_item->delayed_node->count--;
487 finish_one_item(delayed_root);
490 static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
493 __btrfs_remove_delayed_item(item);
494 if (atomic_dec_and_test(&item->refs))
499 static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
500 struct btrfs_delayed_node *delayed_node)
503 struct btrfs_delayed_item *item = NULL;
505 p = rb_first(&delayed_node->ins_root);
507 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
512 static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
513 struct btrfs_delayed_node *delayed_node)
516 struct btrfs_delayed_item *item = NULL;
518 p = rb_first(&delayed_node->del_root);
520 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
525 static struct btrfs_delayed_item *__btrfs_next_delayed_item(
526 struct btrfs_delayed_item *item)
529 struct btrfs_delayed_item *next = NULL;
531 p = rb_next(&item->rb_node);
533 next = rb_entry(p, struct btrfs_delayed_item, rb_node);
538 static inline struct btrfs_root *btrfs_get_fs_root(struct btrfs_root *root,
541 struct btrfs_key root_key;
543 if (root->objectid == root_id)
546 root_key.objectid = root_id;
547 root_key.type = BTRFS_ROOT_ITEM_KEY;
548 root_key.offset = (u64)-1;
549 return btrfs_read_fs_root_no_name(root->fs_info, &root_key);
552 static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
553 struct btrfs_root *root,
554 struct btrfs_delayed_item *item)
556 struct btrfs_block_rsv *src_rsv;
557 struct btrfs_block_rsv *dst_rsv;
561 if (!trans->bytes_reserved)
564 src_rsv = trans->block_rsv;
565 dst_rsv = &root->fs_info->delayed_block_rsv;
567 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
568 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
570 trace_btrfs_space_reservation(root->fs_info, "delayed_item",
573 item->bytes_reserved = num_bytes;
579 static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
580 struct btrfs_delayed_item *item)
582 struct btrfs_block_rsv *rsv;
584 if (!item->bytes_reserved)
587 rsv = &root->fs_info->delayed_block_rsv;
588 trace_btrfs_space_reservation(root->fs_info, "delayed_item",
589 item->key.objectid, item->bytes_reserved,
591 btrfs_block_rsv_release(root, rsv,
592 item->bytes_reserved);
595 static int btrfs_delayed_inode_reserve_metadata(
596 struct btrfs_trans_handle *trans,
597 struct btrfs_root *root,
599 struct btrfs_delayed_node *node)
601 struct btrfs_block_rsv *src_rsv;
602 struct btrfs_block_rsv *dst_rsv;
605 bool release = false;
607 src_rsv = trans->block_rsv;
608 dst_rsv = &root->fs_info->delayed_block_rsv;
610 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
613 * btrfs_dirty_inode will update the inode under btrfs_join_transaction
614 * which doesn't reserve space for speed. This is a problem since we
615 * still need to reserve space for this update, so try to reserve the
618 * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
619 * we're accounted for.
621 if (!src_rsv || (!trans->bytes_reserved &&
622 src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
623 ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
624 BTRFS_RESERVE_NO_FLUSH);
626 * Since we're under a transaction reserve_metadata_bytes could
627 * try to commit the transaction which will make it return
628 * EAGAIN to make us stop the transaction we have, so return
629 * ENOSPC instead so that btrfs_dirty_inode knows what to do.
634 node->bytes_reserved = num_bytes;
635 trace_btrfs_space_reservation(root->fs_info,
641 } else if (src_rsv->type == BTRFS_BLOCK_RSV_DELALLOC) {
642 spin_lock(&BTRFS_I(inode)->lock);
643 if (test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
644 &BTRFS_I(inode)->runtime_flags)) {
645 spin_unlock(&BTRFS_I(inode)->lock);
649 spin_unlock(&BTRFS_I(inode)->lock);
651 /* Ok we didn't have space pre-reserved. This shouldn't happen
652 * too often but it can happen if we do delalloc to an existing
653 * inode which gets dirtied because of the time update, and then
654 * isn't touched again until after the transaction commits and
655 * then we try to write out the data. First try to be nice and
656 * reserve something strictly for us. If not be a pain and try
657 * to steal from the delalloc block rsv.
659 ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
660 BTRFS_RESERVE_NO_FLUSH);
664 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
669 * Ok this is a problem, let's just steal from the global rsv
670 * since this really shouldn't happen that often.
673 ret = btrfs_block_rsv_migrate(&root->fs_info->global_block_rsv,
679 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
683 * Migrate only takes a reservation, it doesn't touch the size of the
684 * block_rsv. This is to simplify people who don't normally have things
685 * migrated from their block rsv. If they go to release their
686 * reservation, that will decrease the size as well, so if migrate
687 * reduced size we'd end up with a negative size. But for the
688 * delalloc_meta_reserved stuff we will only know to drop 1 reservation,
689 * but we could in fact do this reserve/migrate dance several times
690 * between the time we did the original reservation and we'd clean it
691 * up. So to take care of this, release the space for the meta
692 * reservation here. I think it may be time for a documentation page on
693 * how block rsvs. work.
696 trace_btrfs_space_reservation(root->fs_info, "delayed_inode",
697 btrfs_ino(inode), num_bytes, 1);
698 node->bytes_reserved = num_bytes;
702 trace_btrfs_space_reservation(root->fs_info, "delalloc",
703 btrfs_ino(inode), num_bytes, 0);
704 btrfs_block_rsv_release(root, src_rsv, num_bytes);
710 static void btrfs_delayed_inode_release_metadata(struct btrfs_root *root,
711 struct btrfs_delayed_node *node)
713 struct btrfs_block_rsv *rsv;
715 if (!node->bytes_reserved)
718 rsv = &root->fs_info->delayed_block_rsv;
719 trace_btrfs_space_reservation(root->fs_info, "delayed_inode",
720 node->inode_id, node->bytes_reserved, 0);
721 btrfs_block_rsv_release(root, rsv,
722 node->bytes_reserved);
723 node->bytes_reserved = 0;
727 * This helper will insert some continuous items into the same leaf according
728 * to the free space of the leaf.
730 static int btrfs_batch_insert_items(struct btrfs_root *root,
731 struct btrfs_path *path,
732 struct btrfs_delayed_item *item)
734 struct btrfs_delayed_item *curr, *next;
736 int total_data_size = 0, total_size = 0;
737 struct extent_buffer *leaf;
739 struct btrfs_key *keys;
741 struct list_head head;
747 BUG_ON(!path->nodes[0]);
749 leaf = path->nodes[0];
750 free_space = btrfs_leaf_free_space(root, leaf);
751 INIT_LIST_HEAD(&head);
757 * count the number of the continuous items that we can insert in batch
759 while (total_size + next->data_len + sizeof(struct btrfs_item) <=
761 total_data_size += next->data_len;
762 total_size += next->data_len + sizeof(struct btrfs_item);
763 list_add_tail(&next->tree_list, &head);
767 next = __btrfs_next_delayed_item(curr);
771 if (!btrfs_is_continuous_delayed_item(curr, next))
781 * we need allocate some memory space, but it might cause the task
782 * to sleep, so we set all locked nodes in the path to blocking locks
785 btrfs_set_path_blocking(path);
787 keys = kmalloc(sizeof(struct btrfs_key) * nitems, GFP_NOFS);
793 data_size = kmalloc(sizeof(u32) * nitems, GFP_NOFS);
799 /* get keys of all the delayed items */
801 list_for_each_entry(next, &head, tree_list) {
803 data_size[i] = next->data_len;
807 /* reset all the locked nodes in the patch to spinning locks. */
808 btrfs_clear_path_blocking(path, NULL, 0);
810 /* insert the keys of the items */
811 setup_items_for_insert(root, path, keys, data_size,
812 total_data_size, total_size, nitems);
814 /* insert the dir index items */
815 slot = path->slots[0];
816 list_for_each_entry_safe(curr, next, &head, tree_list) {
817 data_ptr = btrfs_item_ptr(leaf, slot, char);
818 write_extent_buffer(leaf, &curr->data,
819 (unsigned long)data_ptr,
823 btrfs_delayed_item_release_metadata(root, curr);
825 list_del(&curr->tree_list);
826 btrfs_release_delayed_item(curr);
837 * This helper can just do simple insertion that needn't extend item for new
838 * data, such as directory name index insertion, inode insertion.
840 static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
841 struct btrfs_root *root,
842 struct btrfs_path *path,
843 struct btrfs_delayed_item *delayed_item)
845 struct extent_buffer *leaf;
849 ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->key,
850 delayed_item->data_len);
851 if (ret < 0 && ret != -EEXIST)
854 leaf = path->nodes[0];
856 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
858 write_extent_buffer(leaf, delayed_item->data, (unsigned long)ptr,
859 delayed_item->data_len);
860 btrfs_mark_buffer_dirty(leaf);
862 btrfs_delayed_item_release_metadata(root, delayed_item);
867 * we insert an item first, then if there are some continuous items, we try
868 * to insert those items into the same leaf.
870 static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,
871 struct btrfs_path *path,
872 struct btrfs_root *root,
873 struct btrfs_delayed_node *node)
875 struct btrfs_delayed_item *curr, *prev;
879 mutex_lock(&node->mutex);
880 curr = __btrfs_first_delayed_insertion_item(node);
884 ret = btrfs_insert_delayed_item(trans, root, path, curr);
886 btrfs_release_path(path);
891 curr = __btrfs_next_delayed_item(prev);
892 if (curr && btrfs_is_continuous_delayed_item(prev, curr)) {
893 /* insert the continuous items into the same leaf */
895 btrfs_batch_insert_items(root, path, curr);
897 btrfs_release_delayed_item(prev);
898 btrfs_mark_buffer_dirty(path->nodes[0]);
900 btrfs_release_path(path);
901 mutex_unlock(&node->mutex);
905 mutex_unlock(&node->mutex);
909 static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
910 struct btrfs_root *root,
911 struct btrfs_path *path,
912 struct btrfs_delayed_item *item)
914 struct btrfs_delayed_item *curr, *next;
915 struct extent_buffer *leaf;
916 struct btrfs_key key;
917 struct list_head head;
918 int nitems, i, last_item;
921 BUG_ON(!path->nodes[0]);
923 leaf = path->nodes[0];
926 last_item = btrfs_header_nritems(leaf) - 1;
928 return -ENOENT; /* FIXME: Is errno suitable? */
931 INIT_LIST_HEAD(&head);
932 btrfs_item_key_to_cpu(leaf, &key, i);
935 * count the number of the dir index items that we can delete in batch
937 while (btrfs_comp_cpu_keys(&next->key, &key) == 0) {
938 list_add_tail(&next->tree_list, &head);
942 next = __btrfs_next_delayed_item(curr);
946 if (!btrfs_is_continuous_delayed_item(curr, next))
952 btrfs_item_key_to_cpu(leaf, &key, i);
958 ret = btrfs_del_items(trans, root, path, path->slots[0], nitems);
962 list_for_each_entry_safe(curr, next, &head, tree_list) {
963 btrfs_delayed_item_release_metadata(root, curr);
964 list_del(&curr->tree_list);
965 btrfs_release_delayed_item(curr);
972 static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans,
973 struct btrfs_path *path,
974 struct btrfs_root *root,
975 struct btrfs_delayed_node *node)
977 struct btrfs_delayed_item *curr, *prev;
981 mutex_lock(&node->mutex);
982 curr = __btrfs_first_delayed_deletion_item(node);
986 ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1);
991 * can't find the item which the node points to, so this node
992 * is invalid, just drop it.
995 curr = __btrfs_next_delayed_item(prev);
996 btrfs_release_delayed_item(prev);
998 btrfs_release_path(path);
1000 mutex_unlock(&node->mutex);
1006 btrfs_batch_delete_items(trans, root, path, curr);
1007 btrfs_release_path(path);
1008 mutex_unlock(&node->mutex);
1012 btrfs_release_path(path);
1013 mutex_unlock(&node->mutex);
1017 static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
1019 struct btrfs_delayed_root *delayed_root;
1021 if (delayed_node && delayed_node->inode_dirty) {
1022 BUG_ON(!delayed_node->root);
1023 delayed_node->inode_dirty = 0;
1024 delayed_node->count--;
1026 delayed_root = delayed_node->root->fs_info->delayed_root;
1027 finish_one_item(delayed_root);
1031 static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1032 struct btrfs_root *root,
1033 struct btrfs_path *path,
1034 struct btrfs_delayed_node *node)
1036 struct btrfs_key key;
1037 struct btrfs_inode_item *inode_item;
1038 struct extent_buffer *leaf;
1041 key.objectid = node->inode_id;
1042 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1045 ret = btrfs_lookup_inode(trans, root, path, &key, 1);
1047 btrfs_release_path(path);
1049 } else if (ret < 0) {
1053 btrfs_unlock_up_safe(path, 1);
1054 leaf = path->nodes[0];
1055 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1056 struct btrfs_inode_item);
1057 write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
1058 sizeof(struct btrfs_inode_item));
1059 btrfs_mark_buffer_dirty(leaf);
1060 btrfs_release_path(path);
1062 btrfs_delayed_inode_release_metadata(root, node);
1063 btrfs_release_delayed_inode(node);
1068 static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1069 struct btrfs_root *root,
1070 struct btrfs_path *path,
1071 struct btrfs_delayed_node *node)
1075 mutex_lock(&node->mutex);
1076 if (!node->inode_dirty) {
1077 mutex_unlock(&node->mutex);
1081 ret = __btrfs_update_delayed_inode(trans, root, path, node);
1082 mutex_unlock(&node->mutex);
1087 __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1088 struct btrfs_path *path,
1089 struct btrfs_delayed_node *node)
1093 ret = btrfs_insert_delayed_items(trans, path, node->root, node);
1097 ret = btrfs_delete_delayed_items(trans, path, node->root, node);
1101 ret = btrfs_update_delayed_inode(trans, node->root, path, node);
1106 * Called when committing the transaction.
1107 * Returns 0 on success.
1108 * Returns < 0 on error and returns with an aborted transaction with any
1109 * outstanding delayed items cleaned up.
1111 static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1112 struct btrfs_root *root, int nr)
1114 struct btrfs_delayed_root *delayed_root;
1115 struct btrfs_delayed_node *curr_node, *prev_node;
1116 struct btrfs_path *path;
1117 struct btrfs_block_rsv *block_rsv;
1119 bool count = (nr > 0);
1124 path = btrfs_alloc_path();
1127 path->leave_spinning = 1;
1129 block_rsv = trans->block_rsv;
1130 trans->block_rsv = &root->fs_info->delayed_block_rsv;
1132 delayed_root = btrfs_get_delayed_root(root);
1134 curr_node = btrfs_first_delayed_node(delayed_root);
1135 while (curr_node && (!count || (count && nr--))) {
1136 ret = __btrfs_commit_inode_delayed_items(trans, path,
1139 btrfs_release_delayed_node(curr_node);
1141 btrfs_abort_transaction(trans, root, ret);
1145 prev_node = curr_node;
1146 curr_node = btrfs_next_delayed_node(curr_node);
1147 btrfs_release_delayed_node(prev_node);
1151 btrfs_release_delayed_node(curr_node);
1152 btrfs_free_path(path);
1153 trans->block_rsv = block_rsv;
1158 int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1159 struct btrfs_root *root)
1161 return __btrfs_run_delayed_items(trans, root, -1);
1164 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans,
1165 struct btrfs_root *root, int nr)
1167 return __btrfs_run_delayed_items(trans, root, nr);
1170 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1171 struct inode *inode)
1173 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1174 struct btrfs_path *path;
1175 struct btrfs_block_rsv *block_rsv;
1181 mutex_lock(&delayed_node->mutex);
1182 if (!delayed_node->count) {
1183 mutex_unlock(&delayed_node->mutex);
1184 btrfs_release_delayed_node(delayed_node);
1187 mutex_unlock(&delayed_node->mutex);
1189 path = btrfs_alloc_path();
1192 path->leave_spinning = 1;
1194 block_rsv = trans->block_rsv;
1195 trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
1197 ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1199 btrfs_release_delayed_node(delayed_node);
1200 btrfs_free_path(path);
1201 trans->block_rsv = block_rsv;
1206 int btrfs_commit_inode_delayed_inode(struct inode *inode)
1208 struct btrfs_trans_handle *trans;
1209 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1210 struct btrfs_path *path;
1211 struct btrfs_block_rsv *block_rsv;
1217 mutex_lock(&delayed_node->mutex);
1218 if (!delayed_node->inode_dirty) {
1219 mutex_unlock(&delayed_node->mutex);
1220 btrfs_release_delayed_node(delayed_node);
1223 mutex_unlock(&delayed_node->mutex);
1225 trans = btrfs_join_transaction(delayed_node->root);
1226 if (IS_ERR(trans)) {
1227 ret = PTR_ERR(trans);
1231 path = btrfs_alloc_path();
1236 path->leave_spinning = 1;
1238 block_rsv = trans->block_rsv;
1239 trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
1241 mutex_lock(&delayed_node->mutex);
1242 if (delayed_node->inode_dirty)
1243 ret = __btrfs_update_delayed_inode(trans, delayed_node->root,
1244 path, delayed_node);
1247 mutex_unlock(&delayed_node->mutex);
1249 btrfs_free_path(path);
1250 trans->block_rsv = block_rsv;
1252 btrfs_end_transaction(trans, delayed_node->root);
1253 btrfs_btree_balance_dirty(delayed_node->root);
1255 btrfs_release_delayed_node(delayed_node);
1260 void btrfs_remove_delayed_node(struct inode *inode)
1262 struct btrfs_delayed_node *delayed_node;
1264 delayed_node = ACCESS_ONCE(BTRFS_I(inode)->delayed_node);
1268 BTRFS_I(inode)->delayed_node = NULL;
1269 btrfs_release_delayed_node(delayed_node);
1272 struct btrfs_async_delayed_work {
1273 struct btrfs_delayed_root *delayed_root;
1275 struct btrfs_work work;
1278 static void btrfs_async_run_delayed_root(struct btrfs_work *work)
1280 struct btrfs_async_delayed_work *async_work;
1281 struct btrfs_delayed_root *delayed_root;
1282 struct btrfs_trans_handle *trans;
1283 struct btrfs_path *path;
1284 struct btrfs_delayed_node *delayed_node = NULL;
1285 struct btrfs_root *root;
1286 struct btrfs_block_rsv *block_rsv;
1289 async_work = container_of(work, struct btrfs_async_delayed_work, work);
1290 delayed_root = async_work->delayed_root;
1292 path = btrfs_alloc_path();
1297 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND / 2)
1300 delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
1304 path->leave_spinning = 1;
1305 root = delayed_node->root;
1307 trans = btrfs_join_transaction(root);
1311 block_rsv = trans->block_rsv;
1312 trans->block_rsv = &root->fs_info->delayed_block_rsv;
1314 __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1316 * Maybe new delayed items have been inserted, so we need requeue
1317 * the work. Besides that, we must dequeue the empty delayed nodes
1318 * to avoid the race between delayed items balance and the worker.
1319 * The race like this:
1320 * Task1 Worker thread
1321 * count == 0, needn't requeue
1322 * also needn't insert the
1323 * delayed node into prepare
1325 * add lots of delayed items
1326 * queue the delayed node
1327 * already in the list,
1328 * and not in the prepare
1329 * list, it means the delayed
1330 * node is being dealt with
1332 * do delayed items balance
1333 * the delayed node is being
1334 * dealt with by the worker
1336 * the worker goto idle.
1337 * Task1 will sleep until the transaction is commited.
1339 mutex_lock(&delayed_node->mutex);
1340 btrfs_dequeue_delayed_node(root->fs_info->delayed_root, delayed_node);
1341 mutex_unlock(&delayed_node->mutex);
1343 trans->block_rsv = block_rsv;
1344 btrfs_end_transaction_dmeta(trans, root);
1345 btrfs_btree_balance_dirty_nodelay(root);
1348 btrfs_release_path(path);
1351 btrfs_release_prepared_delayed_node(delayed_node);
1352 if (async_work->nr == 0 || total_done < async_work->nr)
1356 btrfs_free_path(path);
1358 wake_up(&delayed_root->wait);
1363 static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
1364 struct btrfs_root *root, int nr)
1366 struct btrfs_async_delayed_work *async_work;
1368 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1371 async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
1375 async_work->delayed_root = delayed_root;
1376 async_work->work.func = btrfs_async_run_delayed_root;
1377 async_work->work.flags = 0;
1378 async_work->nr = nr;
1380 btrfs_queue_worker(&root->fs_info->delayed_workers, &async_work->work);
1384 void btrfs_assert_delayed_root_empty(struct btrfs_root *root)
1386 struct btrfs_delayed_root *delayed_root;
1387 delayed_root = btrfs_get_delayed_root(root);
1388 WARN_ON(btrfs_first_delayed_node(delayed_root));
1391 static int refs_newer(struct btrfs_delayed_root *delayed_root,
1394 int val = atomic_read(&delayed_root->items_seq);
1396 if (val < seq || val >= seq + count)
1401 void btrfs_balance_delayed_items(struct btrfs_root *root)
1403 struct btrfs_delayed_root *delayed_root;
1406 delayed_root = btrfs_get_delayed_root(root);
1408 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1411 seq = atomic_read(&delayed_root->items_seq);
1413 if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
1415 DEFINE_WAIT(__wait);
1417 ret = btrfs_wq_run_delayed_node(delayed_root, root, 0);
1422 prepare_to_wait(&delayed_root->wait, &__wait,
1423 TASK_INTERRUPTIBLE);
1425 if (refs_newer(delayed_root, seq,
1426 BTRFS_DELAYED_BATCH) ||
1427 atomic_read(&delayed_root->items) <
1428 BTRFS_DELAYED_BACKGROUND) {
1431 if (!signal_pending(current))
1436 finish_wait(&delayed_root->wait, &__wait);
1439 btrfs_wq_run_delayed_node(delayed_root, root, BTRFS_DELAYED_BATCH);
1442 /* Will return 0 or -ENOMEM */
1443 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1444 struct btrfs_root *root, const char *name,
1445 int name_len, struct inode *dir,
1446 struct btrfs_disk_key *disk_key, u8 type,
1449 struct btrfs_delayed_node *delayed_node;
1450 struct btrfs_delayed_item *delayed_item;
1451 struct btrfs_dir_item *dir_item;
1454 delayed_node = btrfs_get_or_create_delayed_node(dir);
1455 if (IS_ERR(delayed_node))
1456 return PTR_ERR(delayed_node);
1458 delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len);
1459 if (!delayed_item) {
1464 delayed_item->key.objectid = btrfs_ino(dir);
1465 btrfs_set_key_type(&delayed_item->key, BTRFS_DIR_INDEX_KEY);
1466 delayed_item->key.offset = index;
1468 dir_item = (struct btrfs_dir_item *)delayed_item->data;
1469 dir_item->location = *disk_key;
1470 dir_item->transid = cpu_to_le64(trans->transid);
1471 dir_item->data_len = 0;
1472 dir_item->name_len = cpu_to_le16(name_len);
1473 dir_item->type = type;
1474 memcpy((char *)(dir_item + 1), name, name_len);
1476 ret = btrfs_delayed_item_reserve_metadata(trans, root, delayed_item);
1478 * we have reserved enough space when we start a new transaction,
1479 * so reserving metadata failure is impossible
1484 mutex_lock(&delayed_node->mutex);
1485 ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
1486 if (unlikely(ret)) {
1487 printk(KERN_ERR "err add delayed dir index item(name: %s) into "
1488 "the insertion tree of the delayed node"
1489 "(root id: %llu, inode id: %llu, errno: %d)\n",
1491 (unsigned long long)delayed_node->root->objectid,
1492 (unsigned long long)delayed_node->inode_id,
1496 mutex_unlock(&delayed_node->mutex);
1499 btrfs_release_delayed_node(delayed_node);
1503 static int btrfs_delete_delayed_insertion_item(struct btrfs_root *root,
1504 struct btrfs_delayed_node *node,
1505 struct btrfs_key *key)
1507 struct btrfs_delayed_item *item;
1509 mutex_lock(&node->mutex);
1510 item = __btrfs_lookup_delayed_insertion_item(node, key);
1512 mutex_unlock(&node->mutex);
1516 btrfs_delayed_item_release_metadata(root, item);
1517 btrfs_release_delayed_item(item);
1518 mutex_unlock(&node->mutex);
1522 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1523 struct btrfs_root *root, struct inode *dir,
1526 struct btrfs_delayed_node *node;
1527 struct btrfs_delayed_item *item;
1528 struct btrfs_key item_key;
1531 node = btrfs_get_or_create_delayed_node(dir);
1533 return PTR_ERR(node);
1535 item_key.objectid = btrfs_ino(dir);
1536 btrfs_set_key_type(&item_key, BTRFS_DIR_INDEX_KEY);
1537 item_key.offset = index;
1539 ret = btrfs_delete_delayed_insertion_item(root, node, &item_key);
1543 item = btrfs_alloc_delayed_item(0);
1549 item->key = item_key;
1551 ret = btrfs_delayed_item_reserve_metadata(trans, root, item);
1553 * we have reserved enough space when we start a new transaction,
1554 * so reserving metadata failure is impossible.
1558 mutex_lock(&node->mutex);
1559 ret = __btrfs_add_delayed_deletion_item(node, item);
1560 if (unlikely(ret)) {
1561 printk(KERN_ERR "err add delayed dir index item(index: %llu) "
1562 "into the deletion tree of the delayed node"
1563 "(root id: %llu, inode id: %llu, errno: %d)\n",
1564 (unsigned long long)index,
1565 (unsigned long long)node->root->objectid,
1566 (unsigned long long)node->inode_id,
1570 mutex_unlock(&node->mutex);
1572 btrfs_release_delayed_node(node);
1576 int btrfs_inode_delayed_dir_index_count(struct inode *inode)
1578 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1584 * Since we have held i_mutex of this directory, it is impossible that
1585 * a new directory index is added into the delayed node and index_cnt
1586 * is updated now. So we needn't lock the delayed node.
1588 if (!delayed_node->index_cnt) {
1589 btrfs_release_delayed_node(delayed_node);
1593 BTRFS_I(inode)->index_cnt = delayed_node->index_cnt;
1594 btrfs_release_delayed_node(delayed_node);
1598 void btrfs_get_delayed_items(struct inode *inode, struct list_head *ins_list,
1599 struct list_head *del_list)
1601 struct btrfs_delayed_node *delayed_node;
1602 struct btrfs_delayed_item *item;
1604 delayed_node = btrfs_get_delayed_node(inode);
1608 mutex_lock(&delayed_node->mutex);
1609 item = __btrfs_first_delayed_insertion_item(delayed_node);
1611 atomic_inc(&item->refs);
1612 list_add_tail(&item->readdir_list, ins_list);
1613 item = __btrfs_next_delayed_item(item);
1616 item = __btrfs_first_delayed_deletion_item(delayed_node);
1618 atomic_inc(&item->refs);
1619 list_add_tail(&item->readdir_list, del_list);
1620 item = __btrfs_next_delayed_item(item);
1622 mutex_unlock(&delayed_node->mutex);
1624 * This delayed node is still cached in the btrfs inode, so refs
1625 * must be > 1 now, and we needn't check it is going to be freed
1628 * Besides that, this function is used to read dir, we do not
1629 * insert/delete delayed items in this period. So we also needn't
1630 * requeue or dequeue this delayed node.
1632 atomic_dec(&delayed_node->refs);
1635 void btrfs_put_delayed_items(struct list_head *ins_list,
1636 struct list_head *del_list)
1638 struct btrfs_delayed_item *curr, *next;
1640 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1641 list_del(&curr->readdir_list);
1642 if (atomic_dec_and_test(&curr->refs))
1646 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1647 list_del(&curr->readdir_list);
1648 if (atomic_dec_and_test(&curr->refs))
1653 int btrfs_should_delete_dir_index(struct list_head *del_list,
1656 struct btrfs_delayed_item *curr, *next;
1659 if (list_empty(del_list))
1662 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1663 if (curr->key.offset > index)
1666 list_del(&curr->readdir_list);
1667 ret = (curr->key.offset == index);
1669 if (atomic_dec_and_test(&curr->refs))
1681 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1684 int btrfs_readdir_delayed_dir_index(struct file *filp, void *dirent,
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 < filp->f_pos) {
1708 if (atomic_dec_and_test(&curr->refs))
1713 filp->f_pos = curr->key.offset;
1715 di = (struct btrfs_dir_item *)curr->data;
1716 name = (char *)(di + 1);
1717 name_len = le16_to_cpu(di->name_len);
1719 d_type = btrfs_filetype_table[di->type];
1720 btrfs_disk_key_to_cpu(&location, &di->location);
1722 over = filldir(dirent, name, name_len, curr->key.offset,
1723 location.objectid, d_type);
1725 if (atomic_dec_and_test(&curr->refs))
1734 BTRFS_SETGET_STACK_FUNCS(stack_inode_generation, struct btrfs_inode_item,
1736 BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence, struct btrfs_inode_item,
1738 BTRFS_SETGET_STACK_FUNCS(stack_inode_transid, struct btrfs_inode_item,
1740 BTRFS_SETGET_STACK_FUNCS(stack_inode_size, struct btrfs_inode_item, size, 64);
1741 BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes, struct btrfs_inode_item,
1743 BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group, struct btrfs_inode_item,
1745 BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink, struct btrfs_inode_item, nlink, 32);
1746 BTRFS_SETGET_STACK_FUNCS(stack_inode_uid, struct btrfs_inode_item, uid, 32);
1747 BTRFS_SETGET_STACK_FUNCS(stack_inode_gid, struct btrfs_inode_item, gid, 32);
1748 BTRFS_SETGET_STACK_FUNCS(stack_inode_mode, struct btrfs_inode_item, mode, 32);
1749 BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev, struct btrfs_inode_item, rdev, 64);
1750 BTRFS_SETGET_STACK_FUNCS(stack_inode_flags, struct btrfs_inode_item, flags, 64);
1752 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64);
1753 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32);
1755 static void fill_stack_inode_item(struct btrfs_trans_handle *trans,
1756 struct btrfs_inode_item *inode_item,
1757 struct inode *inode)
1759 btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
1760 btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
1761 btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size);
1762 btrfs_set_stack_inode_mode(inode_item, inode->i_mode);
1763 btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink);
1764 btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode));
1765 btrfs_set_stack_inode_generation(inode_item,
1766 BTRFS_I(inode)->generation);
1767 btrfs_set_stack_inode_sequence(inode_item, inode->i_version);
1768 btrfs_set_stack_inode_transid(inode_item, trans->transid);
1769 btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev);
1770 btrfs_set_stack_inode_flags(inode_item, BTRFS_I(inode)->flags);
1771 btrfs_set_stack_inode_block_group(inode_item, 0);
1773 btrfs_set_stack_timespec_sec(btrfs_inode_atime(inode_item),
1774 inode->i_atime.tv_sec);
1775 btrfs_set_stack_timespec_nsec(btrfs_inode_atime(inode_item),
1776 inode->i_atime.tv_nsec);
1778 btrfs_set_stack_timespec_sec(btrfs_inode_mtime(inode_item),
1779 inode->i_mtime.tv_sec);
1780 btrfs_set_stack_timespec_nsec(btrfs_inode_mtime(inode_item),
1781 inode->i_mtime.tv_nsec);
1783 btrfs_set_stack_timespec_sec(btrfs_inode_ctime(inode_item),
1784 inode->i_ctime.tv_sec);
1785 btrfs_set_stack_timespec_nsec(btrfs_inode_ctime(inode_item),
1786 inode->i_ctime.tv_nsec);
1789 int btrfs_fill_inode(struct inode *inode, u32 *rdev)
1791 struct btrfs_delayed_node *delayed_node;
1792 struct btrfs_inode_item *inode_item;
1793 struct btrfs_timespec *tspec;
1795 delayed_node = btrfs_get_delayed_node(inode);
1799 mutex_lock(&delayed_node->mutex);
1800 if (!delayed_node->inode_dirty) {
1801 mutex_unlock(&delayed_node->mutex);
1802 btrfs_release_delayed_node(delayed_node);
1806 inode_item = &delayed_node->inode_item;
1808 i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
1809 i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
1810 btrfs_i_size_write(inode, btrfs_stack_inode_size(inode_item));
1811 inode->i_mode = btrfs_stack_inode_mode(inode_item);
1812 set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
1813 inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item));
1814 BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
1815 inode->i_version = btrfs_stack_inode_sequence(inode_item);
1817 *rdev = btrfs_stack_inode_rdev(inode_item);
1818 BTRFS_I(inode)->flags = btrfs_stack_inode_flags(inode_item);
1820 tspec = btrfs_inode_atime(inode_item);
1821 inode->i_atime.tv_sec = btrfs_stack_timespec_sec(tspec);
1822 inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1824 tspec = btrfs_inode_mtime(inode_item);
1825 inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(tspec);
1826 inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1828 tspec = btrfs_inode_ctime(inode_item);
1829 inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(tspec);
1830 inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1832 inode->i_generation = BTRFS_I(inode)->generation;
1833 BTRFS_I(inode)->index_cnt = (u64)-1;
1835 mutex_unlock(&delayed_node->mutex);
1836 btrfs_release_delayed_node(delayed_node);
1840 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
1841 struct btrfs_root *root, struct inode *inode)
1843 struct btrfs_delayed_node *delayed_node;
1846 delayed_node = btrfs_get_or_create_delayed_node(inode);
1847 if (IS_ERR(delayed_node))
1848 return PTR_ERR(delayed_node);
1850 mutex_lock(&delayed_node->mutex);
1851 if (delayed_node->inode_dirty) {
1852 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1856 ret = btrfs_delayed_inode_reserve_metadata(trans, root, inode,
1861 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1862 delayed_node->inode_dirty = 1;
1863 delayed_node->count++;
1864 atomic_inc(&root->fs_info->delayed_root->items);
1866 mutex_unlock(&delayed_node->mutex);
1867 btrfs_release_delayed_node(delayed_node);
1871 static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1873 struct btrfs_root *root = delayed_node->root;
1874 struct btrfs_delayed_item *curr_item, *prev_item;
1876 mutex_lock(&delayed_node->mutex);
1877 curr_item = __btrfs_first_delayed_insertion_item(delayed_node);
1879 btrfs_delayed_item_release_metadata(root, curr_item);
1880 prev_item = curr_item;
1881 curr_item = __btrfs_next_delayed_item(prev_item);
1882 btrfs_release_delayed_item(prev_item);
1885 curr_item = __btrfs_first_delayed_deletion_item(delayed_node);
1887 btrfs_delayed_item_release_metadata(root, curr_item);
1888 prev_item = curr_item;
1889 curr_item = __btrfs_next_delayed_item(prev_item);
1890 btrfs_release_delayed_item(prev_item);
1893 if (delayed_node->inode_dirty) {
1894 btrfs_delayed_inode_release_metadata(root, delayed_node);
1895 btrfs_release_delayed_inode(delayed_node);
1897 mutex_unlock(&delayed_node->mutex);
1900 void btrfs_kill_delayed_inode_items(struct inode *inode)
1902 struct btrfs_delayed_node *delayed_node;
1904 delayed_node = btrfs_get_delayed_node(inode);
1908 __btrfs_kill_delayed_node(delayed_node);
1909 btrfs_release_delayed_node(delayed_node);
1912 void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
1915 struct btrfs_delayed_node *delayed_nodes[8];
1919 spin_lock(&root->inode_lock);
1920 n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
1921 (void **)delayed_nodes, inode_id,
1922 ARRAY_SIZE(delayed_nodes));
1924 spin_unlock(&root->inode_lock);
1928 inode_id = delayed_nodes[n - 1]->inode_id + 1;
1930 for (i = 0; i < n; i++)
1931 atomic_inc(&delayed_nodes[i]->refs);
1932 spin_unlock(&root->inode_lock);
1934 for (i = 0; i < n; i++) {
1935 __btrfs_kill_delayed_node(delayed_nodes[i]);
1936 btrfs_release_delayed_node(delayed_nodes[i]);
1941 void btrfs_destroy_delayed_inodes(struct btrfs_root *root)
1943 struct btrfs_delayed_root *delayed_root;
1944 struct btrfs_delayed_node *curr_node, *prev_node;
1946 delayed_root = btrfs_get_delayed_root(root);
1948 curr_node = btrfs_first_delayed_node(delayed_root);
1950 __btrfs_kill_delayed_node(curr_node);
1952 prev_node = curr_node;
1953 curr_node = btrfs_next_delayed_node(curr_node);
1954 btrfs_release_delayed_node(prev_node);