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; 451d57ee94SWang Xiaoguang /* 461d57ee94SWang Xiaoguang * If action is BTRFS_ADD_DELAYED_REF, also link this node to 471d57ee94SWang Xiaoguang * ref_head->ref_add_list, then we do not need to iterate the 481d57ee94SWang Xiaoguang * whole ref_head->ref_list to find BTRFS_ADD_DELAYED_REF nodes. 491d57ee94SWang Xiaoguang */ 501d57ee94SWang Xiaoguang struct list_head add_list; 51c6fc2454SQu Wenruo 5256bec294SChris Mason /* the starting bytenr of the extent */ 5356bec294SChris Mason u64 bytenr; 5456bec294SChris Mason 5556bec294SChris Mason /* the size of the extent */ 5656bec294SChris Mason u64 num_bytes; 5756bec294SChris Mason 5800f04b88SArne Jansen /* seq number to keep track of insertion order */ 5900f04b88SArne Jansen u64 seq; 6000f04b88SArne Jansen 6156bec294SChris Mason /* ref count on this data structure */ 6256bec294SChris Mason atomic_t refs; 6356bec294SChris Mason 6456bec294SChris Mason /* 6556bec294SChris Mason * how many refs is this entry adding or deleting. For 6656bec294SChris Mason * head refs, this may be a negative number because it is keeping 6756bec294SChris Mason * track of the total mods done to the reference count. 6856bec294SChris Mason * For individual refs, this will always be a positive number 6956bec294SChris Mason * 7056bec294SChris Mason * It may be more than one, since it is possible for a single 7156bec294SChris Mason * parent to have more than one ref on an extent 7256bec294SChris Mason */ 7356bec294SChris Mason int ref_mod; 7456bec294SChris Mason 755d4f98a2SYan Zheng unsigned int action:8; 765d4f98a2SYan Zheng unsigned int type:8; 7756bec294SChris Mason /* is this node still in the rbtree? */ 785d4f98a2SYan Zheng unsigned int is_head:1; 7956bec294SChris Mason unsigned int in_tree:1; 8056bec294SChris Mason }; 8156bec294SChris Mason 825d4f98a2SYan Zheng struct btrfs_delayed_extent_op { 835d4f98a2SYan Zheng struct btrfs_disk_key key; 8435b3ad50SDavid Sterba u8 level; 8535b3ad50SDavid Sterba bool update_key; 8635b3ad50SDavid Sterba bool update_flags; 8735b3ad50SDavid Sterba bool is_data; 885d4f98a2SYan Zheng u64 flags_to_set; 895d4f98a2SYan Zheng }; 905d4f98a2SYan Zheng 9156bec294SChris Mason /* 9256bec294SChris Mason * the head refs are used to hold a lock on a given extent, which allows us 9356bec294SChris Mason * to make sure that only one process is running the delayed refs 9456bec294SChris Mason * at a time for a single extent. They also store the sum of all the 9556bec294SChris Mason * reference count modifications we've queued up. 9656bec294SChris Mason */ 9756bec294SChris Mason struct btrfs_delayed_ref_head { 9856bec294SChris Mason struct btrfs_delayed_ref_node node; 9956bec294SChris Mason 10056bec294SChris Mason /* 10156bec294SChris Mason * the mutex is held while running the refs, and it is also 10256bec294SChris Mason * held when checking the sum of reference modifications. 10356bec294SChris Mason */ 10456bec294SChris Mason struct mutex mutex; 10556bec294SChris Mason 106d7df2c79SJosef Bacik spinlock_t lock; 107c6fc2454SQu Wenruo struct list_head ref_list; 1081d57ee94SWang Xiaoguang /* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */ 1091d57ee94SWang Xiaoguang struct list_head ref_add_list; 110c3e69d58SChris Mason 111c46effa6SLiu Bo struct rb_node href_node; 112c46effa6SLiu Bo 1135d4f98a2SYan Zheng struct btrfs_delayed_extent_op *extent_op; 1141262133bSJosef Bacik 1151262133bSJosef Bacik /* 1161262133bSJosef Bacik * This is used to track the final ref_mod from all the refs associated 1171262133bSJosef Bacik * with this head ref, this is not adjusted as delayed refs are run, 1181262133bSJosef Bacik * this is meant to track if we need to do the csum accounting or not. 1191262133bSJosef Bacik */ 1201262133bSJosef Bacik int total_ref_mod; 1211262133bSJosef Bacik 12256bec294SChris Mason /* 123f64d5ca8SQu Wenruo * For qgroup reserved space freeing. 124f64d5ca8SQu Wenruo * 125f64d5ca8SQu Wenruo * ref_root and reserved will be recorded after 126f64d5ca8SQu Wenruo * BTRFS_ADD_DELAYED_EXTENT is called. 127f64d5ca8SQu Wenruo * And will be used to free reserved qgroup space at 128f64d5ca8SQu Wenruo * run_delayed_refs() time. 129f64d5ca8SQu Wenruo */ 130f64d5ca8SQu Wenruo u64 qgroup_ref_root; 131f64d5ca8SQu Wenruo u64 qgroup_reserved; 132f64d5ca8SQu Wenruo 133f64d5ca8SQu Wenruo /* 13456bec294SChris Mason * when a new extent is allocated, it is just reserved in memory 13556bec294SChris Mason * The actual extent isn't inserted into the extent allocation tree 13656bec294SChris Mason * until the delayed ref is processed. must_insert_reserved is 13756bec294SChris Mason * used to flag a delayed ref so the accounting can be updated 13856bec294SChris Mason * when a full insert is done. 13956bec294SChris Mason * 14056bec294SChris Mason * It is possible the extent will be freed before it is ever 14156bec294SChris Mason * inserted into the extent allocation tree. In this case 14256bec294SChris Mason * we need to update the in ram accounting to properly reflect 14356bec294SChris Mason * the free has happened. 14456bec294SChris Mason */ 14556bec294SChris Mason unsigned int must_insert_reserved:1; 1465d4f98a2SYan Zheng unsigned int is_data:1; 147d7df2c79SJosef Bacik unsigned int processing:1; 14856bec294SChris Mason }; 14956bec294SChris Mason 1505d4f98a2SYan Zheng struct btrfs_delayed_tree_ref { 15156bec294SChris Mason struct btrfs_delayed_ref_node node; 15256bec294SChris Mason u64 root; 1535d4f98a2SYan Zheng u64 parent; 1545d4f98a2SYan Zheng int level; 1555d4f98a2SYan Zheng }; 15656bec294SChris Mason 1575d4f98a2SYan Zheng struct btrfs_delayed_data_ref { 1585d4f98a2SYan Zheng struct btrfs_delayed_ref_node node; 1595d4f98a2SYan Zheng u64 root; 1605d4f98a2SYan Zheng u64 parent; 1615d4f98a2SYan Zheng u64 objectid; 1625d4f98a2SYan Zheng u64 offset; 16356bec294SChris Mason }; 16456bec294SChris Mason 16556bec294SChris Mason struct btrfs_delayed_ref_root { 166c46effa6SLiu Bo /* head ref rbtree */ 167c46effa6SLiu Bo struct rb_root href_root; 168c46effa6SLiu Bo 1693368d001SQu Wenruo /* dirty extent records */ 1703368d001SQu Wenruo struct rb_root dirty_extent_root; 1713368d001SQu Wenruo 17256bec294SChris Mason /* this spin lock protects the rbtree and the entries inside */ 17356bec294SChris Mason spinlock_t lock; 17456bec294SChris Mason 17556bec294SChris Mason /* how many delayed ref updates we've queued, used by the 17656bec294SChris Mason * throttling code 17756bec294SChris Mason */ 178d7df2c79SJosef Bacik atomic_t num_entries; 17956bec294SChris Mason 180c3e69d58SChris Mason /* total number of head nodes in tree */ 181c3e69d58SChris Mason unsigned long num_heads; 182c3e69d58SChris Mason 183c3e69d58SChris Mason /* total number of head nodes ready for processing */ 184c3e69d58SChris Mason unsigned long num_heads_ready; 185c3e69d58SChris Mason 1861262133bSJosef Bacik u64 pending_csums; 1871262133bSJosef Bacik 18856bec294SChris Mason /* 18956bec294SChris Mason * set when the tree is flushing before a transaction commit, 19056bec294SChris Mason * used by the throttling code to decide if new updates need 19156bec294SChris Mason * to be run right away 19256bec294SChris Mason */ 19356bec294SChris Mason int flushing; 194c3e69d58SChris Mason 195c3e69d58SChris Mason u64 run_delayed_start; 1969086db86SQu Wenruo 1979086db86SQu Wenruo /* 1989086db86SQu Wenruo * To make qgroup to skip given root. 19901327610SNicholas D Steeves * This is for snapshot, as btrfs_qgroup_inherit() will manually 2009086db86SQu Wenruo * modify counters for snapshot and its source, so we should skip 2019086db86SQu Wenruo * the snapshot in new_root/old_roots or it will get calculated twice 2029086db86SQu Wenruo */ 2039086db86SQu Wenruo u64 qgroup_to_skip; 20456bec294SChris Mason }; 20556bec294SChris Mason 20678a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_ref_head_cachep; 20778a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_tree_ref_cachep; 20878a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_data_ref_cachep; 20978a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_extent_op_cachep; 21078a6184aSMiao Xie 21178a6184aSMiao Xie int btrfs_delayed_ref_init(void); 21278a6184aSMiao Xie void btrfs_delayed_ref_exit(void); 21378a6184aSMiao Xie 21478a6184aSMiao Xie static inline struct btrfs_delayed_extent_op * 21578a6184aSMiao Xie btrfs_alloc_delayed_extent_op(void) 21678a6184aSMiao Xie { 21778a6184aSMiao Xie return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS); 21878a6184aSMiao Xie } 21978a6184aSMiao Xie 22078a6184aSMiao Xie static inline void 22178a6184aSMiao Xie btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op) 22278a6184aSMiao Xie { 22378a6184aSMiao Xie if (op) 22478a6184aSMiao Xie kmem_cache_free(btrfs_delayed_extent_op_cachep, op); 22578a6184aSMiao Xie } 22678a6184aSMiao Xie 22756bec294SChris Mason static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref) 22856bec294SChris Mason { 22956bec294SChris Mason WARN_ON(atomic_read(&ref->refs) == 0); 23056bec294SChris Mason if (atomic_dec_and_test(&ref->refs)) { 23156bec294SChris Mason WARN_ON(ref->in_tree); 23278a6184aSMiao Xie switch (ref->type) { 23378a6184aSMiao Xie case BTRFS_TREE_BLOCK_REF_KEY: 23478a6184aSMiao Xie case BTRFS_SHARED_BLOCK_REF_KEY: 23578a6184aSMiao Xie kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref); 23678a6184aSMiao Xie break; 23778a6184aSMiao Xie case BTRFS_EXTENT_DATA_REF_KEY: 23878a6184aSMiao Xie case BTRFS_SHARED_DATA_REF_KEY: 23978a6184aSMiao Xie kmem_cache_free(btrfs_delayed_data_ref_cachep, ref); 24078a6184aSMiao Xie break; 24178a6184aSMiao Xie case 0: 24278a6184aSMiao Xie kmem_cache_free(btrfs_delayed_ref_head_cachep, ref); 24378a6184aSMiao Xie break; 24478a6184aSMiao Xie default: 24578a6184aSMiao Xie BUG(); 24678a6184aSMiao Xie } 24756bec294SChris Mason } 24856bec294SChris Mason } 24956bec294SChris Mason 25066d7e7f0SArne Jansen int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info, 25166d7e7f0SArne Jansen struct btrfs_trans_handle *trans, 2525d4f98a2SYan Zheng u64 bytenr, u64 num_bytes, u64 parent, 2535d4f98a2SYan Zheng u64 ref_root, int level, int action, 254b06c4bf5SFilipe Manana struct btrfs_delayed_extent_op *extent_op); 25566d7e7f0SArne Jansen int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info, 25666d7e7f0SArne Jansen struct btrfs_trans_handle *trans, 2575d4f98a2SYan Zheng u64 bytenr, u64 num_bytes, 2585d4f98a2SYan Zheng u64 parent, u64 ref_root, 2595846a3c2SQu Wenruo u64 owner, u64 offset, u64 reserved, int action, 260b06c4bf5SFilipe Manana struct btrfs_delayed_extent_op *extent_op); 26166d7e7f0SArne Jansen int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info, 26266d7e7f0SArne Jansen struct btrfs_trans_handle *trans, 2635d4f98a2SYan Zheng u64 bytenr, u64 num_bytes, 2645d4f98a2SYan Zheng struct btrfs_delayed_extent_op *extent_op); 265ae1e206bSJosef Bacik void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans, 266ae1e206bSJosef Bacik struct btrfs_fs_info *fs_info, 267ae1e206bSJosef Bacik struct btrfs_delayed_ref_root *delayed_refs, 268ae1e206bSJosef Bacik struct btrfs_delayed_ref_head *head); 26956bec294SChris Mason 2701887be66SChris Mason struct btrfs_delayed_ref_head * 2711887be66SChris Mason btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr); 272c3e69d58SChris Mason int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans, 273c3e69d58SChris Mason struct btrfs_delayed_ref_head *head); 274093486c4SMiao Xie static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head) 275093486c4SMiao Xie { 276093486c4SMiao Xie mutex_unlock(&head->mutex); 277093486c4SMiao Xie } 278093486c4SMiao Xie 279d7df2c79SJosef Bacik 280d7df2c79SJosef Bacik struct btrfs_delayed_ref_head * 281d7df2c79SJosef Bacik btrfs_select_ref_head(struct btrfs_trans_handle *trans); 28200f04b88SArne Jansen 283097b8a7cSJan Schmidt int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, 284097b8a7cSJan Schmidt struct btrfs_delayed_ref_root *delayed_refs, 28500f04b88SArne Jansen u64 seq); 28600f04b88SArne Jansen 28700f04b88SArne Jansen /* 28856bec294SChris Mason * a node might live in a head or a regular ref, this lets you 28956bec294SChris Mason * test for the proper type to use. 29056bec294SChris Mason */ 29156bec294SChris Mason static int btrfs_delayed_ref_is_head(struct btrfs_delayed_ref_node *node) 29256bec294SChris Mason { 2935d4f98a2SYan Zheng return node->is_head; 29456bec294SChris Mason } 29556bec294SChris Mason 29656bec294SChris Mason /* 29756bec294SChris Mason * helper functions to cast a node into its container 29856bec294SChris Mason */ 2995d4f98a2SYan Zheng static inline struct btrfs_delayed_tree_ref * 3005d4f98a2SYan Zheng btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node) 30156bec294SChris Mason { 30256bec294SChris Mason WARN_ON(btrfs_delayed_ref_is_head(node)); 3035d4f98a2SYan Zheng return container_of(node, struct btrfs_delayed_tree_ref, node); 3045d4f98a2SYan Zheng } 30556bec294SChris Mason 3065d4f98a2SYan Zheng static inline struct btrfs_delayed_data_ref * 3075d4f98a2SYan Zheng btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node) 3085d4f98a2SYan Zheng { 3095d4f98a2SYan Zheng WARN_ON(btrfs_delayed_ref_is_head(node)); 3105d4f98a2SYan Zheng return container_of(node, struct btrfs_delayed_data_ref, node); 31156bec294SChris Mason } 31256bec294SChris Mason 31356bec294SChris Mason static inline struct btrfs_delayed_ref_head * 31456bec294SChris Mason btrfs_delayed_node_to_head(struct btrfs_delayed_ref_node *node) 31556bec294SChris Mason { 31656bec294SChris Mason WARN_ON(!btrfs_delayed_ref_is_head(node)); 31756bec294SChris Mason return container_of(node, struct btrfs_delayed_ref_head, node); 31856bec294SChris Mason } 31956bec294SChris Mason #endif 320