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