xref: /openbmc/linux/fs/btrfs/delayed-ref.h (revision 5d4f98a2)
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 
2156bec294SChris Mason /* these are the possible values of struct btrfs_delayed_ref->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 
3656bec294SChris Mason 	/* ref count on this data structure */
3756bec294SChris Mason 	atomic_t refs;
3856bec294SChris Mason 
3956bec294SChris Mason 	/*
4056bec294SChris Mason 	 * how many refs is this entry adding or deleting.  For
4156bec294SChris Mason 	 * head refs, this may be a negative number because it is keeping
4256bec294SChris Mason 	 * track of the total mods done to the reference count.
4356bec294SChris Mason 	 * For individual refs, this will always be a positive number
4456bec294SChris Mason 	 *
4556bec294SChris Mason 	 * It may be more than one, since it is possible for a single
4656bec294SChris Mason 	 * parent to have more than one ref on an extent
4756bec294SChris Mason 	 */
4856bec294SChris Mason 	int ref_mod;
4956bec294SChris Mason 
505d4f98a2SYan Zheng 	unsigned int action:8;
515d4f98a2SYan Zheng 	unsigned int type:8;
5256bec294SChris Mason 	/* is this node still in the rbtree? */
535d4f98a2SYan Zheng 	unsigned int is_head:1;
5456bec294SChris Mason 	unsigned int in_tree:1;
5556bec294SChris Mason };
5656bec294SChris Mason 
575d4f98a2SYan Zheng struct btrfs_delayed_extent_op {
585d4f98a2SYan Zheng 	struct btrfs_disk_key key;
595d4f98a2SYan Zheng 	u64 flags_to_set;
605d4f98a2SYan Zheng 	unsigned int update_key:1;
615d4f98a2SYan Zheng 	unsigned int update_flags:1;
625d4f98a2SYan Zheng 	unsigned int is_data:1;
635d4f98a2SYan Zheng };
645d4f98a2SYan Zheng 
6556bec294SChris Mason /*
6656bec294SChris Mason  * the head refs are used to hold a lock on a given extent, which allows us
6756bec294SChris Mason  * to make sure that only one process is running the delayed refs
6856bec294SChris Mason  * at a time for a single extent.  They also store the sum of all the
6956bec294SChris Mason  * reference count modifications we've queued up.
7056bec294SChris Mason  */
7156bec294SChris Mason struct btrfs_delayed_ref_head {
7256bec294SChris Mason 	struct btrfs_delayed_ref_node node;
7356bec294SChris Mason 
7456bec294SChris Mason 	/*
7556bec294SChris Mason 	 * the mutex is held while running the refs, and it is also
7656bec294SChris Mason 	 * held when checking the sum of reference modifications.
7756bec294SChris Mason 	 */
7856bec294SChris Mason 	struct mutex mutex;
7956bec294SChris Mason 
80c3e69d58SChris Mason 	struct list_head cluster;
81c3e69d58SChris Mason 
825d4f98a2SYan Zheng 	struct btrfs_delayed_extent_op *extent_op;
8356bec294SChris Mason 	/*
8456bec294SChris Mason 	 * when a new extent is allocated, it is just reserved in memory
8556bec294SChris Mason 	 * The actual extent isn't inserted into the extent allocation tree
8656bec294SChris Mason 	 * until the delayed ref is processed.  must_insert_reserved is
8756bec294SChris Mason 	 * used to flag a delayed ref so the accounting can be updated
8856bec294SChris Mason 	 * when a full insert is done.
8956bec294SChris Mason 	 *
9056bec294SChris Mason 	 * It is possible the extent will be freed before it is ever
9156bec294SChris Mason 	 * inserted into the extent allocation tree.  In this case
9256bec294SChris Mason 	 * we need to update the in ram accounting to properly reflect
9356bec294SChris Mason 	 * the free has happened.
9456bec294SChris Mason 	 */
9556bec294SChris Mason 	unsigned int must_insert_reserved:1;
965d4f98a2SYan Zheng 	unsigned int is_data:1;
9756bec294SChris Mason };
9856bec294SChris Mason 
995d4f98a2SYan Zheng struct btrfs_delayed_tree_ref {
10056bec294SChris Mason 	struct btrfs_delayed_ref_node node;
1015d4f98a2SYan Zheng 	union {
10256bec294SChris Mason 		u64 root;
1035d4f98a2SYan Zheng 		u64 parent;
1045d4f98a2SYan Zheng 	};
1055d4f98a2SYan Zheng 	int level;
1065d4f98a2SYan Zheng };
10756bec294SChris Mason 
1085d4f98a2SYan Zheng struct btrfs_delayed_data_ref {
1095d4f98a2SYan Zheng 	struct btrfs_delayed_ref_node node;
1105d4f98a2SYan Zheng 	union {
1115d4f98a2SYan Zheng 		u64 root;
1125d4f98a2SYan Zheng 		u64 parent;
1135d4f98a2SYan Zheng 	};
1145d4f98a2SYan Zheng 	u64 objectid;
1155d4f98a2SYan Zheng 	u64 offset;
11656bec294SChris Mason };
11756bec294SChris Mason 
11856bec294SChris Mason struct btrfs_delayed_ref_root {
11956bec294SChris Mason 	struct rb_root root;
12056bec294SChris Mason 
12156bec294SChris Mason 	/* this spin lock protects the rbtree and the entries inside */
12256bec294SChris Mason 	spinlock_t lock;
12356bec294SChris Mason 
12456bec294SChris Mason 	/* how many delayed ref updates we've queued, used by the
12556bec294SChris Mason 	 * throttling code
12656bec294SChris Mason 	 */
12756bec294SChris Mason 	unsigned long num_entries;
12856bec294SChris Mason 
129c3e69d58SChris Mason 	/* total number of head nodes in tree */
130c3e69d58SChris Mason 	unsigned long num_heads;
131c3e69d58SChris Mason 
132c3e69d58SChris Mason 	/* total number of head nodes ready for processing */
133c3e69d58SChris Mason 	unsigned long num_heads_ready;
134c3e69d58SChris Mason 
13556bec294SChris Mason 	/*
13656bec294SChris Mason 	 * set when the tree is flushing before a transaction commit,
13756bec294SChris Mason 	 * used by the throttling code to decide if new updates need
13856bec294SChris Mason 	 * to be run right away
13956bec294SChris Mason 	 */
14056bec294SChris Mason 	int flushing;
141c3e69d58SChris Mason 
142c3e69d58SChris Mason 	u64 run_delayed_start;
14356bec294SChris Mason };
14456bec294SChris Mason 
14556bec294SChris Mason static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
14656bec294SChris Mason {
14756bec294SChris Mason 	WARN_ON(atomic_read(&ref->refs) == 0);
14856bec294SChris Mason 	if (atomic_dec_and_test(&ref->refs)) {
14956bec294SChris Mason 		WARN_ON(ref->in_tree);
15056bec294SChris Mason 		kfree(ref);
15156bec294SChris Mason 	}
15256bec294SChris Mason }
15356bec294SChris Mason 
1545d4f98a2SYan Zheng int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
1555d4f98a2SYan Zheng 			       u64 bytenr, u64 num_bytes, u64 parent,
1565d4f98a2SYan Zheng 			       u64 ref_root, int level, int action,
1575d4f98a2SYan Zheng 			       struct btrfs_delayed_extent_op *extent_op);
1585d4f98a2SYan Zheng int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
1595d4f98a2SYan Zheng 			       u64 bytenr, u64 num_bytes,
1605d4f98a2SYan Zheng 			       u64 parent, u64 ref_root,
1615d4f98a2SYan Zheng 			       u64 owner, u64 offset, int action,
1625d4f98a2SYan Zheng 			       struct btrfs_delayed_extent_op *extent_op);
1635d4f98a2SYan Zheng int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
1645d4f98a2SYan Zheng 				u64 bytenr, u64 num_bytes,
1655d4f98a2SYan Zheng 				struct btrfs_delayed_extent_op *extent_op);
16656bec294SChris Mason 
1671887be66SChris Mason struct btrfs_delayed_ref_head *
1681887be66SChris Mason btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr);
16956bec294SChris Mason int btrfs_delayed_ref_pending(struct btrfs_trans_handle *trans, u64 bytenr);
1705d4f98a2SYan Zheng int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
17156bec294SChris Mason 			     struct btrfs_root *root, u64 bytenr,
1725d4f98a2SYan Zheng 			     u64 num_bytes, u64 *refs, u64 *flags);
17356bec294SChris Mason int btrfs_update_delayed_ref(struct btrfs_trans_handle *trans,
17456bec294SChris Mason 			  u64 bytenr, u64 num_bytes, u64 orig_parent,
17556bec294SChris Mason 			  u64 parent, u64 orig_ref_root, u64 ref_root,
17656bec294SChris Mason 			  u64 orig_ref_generation, u64 ref_generation,
17756bec294SChris Mason 			  u64 owner_objectid, int pin);
178c3e69d58SChris Mason int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
179c3e69d58SChris Mason 			   struct btrfs_delayed_ref_head *head);
180c3e69d58SChris Mason int btrfs_find_ref_cluster(struct btrfs_trans_handle *trans,
181c3e69d58SChris Mason 			   struct list_head *cluster, u64 search_start);
18256bec294SChris Mason /*
18356bec294SChris Mason  * a node might live in a head or a regular ref, this lets you
18456bec294SChris Mason  * test for the proper type to use.
18556bec294SChris Mason  */
18656bec294SChris Mason static int btrfs_delayed_ref_is_head(struct btrfs_delayed_ref_node *node)
18756bec294SChris Mason {
1885d4f98a2SYan Zheng 	return node->is_head;
18956bec294SChris Mason }
19056bec294SChris Mason 
19156bec294SChris Mason /*
19256bec294SChris Mason  * helper functions to cast a node into its container
19356bec294SChris Mason  */
1945d4f98a2SYan Zheng static inline struct btrfs_delayed_tree_ref *
1955d4f98a2SYan Zheng btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
19656bec294SChris Mason {
19756bec294SChris Mason 	WARN_ON(btrfs_delayed_ref_is_head(node));
1985d4f98a2SYan Zheng 	return container_of(node, struct btrfs_delayed_tree_ref, node);
1995d4f98a2SYan Zheng }
20056bec294SChris Mason 
2015d4f98a2SYan Zheng static inline struct btrfs_delayed_data_ref *
2025d4f98a2SYan Zheng btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
2035d4f98a2SYan Zheng {
2045d4f98a2SYan Zheng 	WARN_ON(btrfs_delayed_ref_is_head(node));
2055d4f98a2SYan Zheng 	return container_of(node, struct btrfs_delayed_data_ref, node);
20656bec294SChris Mason }
20756bec294SChris Mason 
20856bec294SChris Mason static inline struct btrfs_delayed_ref_head *
20956bec294SChris Mason btrfs_delayed_node_to_head(struct btrfs_delayed_ref_node *node)
21056bec294SChris Mason {
21156bec294SChris Mason 	WARN_ON(!btrfs_delayed_ref_is_head(node));
21256bec294SChris Mason 	return container_of(node, struct btrfs_delayed_ref_head, node);
21356bec294SChris Mason }
21456bec294SChris Mason #endif
215