18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2011 Fujitsu.  All rights reserved.
48c2ecf20Sopenharmony_ci * Written by Miao Xie <miaox@cn.fujitsu.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef BTRFS_DELAYED_INODE_H
88c2ecf20Sopenharmony_ci#define BTRFS_DELAYED_INODE_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/rbtree.h>
118c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
128c2ecf20Sopenharmony_ci#include <linux/mutex.h>
138c2ecf20Sopenharmony_ci#include <linux/list.h>
148c2ecf20Sopenharmony_ci#include <linux/wait.h>
158c2ecf20Sopenharmony_ci#include <linux/atomic.h>
168c2ecf20Sopenharmony_ci#include <linux/refcount.h>
178c2ecf20Sopenharmony_ci#include "ctree.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* types of the delayed item */
208c2ecf20Sopenharmony_ci#define BTRFS_DELAYED_INSERTION_ITEM	1
218c2ecf20Sopenharmony_ci#define BTRFS_DELAYED_DELETION_ITEM	2
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistruct btrfs_delayed_root {
248c2ecf20Sopenharmony_ci	spinlock_t lock;
258c2ecf20Sopenharmony_ci	struct list_head node_list;
268c2ecf20Sopenharmony_ci	/*
278c2ecf20Sopenharmony_ci	 * Used for delayed nodes which is waiting to be dealt with by the
288c2ecf20Sopenharmony_ci	 * worker. If the delayed node is inserted into the work queue, we
298c2ecf20Sopenharmony_ci	 * drop it from this list.
308c2ecf20Sopenharmony_ci	 */
318c2ecf20Sopenharmony_ci	struct list_head prepare_list;
328c2ecf20Sopenharmony_ci	atomic_t items;		/* for delayed items */
338c2ecf20Sopenharmony_ci	atomic_t items_seq;	/* for delayed items */
348c2ecf20Sopenharmony_ci	int nodes;		/* for delayed nodes */
358c2ecf20Sopenharmony_ci	wait_queue_head_t wait;
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define BTRFS_DELAYED_NODE_IN_LIST	0
398c2ecf20Sopenharmony_ci#define BTRFS_DELAYED_NODE_INODE_DIRTY	1
408c2ecf20Sopenharmony_ci#define BTRFS_DELAYED_NODE_DEL_IREF	2
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistruct btrfs_delayed_node {
438c2ecf20Sopenharmony_ci	u64 inode_id;
448c2ecf20Sopenharmony_ci	u64 bytes_reserved;
458c2ecf20Sopenharmony_ci	struct btrfs_root *root;
468c2ecf20Sopenharmony_ci	/* Used to add the node into the delayed root's node list. */
478c2ecf20Sopenharmony_ci	struct list_head n_list;
488c2ecf20Sopenharmony_ci	/*
498c2ecf20Sopenharmony_ci	 * Used to add the node into the prepare list, the nodes in this list
508c2ecf20Sopenharmony_ci	 * is waiting to be dealt with by the async worker.
518c2ecf20Sopenharmony_ci	 */
528c2ecf20Sopenharmony_ci	struct list_head p_list;
538c2ecf20Sopenharmony_ci	struct rb_root_cached ins_root;
548c2ecf20Sopenharmony_ci	struct rb_root_cached del_root;
558c2ecf20Sopenharmony_ci	struct mutex mutex;
568c2ecf20Sopenharmony_ci	struct btrfs_inode_item inode_item;
578c2ecf20Sopenharmony_ci	refcount_t refs;
588c2ecf20Sopenharmony_ci	u64 index_cnt;
598c2ecf20Sopenharmony_ci	unsigned long flags;
608c2ecf20Sopenharmony_ci	int count;
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistruct btrfs_delayed_item {
648c2ecf20Sopenharmony_ci	struct rb_node rb_node;
658c2ecf20Sopenharmony_ci	struct btrfs_key key;
668c2ecf20Sopenharmony_ci	struct list_head tree_list;	/* used for batch insert/delete items */
678c2ecf20Sopenharmony_ci	struct list_head readdir_list;	/* used for readdir items */
688c2ecf20Sopenharmony_ci	u64 bytes_reserved;
698c2ecf20Sopenharmony_ci	struct btrfs_delayed_node *delayed_node;
708c2ecf20Sopenharmony_ci	refcount_t refs;
718c2ecf20Sopenharmony_ci	int ins_or_del;
728c2ecf20Sopenharmony_ci	u32 data_len;
738c2ecf20Sopenharmony_ci	char data[];
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic inline void btrfs_init_delayed_root(
778c2ecf20Sopenharmony_ci				struct btrfs_delayed_root *delayed_root)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	atomic_set(&delayed_root->items, 0);
808c2ecf20Sopenharmony_ci	atomic_set(&delayed_root->items_seq, 0);
818c2ecf20Sopenharmony_ci	delayed_root->nodes = 0;
828c2ecf20Sopenharmony_ci	spin_lock_init(&delayed_root->lock);
838c2ecf20Sopenharmony_ci	init_waitqueue_head(&delayed_root->wait);
848c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&delayed_root->node_list);
858c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&delayed_root->prepare_list);
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ciint btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
898c2ecf20Sopenharmony_ci				   const char *name, int name_len,
908c2ecf20Sopenharmony_ci				   struct btrfs_inode *dir,
918c2ecf20Sopenharmony_ci				   struct btrfs_disk_key *disk_key, u8 type,
928c2ecf20Sopenharmony_ci				   u64 index);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciint btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
958c2ecf20Sopenharmony_ci				   struct btrfs_inode *dir, u64 index);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ciint btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ciint btrfs_run_delayed_items(struct btrfs_trans_handle *trans);
1008c2ecf20Sopenharmony_ciint btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_civoid btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciint btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1058c2ecf20Sopenharmony_ci				     struct btrfs_inode *inode);
1068c2ecf20Sopenharmony_ci/* Used for evicting the inode. */
1078c2ecf20Sopenharmony_civoid btrfs_remove_delayed_node(struct btrfs_inode *inode);
1088c2ecf20Sopenharmony_civoid btrfs_kill_delayed_inode_items(struct btrfs_inode *inode);
1098c2ecf20Sopenharmony_ciint btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ciint btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
1138c2ecf20Sopenharmony_ci			       struct btrfs_root *root, struct inode *inode);
1148c2ecf20Sopenharmony_ciint btrfs_fill_inode(struct inode *inode, u32 *rdev);
1158c2ecf20Sopenharmony_ciint btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/* Used for drop dead root */
1188c2ecf20Sopenharmony_civoid btrfs_kill_all_delayed_nodes(struct btrfs_root *root);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/* Used for clean the transaction */
1218c2ecf20Sopenharmony_civoid btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* Used for readdir() */
1248c2ecf20Sopenharmony_cibool btrfs_readdir_get_delayed_items(struct inode *inode,
1258c2ecf20Sopenharmony_ci				     struct list_head *ins_list,
1268c2ecf20Sopenharmony_ci				     struct list_head *del_list);
1278c2ecf20Sopenharmony_civoid btrfs_readdir_put_delayed_items(struct inode *inode,
1288c2ecf20Sopenharmony_ci				     struct list_head *ins_list,
1298c2ecf20Sopenharmony_ci				     struct list_head *del_list);
1308c2ecf20Sopenharmony_ciint btrfs_should_delete_dir_index(struct list_head *del_list,
1318c2ecf20Sopenharmony_ci				  u64 index);
1328c2ecf20Sopenharmony_ciint btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
1338c2ecf20Sopenharmony_ci				    struct list_head *ins_list);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/* for init */
1368c2ecf20Sopenharmony_ciint __init btrfs_delayed_inode_init(void);
1378c2ecf20Sopenharmony_civoid __cold btrfs_delayed_inode_exit(void);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/* for debugging */
1408c2ecf20Sopenharmony_civoid btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#endif
143