1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2011 Fujitsu. All rights reserved.
4 * Written by Miao Xie <miaox@cn.fujitsu.com>
7 #ifndef BTRFS_DELAYED_INODE_H
8 #define BTRFS_DELAYED_INODE_H
10 #include <linux/rbtree.h>
11 #include <linux/spinlock.h>
12 #include <linux/mutex.h>
13 #include <linux/list.h>
14 #include <linux/wait.h>
15 #include <linux/atomic.h>
16 #include <linux/refcount.h>
19 /* types of the delayed item */
20 #define BTRFS_DELAYED_INSERTION_ITEM 1
21 #define BTRFS_DELAYED_DELETION_ITEM 2
23 struct btrfs_delayed_root {
25 struct list_head node_list;
27 * Used for delayed nodes which is waiting to be dealt with by the
28 * worker. If the delayed node is inserted into the work queue, we
29 * drop it from this list.
31 struct list_head prepare_list;
32 atomic_t items; /* for delayed items */
33 atomic_t items_seq; /* for delayed items */
34 int nodes; /* for delayed nodes */
35 wait_queue_head_t wait;
38 #define BTRFS_DELAYED_NODE_IN_LIST 0
39 #define BTRFS_DELAYED_NODE_INODE_DIRTY 1
40 #define BTRFS_DELAYED_NODE_DEL_IREF 2
42 struct btrfs_delayed_node {
45 struct btrfs_root *root;
46 /* Used to add the node into the delayed root's node list. */
47 struct list_head n_list;
49 * Used to add the node into the prepare list, the nodes in this list
50 * is waiting to be dealt with by the async worker.
52 struct list_head p_list;
53 struct rb_root_cached ins_root;
54 struct rb_root_cached del_root;
56 struct btrfs_inode_item inode_item;
63 struct btrfs_delayed_item {
64 struct rb_node rb_node;
66 struct list_head tree_list; /* used for batch insert/delete items */
67 struct list_head readdir_list; /* used for readdir items */
69 struct btrfs_delayed_node *delayed_node;
76 static inline void btrfs_init_delayed_root(
77 struct btrfs_delayed_root *delayed_root)
79 atomic_set(&delayed_root->items, 0);
80 atomic_set(&delayed_root->items_seq, 0);
81 delayed_root->nodes = 0;
82 spin_lock_init(&delayed_root->lock);
83 init_waitqueue_head(&delayed_root->wait);
84 INIT_LIST_HEAD(&delayed_root->node_list);
85 INIT_LIST_HEAD(&delayed_root->prepare_list);
88 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
89 const char *name, int name_len,
90 struct btrfs_inode *dir,
91 struct btrfs_disk_key *disk_key, u8 type,
94 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
95 struct btrfs_inode *dir, u64 index);
97 int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode);
99 int btrfs_run_delayed_items(struct btrfs_trans_handle *trans);
100 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr);
102 void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info);
104 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
105 struct btrfs_inode *inode);
106 /* Used for evicting the inode. */
107 void btrfs_remove_delayed_node(struct btrfs_inode *inode);
108 void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode);
109 int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode);
112 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
113 struct btrfs_root *root,
114 struct btrfs_inode *inode);
115 int btrfs_fill_inode(struct inode *inode, u32 *rdev);
116 int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode);
118 /* Used for drop dead root */
119 void btrfs_kill_all_delayed_nodes(struct btrfs_root *root);
121 /* Used for clean the transaction */
122 void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info);
124 /* Used for readdir() */
125 bool btrfs_readdir_get_delayed_items(struct inode *inode,
126 struct list_head *ins_list,
127 struct list_head *del_list);
128 void btrfs_readdir_put_delayed_items(struct inode *inode,
129 struct list_head *ins_list,
130 struct list_head *del_list);
131 int btrfs_should_delete_dir_index(struct list_head *del_list,
133 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
134 struct list_head *ins_list);
137 int __init btrfs_delayed_inode_init(void);
138 void __cold btrfs_delayed_inode_exit(void);
141 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info);