/* SPDX-License-Identifier: GPL-2.0 */1/*2* Copyright (C) 2008 Oracle. All rights reserved.3*/45#ifndef BTRFS_DELAYED_REF_H6#define BTRFS_DELAYED_REF_H78#include <linux/types.h>9#include <linux/refcount.h>10#include <linux/list.h>11#include <linux/rbtree.h>12#include <linux/mutex.h>13#include <linux/spinlock.h>14#include <linux/slab.h>15#include <uapi/linux/btrfs_tree.h>16#include "fs.h"17#include "messages.h"1819struct btrfs_trans_handle;20struct btrfs_fs_info;2122/* these are the possible values of struct btrfs_delayed_ref_node->action */23enum btrfs_delayed_ref_action {24/* Add one backref to the tree */25BTRFS_ADD_DELAYED_REF = 1,26/* Delete one backref from the tree */27BTRFS_DROP_DELAYED_REF,28/* Record a full extent allocation */29BTRFS_ADD_DELAYED_EXTENT,30/* Not changing ref count on head ref */31BTRFS_UPDATE_DELAYED_HEAD,32} __packed;3334struct btrfs_data_ref {35/* For EXTENT_DATA_REF */3637/* Inode which refers to this data extent */38u64 objectid;3940/*41* file_offset - extent_offset42*43* file_offset is the key.offset of the EXTENT_DATA key.44* extent_offset is btrfs_file_extent_offset() of the EXTENT_DATA data.45*/46u64 offset;47};4849struct btrfs_tree_ref {50/*51* Level of this tree block.52*53* Shared for skinny (TREE_BLOCK_REF) and normal tree ref.54*/55int level;5657/* For non-skinny metadata, no special member needed */58};5960struct btrfs_delayed_ref_node {61struct rb_node ref_node;62/*63* If action is BTRFS_ADD_DELAYED_REF, also link this node to64* ref_head->ref_add_list, then we do not need to iterate the65* refs rbtree in the corresponding delayed ref head66* (struct btrfs_delayed_ref_head::ref_tree).67*/68struct list_head add_list;6970/* the starting bytenr of the extent */71u64 bytenr;7273/* the size of the extent */74u64 num_bytes;7576/* seq number to keep track of insertion order */77u64 seq;7879/* The ref_root for this ref */80u64 ref_root;8182/*83* The parent for this ref, if this isn't set the ref_root is the84* reference owner.85*/86u64 parent;8788/* ref count on this data structure */89refcount_t refs;9091/*92* how many refs is this entry adding or deleting. For93* head refs, this may be a negative number because it is keeping94* track of the total mods done to the reference count.95* For individual refs, this will always be a positive number96*97* It may be more than one, since it is possible for a single98* parent to have more than one ref on an extent99*/100int ref_mod;101102unsigned int action:8;103unsigned int type:8;104105union {106struct btrfs_tree_ref tree_ref;107struct btrfs_data_ref data_ref;108};109};110111struct btrfs_delayed_extent_op {112struct btrfs_disk_key key;113bool update_key;114bool update_flags;115u64 flags_to_set;116};117118/*119* the head refs are used to hold a lock on a given extent, which allows us120* to make sure that only one process is running the delayed refs121* at a time for a single extent. They also store the sum of all the122* reference count modifications we've queued up.123*/124struct btrfs_delayed_ref_head {125u64 bytenr;126u64 num_bytes;127/*128* the mutex is held while running the refs, and it is also129* held when checking the sum of reference modifications.130*/131struct mutex mutex;132133refcount_t refs;134135/* Protects 'ref_tree' and 'ref_add_list'. */136spinlock_t lock;137struct rb_root_cached ref_tree;138/* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */139struct list_head ref_add_list;140141struct btrfs_delayed_extent_op *extent_op;142143/*144* This is used to track the final ref_mod from all the refs associated145* with this head ref, this is not adjusted as delayed refs are run,146* this is meant to track if we need to do the csum accounting or not.147*/148int total_ref_mod;149150/*151* This is the current outstanding mod references for this bytenr. This152* is used with lookup_extent_info to get an accurate reference count153* for a bytenr, so it is adjusted as delayed refs are run so that any154* on disk reference count + ref_mod is accurate.155*/156int ref_mod;157158/*159* The root that triggered the allocation when must_insert_reserved is160* set to true.161*/162u64 owning_root;163164/*165* Track reserved bytes when setting must_insert_reserved. On success166* or cleanup, we will need to free the reservation.167*/168u64 reserved_bytes;169170/* Tree block level, for metadata only. */171u8 level;172173/*174* when a new extent is allocated, it is just reserved in memory175* The actual extent isn't inserted into the extent allocation tree176* until the delayed ref is processed. must_insert_reserved is177* used to flag a delayed ref so the accounting can be updated178* when a full insert is done.179*180* It is possible the extent will be freed before it is ever181* inserted into the extent allocation tree. In this case182* we need to update the in ram accounting to properly reflect183* the free has happened.184*/185bool must_insert_reserved;186187bool is_data;188bool is_system;189bool processing;190/*191* Indicate if it's currently in the data structure that tracks head192* refs (struct btrfs_delayed_ref_root::head_refs).193*/194bool tracked;195};196197enum btrfs_delayed_ref_flags {198/* Indicate that we are flushing delayed refs for the commit */199BTRFS_DELAYED_REFS_FLUSHING,200};201202struct btrfs_delayed_ref_root {203/*204* Track head references.205* The keys correspond to the logical address of the extent ("bytenr")206* right shifted by fs_info->sectorsize_bits. This is both to get a more207* dense index space (optimizes xarray structure) and because indexes in208* xarrays are of "unsigned long" type, meaning they are 32 bits wide on209* 32 bits platforms, limiting the extent range to 4G which is too low210* and makes it unusable (truncated index values) on 32 bits platforms.211* Protected by the spinlock 'lock' defined below.212*/213struct xarray head_refs;214215/*216* Track dirty extent records.217* The keys correspond to the logical address of the extent ("bytenr")218* right shifted by fs_info->sectorsize_bits, for same reasons as above.219*/220struct xarray dirty_extents;221222/*223* Protects the xarray head_refs, its entries and the following fields:224* num_heads, num_heads_ready, pending_csums and run_delayed_start.225*/226spinlock_t lock;227228/* Total number of head refs, protected by the spinlock 'lock'. */229unsigned long num_heads;230231/*232* Total number of head refs ready for processing, protected by the233* spinlock 'lock'.234*/235unsigned long num_heads_ready;236237/*238* Track space reserved for deleting csums of data extents.239* Protected by the spinlock 'lock'.240*/241u64 pending_csums;242243unsigned long flags;244245/*246* Track from which bytenr to start searching ref heads.247* Protected by the spinlock 'lock'.248*/249u64 run_delayed_start;250251/*252* To make qgroup to skip given root.253* This is for snapshot, as btrfs_qgroup_inherit() will manually254* modify counters for snapshot and its source, so we should skip255* the snapshot in new_root/old_roots or it will get calculated twice256*/257u64 qgroup_to_skip;258};259260enum btrfs_ref_type {261BTRFS_REF_NOT_SET,262BTRFS_REF_DATA,263BTRFS_REF_METADATA,264} __packed;265266struct btrfs_ref {267enum btrfs_ref_type type;268enum btrfs_delayed_ref_action action;269270/*271* Whether this extent should go through qgroup record.272*273* Normally false, but for certain cases like delayed subtree scan,274* setting this flag can hugely reduce qgroup overhead.275*/276bool skip_qgroup;277278u64 bytenr;279u64 num_bytes;280u64 owning_root;281282/*283* The root that owns the reference for this reference, this will be set284* or ->parent will be set, depending on what type of reference this is.285*/286u64 ref_root;287288/* Bytenr of the parent tree block */289u64 parent;290union {291struct btrfs_data_ref data_ref;292struct btrfs_tree_ref tree_ref;293};294295#ifdef CONFIG_BTRFS_DEBUG296/* Through which root is this modification. */297u64 real_root;298#endif299};300301extern struct kmem_cache *btrfs_delayed_ref_head_cachep;302extern struct kmem_cache *btrfs_delayed_ref_node_cachep;303extern struct kmem_cache *btrfs_delayed_extent_op_cachep;304305int __init btrfs_delayed_ref_init(void);306void __cold btrfs_delayed_ref_exit(void);307308static inline u64 btrfs_calc_delayed_ref_bytes(const struct btrfs_fs_info *fs_info,309int num_delayed_refs)310{311u64 num_bytes;312313num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_delayed_refs);314315/*316* We have to check the mount option here because we could be enabling317* the free space tree for the first time and don't have the compat_ro318* option set yet.319*320* We need extra reservations if we have the free space tree because321* we'll have to modify that tree as well.322*/323if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))324num_bytes *= 2;325326return num_bytes;327}328329static inline u64 btrfs_calc_delayed_ref_csum_bytes(const struct btrfs_fs_info *fs_info,330int num_csum_items)331{332/*333* Deleting csum items does not result in new nodes/leaves and does not334* require changing the free space tree, only the csum tree, so this is335* all we need.336*/337return btrfs_calc_metadata_size(fs_info, num_csum_items);338}339340void btrfs_init_tree_ref(struct btrfs_ref *generic_ref, int level, u64 mod_root,341bool skip_qgroup);342void btrfs_init_data_ref(struct btrfs_ref *generic_ref, u64 ino, u64 offset,343u64 mod_root, bool skip_qgroup);344345static inline struct btrfs_delayed_extent_op *346btrfs_alloc_delayed_extent_op(void)347{348return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);349}350351static inline void352btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)353{354if (op)355kmem_cache_free(btrfs_delayed_extent_op_cachep, op);356}357358void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref);359360static inline u64 btrfs_ref_head_to_space_flags(361struct btrfs_delayed_ref_head *head_ref)362{363if (head_ref->is_data)364return BTRFS_BLOCK_GROUP_DATA;365else if (head_ref->is_system)366return BTRFS_BLOCK_GROUP_SYSTEM;367return BTRFS_BLOCK_GROUP_METADATA;368}369370static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head)371{372if (refcount_dec_and_test(&head->refs))373kmem_cache_free(btrfs_delayed_ref_head_cachep, head);374}375376int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,377struct btrfs_ref *generic_ref,378struct btrfs_delayed_extent_op *extent_op);379int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,380struct btrfs_ref *generic_ref,381u64 reserved);382int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,383u64 bytenr, u64 num_bytes, u8 level,384struct btrfs_delayed_extent_op *extent_op);385void btrfs_merge_delayed_refs(struct btrfs_fs_info *fs_info,386struct btrfs_delayed_ref_root *delayed_refs,387struct btrfs_delayed_ref_head *head);388389struct btrfs_delayed_ref_head *390btrfs_find_delayed_ref_head(const struct btrfs_fs_info *fs_info,391struct btrfs_delayed_ref_root *delayed_refs,392u64 bytenr);393static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)394{395mutex_unlock(&head->mutex);396}397void btrfs_delete_ref_head(const struct btrfs_fs_info *fs_info,398struct btrfs_delayed_ref_root *delayed_refs,399struct btrfs_delayed_ref_head *head);400401struct btrfs_delayed_ref_head *btrfs_select_ref_head(402const struct btrfs_fs_info *fs_info,403struct btrfs_delayed_ref_root *delayed_refs);404void btrfs_unselect_ref_head(struct btrfs_delayed_ref_root *delayed_refs,405struct btrfs_delayed_ref_head *head);406struct btrfs_delayed_ref_node *btrfs_select_delayed_ref(struct btrfs_delayed_ref_head *head);407408int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);409410void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr_refs, int nr_csums);411void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);412void btrfs_inc_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info);413void btrfs_dec_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info);414void btrfs_inc_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info);415void btrfs_dec_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info);416int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,417enum btrfs_reserve_flush_enum flush);418bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);419bool btrfs_find_delayed_tree_ref(struct btrfs_delayed_ref_head *head,420u64 root, u64 parent);421void btrfs_destroy_delayed_refs(struct btrfs_transaction *trans);422423static inline u64 btrfs_delayed_ref_owner(const struct btrfs_delayed_ref_node *node)424{425if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||426node->type == BTRFS_SHARED_DATA_REF_KEY)427return node->data_ref.objectid;428return node->tree_ref.level;429}430431static inline u64 btrfs_delayed_ref_offset(const struct btrfs_delayed_ref_node *node)432{433if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||434node->type == BTRFS_SHARED_DATA_REF_KEY)435return node->data_ref.offset;436return 0;437}438439static inline u8 btrfs_ref_type(const struct btrfs_ref *ref)440{441ASSERT(ref->type == BTRFS_REF_DATA || ref->type == BTRFS_REF_METADATA);442443if (ref->type == BTRFS_REF_DATA) {444if (ref->parent)445return BTRFS_SHARED_DATA_REF_KEY;446else447return BTRFS_EXTENT_DATA_REF_KEY;448} else {449if (ref->parent)450return BTRFS_SHARED_BLOCK_REF_KEY;451else452return BTRFS_TREE_BLOCK_REF_KEY;453}454455return 0;456}457458#endif459460461