xref: /openbmc/linux/fs/btrfs/delayed-ref.h (revision 3368d001)
156bec294SChris Mason /*
256bec294SChris Mason  * Copyright (C) 2008 Oracle.  All rights reserved.
356bec294SChris Mason  *
456bec294SChris Mason  * This program is free software; you can redistribute it and/or
556bec294SChris Mason  * modify it under the terms of the GNU General Public
656bec294SChris Mason  * License v2 as published by the Free Software Foundation.
756bec294SChris Mason  *
856bec294SChris Mason  * This program is distributed in the hope that it will be useful,
956bec294SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1056bec294SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1156bec294SChris Mason  * General Public License for more details.
1256bec294SChris Mason  *
1356bec294SChris Mason  * You should have received a copy of the GNU General Public
1456bec294SChris Mason  * License along with this program; if not, write to the
1556bec294SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1656bec294SChris Mason  * Boston, MA 021110-1307, USA.
1756bec294SChris Mason  */
1856bec294SChris Mason #ifndef __DELAYED_REF__
1956bec294SChris Mason #define __DELAYED_REF__
2056bec294SChris Mason 
2144a075bdSWang Sheng-Hui /* these are the possible values of struct btrfs_delayed_ref_node->action */
2256bec294SChris Mason #define BTRFS_ADD_DELAYED_REF    1 /* add one backref to the tree */
2356bec294SChris Mason #define BTRFS_DROP_DELAYED_REF   2 /* delete one backref from the tree */
2456bec294SChris Mason #define BTRFS_ADD_DELAYED_EXTENT 3 /* record a full extent allocation */
251a81af4dSChris Mason #define BTRFS_UPDATE_DELAYED_HEAD 4 /* not changing ref count on head ref */
2656bec294SChris Mason 
27c6fc2454SQu Wenruo /*
28c6fc2454SQu Wenruo  * XXX: Qu: I really hate the design that ref_head and tree/data ref shares the
29c6fc2454SQu Wenruo  * same ref_node structure.
30c6fc2454SQu Wenruo  * Ref_head is in a higher logic level than tree/data ref, and duplicated
31c6fc2454SQu Wenruo  * bytenr/num_bytes in ref_node is really a waste or memory, they should be
32c6fc2454SQu Wenruo  * referred from ref_head.
33c6fc2454SQu Wenruo  * This gets more disgusting after we use list to store tree/data ref in
34c6fc2454SQu Wenruo  * ref_head. Must clean this mess up later.
35c6fc2454SQu Wenruo  */
3656bec294SChris Mason struct btrfs_delayed_ref_node {
37c6fc2454SQu Wenruo 	/*
38c6fc2454SQu Wenruo 	 * ref_head use rb tree, stored in ref_root->href.
39c6fc2454SQu Wenruo 	 * indexed by bytenr
40c6fc2454SQu Wenruo 	 */
4156bec294SChris Mason 	struct rb_node rb_node;
4256bec294SChris Mason 
43c6fc2454SQu Wenruo 	/*data/tree ref use list, stored in ref_head->ref_list. */
44c6fc2454SQu Wenruo 	struct list_head list;
45c6fc2454SQu Wenruo 
4656bec294SChris Mason 	/* the starting bytenr of the extent */
4756bec294SChris Mason 	u64 bytenr;
4856bec294SChris Mason 
4956bec294SChris Mason 	/* the size of the extent */
5056bec294SChris Mason 	u64 num_bytes;
5156bec294SChris Mason 
5200f04b88SArne Jansen 	/* seq number to keep track of insertion order */
5300f04b88SArne Jansen 	u64 seq;
5400f04b88SArne Jansen 
5556bec294SChris Mason 	/* ref count on this data structure */
5656bec294SChris Mason 	atomic_t refs;
5756bec294SChris Mason 
5856bec294SChris Mason 	/*
5956bec294SChris Mason 	 * how many refs is this entry adding or deleting.  For
6056bec294SChris Mason 	 * head refs, this may be a negative number because it is keeping
6156bec294SChris Mason 	 * track of the total mods done to the reference count.
6256bec294SChris Mason 	 * For individual refs, this will always be a positive number
6356bec294SChris Mason 	 *
6456bec294SChris Mason 	 * It may be more than one, since it is possible for a single
6556bec294SChris Mason 	 * parent to have more than one ref on an extent
6656bec294SChris Mason 	 */
6756bec294SChris Mason 	int ref_mod;
6856bec294SChris Mason 
695d4f98a2SYan Zheng 	unsigned int action:8;
705d4f98a2SYan Zheng 	unsigned int type:8;
71fcebe456SJosef Bacik 	unsigned int no_quota:1;
7256bec294SChris Mason 	/* is this node still in the rbtree? */
735d4f98a2SYan Zheng 	unsigned int is_head:1;
7456bec294SChris Mason 	unsigned int in_tree:1;
7556bec294SChris Mason };
7656bec294SChris Mason 
775d4f98a2SYan Zheng struct btrfs_delayed_extent_op {
785d4f98a2SYan Zheng 	struct btrfs_disk_key key;
795d4f98a2SYan Zheng 	u64 flags_to_set;
80b1c79e09SJosef Bacik 	int level;
815d4f98a2SYan Zheng 	unsigned int update_key:1;
825d4f98a2SYan Zheng 	unsigned int update_flags:1;
835d4f98a2SYan Zheng 	unsigned int is_data:1;
845d4f98a2SYan Zheng };
855d4f98a2SYan Zheng 
8656bec294SChris Mason /*
8756bec294SChris Mason  * the head refs are used to hold a lock on a given extent, which allows us
8856bec294SChris Mason  * to make sure that only one process is running the delayed refs
8956bec294SChris Mason  * at a time for a single extent.  They also store the sum of all the
9056bec294SChris Mason  * reference count modifications we've queued up.
9156bec294SChris Mason  */
9256bec294SChris Mason struct btrfs_delayed_ref_head {
9356bec294SChris Mason 	struct btrfs_delayed_ref_node node;
9456bec294SChris Mason 
9556bec294SChris Mason 	/*
9656bec294SChris Mason 	 * the mutex is held while running the refs, and it is also
9756bec294SChris Mason 	 * held when checking the sum of reference modifications.
9856bec294SChris Mason 	 */
9956bec294SChris Mason 	struct mutex mutex;
10056bec294SChris Mason 
101d7df2c79SJosef Bacik 	spinlock_t lock;
102c6fc2454SQu Wenruo 	struct list_head ref_list;
103c3e69d58SChris Mason 
104c46effa6SLiu Bo 	struct rb_node href_node;
105c46effa6SLiu Bo 
1065d4f98a2SYan Zheng 	struct btrfs_delayed_extent_op *extent_op;
1071262133bSJosef Bacik 
1081262133bSJosef Bacik 	/*
1091262133bSJosef Bacik 	 * This is used to track the final ref_mod from all the refs associated
1101262133bSJosef Bacik 	 * with this head ref, this is not adjusted as delayed refs are run,
1111262133bSJosef Bacik 	 * this is meant to track if we need to do the csum accounting or not.
1121262133bSJosef Bacik 	 */
1131262133bSJosef Bacik 	int total_ref_mod;
1141262133bSJosef Bacik 
11556bec294SChris Mason 	/*
11656bec294SChris Mason 	 * when a new extent is allocated, it is just reserved in memory
11756bec294SChris Mason 	 * The actual extent isn't inserted into the extent allocation tree
11856bec294SChris Mason 	 * until the delayed ref is processed.  must_insert_reserved is
11956bec294SChris Mason 	 * used to flag a delayed ref so the accounting can be updated
12056bec294SChris Mason 	 * when a full insert is done.
12156bec294SChris Mason 	 *
12256bec294SChris Mason 	 * It is possible the extent will be freed before it is ever
12356bec294SChris Mason 	 * inserted into the extent allocation tree.  In this case
12456bec294SChris Mason 	 * we need to update the in ram accounting to properly reflect
12556bec294SChris Mason 	 * the free has happened.
12656bec294SChris Mason 	 */
12756bec294SChris Mason 	unsigned int must_insert_reserved:1;
1285d4f98a2SYan Zheng 	unsigned int is_data:1;
129d7df2c79SJosef Bacik 	unsigned int processing:1;
13056bec294SChris Mason };
13156bec294SChris Mason 
1325d4f98a2SYan Zheng struct btrfs_delayed_tree_ref {
13356bec294SChris Mason 	struct btrfs_delayed_ref_node node;
13456bec294SChris Mason 	u64 root;
1355d4f98a2SYan Zheng 	u64 parent;
1365d4f98a2SYan Zheng 	int level;
1375d4f98a2SYan Zheng };
13856bec294SChris Mason 
1395d4f98a2SYan Zheng struct btrfs_delayed_data_ref {
1405d4f98a2SYan Zheng 	struct btrfs_delayed_ref_node node;
1415d4f98a2SYan Zheng 	u64 root;
1425d4f98a2SYan Zheng 	u64 parent;
1435d4f98a2SYan Zheng 	u64 objectid;
1445d4f98a2SYan Zheng 	u64 offset;
14556bec294SChris Mason };
14656bec294SChris Mason 
14756bec294SChris Mason struct btrfs_delayed_ref_root {
148c46effa6SLiu Bo 	/* head ref rbtree */
149c46effa6SLiu Bo 	struct rb_root href_root;
150c46effa6SLiu Bo 
1513368d001SQu Wenruo 	/* dirty extent records */
1523368d001SQu Wenruo 	struct rb_root dirty_extent_root;
1533368d001SQu Wenruo 
15456bec294SChris Mason 	/* this spin lock protects the rbtree and the entries inside */
15556bec294SChris Mason 	spinlock_t lock;
15656bec294SChris Mason 
15756bec294SChris Mason 	/* how many delayed ref updates we've queued, used by the
15856bec294SChris Mason 	 * throttling code
15956bec294SChris Mason 	 */
160d7df2c79SJosef Bacik 	atomic_t num_entries;
16156bec294SChris Mason 
162c3e69d58SChris Mason 	/* total number of head nodes in tree */
163c3e69d58SChris Mason 	unsigned long num_heads;
164c3e69d58SChris Mason 
165c3e69d58SChris Mason 	/* total number of head nodes ready for processing */
166c3e69d58SChris Mason 	unsigned long num_heads_ready;
167c3e69d58SChris Mason 
1681262133bSJosef Bacik 	u64 pending_csums;
1691262133bSJosef Bacik 
17056bec294SChris Mason 	/*
17156bec294SChris Mason 	 * set when the tree is flushing before a transaction commit,
17256bec294SChris Mason 	 * used by the throttling code to decide if new updates need
17356bec294SChris Mason 	 * to be run right away
17456bec294SChris Mason 	 */
17556bec294SChris Mason 	int flushing;
176c3e69d58SChris Mason 
177c3e69d58SChris Mason 	u64 run_delayed_start;
17856bec294SChris Mason };
17956bec294SChris Mason 
18078a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
18178a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
18278a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
18378a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
18478a6184aSMiao Xie 
18578a6184aSMiao Xie int btrfs_delayed_ref_init(void);
18678a6184aSMiao Xie void btrfs_delayed_ref_exit(void);
18778a6184aSMiao Xie 
18878a6184aSMiao Xie static inline struct btrfs_delayed_extent_op *
18978a6184aSMiao Xie btrfs_alloc_delayed_extent_op(void)
19078a6184aSMiao Xie {
19178a6184aSMiao Xie 	return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);
19278a6184aSMiao Xie }
19378a6184aSMiao Xie 
19478a6184aSMiao Xie static inline void
19578a6184aSMiao Xie btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)
19678a6184aSMiao Xie {
19778a6184aSMiao Xie 	if (op)
19878a6184aSMiao Xie 		kmem_cache_free(btrfs_delayed_extent_op_cachep, op);
19978a6184aSMiao Xie }
20078a6184aSMiao Xie 
20156bec294SChris Mason static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
20256bec294SChris Mason {
20356bec294SChris Mason 	WARN_ON(atomic_read(&ref->refs) == 0);
20456bec294SChris Mason 	if (atomic_dec_and_test(&ref->refs)) {
20556bec294SChris Mason 		WARN_ON(ref->in_tree);
20678a6184aSMiao Xie 		switch (ref->type) {
20778a6184aSMiao Xie 		case BTRFS_TREE_BLOCK_REF_KEY:
20878a6184aSMiao Xie 		case BTRFS_SHARED_BLOCK_REF_KEY:
20978a6184aSMiao Xie 			kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
21078a6184aSMiao Xie 			break;
21178a6184aSMiao Xie 		case BTRFS_EXTENT_DATA_REF_KEY:
21278a6184aSMiao Xie 		case BTRFS_SHARED_DATA_REF_KEY:
21378a6184aSMiao Xie 			kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
21478a6184aSMiao Xie 			break;
21578a6184aSMiao Xie 		case 0:
21678a6184aSMiao Xie 			kmem_cache_free(btrfs_delayed_ref_head_cachep, ref);
21778a6184aSMiao Xie 			break;
21878a6184aSMiao Xie 		default:
21978a6184aSMiao Xie 			BUG();
22078a6184aSMiao Xie 		}
22156bec294SChris Mason 	}
22256bec294SChris Mason }
22356bec294SChris Mason 
22466d7e7f0SArne Jansen int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
22566d7e7f0SArne Jansen 			       struct btrfs_trans_handle *trans,
2265d4f98a2SYan Zheng 			       u64 bytenr, u64 num_bytes, u64 parent,
2275d4f98a2SYan Zheng 			       u64 ref_root, int level, int action,
22866d7e7f0SArne Jansen 			       struct btrfs_delayed_extent_op *extent_op,
229fcebe456SJosef Bacik 			       int no_quota);
23066d7e7f0SArne Jansen int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
23166d7e7f0SArne Jansen 			       struct btrfs_trans_handle *trans,
2325d4f98a2SYan Zheng 			       u64 bytenr, u64 num_bytes,
2335d4f98a2SYan Zheng 			       u64 parent, u64 ref_root,
2345d4f98a2SYan Zheng 			       u64 owner, u64 offset, int action,
23566d7e7f0SArne Jansen 			       struct btrfs_delayed_extent_op *extent_op,
236fcebe456SJosef Bacik 			       int no_quota);
23766d7e7f0SArne Jansen int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
23866d7e7f0SArne Jansen 				struct btrfs_trans_handle *trans,
2395d4f98a2SYan Zheng 				u64 bytenr, u64 num_bytes,
2405d4f98a2SYan Zheng 				struct btrfs_delayed_extent_op *extent_op);
241ae1e206bSJosef Bacik void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
242ae1e206bSJosef Bacik 			      struct btrfs_fs_info *fs_info,
243ae1e206bSJosef Bacik 			      struct btrfs_delayed_ref_root *delayed_refs,
244ae1e206bSJosef Bacik 			      struct btrfs_delayed_ref_head *head);
24556bec294SChris Mason 
2461887be66SChris Mason struct btrfs_delayed_ref_head *
2471887be66SChris Mason btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr);
248c3e69d58SChris Mason int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
249c3e69d58SChris Mason 			   struct btrfs_delayed_ref_head *head);
250093486c4SMiao Xie static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
251093486c4SMiao Xie {
252093486c4SMiao Xie 	mutex_unlock(&head->mutex);
253093486c4SMiao Xie }
254093486c4SMiao Xie 
255d7df2c79SJosef Bacik 
256d7df2c79SJosef Bacik struct btrfs_delayed_ref_head *
257d7df2c79SJosef Bacik btrfs_select_ref_head(struct btrfs_trans_handle *trans);
25800f04b88SArne Jansen 
259097b8a7cSJan Schmidt int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
260097b8a7cSJan Schmidt 			    struct btrfs_delayed_ref_root *delayed_refs,
26100f04b88SArne Jansen 			    u64 seq);
26200f04b88SArne Jansen 
26300f04b88SArne Jansen /*
26456bec294SChris Mason  * a node might live in a head or a regular ref, this lets you
26556bec294SChris Mason  * test for the proper type to use.
26656bec294SChris Mason  */
26756bec294SChris Mason static int btrfs_delayed_ref_is_head(struct btrfs_delayed_ref_node *node)
26856bec294SChris Mason {
2695d4f98a2SYan Zheng 	return node->is_head;
27056bec294SChris Mason }
27156bec294SChris Mason 
27256bec294SChris Mason /*
27356bec294SChris Mason  * helper functions to cast a node into its container
27456bec294SChris Mason  */
2755d4f98a2SYan Zheng static inline struct btrfs_delayed_tree_ref *
2765d4f98a2SYan Zheng btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
27756bec294SChris Mason {
27856bec294SChris Mason 	WARN_ON(btrfs_delayed_ref_is_head(node));
2795d4f98a2SYan Zheng 	return container_of(node, struct btrfs_delayed_tree_ref, node);
2805d4f98a2SYan Zheng }
28156bec294SChris Mason 
2825d4f98a2SYan Zheng static inline struct btrfs_delayed_data_ref *
2835d4f98a2SYan Zheng btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
2845d4f98a2SYan Zheng {
2855d4f98a2SYan Zheng 	WARN_ON(btrfs_delayed_ref_is_head(node));
2865d4f98a2SYan Zheng 	return container_of(node, struct btrfs_delayed_data_ref, node);
28756bec294SChris Mason }
28856bec294SChris Mason 
28956bec294SChris Mason static inline struct btrfs_delayed_ref_head *
29056bec294SChris Mason btrfs_delayed_node_to_head(struct btrfs_delayed_ref_node *node)
29156bec294SChris Mason {
29256bec294SChris Mason 	WARN_ON(!btrfs_delayed_ref_is_head(node));
29356bec294SChris Mason 	return container_of(node, struct btrfs_delayed_ref_head, node);
29456bec294SChris Mason }
29556bec294SChris Mason #endif
296