blob: ca96ef007d8f2d9d689091fae4894dda6d1292f5 [file] [log] [blame]
David Sterba9888c342018-04-03 19:16:55 +02001/* SPDX-License-Identifier: GPL-2.0 */
Miao Xie16cdcec2011-04-22 18:12:22 +08002/*
3 * Copyright (C) 2011 Fujitsu. All rights reserved.
4 * Written by Miao Xie <miaox@cn.fujitsu.com>
Miao Xie16cdcec2011-04-22 18:12:22 +08005 */
6
David Sterba9888c342018-04-03 19:16:55 +02007#ifndef BTRFS_DELAYED_INODE_H
8#define BTRFS_DELAYED_INODE_H
Miao Xie16cdcec2011-04-22 18:12:22 +08009
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>
Arun Sharma600634972011-07-26 16:09:06 -070015#include <linux/atomic.h>
Elena Reshetova6de5f182017-03-03 10:55:16 +020016#include <linux/refcount.h>
Miao Xie16cdcec2011-04-22 18:12:22 +080017#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
23struct 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 */
Chris Masonde3cb942013-03-04 17:13:31 -050033 atomic_t items_seq; /* for delayed items */
Miao Xie16cdcec2011-04-22 18:12:22 +080034 int nodes; /* for delayed nodes */
35 wait_queue_head_t wait;
36};
37
Miao Xie7cf35d92013-12-26 13:07:05 +080038#define BTRFS_DELAYED_NODE_IN_LIST 0
39#define BTRFS_DELAYED_NODE_INODE_DIRTY 1
Miao Xie67de1172013-12-26 13:07:06 +080040#define BTRFS_DELAYED_NODE_DEL_IREF 2
Miao Xie7cf35d92013-12-26 13:07:05 +080041
Miao Xie16cdcec2011-04-22 18:12:22 +080042struct 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;
Liu Bo03a1d4c2018-08-23 03:51:51 +080053 struct rb_root_cached ins_root;
54 struct rb_root_cached del_root;
Miao Xie16cdcec2011-04-22 18:12:22 +080055 struct mutex mutex;
56 struct btrfs_inode_item inode_item;
Elena Reshetova6de5f182017-03-03 10:55:16 +020057 refcount_t refs;
Miao Xie16cdcec2011-04-22 18:12:22 +080058 u64 index_cnt;
Miao Xie7cf35d92013-12-26 13:07:05 +080059 unsigned long flags;
Miao Xie16cdcec2011-04-22 18:12:22 +080060 int count;
61};
62
63struct btrfs_delayed_item {
64 struct rb_node rb_node;
65 struct btrfs_key key;
66 struct list_head tree_list; /* used for batch insert/delete items */
67 struct list_head readdir_list; /* used for readdir items */
68 u64 bytes_reserved;
Miao Xie16cdcec2011-04-22 18:12:22 +080069 struct btrfs_delayed_node *delayed_node;
Elena Reshetova089e77e2017-03-03 10:55:17 +020070 refcount_t refs;
Miao Xie16cdcec2011-04-22 18:12:22 +080071 int ins_or_del;
72 u32 data_len;
Gustavo A. R. Silva17b238a2020-03-06 10:49:12 -060073 char data[];
Miao Xie16cdcec2011-04-22 18:12:22 +080074};
75
76static inline void btrfs_init_delayed_root(
77 struct btrfs_delayed_root *delayed_root)
78{
79 atomic_set(&delayed_root->items, 0);
Chris Masonde3cb942013-03-04 17:13:31 -050080 atomic_set(&delayed_root->items_seq, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +080081 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);
86}
87
88int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040089 const char *name, int name_len,
Nikolay Borisov6f45d182017-01-10 20:35:35 +020090 struct btrfs_inode *dir,
Miao Xie16cdcec2011-04-22 18:12:22 +080091 struct btrfs_disk_key *disk_key, u8 type,
92 u64 index);
93
94int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
Nikolay Borisove67bbbb2017-01-10 20:35:36 +020095 struct btrfs_inode *dir, u64 index);
Miao Xie16cdcec2011-04-22 18:12:22 +080096
Nikolay Borisovf5cc7b82017-01-10 20:35:42 +020097int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode);
Miao Xie16cdcec2011-04-22 18:12:22 +080098
Nikolay Borisove5c304e62018-02-07 17:55:43 +020099int btrfs_run_delayed_items(struct btrfs_trans_handle *trans);
100int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr);
Miao Xie16cdcec2011-04-22 18:12:22 +0800101
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400102void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info);
Miao Xie16cdcec2011-04-22 18:12:22 +0800103
104int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
Nikolay Borisov5f4b32e2017-01-10 20:35:41 +0200105 struct btrfs_inode *inode);
Miao Xie16cdcec2011-04-22 18:12:22 +0800106/* Used for evicting the inode. */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +0200107void btrfs_remove_delayed_node(struct btrfs_inode *inode);
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +0200108void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode);
Nikolay Borisovaa790212017-01-10 20:35:40 +0200109int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode);
Miao Xie16cdcec2011-04-22 18:12:22 +0800110
111
112int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
113 struct btrfs_root *root, struct inode *inode);
Miao Xie2f7e33d2011-06-23 07:27:13 +0000114int btrfs_fill_inode(struct inode *inode, u32 *rdev);
Nikolay Borisove07222c2017-01-10 20:35:37 +0200115int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode);
Miao Xie16cdcec2011-04-22 18:12:22 +0800116
117/* Used for drop dead root */
118void btrfs_kill_all_delayed_nodes(struct btrfs_root *root);
119
Miao Xie67cde342012-06-14 02:23:22 -0600120/* Used for clean the transaction */
Jeff Mahoneyccdf9b32016-06-22 18:54:23 -0400121void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info);
Miao Xie67cde342012-06-14 02:23:22 -0600122
Miao Xie16cdcec2011-04-22 18:12:22 +0800123/* Used for readdir() */
Omar Sandoval02dbfc92016-05-20 13:50:33 -0700124bool btrfs_readdir_get_delayed_items(struct inode *inode,
125 struct list_head *ins_list,
126 struct list_head *del_list);
127void btrfs_readdir_put_delayed_items(struct inode *inode,
128 struct list_head *ins_list,
129 struct list_head *del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +0800130int btrfs_should_delete_dir_index(struct list_head *del_list,
131 u64 index);
Al Viro9cdda8d2013-05-22 16:48:09 -0400132int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -0400133 struct list_head *ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +0800134
135/* for init */
136int __init btrfs_delayed_inode_init(void);
David Sterbae67c7182018-02-19 17:24:18 +0100137void __cold btrfs_delayed_inode_exit(void);
Chris Masone9993762011-06-17 16:14:09 -0400138
139/* for debugging */
Jeff Mahoneyccdf9b32016-06-22 18:54:23 -0400140void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info);
Chris Masone9993762011-06-17 16:14:09 -0400141
Miao Xie16cdcec2011-04-22 18:12:22 +0800142#endif