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 216df8cdf5SElena Reshetova #include <linux/refcount.h> 226df8cdf5SElena Reshetova 2344a075bdSWang Sheng-Hui /* these are the possible values of struct btrfs_delayed_ref_node->action */ 2456bec294SChris Mason #define BTRFS_ADD_DELAYED_REF 1 /* add one backref to the tree */ 2556bec294SChris Mason #define BTRFS_DROP_DELAYED_REF 2 /* delete one backref from the tree */ 2656bec294SChris Mason #define BTRFS_ADD_DELAYED_EXTENT 3 /* record a full extent allocation */ 271a81af4dSChris Mason #define BTRFS_UPDATE_DELAYED_HEAD 4 /* not changing ref count on head ref */ 2856bec294SChris Mason 2956bec294SChris Mason struct btrfs_delayed_ref_node { 300e0adbcfSJosef Bacik struct rb_node ref_node; 311d57ee94SWang Xiaoguang /* 321d57ee94SWang Xiaoguang * If action is BTRFS_ADD_DELAYED_REF, also link this node to 331d57ee94SWang Xiaoguang * ref_head->ref_add_list, then we do not need to iterate the 341d57ee94SWang Xiaoguang * whole ref_head->ref_list to find BTRFS_ADD_DELAYED_REF nodes. 351d57ee94SWang Xiaoguang */ 361d57ee94SWang Xiaoguang struct list_head add_list; 37c6fc2454SQu Wenruo 3856bec294SChris Mason /* the starting bytenr of the extent */ 3956bec294SChris Mason u64 bytenr; 4056bec294SChris Mason 4156bec294SChris Mason /* the size of the extent */ 4256bec294SChris Mason u64 num_bytes; 4356bec294SChris Mason 4400f04b88SArne Jansen /* seq number to keep track of insertion order */ 4500f04b88SArne Jansen u64 seq; 4600f04b88SArne Jansen 4756bec294SChris Mason /* ref count on this data structure */ 486df8cdf5SElena Reshetova refcount_t refs; 4956bec294SChris Mason 5056bec294SChris Mason /* 5156bec294SChris Mason * how many refs is this entry adding or deleting. For 5256bec294SChris Mason * head refs, this may be a negative number because it is keeping 5356bec294SChris Mason * track of the total mods done to the reference count. 5456bec294SChris Mason * For individual refs, this will always be a positive number 5556bec294SChris Mason * 5656bec294SChris Mason * It may be more than one, since it is possible for a single 5756bec294SChris Mason * parent to have more than one ref on an extent 5856bec294SChris Mason */ 5956bec294SChris Mason int ref_mod; 6056bec294SChris Mason 615d4f98a2SYan Zheng unsigned int action:8; 625d4f98a2SYan Zheng unsigned int type:8; 6356bec294SChris Mason /* is this node still in the rbtree? */ 645d4f98a2SYan Zheng unsigned int is_head:1; 6556bec294SChris Mason unsigned int in_tree:1; 6656bec294SChris Mason }; 6756bec294SChris Mason 685d4f98a2SYan Zheng struct btrfs_delayed_extent_op { 695d4f98a2SYan Zheng struct btrfs_disk_key key; 7035b3ad50SDavid Sterba u8 level; 7135b3ad50SDavid Sterba bool update_key; 7235b3ad50SDavid Sterba bool update_flags; 7335b3ad50SDavid Sterba bool is_data; 745d4f98a2SYan Zheng u64 flags_to_set; 755d4f98a2SYan Zheng }; 765d4f98a2SYan Zheng 7756bec294SChris Mason /* 7856bec294SChris Mason * the head refs are used to hold a lock on a given extent, which allows us 7956bec294SChris Mason * to make sure that only one process is running the delayed refs 8056bec294SChris Mason * at a time for a single extent. They also store the sum of all the 8156bec294SChris Mason * reference count modifications we've queued up. 8256bec294SChris Mason */ 8356bec294SChris Mason struct btrfs_delayed_ref_head { 84d278850eSJosef Bacik u64 bytenr; 85d278850eSJosef Bacik u64 num_bytes; 86d278850eSJosef Bacik refcount_t refs; 8756bec294SChris Mason /* 8856bec294SChris Mason * the mutex is held while running the refs, and it is also 8956bec294SChris Mason * held when checking the sum of reference modifications. 9056bec294SChris Mason */ 9156bec294SChris Mason struct mutex mutex; 9256bec294SChris Mason 93d7df2c79SJosef Bacik spinlock_t lock; 940e0adbcfSJosef Bacik struct rb_root ref_tree; 951d57ee94SWang Xiaoguang /* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */ 961d57ee94SWang Xiaoguang struct list_head ref_add_list; 97c3e69d58SChris Mason 98c46effa6SLiu Bo struct rb_node href_node; 99c46effa6SLiu Bo 1005d4f98a2SYan Zheng struct btrfs_delayed_extent_op *extent_op; 1011262133bSJosef Bacik 1021262133bSJosef Bacik /* 1031262133bSJosef Bacik * This is used to track the final ref_mod from all the refs associated 1041262133bSJosef Bacik * with this head ref, this is not adjusted as delayed refs are run, 1051262133bSJosef Bacik * this is meant to track if we need to do the csum accounting or not. 1061262133bSJosef Bacik */ 1071262133bSJosef Bacik int total_ref_mod; 1081262133bSJosef Bacik 10956bec294SChris Mason /* 110d278850eSJosef Bacik * This is the current outstanding mod references for this bytenr. This 111d278850eSJosef Bacik * is used with lookup_extent_info to get an accurate reference count 112d278850eSJosef Bacik * for a bytenr, so it is adjusted as delayed refs are run so that any 113d278850eSJosef Bacik * on disk reference count + ref_mod is accurate. 114d278850eSJosef Bacik */ 115d278850eSJosef Bacik int ref_mod; 116d278850eSJosef Bacik 117d278850eSJosef Bacik /* 118f64d5ca8SQu Wenruo * For qgroup reserved space freeing. 119f64d5ca8SQu Wenruo * 120f64d5ca8SQu Wenruo * ref_root and reserved will be recorded after 121f64d5ca8SQu Wenruo * BTRFS_ADD_DELAYED_EXTENT is called. 122f64d5ca8SQu Wenruo * And will be used to free reserved qgroup space at 123f64d5ca8SQu Wenruo * run_delayed_refs() time. 124f64d5ca8SQu Wenruo */ 125f64d5ca8SQu Wenruo u64 qgroup_ref_root; 126f64d5ca8SQu Wenruo u64 qgroup_reserved; 127f64d5ca8SQu Wenruo 128f64d5ca8SQu Wenruo /* 12956bec294SChris Mason * when a new extent is allocated, it is just reserved in memory 13056bec294SChris Mason * The actual extent isn't inserted into the extent allocation tree 13156bec294SChris Mason * until the delayed ref is processed. must_insert_reserved is 13256bec294SChris Mason * used to flag a delayed ref so the accounting can be updated 13356bec294SChris Mason * when a full insert is done. 13456bec294SChris Mason * 13556bec294SChris Mason * It is possible the extent will be freed before it is ever 13656bec294SChris Mason * inserted into the extent allocation tree. In this case 13756bec294SChris Mason * we need to update the in ram accounting to properly reflect 13856bec294SChris Mason * the free has happened. 13956bec294SChris Mason */ 14056bec294SChris Mason unsigned int must_insert_reserved:1; 1415d4f98a2SYan Zheng unsigned int is_data:1; 142d7df2c79SJosef Bacik unsigned int processing:1; 14356bec294SChris Mason }; 14456bec294SChris Mason 1455d4f98a2SYan Zheng struct btrfs_delayed_tree_ref { 14656bec294SChris Mason struct btrfs_delayed_ref_node node; 14756bec294SChris Mason u64 root; 1485d4f98a2SYan Zheng u64 parent; 1495d4f98a2SYan Zheng int level; 1505d4f98a2SYan Zheng }; 15156bec294SChris Mason 1525d4f98a2SYan Zheng struct btrfs_delayed_data_ref { 1535d4f98a2SYan Zheng struct btrfs_delayed_ref_node node; 1545d4f98a2SYan Zheng u64 root; 1555d4f98a2SYan Zheng u64 parent; 1565d4f98a2SYan Zheng u64 objectid; 1575d4f98a2SYan Zheng u64 offset; 15856bec294SChris Mason }; 15956bec294SChris Mason 16056bec294SChris Mason struct btrfs_delayed_ref_root { 161c46effa6SLiu Bo /* head ref rbtree */ 162c46effa6SLiu Bo struct rb_root href_root; 163c46effa6SLiu Bo 1643368d001SQu Wenruo /* dirty extent records */ 1653368d001SQu Wenruo struct rb_root dirty_extent_root; 1663368d001SQu Wenruo 16756bec294SChris Mason /* this spin lock protects the rbtree and the entries inside */ 16856bec294SChris Mason spinlock_t lock; 16956bec294SChris Mason 17056bec294SChris Mason /* how many delayed ref updates we've queued, used by the 17156bec294SChris Mason * throttling code 17256bec294SChris Mason */ 173d7df2c79SJosef Bacik atomic_t num_entries; 17456bec294SChris Mason 175c3e69d58SChris Mason /* total number of head nodes in tree */ 176c3e69d58SChris Mason unsigned long num_heads; 177c3e69d58SChris Mason 178c3e69d58SChris Mason /* total number of head nodes ready for processing */ 179c3e69d58SChris Mason unsigned long num_heads_ready; 180c3e69d58SChris Mason 1811262133bSJosef Bacik u64 pending_csums; 1821262133bSJosef Bacik 18356bec294SChris Mason /* 18456bec294SChris Mason * set when the tree is flushing before a transaction commit, 18556bec294SChris Mason * used by the throttling code to decide if new updates need 18656bec294SChris Mason * to be run right away 18756bec294SChris Mason */ 18856bec294SChris Mason int flushing; 189c3e69d58SChris Mason 190c3e69d58SChris Mason u64 run_delayed_start; 1919086db86SQu Wenruo 1929086db86SQu Wenruo /* 1939086db86SQu Wenruo * To make qgroup to skip given root. 19401327610SNicholas D Steeves * This is for snapshot, as btrfs_qgroup_inherit() will manually 1959086db86SQu Wenruo * modify counters for snapshot and its source, so we should skip 1969086db86SQu Wenruo * the snapshot in new_root/old_roots or it will get calculated twice 1979086db86SQu Wenruo */ 1989086db86SQu Wenruo u64 qgroup_to_skip; 19956bec294SChris Mason }; 20056bec294SChris Mason 20178a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_ref_head_cachep; 20278a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_tree_ref_cachep; 20378a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_data_ref_cachep; 20478a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_extent_op_cachep; 20578a6184aSMiao Xie 20678a6184aSMiao Xie int btrfs_delayed_ref_init(void); 20778a6184aSMiao Xie void btrfs_delayed_ref_exit(void); 20878a6184aSMiao Xie 20978a6184aSMiao Xie static inline struct btrfs_delayed_extent_op * 21078a6184aSMiao Xie btrfs_alloc_delayed_extent_op(void) 21178a6184aSMiao Xie { 21278a6184aSMiao Xie return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS); 21378a6184aSMiao Xie } 21478a6184aSMiao Xie 21578a6184aSMiao Xie static inline void 21678a6184aSMiao Xie btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op) 21778a6184aSMiao Xie { 21878a6184aSMiao Xie if (op) 21978a6184aSMiao Xie kmem_cache_free(btrfs_delayed_extent_op_cachep, op); 22078a6184aSMiao Xie } 22178a6184aSMiao Xie 22256bec294SChris Mason static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref) 22356bec294SChris Mason { 2246df8cdf5SElena Reshetova WARN_ON(refcount_read(&ref->refs) == 0); 2256df8cdf5SElena Reshetova if (refcount_dec_and_test(&ref->refs)) { 22656bec294SChris Mason WARN_ON(ref->in_tree); 22778a6184aSMiao Xie switch (ref->type) { 22878a6184aSMiao Xie case BTRFS_TREE_BLOCK_REF_KEY: 22978a6184aSMiao Xie case BTRFS_SHARED_BLOCK_REF_KEY: 23078a6184aSMiao Xie kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref); 23178a6184aSMiao Xie break; 23278a6184aSMiao Xie case BTRFS_EXTENT_DATA_REF_KEY: 23378a6184aSMiao Xie case BTRFS_SHARED_DATA_REF_KEY: 23478a6184aSMiao Xie kmem_cache_free(btrfs_delayed_data_ref_cachep, ref); 23578a6184aSMiao Xie break; 23678a6184aSMiao Xie default: 23778a6184aSMiao Xie BUG(); 23878a6184aSMiao Xie } 23956bec294SChris Mason } 24056bec294SChris Mason } 24156bec294SChris Mason 242d278850eSJosef Bacik static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head) 243d278850eSJosef Bacik { 244d278850eSJosef Bacik if (refcount_dec_and_test(&head->refs)) 245d278850eSJosef Bacik kmem_cache_free(btrfs_delayed_ref_head_cachep, head); 246d278850eSJosef Bacik } 247d278850eSJosef Bacik 24866d7e7f0SArne Jansen int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info, 24966d7e7f0SArne Jansen struct btrfs_trans_handle *trans, 2505d4f98a2SYan Zheng u64 bytenr, u64 num_bytes, u64 parent, 2515d4f98a2SYan Zheng u64 ref_root, int level, int action, 2527be07912SOmar Sandoval struct btrfs_delayed_extent_op *extent_op, 2537be07912SOmar Sandoval int *old_ref_mod, int *new_ref_mod); 25466d7e7f0SArne Jansen int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info, 25566d7e7f0SArne Jansen struct btrfs_trans_handle *trans, 2565d4f98a2SYan Zheng u64 bytenr, u64 num_bytes, 2575d4f98a2SYan Zheng u64 parent, u64 ref_root, 2587be07912SOmar Sandoval u64 owner, u64 offset, u64 reserved, int action, 2597be07912SOmar Sandoval int *old_ref_mod, int *new_ref_mod); 26066d7e7f0SArne Jansen int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info, 26166d7e7f0SArne Jansen struct btrfs_trans_handle *trans, 2625d4f98a2SYan Zheng u64 bytenr, u64 num_bytes, 2635d4f98a2SYan Zheng struct btrfs_delayed_extent_op *extent_op); 264ae1e206bSJosef Bacik void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans, 265ae1e206bSJosef Bacik struct btrfs_fs_info *fs_info, 266ae1e206bSJosef Bacik struct btrfs_delayed_ref_root *delayed_refs, 267ae1e206bSJosef Bacik struct btrfs_delayed_ref_head *head); 26856bec294SChris Mason 2691887be66SChris Mason struct btrfs_delayed_ref_head * 270f72ad18eSLiu Bo btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, 271f72ad18eSLiu Bo 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 * helper functions to cast a node into its container 28956bec294SChris Mason */ 2905d4f98a2SYan Zheng static inline struct btrfs_delayed_tree_ref * 2915d4f98a2SYan Zheng btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node) 29256bec294SChris Mason { 2935d4f98a2SYan Zheng return container_of(node, struct btrfs_delayed_tree_ref, node); 2945d4f98a2SYan Zheng } 29556bec294SChris Mason 2965d4f98a2SYan Zheng static inline struct btrfs_delayed_data_ref * 2975d4f98a2SYan Zheng btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node) 2985d4f98a2SYan Zheng { 2995d4f98a2SYan Zheng return container_of(node, struct btrfs_delayed_data_ref, node); 30056bec294SChris Mason } 30156bec294SChris Mason #endif 302