delayed-ref.h revision c3e69d58e86c3917ae4e9e31b4acf490a7cafe60
156bec294dea971335d4466b30f2d959f28f6e36dChris Mason/*
256bec294dea971335d4466b30f2d959f28f6e36dChris Mason * Copyright (C) 2008 Oracle.  All rights reserved.
356bec294dea971335d4466b30f2d959f28f6e36dChris Mason *
456bec294dea971335d4466b30f2d959f28f6e36dChris Mason * This program is free software; you can redistribute it and/or
556bec294dea971335d4466b30f2d959f28f6e36dChris Mason * modify it under the terms of the GNU General Public
656bec294dea971335d4466b30f2d959f28f6e36dChris Mason * License v2 as published by the Free Software Foundation.
756bec294dea971335d4466b30f2d959f28f6e36dChris Mason *
856bec294dea971335d4466b30f2d959f28f6e36dChris Mason * This program is distributed in the hope that it will be useful,
956bec294dea971335d4466b30f2d959f28f6e36dChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of
1056bec294dea971335d4466b30f2d959f28f6e36dChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1156bec294dea971335d4466b30f2d959f28f6e36dChris Mason * General Public License for more details.
1256bec294dea971335d4466b30f2d959f28f6e36dChris Mason *
1356bec294dea971335d4466b30f2d959f28f6e36dChris Mason * You should have received a copy of the GNU General Public
1456bec294dea971335d4466b30f2d959f28f6e36dChris Mason * License along with this program; if not, write to the
1556bec294dea971335d4466b30f2d959f28f6e36dChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1656bec294dea971335d4466b30f2d959f28f6e36dChris Mason * Boston, MA 021110-1307, USA.
1756bec294dea971335d4466b30f2d959f28f6e36dChris Mason */
1856bec294dea971335d4466b30f2d959f28f6e36dChris Mason#ifndef __DELAYED_REF__
1956bec294dea971335d4466b30f2d959f28f6e36dChris Mason#define __DELAYED_REF__
2056bec294dea971335d4466b30f2d959f28f6e36dChris Mason
2156bec294dea971335d4466b30f2d959f28f6e36dChris Mason/* these are the possible values of struct btrfs_delayed_ref->action */
2256bec294dea971335d4466b30f2d959f28f6e36dChris Mason#define BTRFS_ADD_DELAYED_REF    1 /* add one backref to the tree */
2356bec294dea971335d4466b30f2d959f28f6e36dChris Mason#define BTRFS_DROP_DELAYED_REF   2 /* delete one backref from the tree */
2456bec294dea971335d4466b30f2d959f28f6e36dChris Mason#define BTRFS_ADD_DELAYED_EXTENT 3 /* record a full extent allocation */
2556bec294dea971335d4466b30f2d959f28f6e36dChris Mason
2656bec294dea971335d4466b30f2d959f28f6e36dChris Masonstruct btrfs_delayed_ref_node {
2756bec294dea971335d4466b30f2d959f28f6e36dChris Mason	struct rb_node rb_node;
2856bec294dea971335d4466b30f2d959f28f6e36dChris Mason
2956bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/* the starting bytenr of the extent */
3056bec294dea971335d4466b30f2d959f28f6e36dChris Mason	u64 bytenr;
3156bec294dea971335d4466b30f2d959f28f6e36dChris Mason
3256bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/* the parent our backref will point to */
3356bec294dea971335d4466b30f2d959f28f6e36dChris Mason	u64 parent;
3456bec294dea971335d4466b30f2d959f28f6e36dChris Mason
3556bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/* the size of the extent */
3656bec294dea971335d4466b30f2d959f28f6e36dChris Mason	u64 num_bytes;
3756bec294dea971335d4466b30f2d959f28f6e36dChris Mason
3856bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/* ref count on this data structure */
3956bec294dea971335d4466b30f2d959f28f6e36dChris Mason	atomic_t refs;
4056bec294dea971335d4466b30f2d959f28f6e36dChris Mason
4156bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/*
4256bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * how many refs is this entry adding or deleting.  For
4356bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * head refs, this may be a negative number because it is keeping
4456bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * track of the total mods done to the reference count.
4556bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * For individual refs, this will always be a positive number
4656bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 *
4756bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * It may be more than one, since it is possible for a single
4856bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * parent to have more than one ref on an extent
4956bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 */
5056bec294dea971335d4466b30f2d959f28f6e36dChris Mason	int ref_mod;
5156bec294dea971335d4466b30f2d959f28f6e36dChris Mason
5256bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/* is this node still in the rbtree? */
5356bec294dea971335d4466b30f2d959f28f6e36dChris Mason	unsigned int in_tree:1;
5456bec294dea971335d4466b30f2d959f28f6e36dChris Mason};
5556bec294dea971335d4466b30f2d959f28f6e36dChris Mason
5656bec294dea971335d4466b30f2d959f28f6e36dChris Mason/*
5756bec294dea971335d4466b30f2d959f28f6e36dChris Mason * the head refs are used to hold a lock on a given extent, which allows us
5856bec294dea971335d4466b30f2d959f28f6e36dChris Mason * to make sure that only one process is running the delayed refs
5956bec294dea971335d4466b30f2d959f28f6e36dChris Mason * at a time for a single extent.  They also store the sum of all the
6056bec294dea971335d4466b30f2d959f28f6e36dChris Mason * reference count modifications we've queued up.
6156bec294dea971335d4466b30f2d959f28f6e36dChris Mason */
6256bec294dea971335d4466b30f2d959f28f6e36dChris Masonstruct btrfs_delayed_ref_head {
6356bec294dea971335d4466b30f2d959f28f6e36dChris Mason	struct btrfs_delayed_ref_node node;
6456bec294dea971335d4466b30f2d959f28f6e36dChris Mason
6556bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/*
6656bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * the mutex is held while running the refs, and it is also
6756bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * held when checking the sum of reference modifications.
6856bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 */
6956bec294dea971335d4466b30f2d959f28f6e36dChris Mason	struct mutex mutex;
7056bec294dea971335d4466b30f2d959f28f6e36dChris Mason
71c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Mason	struct list_head cluster;
72c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Mason
7356bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/*
7456bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * when a new extent is allocated, it is just reserved in memory
7556bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * The actual extent isn't inserted into the extent allocation tree
7656bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * until the delayed ref is processed.  must_insert_reserved is
7756bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * used to flag a delayed ref so the accounting can be updated
7856bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * when a full insert is done.
7956bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 *
8056bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * It is possible the extent will be freed before it is ever
8156bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * inserted into the extent allocation tree.  In this case
8256bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * we need to update the in ram accounting to properly reflect
8356bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * the free has happened.
8456bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 */
8556bec294dea971335d4466b30f2d959f28f6e36dChris Mason	unsigned int must_insert_reserved:1;
8656bec294dea971335d4466b30f2d959f28f6e36dChris Mason};
8756bec294dea971335d4466b30f2d959f28f6e36dChris Mason
8856bec294dea971335d4466b30f2d959f28f6e36dChris Masonstruct btrfs_delayed_ref {
8956bec294dea971335d4466b30f2d959f28f6e36dChris Mason	struct btrfs_delayed_ref_node node;
9056bec294dea971335d4466b30f2d959f28f6e36dChris Mason
9156bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/* the root objectid our ref will point to */
9256bec294dea971335d4466b30f2d959f28f6e36dChris Mason	u64 root;
9356bec294dea971335d4466b30f2d959f28f6e36dChris Mason
9456bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/* the generation for the backref */
9556bec294dea971335d4466b30f2d959f28f6e36dChris Mason	u64 generation;
9656bec294dea971335d4466b30f2d959f28f6e36dChris Mason
9756bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/* owner_objectid of the backref  */
9856bec294dea971335d4466b30f2d959f28f6e36dChris Mason	u64 owner_objectid;
9956bec294dea971335d4466b30f2d959f28f6e36dChris Mason
10056bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/* operation done by this entry in the rbtree */
10156bec294dea971335d4466b30f2d959f28f6e36dChris Mason	u8 action;
10256bec294dea971335d4466b30f2d959f28f6e36dChris Mason
10356bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/* if pin == 1, when the extent is freed it will be pinned until
10456bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * transaction commit
10556bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 */
10656bec294dea971335d4466b30f2d959f28f6e36dChris Mason	unsigned int pin:1;
10756bec294dea971335d4466b30f2d959f28f6e36dChris Mason};
10856bec294dea971335d4466b30f2d959f28f6e36dChris Mason
10956bec294dea971335d4466b30f2d959f28f6e36dChris Masonstruct btrfs_delayed_ref_root {
11056bec294dea971335d4466b30f2d959f28f6e36dChris Mason	struct rb_root root;
11156bec294dea971335d4466b30f2d959f28f6e36dChris Mason
11256bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/* this spin lock protects the rbtree and the entries inside */
11356bec294dea971335d4466b30f2d959f28f6e36dChris Mason	spinlock_t lock;
11456bec294dea971335d4466b30f2d959f28f6e36dChris Mason
11556bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/* how many delayed ref updates we've queued, used by the
11656bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * throttling code
11756bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 */
11856bec294dea971335d4466b30f2d959f28f6e36dChris Mason	unsigned long num_entries;
11956bec294dea971335d4466b30f2d959f28f6e36dChris Mason
120c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Mason	/* total number of head nodes in tree */
121c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Mason	unsigned long num_heads;
122c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Mason
123c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Mason	/* total number of head nodes ready for processing */
124c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Mason	unsigned long num_heads_ready;
125c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Mason
12656bec294dea971335d4466b30f2d959f28f6e36dChris Mason	/*
12756bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * set when the tree is flushing before a transaction commit,
12856bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * used by the throttling code to decide if new updates need
12956bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 * to be run right away
13056bec294dea971335d4466b30f2d959f28f6e36dChris Mason	 */
13156bec294dea971335d4466b30f2d959f28f6e36dChris Mason	int flushing;
132c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Mason
133c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Mason	u64 run_delayed_start;
13456bec294dea971335d4466b30f2d959f28f6e36dChris Mason};
13556bec294dea971335d4466b30f2d959f28f6e36dChris Mason
13656bec294dea971335d4466b30f2d959f28f6e36dChris Masonstatic inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
13756bec294dea971335d4466b30f2d959f28f6e36dChris Mason{
13856bec294dea971335d4466b30f2d959f28f6e36dChris Mason	WARN_ON(atomic_read(&ref->refs) == 0);
13956bec294dea971335d4466b30f2d959f28f6e36dChris Mason	if (atomic_dec_and_test(&ref->refs)) {
14056bec294dea971335d4466b30f2d959f28f6e36dChris Mason		WARN_ON(ref->in_tree);
14156bec294dea971335d4466b30f2d959f28f6e36dChris Mason		kfree(ref);
14256bec294dea971335d4466b30f2d959f28f6e36dChris Mason	}
14356bec294dea971335d4466b30f2d959f28f6e36dChris Mason}
14456bec294dea971335d4466b30f2d959f28f6e36dChris Mason
14556bec294dea971335d4466b30f2d959f28f6e36dChris Masonint btrfs_add_delayed_ref(struct btrfs_trans_handle *trans,
14656bec294dea971335d4466b30f2d959f28f6e36dChris Mason			  u64 bytenr, u64 num_bytes, u64 parent, u64 ref_root,
14756bec294dea971335d4466b30f2d959f28f6e36dChris Mason			  u64 ref_generation, u64 owner_objectid, int action,
14856bec294dea971335d4466b30f2d959f28f6e36dChris Mason			  int pin);
14956bec294dea971335d4466b30f2d959f28f6e36dChris Mason
1501887be66dcc3140a81d1299958a41fc0eedfa64fChris Masonstruct btrfs_delayed_ref_head *
1511887be66dcc3140a81d1299958a41fc0eedfa64fChris Masonbtrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr);
15256bec294dea971335d4466b30f2d959f28f6e36dChris Masonint btrfs_delayed_ref_pending(struct btrfs_trans_handle *trans, u64 bytenr);
15356bec294dea971335d4466b30f2d959f28f6e36dChris Masonint btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
15456bec294dea971335d4466b30f2d959f28f6e36dChris Mason			    struct btrfs_root *root, u64 bytenr,
15556bec294dea971335d4466b30f2d959f28f6e36dChris Mason			    u64 num_bytes, u32 *refs);
15656bec294dea971335d4466b30f2d959f28f6e36dChris Masonint btrfs_update_delayed_ref(struct btrfs_trans_handle *trans,
15756bec294dea971335d4466b30f2d959f28f6e36dChris Mason			  u64 bytenr, u64 num_bytes, u64 orig_parent,
15856bec294dea971335d4466b30f2d959f28f6e36dChris Mason			  u64 parent, u64 orig_ref_root, u64 ref_root,
15956bec294dea971335d4466b30f2d959f28f6e36dChris Mason			  u64 orig_ref_generation, u64 ref_generation,
16056bec294dea971335d4466b30f2d959f28f6e36dChris Mason			  u64 owner_objectid, int pin);
161c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Masonint btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
162c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Mason			   struct btrfs_delayed_ref_head *head);
163c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Masonint btrfs_find_ref_cluster(struct btrfs_trans_handle *trans,
164c3e69d58e86c3917ae4e9e31b4acf490a7cafe60Chris Mason			   struct list_head *cluster, u64 search_start);
16556bec294dea971335d4466b30f2d959f28f6e36dChris Mason/*
16656bec294dea971335d4466b30f2d959f28f6e36dChris Mason * a node might live in a head or a regular ref, this lets you
16756bec294dea971335d4466b30f2d959f28f6e36dChris Mason * test for the proper type to use.
16856bec294dea971335d4466b30f2d959f28f6e36dChris Mason */
16956bec294dea971335d4466b30f2d959f28f6e36dChris Masonstatic int btrfs_delayed_ref_is_head(struct btrfs_delayed_ref_node *node)
17056bec294dea971335d4466b30f2d959f28f6e36dChris Mason{
17156bec294dea971335d4466b30f2d959f28f6e36dChris Mason	return node->parent == (u64)-1;
17256bec294dea971335d4466b30f2d959f28f6e36dChris Mason}
17356bec294dea971335d4466b30f2d959f28f6e36dChris Mason
17456bec294dea971335d4466b30f2d959f28f6e36dChris Mason/*
17556bec294dea971335d4466b30f2d959f28f6e36dChris Mason * helper functions to cast a node into its container
17656bec294dea971335d4466b30f2d959f28f6e36dChris Mason */
17756bec294dea971335d4466b30f2d959f28f6e36dChris Masonstatic inline struct btrfs_delayed_ref *
17856bec294dea971335d4466b30f2d959f28f6e36dChris Masonbtrfs_delayed_node_to_ref(struct btrfs_delayed_ref_node *node)
17956bec294dea971335d4466b30f2d959f28f6e36dChris Mason{
18056bec294dea971335d4466b30f2d959f28f6e36dChris Mason	WARN_ON(btrfs_delayed_ref_is_head(node));
18156bec294dea971335d4466b30f2d959f28f6e36dChris Mason	return container_of(node, struct btrfs_delayed_ref, node);
18256bec294dea971335d4466b30f2d959f28f6e36dChris Mason
18356bec294dea971335d4466b30f2d959f28f6e36dChris Mason}
18456bec294dea971335d4466b30f2d959f28f6e36dChris Mason
18556bec294dea971335d4466b30f2d959f28f6e36dChris Masonstatic inline struct btrfs_delayed_ref_head *
18656bec294dea971335d4466b30f2d959f28f6e36dChris Masonbtrfs_delayed_node_to_head(struct btrfs_delayed_ref_node *node)
18756bec294dea971335d4466b30f2d959f28f6e36dChris Mason{
18856bec294dea971335d4466b30f2d959f28f6e36dChris Mason	WARN_ON(!btrfs_delayed_ref_is_head(node));
18956bec294dea971335d4466b30f2d959f28f6e36dChris Mason	return container_of(node, struct btrfs_delayed_ref_head, node);
19056bec294dea971335d4466b30f2d959f28f6e36dChris Mason
19156bec294dea971335d4466b30f2d959f28f6e36dChris Mason}
19256bec294dea971335d4466b30f2d959f28f6e36dChris Mason#endif
193