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 enum btrfs_delayed_item_type {
20 	BTRFS_DELAYED_INSERTION_ITEM,
21 	BTRFS_DELAYED_DELETION_ITEM
22 };
23 
24 struct btrfs_delayed_root {
25 	spinlock_t lock;
26 	struct list_head node_list;
27 	/*
28 	 * Used for delayed nodes which is waiting to be dealt with by the
29 	 * worker. If the delayed node is inserted into the work queue, we
30 	 * drop it from this list.
31 	 */
32 	struct list_head prepare_list;
33 	atomic_t items;		/* for delayed items */
34 	atomic_t items_seq;	/* for delayed items */
35 	int nodes;		/* for delayed nodes */
36 	wait_queue_head_t wait;
37 };
38 
39 #define BTRFS_DELAYED_NODE_IN_LIST	0
40 #define BTRFS_DELAYED_NODE_INODE_DIRTY	1
41 #define BTRFS_DELAYED_NODE_DEL_IREF	2
42 
43 struct btrfs_delayed_node {
44 	u64 inode_id;
45 	u64 bytes_reserved;
46 	struct btrfs_root *root;
47 	/* Used to add the node into the delayed root's node list. */
48 	struct list_head n_list;
49 	/*
50 	 * Used to add the node into the prepare list, the nodes in this list
51 	 * is waiting to be dealt with by the async worker.
52 	 */
53 	struct list_head p_list;
54 	struct rb_root_cached ins_root;
55 	struct rb_root_cached del_root;
56 	struct mutex mutex;
57 	struct btrfs_inode_item inode_item;
58 	refcount_t refs;
59 	u64 index_cnt;
60 	unsigned long flags;
61 	int count;
62 	/*
63 	 * The size of the next batch of dir index items to insert (if this
64 	 * node is from a directory inode). Protected by @mutex.
65 	 */
66 	u32 curr_index_batch_size;
67 	/*
68 	 * Number of leaves reserved for inserting dir index items (if this
69 	 * node belongs to a directory inode). This may be larger then the
70 	 * actual number of leaves we end up using. Protected by @mutex.
71 	 */
72 	u32 index_item_leaves;
73 };
74 
75 struct btrfs_delayed_item {
76 	struct rb_node rb_node;
77 	/* Offset value of the corresponding dir index key. */
78 	u64 index;
79 	struct list_head tree_list;	/* used for batch insert/delete items */
80 	struct list_head readdir_list;	/* used for readdir items */
81 	/*
82 	 * Used when logging a directory.
83 	 * Insertions and deletions to this list are protected by the parent
84 	 * delayed node's mutex.
85 	 */
86 	struct list_head log_list;
87 	u64 bytes_reserved;
88 	struct btrfs_delayed_node *delayed_node;
89 	refcount_t refs;
90 	enum btrfs_delayed_item_type type:8;
91 	/*
92 	 * Track if this delayed item was already logged.
93 	 * Protected by the mutex of the parent delayed inode.
94 	 */
95 	bool logged;
96 	/* The maximum leaf size is 64K, so u16 is more than enough. */
97 	u16 data_len;
98 	char data[] __counted_by(data_len);
99 };
100 
btrfs_init_delayed_root(struct btrfs_delayed_root * delayed_root)101 static inline void btrfs_init_delayed_root(
102 				struct btrfs_delayed_root *delayed_root)
103 {
104 	atomic_set(&delayed_root->items, 0);
105 	atomic_set(&delayed_root->items_seq, 0);
106 	delayed_root->nodes = 0;
107 	spin_lock_init(&delayed_root->lock);
108 	init_waitqueue_head(&delayed_root->wait);
109 	INIT_LIST_HEAD(&delayed_root->node_list);
110 	INIT_LIST_HEAD(&delayed_root->prepare_list);
111 }
112 
113 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
114 				   const char *name, int name_len,
115 				   struct btrfs_inode *dir,
116 				   struct btrfs_disk_key *disk_key, u8 flags,
117 				   u64 index);
118 
119 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
120 				   struct btrfs_inode *dir, u64 index);
121 
122 int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode);
123 
124 int btrfs_run_delayed_items(struct btrfs_trans_handle *trans);
125 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr);
126 
127 void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info);
128 
129 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
130 				     struct btrfs_inode *inode);
131 /* Used for evicting the inode. */
132 void btrfs_remove_delayed_node(struct btrfs_inode *inode);
133 void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode);
134 int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode);
135 
136 
137 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
138 			       struct btrfs_root *root,
139 			       struct btrfs_inode *inode);
140 int btrfs_fill_inode(struct inode *inode, u32 *rdev);
141 int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode);
142 
143 /* Used for drop dead root */
144 void btrfs_kill_all_delayed_nodes(struct btrfs_root *root);
145 
146 /* Used for clean the transaction */
147 void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info);
148 
149 /* Used for readdir() */
150 bool btrfs_readdir_get_delayed_items(struct inode *inode,
151 				     u64 last_index,
152 				     struct list_head *ins_list,
153 				     struct list_head *del_list);
154 void btrfs_readdir_put_delayed_items(struct inode *inode,
155 				     struct list_head *ins_list,
156 				     struct list_head *del_list);
157 int btrfs_should_delete_dir_index(struct list_head *del_list,
158 				  u64 index);
159 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
160 				    struct list_head *ins_list);
161 
162 /* Used during directory logging. */
163 void btrfs_log_get_delayed_items(struct btrfs_inode *inode,
164 				 struct list_head *ins_list,
165 				 struct list_head *del_list);
166 void btrfs_log_put_delayed_items(struct btrfs_inode *inode,
167 				 struct list_head *ins_list,
168 				 struct list_head *del_list);
169 
170 /* for init */
171 int __init btrfs_delayed_inode_init(void);
172 void __cold btrfs_delayed_inode_exit(void);
173 
174 /* for debugging */
175 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info);
176 
177 #endif
178