xref: /openbmc/linux/fs/btrfs/delayed-ref.h (revision c46effa6)
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 
2756bec294SChris Mason struct btrfs_delayed_ref_node {
2856bec294SChris Mason 	struct rb_node rb_node;
2956bec294SChris Mason 
3056bec294SChris Mason 	/* the starting bytenr of the extent */
3156bec294SChris Mason 	u64 bytenr;
3256bec294SChris Mason 
3356bec294SChris Mason 	/* the size of the extent */
3456bec294SChris Mason 	u64 num_bytes;
3556bec294SChris Mason 
3600f04b88SArne Jansen 	/* seq number to keep track of insertion order */
3700f04b88SArne Jansen 	u64 seq;
3800f04b88SArne Jansen 
3956bec294SChris Mason 	/* ref count on this data structure */
4056bec294SChris Mason 	atomic_t refs;
4156bec294SChris Mason 
4256bec294SChris Mason 	/*
4356bec294SChris Mason 	 * how many refs is this entry adding or deleting.  For
4456bec294SChris Mason 	 * head refs, this may be a negative number because it is keeping
4556bec294SChris Mason 	 * track of the total mods done to the reference count.
4656bec294SChris Mason 	 * For individual refs, this will always be a positive number
4756bec294SChris Mason 	 *
4856bec294SChris Mason 	 * It may be more than one, since it is possible for a single
4956bec294SChris Mason 	 * parent to have more than one ref on an extent
5056bec294SChris Mason 	 */
5156bec294SChris Mason 	int ref_mod;
5256bec294SChris Mason 
535d4f98a2SYan Zheng 	unsigned int action:8;
545d4f98a2SYan Zheng 	unsigned int type:8;
5556bec294SChris Mason 	/* is this node still in the rbtree? */
565d4f98a2SYan Zheng 	unsigned int is_head:1;
5756bec294SChris Mason 	unsigned int in_tree:1;
5856bec294SChris Mason };
5956bec294SChris Mason 
605d4f98a2SYan Zheng struct btrfs_delayed_extent_op {
615d4f98a2SYan Zheng 	struct btrfs_disk_key key;
625d4f98a2SYan Zheng 	u64 flags_to_set;
63b1c79e09SJosef Bacik 	int level;
645d4f98a2SYan Zheng 	unsigned int update_key:1;
655d4f98a2SYan Zheng 	unsigned int update_flags:1;
665d4f98a2SYan Zheng 	unsigned int is_data:1;
675d4f98a2SYan Zheng };
685d4f98a2SYan Zheng 
6956bec294SChris Mason /*
7056bec294SChris Mason  * the head refs are used to hold a lock on a given extent, which allows us
7156bec294SChris Mason  * to make sure that only one process is running the delayed refs
7256bec294SChris Mason  * at a time for a single extent.  They also store the sum of all the
7356bec294SChris Mason  * reference count modifications we've queued up.
7456bec294SChris Mason  */
7556bec294SChris Mason struct btrfs_delayed_ref_head {
7656bec294SChris Mason 	struct btrfs_delayed_ref_node node;
7756bec294SChris Mason 
7856bec294SChris Mason 	/*
7956bec294SChris Mason 	 * the mutex is held while running the refs, and it is also
8056bec294SChris Mason 	 * held when checking the sum of reference modifications.
8156bec294SChris Mason 	 */
8256bec294SChris Mason 	struct mutex mutex;
8356bec294SChris Mason 
84c3e69d58SChris Mason 	struct list_head cluster;
85c3e69d58SChris Mason 
86c46effa6SLiu Bo 	struct rb_node href_node;
87c46effa6SLiu Bo 
885d4f98a2SYan Zheng 	struct btrfs_delayed_extent_op *extent_op;
8956bec294SChris Mason 	/*
9056bec294SChris Mason 	 * when a new extent is allocated, it is just reserved in memory
9156bec294SChris Mason 	 * The actual extent isn't inserted into the extent allocation tree
9256bec294SChris Mason 	 * until the delayed ref is processed.  must_insert_reserved is
9356bec294SChris Mason 	 * used to flag a delayed ref so the accounting can be updated
9456bec294SChris Mason 	 * when a full insert is done.
9556bec294SChris Mason 	 *
9656bec294SChris Mason 	 * It is possible the extent will be freed before it is ever
9756bec294SChris Mason 	 * inserted into the extent allocation tree.  In this case
9856bec294SChris Mason 	 * we need to update the in ram accounting to properly reflect
9956bec294SChris Mason 	 * the free has happened.
10056bec294SChris Mason 	 */
10156bec294SChris Mason 	unsigned int must_insert_reserved:1;
1025d4f98a2SYan Zheng 	unsigned int is_data:1;
10356bec294SChris Mason };
10456bec294SChris Mason 
1055d4f98a2SYan Zheng struct btrfs_delayed_tree_ref {
10656bec294SChris Mason 	struct btrfs_delayed_ref_node node;
10756bec294SChris Mason 	u64 root;
1085d4f98a2SYan Zheng 	u64 parent;
1095d4f98a2SYan Zheng 	int level;
1105d4f98a2SYan Zheng };
11156bec294SChris Mason 
1125d4f98a2SYan Zheng struct btrfs_delayed_data_ref {
1135d4f98a2SYan Zheng 	struct btrfs_delayed_ref_node node;
1145d4f98a2SYan Zheng 	u64 root;
1155d4f98a2SYan Zheng 	u64 parent;
1165d4f98a2SYan Zheng 	u64 objectid;
1175d4f98a2SYan Zheng 	u64 offset;
11856bec294SChris Mason };
11956bec294SChris Mason 
12056bec294SChris Mason struct btrfs_delayed_ref_root {
12156bec294SChris Mason 	struct rb_root root;
12256bec294SChris Mason 
123c46effa6SLiu Bo 	/* head ref rbtree */
124c46effa6SLiu Bo 	struct rb_root href_root;
125c46effa6SLiu Bo 
12656bec294SChris Mason 	/* this spin lock protects the rbtree and the entries inside */
12756bec294SChris Mason 	spinlock_t lock;
12856bec294SChris Mason 
12956bec294SChris Mason 	/* how many delayed ref updates we've queued, used by the
13056bec294SChris Mason 	 * throttling code
13156bec294SChris Mason 	 */
13256bec294SChris Mason 	unsigned long num_entries;
13356bec294SChris Mason 
134c3e69d58SChris Mason 	/* total number of head nodes in tree */
135c3e69d58SChris Mason 	unsigned long num_heads;
136c3e69d58SChris Mason 
137c3e69d58SChris Mason 	/* total number of head nodes ready for processing */
138c3e69d58SChris Mason 	unsigned long num_heads_ready;
139c3e69d58SChris Mason 
14056bec294SChris Mason 	/*
141bb721703SChris Mason 	 * bumped when someone is making progress on the delayed
142bb721703SChris Mason 	 * refs, so that other procs know they are just adding to
143bb721703SChris Mason 	 * contention intead of helping
144bb721703SChris Mason 	 */
145bb721703SChris Mason 	atomic_t procs_running_refs;
146bb721703SChris Mason 	atomic_t ref_seq;
147bb721703SChris Mason 	wait_queue_head_t wait;
148bb721703SChris Mason 
149bb721703SChris Mason 	/*
15056bec294SChris Mason 	 * set when the tree is flushing before a transaction commit,
15156bec294SChris Mason 	 * used by the throttling code to decide if new updates need
15256bec294SChris Mason 	 * to be run right away
15356bec294SChris Mason 	 */
15456bec294SChris Mason 	int flushing;
155c3e69d58SChris Mason 
156c3e69d58SChris Mason 	u64 run_delayed_start;
15756bec294SChris Mason };
15856bec294SChris Mason 
15978a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
16078a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
16178a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
16278a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
16378a6184aSMiao Xie 
16478a6184aSMiao Xie int btrfs_delayed_ref_init(void);
16578a6184aSMiao Xie void btrfs_delayed_ref_exit(void);
16678a6184aSMiao Xie 
16778a6184aSMiao Xie static inline struct btrfs_delayed_extent_op *
16878a6184aSMiao Xie btrfs_alloc_delayed_extent_op(void)
16978a6184aSMiao Xie {
17078a6184aSMiao Xie 	return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);
17178a6184aSMiao Xie }
17278a6184aSMiao Xie 
17378a6184aSMiao Xie static inline void
17478a6184aSMiao Xie btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)
17578a6184aSMiao Xie {
17678a6184aSMiao Xie 	if (op)
17778a6184aSMiao Xie 		kmem_cache_free(btrfs_delayed_extent_op_cachep, op);
17878a6184aSMiao Xie }
17978a6184aSMiao Xie 
18056bec294SChris Mason static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
18156bec294SChris Mason {
18256bec294SChris Mason 	WARN_ON(atomic_read(&ref->refs) == 0);
18356bec294SChris Mason 	if (atomic_dec_and_test(&ref->refs)) {
18456bec294SChris Mason 		WARN_ON(ref->in_tree);
18578a6184aSMiao Xie 		switch (ref->type) {
18678a6184aSMiao Xie 		case BTRFS_TREE_BLOCK_REF_KEY:
18778a6184aSMiao Xie 		case BTRFS_SHARED_BLOCK_REF_KEY:
18878a6184aSMiao Xie 			kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
18978a6184aSMiao Xie 			break;
19078a6184aSMiao Xie 		case BTRFS_EXTENT_DATA_REF_KEY:
19178a6184aSMiao Xie 		case BTRFS_SHARED_DATA_REF_KEY:
19278a6184aSMiao Xie 			kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
19378a6184aSMiao Xie 			break;
19478a6184aSMiao Xie 		case 0:
19578a6184aSMiao Xie 			kmem_cache_free(btrfs_delayed_ref_head_cachep, ref);
19678a6184aSMiao Xie 			break;
19778a6184aSMiao Xie 		default:
19878a6184aSMiao Xie 			BUG();
19978a6184aSMiao Xie 		}
20056bec294SChris Mason 	}
20156bec294SChris Mason }
20256bec294SChris Mason 
20366d7e7f0SArne Jansen int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
20466d7e7f0SArne Jansen 			       struct btrfs_trans_handle *trans,
2055d4f98a2SYan Zheng 			       u64 bytenr, u64 num_bytes, u64 parent,
2065d4f98a2SYan Zheng 			       u64 ref_root, int level, int action,
20766d7e7f0SArne Jansen 			       struct btrfs_delayed_extent_op *extent_op,
20866d7e7f0SArne Jansen 			       int for_cow);
20966d7e7f0SArne Jansen int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
21066d7e7f0SArne Jansen 			       struct btrfs_trans_handle *trans,
2115d4f98a2SYan Zheng 			       u64 bytenr, u64 num_bytes,
2125d4f98a2SYan Zheng 			       u64 parent, u64 ref_root,
2135d4f98a2SYan Zheng 			       u64 owner, u64 offset, int action,
21466d7e7f0SArne Jansen 			       struct btrfs_delayed_extent_op *extent_op,
21566d7e7f0SArne Jansen 			       int for_cow);
21666d7e7f0SArne Jansen int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
21766d7e7f0SArne Jansen 				struct btrfs_trans_handle *trans,
2185d4f98a2SYan Zheng 				u64 bytenr, u64 num_bytes,
2195d4f98a2SYan Zheng 				struct btrfs_delayed_extent_op *extent_op);
220ae1e206bSJosef Bacik void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
221ae1e206bSJosef Bacik 			      struct btrfs_fs_info *fs_info,
222ae1e206bSJosef Bacik 			      struct btrfs_delayed_ref_root *delayed_refs,
223ae1e206bSJosef Bacik 			      struct btrfs_delayed_ref_head *head);
22456bec294SChris Mason 
2251887be66SChris Mason struct btrfs_delayed_ref_head *
2261887be66SChris Mason btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr);
227c3e69d58SChris Mason int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
228c3e69d58SChris Mason 			   struct btrfs_delayed_ref_head *head);
229093486c4SMiao Xie static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
230093486c4SMiao Xie {
231093486c4SMiao Xie 	mutex_unlock(&head->mutex);
232093486c4SMiao Xie }
233093486c4SMiao Xie 
234c3e69d58SChris Mason int btrfs_find_ref_cluster(struct btrfs_trans_handle *trans,
235c3e69d58SChris Mason 			   struct list_head *cluster, u64 search_start);
236093486c4SMiao Xie void btrfs_release_ref_cluster(struct list_head *cluster);
23700f04b88SArne Jansen 
238097b8a7cSJan Schmidt int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
239097b8a7cSJan Schmidt 			    struct btrfs_delayed_ref_root *delayed_refs,
24000f04b88SArne Jansen 			    u64 seq);
24100f04b88SArne Jansen 
24200f04b88SArne Jansen /*
243546adb0dSJan Schmidt  * delayed refs with a ref_seq > 0 must be held back during backref walking.
244546adb0dSJan Schmidt  * this only applies to items in one of the fs-trees. for_cow items never need
245546adb0dSJan Schmidt  * to be held back, so they won't get a ref_seq number.
246546adb0dSJan Schmidt  */
247546adb0dSJan Schmidt static inline int need_ref_seq(int for_cow, u64 rootid)
248546adb0dSJan Schmidt {
249546adb0dSJan Schmidt 	if (for_cow)
250546adb0dSJan Schmidt 		return 0;
251546adb0dSJan Schmidt 
252546adb0dSJan Schmidt 	if (rootid == BTRFS_FS_TREE_OBJECTID)
253546adb0dSJan Schmidt 		return 1;
254546adb0dSJan Schmidt 
255546adb0dSJan Schmidt 	if ((s64)rootid >= (s64)BTRFS_FIRST_FREE_OBJECTID)
256546adb0dSJan Schmidt 		return 1;
257546adb0dSJan Schmidt 
258546adb0dSJan Schmidt 	return 0;
259546adb0dSJan Schmidt }
260546adb0dSJan Schmidt 
261546adb0dSJan Schmidt /*
26256bec294SChris Mason  * a node might live in a head or a regular ref, this lets you
26356bec294SChris Mason  * test for the proper type to use.
26456bec294SChris Mason  */
26556bec294SChris Mason static int btrfs_delayed_ref_is_head(struct btrfs_delayed_ref_node *node)
26656bec294SChris Mason {
2675d4f98a2SYan Zheng 	return node->is_head;
26856bec294SChris Mason }
26956bec294SChris Mason 
27056bec294SChris Mason /*
27156bec294SChris Mason  * helper functions to cast a node into its container
27256bec294SChris Mason  */
2735d4f98a2SYan Zheng static inline struct btrfs_delayed_tree_ref *
2745d4f98a2SYan Zheng btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
27556bec294SChris Mason {
27656bec294SChris Mason 	WARN_ON(btrfs_delayed_ref_is_head(node));
2775d4f98a2SYan Zheng 	return container_of(node, struct btrfs_delayed_tree_ref, node);
2785d4f98a2SYan Zheng }
27956bec294SChris Mason 
2805d4f98a2SYan Zheng static inline struct btrfs_delayed_data_ref *
2815d4f98a2SYan Zheng btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
2825d4f98a2SYan Zheng {
2835d4f98a2SYan Zheng 	WARN_ON(btrfs_delayed_ref_is_head(node));
2845d4f98a2SYan Zheng 	return container_of(node, struct btrfs_delayed_data_ref, node);
28556bec294SChris Mason }
28656bec294SChris Mason 
28756bec294SChris Mason static inline struct btrfs_delayed_ref_head *
28856bec294SChris Mason btrfs_delayed_node_to_head(struct btrfs_delayed_ref_node *node)
28956bec294SChris Mason {
29056bec294SChris Mason 	WARN_ON(!btrfs_delayed_ref_is_head(node));
29156bec294SChris Mason 	return container_of(node, struct btrfs_delayed_ref_head, node);
29256bec294SChris Mason }
29356bec294SChris Mason #endif
294