blob: a6a13f7e014d05c07b6e4f2a588d5ce5e300f22b [file] [log] [blame]
Miao Xie16cdcec2011-04-22 18:12:22 +08001/*
2 * Copyright (C) 2011 Fujitsu. All rights reserved.
3 * Written by Miao Xie <miaox@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public
7 * License v2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 021110-1307, USA.
18 */
19
20#ifndef __DELAYED_TREE_OPERATION_H
21#define __DELAYED_TREE_OPERATION_H
22
23#include <linux/rbtree.h>
24#include <linux/spinlock.h>
25#include <linux/mutex.h>
26#include <linux/list.h>
27#include <linux/wait.h>
Arun Sharma600634972011-07-26 16:09:06 -070028#include <linux/atomic.h>
Miao Xie16cdcec2011-04-22 18:12:22 +080029
30#include "ctree.h"
31
32/* types of the delayed item */
33#define BTRFS_DELAYED_INSERTION_ITEM 1
34#define BTRFS_DELAYED_DELETION_ITEM 2
35
36struct btrfs_delayed_root {
37 spinlock_t lock;
38 struct list_head node_list;
39 /*
40 * Used for delayed nodes which is waiting to be dealt with by the
41 * worker. If the delayed node is inserted into the work queue, we
42 * drop it from this list.
43 */
44 struct list_head prepare_list;
45 atomic_t items; /* for delayed items */
Chris Masonde3cb942013-03-04 17:13:31 -050046 atomic_t items_seq; /* for delayed items */
Miao Xie16cdcec2011-04-22 18:12:22 +080047 int nodes; /* for delayed nodes */
48 wait_queue_head_t wait;
49};
50
Miao Xie7cf35d92013-12-26 13:07:05 +080051#define BTRFS_DELAYED_NODE_IN_LIST 0
52#define BTRFS_DELAYED_NODE_INODE_DIRTY 1
53
Miao Xie16cdcec2011-04-22 18:12:22 +080054struct btrfs_delayed_node {
55 u64 inode_id;
56 u64 bytes_reserved;
57 struct btrfs_root *root;
58 /* Used to add the node into the delayed root's node list. */
59 struct list_head n_list;
60 /*
61 * Used to add the node into the prepare list, the nodes in this list
62 * is waiting to be dealt with by the async worker.
63 */
64 struct list_head p_list;
65 struct rb_root ins_root;
66 struct rb_root del_root;
67 struct mutex mutex;
68 struct btrfs_inode_item inode_item;
69 atomic_t refs;
70 u64 index_cnt;
Miao Xie7cf35d92013-12-26 13:07:05 +080071 unsigned long flags;
Miao Xie16cdcec2011-04-22 18:12:22 +080072 int count;
73};
74
75struct btrfs_delayed_item {
76 struct rb_node rb_node;
77 struct btrfs_key key;
78 struct list_head tree_list; /* used for batch insert/delete items */
79 struct list_head readdir_list; /* used for readdir items */
80 u64 bytes_reserved;
Miao Xie16cdcec2011-04-22 18:12:22 +080081 struct btrfs_delayed_node *delayed_node;
82 atomic_t refs;
83 int ins_or_del;
84 u32 data_len;
85 char data[0];
86};
87
88static inline void btrfs_init_delayed_root(
89 struct btrfs_delayed_root *delayed_root)
90{
91 atomic_set(&delayed_root->items, 0);
Chris Masonde3cb942013-03-04 17:13:31 -050092 atomic_set(&delayed_root->items_seq, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +080093 delayed_root->nodes = 0;
94 spin_lock_init(&delayed_root->lock);
95 init_waitqueue_head(&delayed_root->wait);
96 INIT_LIST_HEAD(&delayed_root->node_list);
97 INIT_LIST_HEAD(&delayed_root->prepare_list);
98}
99
100int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
101 struct btrfs_root *root, const char *name,
102 int name_len, struct inode *dir,
103 struct btrfs_disk_key *disk_key, u8 type,
104 u64 index);
105
106int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
107 struct btrfs_root *root, struct inode *dir,
108 u64 index);
109
110int btrfs_inode_delayed_dir_index_count(struct inode *inode);
111
112int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
113 struct btrfs_root *root);
Josef Bacik96c3f432012-06-21 14:05:49 -0400114int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans,
115 struct btrfs_root *root, int nr);
Miao Xie16cdcec2011-04-22 18:12:22 +0800116
117void btrfs_balance_delayed_items(struct btrfs_root *root);
118
119int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
120 struct inode *inode);
121/* Used for evicting the inode. */
122void btrfs_remove_delayed_node(struct inode *inode);
123void btrfs_kill_delayed_inode_items(struct inode *inode);
Miao Xie0e8c36a2012-12-19 06:59:51 +0000124int btrfs_commit_inode_delayed_inode(struct inode *inode);
Miao Xie16cdcec2011-04-22 18:12:22 +0800125
126
127int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
128 struct btrfs_root *root, struct inode *inode);
Miao Xie2f7e33d2011-06-23 07:27:13 +0000129int btrfs_fill_inode(struct inode *inode, u32 *rdev);
Miao Xie16cdcec2011-04-22 18:12:22 +0800130
131/* Used for drop dead root */
132void btrfs_kill_all_delayed_nodes(struct btrfs_root *root);
133
Miao Xie67cde342012-06-14 02:23:22 -0600134/* Used for clean the transaction */
135void btrfs_destroy_delayed_inodes(struct btrfs_root *root);
136
Miao Xie16cdcec2011-04-22 18:12:22 +0800137/* Used for readdir() */
138void btrfs_get_delayed_items(struct inode *inode, struct list_head *ins_list,
139 struct list_head *del_list);
140void btrfs_put_delayed_items(struct list_head *ins_list,
141 struct list_head *del_list);
142int btrfs_should_delete_dir_index(struct list_head *del_list,
143 u64 index);
Al Viro9cdda8d2013-05-22 16:48:09 -0400144int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
Miao Xie16cdcec2011-04-22 18:12:22 +0800145 struct list_head *ins_list);
146
147/* for init */
148int __init btrfs_delayed_inode_init(void);
149void btrfs_delayed_inode_exit(void);
Chris Masone9993762011-06-17 16:14:09 -0400150
151/* for debugging */
152void btrfs_assert_delayed_root_empty(struct btrfs_root *root);
153
Miao Xie16cdcec2011-04-22 18:12:22 +0800154#endif