Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (C) 2008 Oracle.  All rights reserved.
0004  */
0005 
0006 #ifndef BTRFS_DELAYED_REF_H
0007 #define BTRFS_DELAYED_REF_H
0008 
0009 #include <linux/refcount.h>
0010 
0011 /* these are the possible values of struct btrfs_delayed_ref_node->action */
0012 #define BTRFS_ADD_DELAYED_REF    1 /* add one backref to the tree */
0013 #define BTRFS_DROP_DELAYED_REF   2 /* delete one backref from the tree */
0014 #define BTRFS_ADD_DELAYED_EXTENT 3 /* record a full extent allocation */
0015 #define BTRFS_UPDATE_DELAYED_HEAD 4 /* not changing ref count on head ref */
0016 
0017 struct btrfs_delayed_ref_node {
0018     struct rb_node ref_node;
0019     /*
0020      * If action is BTRFS_ADD_DELAYED_REF, also link this node to
0021      * ref_head->ref_add_list, then we do not need to iterate the
0022      * whole ref_head->ref_list to find BTRFS_ADD_DELAYED_REF nodes.
0023      */
0024     struct list_head add_list;
0025 
0026     /* the starting bytenr of the extent */
0027     u64 bytenr;
0028 
0029     /* the size of the extent */
0030     u64 num_bytes;
0031 
0032     /* seq number to keep track of insertion order */
0033     u64 seq;
0034 
0035     /* ref count on this data structure */
0036     refcount_t refs;
0037 
0038     /*
0039      * how many refs is this entry adding or deleting.  For
0040      * head refs, this may be a negative number because it is keeping
0041      * track of the total mods done to the reference count.
0042      * For individual refs, this will always be a positive number
0043      *
0044      * It may be more than one, since it is possible for a single
0045      * parent to have more than one ref on an extent
0046      */
0047     int ref_mod;
0048 
0049     unsigned int action:8;
0050     unsigned int type:8;
0051     /* is this node still in the rbtree? */
0052     unsigned int is_head:1;
0053     unsigned int in_tree:1;
0054 };
0055 
0056 struct btrfs_delayed_extent_op {
0057     struct btrfs_disk_key key;
0058     u8 level;
0059     bool update_key;
0060     bool update_flags;
0061     u64 flags_to_set;
0062 };
0063 
0064 /*
0065  * the head refs are used to hold a lock on a given extent, which allows us
0066  * to make sure that only one process is running the delayed refs
0067  * at a time for a single extent.  They also store the sum of all the
0068  * reference count modifications we've queued up.
0069  */
0070 struct btrfs_delayed_ref_head {
0071     u64 bytenr;
0072     u64 num_bytes;
0073     refcount_t refs;
0074     /*
0075      * the mutex is held while running the refs, and it is also
0076      * held when checking the sum of reference modifications.
0077      */
0078     struct mutex mutex;
0079 
0080     spinlock_t lock;
0081     struct rb_root_cached ref_tree;
0082     /* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */
0083     struct list_head ref_add_list;
0084 
0085     struct rb_node href_node;
0086 
0087     struct btrfs_delayed_extent_op *extent_op;
0088 
0089     /*
0090      * This is used to track the final ref_mod from all the refs associated
0091      * with this head ref, this is not adjusted as delayed refs are run,
0092      * this is meant to track if we need to do the csum accounting or not.
0093      */
0094     int total_ref_mod;
0095 
0096     /*
0097      * This is the current outstanding mod references for this bytenr.  This
0098      * is used with lookup_extent_info to get an accurate reference count
0099      * for a bytenr, so it is adjusted as delayed refs are run so that any
0100      * on disk reference count + ref_mod is accurate.
0101      */
0102     int ref_mod;
0103 
0104     /*
0105      * when a new extent is allocated, it is just reserved in memory
0106      * The actual extent isn't inserted into the extent allocation tree
0107      * until the delayed ref is processed.  must_insert_reserved is
0108      * used to flag a delayed ref so the accounting can be updated
0109      * when a full insert is done.
0110      *
0111      * It is possible the extent will be freed before it is ever
0112      * inserted into the extent allocation tree.  In this case
0113      * we need to update the in ram accounting to properly reflect
0114      * the free has happened.
0115      */
0116     unsigned int must_insert_reserved:1;
0117     unsigned int is_data:1;
0118     unsigned int is_system:1;
0119     unsigned int processing:1;
0120 };
0121 
0122 struct btrfs_delayed_tree_ref {
0123     struct btrfs_delayed_ref_node node;
0124     u64 root;
0125     u64 parent;
0126     int level;
0127 };
0128 
0129 struct btrfs_delayed_data_ref {
0130     struct btrfs_delayed_ref_node node;
0131     u64 root;
0132     u64 parent;
0133     u64 objectid;
0134     u64 offset;
0135 };
0136 
0137 enum btrfs_delayed_ref_flags {
0138     /* Indicate that we are flushing delayed refs for the commit */
0139     BTRFS_DELAYED_REFS_FLUSHING,
0140 };
0141 
0142 struct btrfs_delayed_ref_root {
0143     /* head ref rbtree */
0144     struct rb_root_cached href_root;
0145 
0146     /* dirty extent records */
0147     struct rb_root dirty_extent_root;
0148 
0149     /* this spin lock protects the rbtree and the entries inside */
0150     spinlock_t lock;
0151 
0152     /* how many delayed ref updates we've queued, used by the
0153      * throttling code
0154      */
0155     atomic_t num_entries;
0156 
0157     /* total number of head nodes in tree */
0158     unsigned long num_heads;
0159 
0160     /* total number of head nodes ready for processing */
0161     unsigned long num_heads_ready;
0162 
0163     u64 pending_csums;
0164 
0165     unsigned long flags;
0166 
0167     u64 run_delayed_start;
0168 
0169     /*
0170      * To make qgroup to skip given root.
0171      * This is for snapshot, as btrfs_qgroup_inherit() will manually
0172      * modify counters for snapshot and its source, so we should skip
0173      * the snapshot in new_root/old_roots or it will get calculated twice
0174      */
0175     u64 qgroup_to_skip;
0176 };
0177 
0178 enum btrfs_ref_type {
0179     BTRFS_REF_NOT_SET,
0180     BTRFS_REF_DATA,
0181     BTRFS_REF_METADATA,
0182     BTRFS_REF_LAST,
0183 };
0184 
0185 struct btrfs_data_ref {
0186     /* For EXTENT_DATA_REF */
0187 
0188     /* Original root this data extent belongs to */
0189     u64 owning_root;
0190 
0191     /* Inode which refers to this data extent */
0192     u64 ino;
0193 
0194     /*
0195      * file_offset - extent_offset
0196      *
0197      * file_offset is the key.offset of the EXTENT_DATA key.
0198      * extent_offset is btrfs_file_extent_offset() of the EXTENT_DATA data.
0199      */
0200     u64 offset;
0201 };
0202 
0203 struct btrfs_tree_ref {
0204     /*
0205      * Level of this tree block
0206      *
0207      * Shared for skinny (TREE_BLOCK_REF) and normal tree ref.
0208      */
0209     int level;
0210 
0211     /*
0212      * Root which owns this tree block.
0213      *
0214      * For TREE_BLOCK_REF (skinny metadata, either inline or keyed)
0215      */
0216     u64 owning_root;
0217 
0218     /* For non-skinny metadata, no special member needed */
0219 };
0220 
0221 struct btrfs_ref {
0222     enum btrfs_ref_type type;
0223     int action;
0224 
0225     /*
0226      * Whether this extent should go through qgroup record.
0227      *
0228      * Normally false, but for certain cases like delayed subtree scan,
0229      * setting this flag can hugely reduce qgroup overhead.
0230      */
0231     bool skip_qgroup;
0232 
0233 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
0234     /* Through which root is this modification. */
0235     u64 real_root;
0236 #endif
0237     u64 bytenr;
0238     u64 len;
0239 
0240     /* Bytenr of the parent tree block */
0241     u64 parent;
0242     union {
0243         struct btrfs_data_ref data_ref;
0244         struct btrfs_tree_ref tree_ref;
0245     };
0246 };
0247 
0248 extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
0249 extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
0250 extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
0251 extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
0252 
0253 int __init btrfs_delayed_ref_init(void);
0254 void __cold btrfs_delayed_ref_exit(void);
0255 
0256 static inline void btrfs_init_generic_ref(struct btrfs_ref *generic_ref,
0257                 int action, u64 bytenr, u64 len, u64 parent)
0258 {
0259     generic_ref->action = action;
0260     generic_ref->bytenr = bytenr;
0261     generic_ref->len = len;
0262     generic_ref->parent = parent;
0263 }
0264 
0265 static inline void btrfs_init_tree_ref(struct btrfs_ref *generic_ref,
0266                 int level, u64 root, u64 mod_root, bool skip_qgroup)
0267 {
0268 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
0269     /* If @real_root not set, use @root as fallback */
0270     generic_ref->real_root = mod_root ?: root;
0271 #endif
0272     generic_ref->tree_ref.level = level;
0273     generic_ref->tree_ref.owning_root = root;
0274     generic_ref->type = BTRFS_REF_METADATA;
0275     if (skip_qgroup || !(is_fstree(root) &&
0276                  (!mod_root || is_fstree(mod_root))))
0277         generic_ref->skip_qgroup = true;
0278     else
0279         generic_ref->skip_qgroup = false;
0280 
0281 }
0282 
0283 static inline void btrfs_init_data_ref(struct btrfs_ref *generic_ref,
0284                 u64 ref_root, u64 ino, u64 offset, u64 mod_root,
0285                 bool skip_qgroup)
0286 {
0287 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
0288     /* If @real_root not set, use @root as fallback */
0289     generic_ref->real_root = mod_root ?: ref_root;
0290 #endif
0291     generic_ref->data_ref.owning_root = ref_root;
0292     generic_ref->data_ref.ino = ino;
0293     generic_ref->data_ref.offset = offset;
0294     generic_ref->type = BTRFS_REF_DATA;
0295     if (skip_qgroup || !(is_fstree(ref_root) &&
0296                  (!mod_root || is_fstree(mod_root))))
0297         generic_ref->skip_qgroup = true;
0298     else
0299         generic_ref->skip_qgroup = false;
0300 }
0301 
0302 static inline struct btrfs_delayed_extent_op *
0303 btrfs_alloc_delayed_extent_op(void)
0304 {
0305     return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);
0306 }
0307 
0308 static inline void
0309 btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)
0310 {
0311     if (op)
0312         kmem_cache_free(btrfs_delayed_extent_op_cachep, op);
0313 }
0314 
0315 static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
0316 {
0317     WARN_ON(refcount_read(&ref->refs) == 0);
0318     if (refcount_dec_and_test(&ref->refs)) {
0319         WARN_ON(ref->in_tree);
0320         switch (ref->type) {
0321         case BTRFS_TREE_BLOCK_REF_KEY:
0322         case BTRFS_SHARED_BLOCK_REF_KEY:
0323             kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
0324             break;
0325         case BTRFS_EXTENT_DATA_REF_KEY:
0326         case BTRFS_SHARED_DATA_REF_KEY:
0327             kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
0328             break;
0329         default:
0330             BUG();
0331         }
0332     }
0333 }
0334 
0335 static inline u64 btrfs_ref_head_to_space_flags(
0336                 struct btrfs_delayed_ref_head *head_ref)
0337 {
0338     if (head_ref->is_data)
0339         return BTRFS_BLOCK_GROUP_DATA;
0340     else if (head_ref->is_system)
0341         return BTRFS_BLOCK_GROUP_SYSTEM;
0342     return BTRFS_BLOCK_GROUP_METADATA;
0343 }
0344 
0345 static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head)
0346 {
0347     if (refcount_dec_and_test(&head->refs))
0348         kmem_cache_free(btrfs_delayed_ref_head_cachep, head);
0349 }
0350 
0351 int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
0352                    struct btrfs_ref *generic_ref,
0353                    struct btrfs_delayed_extent_op *extent_op);
0354 int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
0355                    struct btrfs_ref *generic_ref,
0356                    u64 reserved);
0357 int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
0358                 u64 bytenr, u64 num_bytes,
0359                 struct btrfs_delayed_extent_op *extent_op);
0360 void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
0361                   struct btrfs_delayed_ref_root *delayed_refs,
0362                   struct btrfs_delayed_ref_head *head);
0363 
0364 struct btrfs_delayed_ref_head *
0365 btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
0366                 u64 bytenr);
0367 int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
0368                struct btrfs_delayed_ref_head *head);
0369 static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
0370 {
0371     mutex_unlock(&head->mutex);
0372 }
0373 void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
0374                struct btrfs_delayed_ref_head *head);
0375 
0376 struct btrfs_delayed_ref_head *btrfs_select_ref_head(
0377         struct btrfs_delayed_ref_root *delayed_refs);
0378 
0379 int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);
0380 
0381 void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr);
0382 void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);
0383 int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
0384                   enum btrfs_reserve_flush_enum flush);
0385 void btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info,
0386                        struct btrfs_block_rsv *src,
0387                        u64 num_bytes);
0388 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans);
0389 bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);
0390 
0391 /*
0392  * helper functions to cast a node into its container
0393  */
0394 static inline struct btrfs_delayed_tree_ref *
0395 btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
0396 {
0397     return container_of(node, struct btrfs_delayed_tree_ref, node);
0398 }
0399 
0400 static inline struct btrfs_delayed_data_ref *
0401 btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
0402 {
0403     return container_of(node, struct btrfs_delayed_data_ref, node);
0404 }
0405 
0406 #endif