David Sterba | 9888c34 | 2018-04-03 19:16:55 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Fujitsu. All rights reserved. |
| 4 | * Written by Miao Xie <miaox@cn.fujitsu.com> |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
David Sterba | 9888c34 | 2018-04-03 19:16:55 +0200 | [diff] [blame] | 7 | #ifndef BTRFS_DELAYED_INODE_H |
| 8 | #define BTRFS_DELAYED_INODE_H |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 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> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 15 | #include <linux/atomic.h> |
Elena Reshetova | 6de5f18 | 2017-03-03 10:55:16 +0200 | [diff] [blame] | 16 | #include <linux/refcount.h> |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 17 | #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 | |
| 23 | struct 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 Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 33 | atomic_t items_seq; /* for delayed items */ |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 34 | int nodes; /* for delayed nodes */ |
| 35 | wait_queue_head_t wait; |
| 36 | }; |
| 37 | |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 38 | #define BTRFS_DELAYED_NODE_IN_LIST 0 |
| 39 | #define BTRFS_DELAYED_NODE_INODE_DIRTY 1 |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 40 | #define BTRFS_DELAYED_NODE_DEL_IREF 2 |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 41 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 42 | struct 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 Bo | 03a1d4c | 2018-08-23 03:51:51 +0800 | [diff] [blame] | 53 | struct rb_root_cached ins_root; |
| 54 | struct rb_root_cached del_root; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 55 | struct mutex mutex; |
| 56 | struct btrfs_inode_item inode_item; |
Elena Reshetova | 6de5f18 | 2017-03-03 10:55:16 +0200 | [diff] [blame] | 57 | refcount_t refs; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 58 | u64 index_cnt; |
Miao Xie | 7cf35d9 | 2013-12-26 13:07:05 +0800 | [diff] [blame] | 59 | unsigned long flags; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 60 | int count; |
| 61 | }; |
| 62 | |
| 63 | struct 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 Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 69 | struct btrfs_delayed_node *delayed_node; |
Elena Reshetova | 089e77e | 2017-03-03 10:55:17 +0200 | [diff] [blame] | 70 | refcount_t refs; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 71 | int ins_or_del; |
| 72 | u32 data_len; |
Gustavo A. R. Silva | 17b238a | 2020-03-06 10:49:12 -0600 | [diff] [blame] | 73 | char data[]; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | static inline void btrfs_init_delayed_root( |
| 77 | struct btrfs_delayed_root *delayed_root) |
| 78 | { |
| 79 | atomic_set(&delayed_root->items, 0); |
Chris Mason | de3cb94 | 2013-03-04 17:13:31 -0500 | [diff] [blame] | 80 | atomic_set(&delayed_root->items_seq, 0); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 81 | 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 | |
| 88 | int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 89 | const char *name, int name_len, |
Nikolay Borisov | 6f45d18 | 2017-01-10 20:35:35 +0200 | [diff] [blame] | 90 | struct btrfs_inode *dir, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 91 | struct btrfs_disk_key *disk_key, u8 type, |
| 92 | u64 index); |
| 93 | |
| 94 | int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e67bbbb | 2017-01-10 20:35:36 +0200 | [diff] [blame] | 95 | struct btrfs_inode *dir, u64 index); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 96 | |
Nikolay Borisov | f5cc7b8 | 2017-01-10 20:35:42 +0200 | [diff] [blame] | 97 | int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 98 | |
Nikolay Borisov | e5c304e6 | 2018-02-07 17:55:43 +0200 | [diff] [blame] | 99 | int btrfs_run_delayed_items(struct btrfs_trans_handle *trans); |
| 100 | int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 101 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 102 | void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 103 | |
| 104 | int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 5f4b32e | 2017-01-10 20:35:41 +0200 | [diff] [blame] | 105 | struct btrfs_inode *inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 106 | /* Used for evicting the inode. */ |
Nikolay Borisov | f48d1cf | 2017-01-10 20:35:39 +0200 | [diff] [blame] | 107 | void btrfs_remove_delayed_node(struct btrfs_inode *inode); |
Nikolay Borisov | 4ccb5c7 | 2017-01-10 20:35:38 +0200 | [diff] [blame] | 108 | void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode); |
Nikolay Borisov | aa79021 | 2017-01-10 20:35:40 +0200 | [diff] [blame] | 109 | int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 110 | |
| 111 | |
| 112 | int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans, |
| 113 | struct btrfs_root *root, struct inode *inode); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 114 | int btrfs_fill_inode(struct inode *inode, u32 *rdev); |
Nikolay Borisov | e07222c | 2017-01-10 20:35:37 +0200 | [diff] [blame] | 115 | int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 116 | |
| 117 | /* Used for drop dead root */ |
| 118 | void btrfs_kill_all_delayed_nodes(struct btrfs_root *root); |
| 119 | |
Miao Xie | 67cde34 | 2012-06-14 02:23:22 -0600 | [diff] [blame] | 120 | /* Used for clean the transaction */ |
Jeff Mahoney | ccdf9b3 | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 121 | void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info); |
Miao Xie | 67cde34 | 2012-06-14 02:23:22 -0600 | [diff] [blame] | 122 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 123 | /* Used for readdir() */ |
Omar Sandoval | 02dbfc9 | 2016-05-20 13:50:33 -0700 | [diff] [blame] | 124 | bool btrfs_readdir_get_delayed_items(struct inode *inode, |
| 125 | struct list_head *ins_list, |
| 126 | struct list_head *del_list); |
| 127 | void btrfs_readdir_put_delayed_items(struct inode *inode, |
| 128 | struct list_head *ins_list, |
| 129 | struct list_head *del_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 130 | int btrfs_should_delete_dir_index(struct list_head *del_list, |
| 131 | u64 index); |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 132 | int btrfs_readdir_delayed_dir_index(struct dir_context *ctx, |
Jeff Mahoney | d2fbb2b | 2016-11-05 13:26:35 -0400 | [diff] [blame] | 133 | struct list_head *ins_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 134 | |
| 135 | /* for init */ |
| 136 | int __init btrfs_delayed_inode_init(void); |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 137 | void __cold btrfs_delayed_inode_exit(void); |
Chris Mason | e999376 | 2011-06-17 16:14:09 -0400 | [diff] [blame] | 138 | |
| 139 | /* for debugging */ |
Jeff Mahoney | ccdf9b3 | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 140 | void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info); |
Chris Mason | e999376 | 2011-06-17 16:14:09 -0400 | [diff] [blame] | 141 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 142 | #endif |