19888c340SDavid Sterba /* SPDX-License-Identifier: GPL-2.0 */ 256bec294SChris Mason /* 356bec294SChris Mason * Copyright (C) 2008 Oracle. All rights reserved. 456bec294SChris Mason */ 59888c340SDavid Sterba 69888c340SDavid Sterba #ifndef BTRFS_DELAYED_REF_H 79888c340SDavid Sterba #define BTRFS_DELAYED_REF_H 856bec294SChris Mason 96df8cdf5SElena Reshetova #include <linux/refcount.h> 106df8cdf5SElena Reshetova 1144a075bdSWang Sheng-Hui /* these are the possible values of struct btrfs_delayed_ref_node->action */ 1256bec294SChris Mason #define BTRFS_ADD_DELAYED_REF 1 /* add one backref to the tree */ 1356bec294SChris Mason #define BTRFS_DROP_DELAYED_REF 2 /* delete one backref from the tree */ 1456bec294SChris Mason #define BTRFS_ADD_DELAYED_EXTENT 3 /* record a full extent allocation */ 151a81af4dSChris Mason #define BTRFS_UPDATE_DELAYED_HEAD 4 /* not changing ref count on head ref */ 1656bec294SChris Mason 1756bec294SChris Mason struct btrfs_delayed_ref_node { 180e0adbcfSJosef Bacik struct rb_node ref_node; 191d57ee94SWang Xiaoguang /* 201d57ee94SWang Xiaoguang * If action is BTRFS_ADD_DELAYED_REF, also link this node to 211d57ee94SWang Xiaoguang * ref_head->ref_add_list, then we do not need to iterate the 221d57ee94SWang Xiaoguang * whole ref_head->ref_list to find BTRFS_ADD_DELAYED_REF nodes. 231d57ee94SWang Xiaoguang */ 241d57ee94SWang Xiaoguang struct list_head add_list; 25c6fc2454SQu Wenruo 2656bec294SChris Mason /* the starting bytenr of the extent */ 2756bec294SChris Mason u64 bytenr; 2856bec294SChris Mason 2956bec294SChris Mason /* the size of the extent */ 3056bec294SChris Mason u64 num_bytes; 3156bec294SChris Mason 3200f04b88SArne Jansen /* seq number to keep track of insertion order */ 3300f04b88SArne Jansen u64 seq; 3400f04b88SArne Jansen 3556bec294SChris Mason /* ref count on this data structure */ 366df8cdf5SElena Reshetova refcount_t refs; 3756bec294SChris Mason 3856bec294SChris Mason /* 3956bec294SChris Mason * how many refs is this entry adding or deleting. For 4056bec294SChris Mason * head refs, this may be a negative number because it is keeping 4156bec294SChris Mason * track of the total mods done to the reference count. 4256bec294SChris Mason * For individual refs, this will always be a positive number 4356bec294SChris Mason * 4456bec294SChris Mason * It may be more than one, since it is possible for a single 4556bec294SChris Mason * parent to have more than one ref on an extent 4656bec294SChris Mason */ 4756bec294SChris Mason int ref_mod; 4856bec294SChris Mason 495d4f98a2SYan Zheng unsigned int action:8; 505d4f98a2SYan Zheng unsigned int type:8; 5156bec294SChris Mason /* is this node still in the rbtree? */ 525d4f98a2SYan Zheng unsigned int is_head:1; 5356bec294SChris Mason unsigned int in_tree:1; 5456bec294SChris Mason }; 5556bec294SChris Mason 565d4f98a2SYan Zheng struct btrfs_delayed_extent_op { 575d4f98a2SYan Zheng struct btrfs_disk_key key; 5835b3ad50SDavid Sterba u8 level; 5935b3ad50SDavid Sterba bool update_key; 6035b3ad50SDavid Sterba bool update_flags; 6135b3ad50SDavid Sterba bool is_data; 625d4f98a2SYan Zheng u64 flags_to_set; 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 { 72d278850eSJosef Bacik u64 bytenr; 73d278850eSJosef Bacik u64 num_bytes; 74d278850eSJosef Bacik refcount_t refs; 7556bec294SChris Mason /* 7656bec294SChris Mason * the mutex is held while running the refs, and it is also 7756bec294SChris Mason * held when checking the sum of reference modifications. 7856bec294SChris Mason */ 7956bec294SChris Mason struct mutex mutex; 8056bec294SChris Mason 81d7df2c79SJosef Bacik spinlock_t lock; 820e0adbcfSJosef Bacik struct rb_root ref_tree; 831d57ee94SWang Xiaoguang /* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */ 841d57ee94SWang Xiaoguang struct list_head ref_add_list; 85c3e69d58SChris Mason 86c46effa6SLiu Bo struct rb_node href_node; 87c46effa6SLiu Bo 885d4f98a2SYan Zheng struct btrfs_delayed_extent_op *extent_op; 891262133bSJosef Bacik 901262133bSJosef Bacik /* 911262133bSJosef Bacik * This is used to track the final ref_mod from all the refs associated 921262133bSJosef Bacik * with this head ref, this is not adjusted as delayed refs are run, 931262133bSJosef Bacik * this is meant to track if we need to do the csum accounting or not. 941262133bSJosef Bacik */ 951262133bSJosef Bacik int total_ref_mod; 961262133bSJosef Bacik 9756bec294SChris Mason /* 98d278850eSJosef Bacik * This is the current outstanding mod references for this bytenr. This 99d278850eSJosef Bacik * is used with lookup_extent_info to get an accurate reference count 100d278850eSJosef Bacik * for a bytenr, so it is adjusted as delayed refs are run so that any 101d278850eSJosef Bacik * on disk reference count + ref_mod is accurate. 102d278850eSJosef Bacik */ 103d278850eSJosef Bacik int ref_mod; 104d278850eSJosef Bacik 105d278850eSJosef Bacik /* 106f64d5ca8SQu Wenruo * For qgroup reserved space freeing. 107f64d5ca8SQu Wenruo * 108f64d5ca8SQu Wenruo * ref_root and reserved will be recorded after 109f64d5ca8SQu Wenruo * BTRFS_ADD_DELAYED_EXTENT is called. 110f64d5ca8SQu Wenruo * And will be used to free reserved qgroup space at 111f64d5ca8SQu Wenruo * run_delayed_refs() time. 112f64d5ca8SQu Wenruo */ 113f64d5ca8SQu Wenruo u64 qgroup_ref_root; 114f64d5ca8SQu Wenruo u64 qgroup_reserved; 115f64d5ca8SQu Wenruo 116f64d5ca8SQu Wenruo /* 11756bec294SChris Mason * when a new extent is allocated, it is just reserved in memory 11856bec294SChris Mason * The actual extent isn't inserted into the extent allocation tree 11956bec294SChris Mason * until the delayed ref is processed. must_insert_reserved is 12056bec294SChris Mason * used to flag a delayed ref so the accounting can be updated 12156bec294SChris Mason * when a full insert is done. 12256bec294SChris Mason * 12356bec294SChris Mason * It is possible the extent will be freed before it is ever 12456bec294SChris Mason * inserted into the extent allocation tree. In this case 12556bec294SChris Mason * we need to update the in ram accounting to properly reflect 12656bec294SChris Mason * the free has happened. 12756bec294SChris Mason */ 12856bec294SChris Mason unsigned int must_insert_reserved:1; 1295d4f98a2SYan Zheng unsigned int is_data:1; 1305e388e95SNikolay Borisov unsigned int is_system:1; 131d7df2c79SJosef Bacik unsigned int processing:1; 13256bec294SChris Mason }; 13356bec294SChris Mason 1345d4f98a2SYan Zheng struct btrfs_delayed_tree_ref { 13556bec294SChris Mason struct btrfs_delayed_ref_node node; 13656bec294SChris Mason u64 root; 1375d4f98a2SYan Zheng u64 parent; 1385d4f98a2SYan Zheng int level; 1395d4f98a2SYan Zheng }; 14056bec294SChris Mason 1415d4f98a2SYan Zheng struct btrfs_delayed_data_ref { 1425d4f98a2SYan Zheng struct btrfs_delayed_ref_node node; 1435d4f98a2SYan Zheng u64 root; 1445d4f98a2SYan Zheng u64 parent; 1455d4f98a2SYan Zheng u64 objectid; 1465d4f98a2SYan Zheng u64 offset; 14756bec294SChris Mason }; 14856bec294SChris Mason 14956bec294SChris Mason struct btrfs_delayed_ref_root { 150c46effa6SLiu Bo /* head ref rbtree */ 1515c9d028bSLiu Bo struct rb_root_cached href_root; 152c46effa6SLiu Bo 1533368d001SQu Wenruo /* dirty extent records */ 1543368d001SQu Wenruo struct rb_root dirty_extent_root; 1553368d001SQu Wenruo 15656bec294SChris Mason /* this spin lock protects the rbtree and the entries inside */ 15756bec294SChris Mason spinlock_t lock; 15856bec294SChris Mason 15956bec294SChris Mason /* how many delayed ref updates we've queued, used by the 16056bec294SChris Mason * throttling code 16156bec294SChris Mason */ 162d7df2c79SJosef Bacik atomic_t num_entries; 16356bec294SChris Mason 164c3e69d58SChris Mason /* total number of head nodes in tree */ 165c3e69d58SChris Mason unsigned long num_heads; 166c3e69d58SChris Mason 167c3e69d58SChris Mason /* total number of head nodes ready for processing */ 168c3e69d58SChris Mason unsigned long num_heads_ready; 169c3e69d58SChris Mason 1701262133bSJosef Bacik u64 pending_csums; 1711262133bSJosef Bacik 17256bec294SChris Mason /* 17356bec294SChris Mason * set when the tree is flushing before a transaction commit, 17456bec294SChris Mason * used by the throttling code to decide if new updates need 17556bec294SChris Mason * to be run right away 17656bec294SChris Mason */ 17756bec294SChris Mason int flushing; 178c3e69d58SChris Mason 179c3e69d58SChris Mason u64 run_delayed_start; 1809086db86SQu Wenruo 1819086db86SQu Wenruo /* 1829086db86SQu Wenruo * To make qgroup to skip given root. 18301327610SNicholas D Steeves * This is for snapshot, as btrfs_qgroup_inherit() will manually 1849086db86SQu Wenruo * modify counters for snapshot and its source, so we should skip 1859086db86SQu Wenruo * the snapshot in new_root/old_roots or it will get calculated twice 1869086db86SQu Wenruo */ 1879086db86SQu Wenruo u64 qgroup_to_skip; 18856bec294SChris Mason }; 18956bec294SChris Mason 19078a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_ref_head_cachep; 19178a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_tree_ref_cachep; 19278a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_data_ref_cachep; 19378a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_extent_op_cachep; 19478a6184aSMiao Xie 195f5c29bd9SLiu Bo int __init btrfs_delayed_ref_init(void); 196e67c718bSDavid Sterba void __cold btrfs_delayed_ref_exit(void); 19778a6184aSMiao Xie 19878a6184aSMiao Xie static inline struct btrfs_delayed_extent_op * 19978a6184aSMiao Xie btrfs_alloc_delayed_extent_op(void) 20078a6184aSMiao Xie { 20178a6184aSMiao Xie return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS); 20278a6184aSMiao Xie } 20378a6184aSMiao Xie 20478a6184aSMiao Xie static inline void 20578a6184aSMiao Xie btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op) 20678a6184aSMiao Xie { 20778a6184aSMiao Xie if (op) 20878a6184aSMiao Xie kmem_cache_free(btrfs_delayed_extent_op_cachep, op); 20978a6184aSMiao Xie } 21078a6184aSMiao Xie 21156bec294SChris Mason static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref) 21256bec294SChris Mason { 2136df8cdf5SElena Reshetova WARN_ON(refcount_read(&ref->refs) == 0); 2146df8cdf5SElena Reshetova if (refcount_dec_and_test(&ref->refs)) { 21556bec294SChris Mason WARN_ON(ref->in_tree); 21678a6184aSMiao Xie switch (ref->type) { 21778a6184aSMiao Xie case BTRFS_TREE_BLOCK_REF_KEY: 21878a6184aSMiao Xie case BTRFS_SHARED_BLOCK_REF_KEY: 21978a6184aSMiao Xie kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref); 22078a6184aSMiao Xie break; 22178a6184aSMiao Xie case BTRFS_EXTENT_DATA_REF_KEY: 22278a6184aSMiao Xie case BTRFS_SHARED_DATA_REF_KEY: 22378a6184aSMiao Xie kmem_cache_free(btrfs_delayed_data_ref_cachep, ref); 22478a6184aSMiao Xie break; 22578a6184aSMiao Xie default: 22678a6184aSMiao Xie BUG(); 22778a6184aSMiao Xie } 22856bec294SChris Mason } 22956bec294SChris Mason } 23056bec294SChris Mason 231d278850eSJosef Bacik static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head) 232d278850eSJosef Bacik { 233d278850eSJosef Bacik if (refcount_dec_and_test(&head->refs)) 234d278850eSJosef Bacik kmem_cache_free(btrfs_delayed_ref_head_cachep, head); 235d278850eSJosef Bacik } 236d278850eSJosef Bacik 23744e1c47dSNikolay Borisov int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans, 2385d4f98a2SYan Zheng u64 bytenr, u64 num_bytes, u64 parent, 2395d4f98a2SYan Zheng u64 ref_root, int level, int action, 2407be07912SOmar Sandoval struct btrfs_delayed_extent_op *extent_op, 2417be07912SOmar Sandoval int *old_ref_mod, int *new_ref_mod); 24288a979c6SNikolay Borisov int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans, 2435d4f98a2SYan Zheng u64 bytenr, u64 num_bytes, 2445d4f98a2SYan Zheng u64 parent, u64 ref_root, 2457be07912SOmar Sandoval u64 owner, u64 offset, u64 reserved, int action, 2467be07912SOmar Sandoval int *old_ref_mod, int *new_ref_mod); 24766d7e7f0SArne Jansen int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info, 24866d7e7f0SArne Jansen struct btrfs_trans_handle *trans, 2495d4f98a2SYan Zheng u64 bytenr, u64 num_bytes, 2505d4f98a2SYan Zheng struct btrfs_delayed_extent_op *extent_op); 251ae1e206bSJosef Bacik void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans, 252ae1e206bSJosef Bacik struct btrfs_delayed_ref_root *delayed_refs, 253ae1e206bSJosef Bacik struct btrfs_delayed_ref_head *head); 25456bec294SChris Mason 2551887be66SChris Mason struct btrfs_delayed_ref_head * 256f72ad18eSLiu Bo btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, 257f72ad18eSLiu Bo u64 bytenr); 258c3e69d58SChris Mason int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans, 259c3e69d58SChris Mason struct btrfs_delayed_ref_head *head); 260093486c4SMiao Xie static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head) 261093486c4SMiao Xie { 262093486c4SMiao Xie mutex_unlock(&head->mutex); 263093486c4SMiao Xie } 264093486c4SMiao Xie 265d7df2c79SJosef Bacik 266d7df2c79SJosef Bacik struct btrfs_delayed_ref_head * 267d7df2c79SJosef Bacik btrfs_select_ref_head(struct btrfs_trans_handle *trans); 26800f04b88SArne Jansen 26941d0bd3bSNikolay Borisov int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq); 27000f04b88SArne Jansen 27100f04b88SArne Jansen /* 27256bec294SChris Mason * helper functions to cast a node into its container 27356bec294SChris Mason */ 2745d4f98a2SYan Zheng static inline struct btrfs_delayed_tree_ref * 2755d4f98a2SYan Zheng btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node) 27656bec294SChris Mason { 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 return container_of(node, struct btrfs_delayed_data_ref, node); 28456bec294SChris Mason } 2859888c340SDavid Sterba 28656bec294SChris Mason #endif 287