xref: /linux/fs/btrfs/delayed-inode.h (revision b37042b2bb7cd751f03b73afb90364a418d870f4)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2011 Fujitsu.  All rights reserved.
4  * Written by Miao Xie <miaox@cn.fujitsu.com>
5  */
6 
7 #ifndef BTRFS_DELAYED_INODE_H
8 #define BTRFS_DELAYED_INODE_H
9 
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>
17 #include "ctree.h"
18 
19 /* types of the delayed item */
20 #define BTRFS_DELAYED_INSERTION_ITEM	1
21 #define BTRFS_DELAYED_DELETION_ITEM	2
22 
23 struct btrfs_delayed_root {
24 	spinlock_t lock;
25 	struct list_head node_list;
26 	/*
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.
30 	 */
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;
36 };
37 
38 #define BTRFS_DELAYED_NODE_IN_LIST	0
39 #define BTRFS_DELAYED_NODE_INODE_DIRTY	1
40 #define BTRFS_DELAYED_NODE_DEL_IREF	2
41 
42 struct btrfs_delayed_node {
43 	u64 inode_id;
44 	u64 bytes_reserved;
45 	struct btrfs_root *root;
46 	/* Used to add the node into the delayed root's node list. */
47 	struct list_head n_list;
48 	/*
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.
51 	 */
52 	struct list_head p_list;
53 	struct rb_root_cached ins_root;
54 	struct rb_root_cached del_root;
55 	struct mutex mutex;
56 	struct btrfs_inode_item inode_item;
57 	refcount_t refs;
58 	u64 index_cnt;
59 	unsigned long flags;
60 	int count;
61 	/*
62 	 * The size of the next batch of dir index items to insert (if this
63 	 * node is from a directory inode). Protected by @mutex.
64 	 */
65 	u32 curr_index_batch_size;
66 	/*
67 	 * Number of leaves reserved for inserting dir index items (if this
68 	 * node belongs to a directory inode). This may be larger then the
69 	 * actual number of leaves we end up using. Protected by @mutex.
70 	 */
71 	u32 index_item_leaves;
72 };
73 
74 struct btrfs_delayed_item {
75 	struct rb_node rb_node;
76 	struct btrfs_key key;
77 	struct list_head tree_list;	/* used for batch insert/delete items */
78 	struct list_head readdir_list;	/* used for readdir items */
79 	u64 bytes_reserved;
80 	struct btrfs_delayed_node *delayed_node;
81 	refcount_t refs;
82 	int ins_or_del;
83 	u32 data_len;
84 	char data[];
85 };
86 
87 static inline void btrfs_init_delayed_root(
88 				struct btrfs_delayed_root *delayed_root)
89 {
90 	atomic_set(&delayed_root->items, 0);
91 	atomic_set(&delayed_root->items_seq, 0);
92 	delayed_root->nodes = 0;
93 	spin_lock_init(&delayed_root->lock);
94 	init_waitqueue_head(&delayed_root->wait);
95 	INIT_LIST_HEAD(&delayed_root->node_list);
96 	INIT_LIST_HEAD(&delayed_root->prepare_list);
97 }
98 
99 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
100 				   const char *name, int name_len,
101 				   struct btrfs_inode *dir,
102 				   struct btrfs_disk_key *disk_key, u8 type,
103 				   u64 index);
104 
105 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
106 				   struct btrfs_inode *dir, u64 index);
107 
108 int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode);
109 
110 int btrfs_run_delayed_items(struct btrfs_trans_handle *trans);
111 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr);
112 
113 void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info);
114 
115 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
116 				     struct btrfs_inode *inode);
117 /* Used for evicting the inode. */
118 void btrfs_remove_delayed_node(struct btrfs_inode *inode);
119 void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode);
120 int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode);
121 
122 
123 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
124 			       struct btrfs_root *root,
125 			       struct btrfs_inode *inode);
126 int btrfs_fill_inode(struct inode *inode, u32 *rdev);
127 int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode);
128 
129 /* Used for drop dead root */
130 void btrfs_kill_all_delayed_nodes(struct btrfs_root *root);
131 
132 /* Used for clean the transaction */
133 void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info);
134 
135 /* Used for readdir() */
136 bool btrfs_readdir_get_delayed_items(struct inode *inode,
137 				     struct list_head *ins_list,
138 				     struct list_head *del_list);
139 void btrfs_readdir_put_delayed_items(struct inode *inode,
140 				     struct list_head *ins_list,
141 				     struct list_head *del_list);
142 int btrfs_should_delete_dir_index(struct list_head *del_list,
143 				  u64 index);
144 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
145 				    struct list_head *ins_list);
146 
147 /* for init */
148 int __init btrfs_delayed_inode_init(void);
149 void __cold btrfs_delayed_inode_exit(void);
150 
151 /* for debugging */
152 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info);
153 
154 #endif
155