/* 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;277278#ifdef CONFIG_BTRFS_FS_REF_VERIFY279/* Through which root is this modification. */280u64 real_root;281#endif282u64 bytenr;283u64 num_bytes;284u64 owning_root;285286/*287* The root that owns the reference for this reference, this will be set288* or ->parent will be set, depending on what type of reference this is.289*/290u64 ref_root;291292/* Bytenr of the parent tree block */293u64 parent;294union {295struct btrfs_data_ref data_ref;296struct btrfs_tree_ref tree_ref;297};298};299300extern struct kmem_cache *btrfs_delayed_ref_head_cachep;301extern struct kmem_cache *btrfs_delayed_ref_node_cachep;302extern struct kmem_cache *btrfs_delayed_extent_op_cachep;303304int __init btrfs_delayed_ref_init(void);305void __cold btrfs_delayed_ref_exit(void);306307static inline u64 btrfs_calc_delayed_ref_bytes(const struct btrfs_fs_info *fs_info,308int num_delayed_refs)309{310u64 num_bytes;311312num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_delayed_refs);313314/*315* We have to check the mount option here because we could be enabling316* the free space tree for the first time and don't have the compat_ro317* option set yet.318*319* We need extra reservations if we have the free space tree because320* we'll have to modify that tree as well.321*/322if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))323num_bytes *= 2;324325return num_bytes;326}327328static inline u64 btrfs_calc_delayed_ref_csum_bytes(const struct btrfs_fs_info *fs_info,329int num_csum_items)330{331/*332* Deleting csum items does not result in new nodes/leaves and does not333* require changing the free space tree, only the csum tree, so this is334* all we need.335*/336return btrfs_calc_metadata_size(fs_info, num_csum_items);337}338339void btrfs_init_tree_ref(struct btrfs_ref *generic_ref, int level, u64 mod_root,340bool skip_qgroup);341void btrfs_init_data_ref(struct btrfs_ref *generic_ref, u64 ino, u64 offset,342u64 mod_root, bool skip_qgroup);343344static inline struct btrfs_delayed_extent_op *345btrfs_alloc_delayed_extent_op(void)346{347return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);348}349350static inline void351btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)352{353if (op)354kmem_cache_free(btrfs_delayed_extent_op_cachep, op);355}356357void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref);358359static inline u64 btrfs_ref_head_to_space_flags(360struct btrfs_delayed_ref_head *head_ref)361{362if (head_ref->is_data)363return BTRFS_BLOCK_GROUP_DATA;364else if (head_ref->is_system)365return BTRFS_BLOCK_GROUP_SYSTEM;366return BTRFS_BLOCK_GROUP_METADATA;367}368369static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head)370{371if (refcount_dec_and_test(&head->refs))372kmem_cache_free(btrfs_delayed_ref_head_cachep, head);373}374375int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,376struct btrfs_ref *generic_ref,377struct btrfs_delayed_extent_op *extent_op);378int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,379struct btrfs_ref *generic_ref,380u64 reserved);381int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,382u64 bytenr, u64 num_bytes, u8 level,383struct btrfs_delayed_extent_op *extent_op);384void btrfs_merge_delayed_refs(struct btrfs_fs_info *fs_info,385struct btrfs_delayed_ref_root *delayed_refs,386struct btrfs_delayed_ref_head *head);387388struct btrfs_delayed_ref_head *389btrfs_find_delayed_ref_head(const struct btrfs_fs_info *fs_info,390struct btrfs_delayed_ref_root *delayed_refs,391u64 bytenr);392static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)393{394mutex_unlock(&head->mutex);395}396void btrfs_delete_ref_head(const struct btrfs_fs_info *fs_info,397struct btrfs_delayed_ref_root *delayed_refs,398struct btrfs_delayed_ref_head *head);399400struct btrfs_delayed_ref_head *btrfs_select_ref_head(401const struct btrfs_fs_info *fs_info,402struct btrfs_delayed_ref_root *delayed_refs);403void btrfs_unselect_ref_head(struct btrfs_delayed_ref_root *delayed_refs,404struct btrfs_delayed_ref_head *head);405struct btrfs_delayed_ref_node *btrfs_select_delayed_ref(struct btrfs_delayed_ref_head *head);406407int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);408409void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr_refs, int nr_csums);410void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);411void btrfs_inc_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info);412void btrfs_dec_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info);413void btrfs_inc_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info);414void btrfs_dec_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info);415int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,416enum btrfs_reserve_flush_enum flush);417bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);418bool btrfs_find_delayed_tree_ref(struct btrfs_delayed_ref_head *head,419u64 root, u64 parent);420void btrfs_destroy_delayed_refs(struct btrfs_transaction *trans);421422static inline u64 btrfs_delayed_ref_owner(const struct btrfs_delayed_ref_node *node)423{424if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||425node->type == BTRFS_SHARED_DATA_REF_KEY)426return node->data_ref.objectid;427return node->tree_ref.level;428}429430static inline u64 btrfs_delayed_ref_offset(const struct btrfs_delayed_ref_node *node)431{432if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||433node->type == BTRFS_SHARED_DATA_REF_KEY)434return node->data_ref.offset;435return 0;436}437438static inline u8 btrfs_ref_type(const struct btrfs_ref *ref)439{440ASSERT(ref->type == BTRFS_REF_DATA || ref->type == BTRFS_REF_METADATA);441442if (ref->type == BTRFS_REF_DATA) {443if (ref->parent)444return BTRFS_SHARED_DATA_REF_KEY;445else446return BTRFS_EXTENT_DATA_REF_KEY;447} else {448if (ref->parent)449return BTRFS_SHARED_BLOCK_REF_KEY;450else451return BTRFS_TREE_BLOCK_REF_KEY;452}453454return 0;455}456457#endif458459460