Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (C) 2007 Oracle.  All rights reserved.
0004  */
0005 
0006 #ifndef BTRFS_TRANSACTION_H
0007 #define BTRFS_TRANSACTION_H
0008 
0009 #include <linux/refcount.h>
0010 #include "btrfs_inode.h"
0011 #include "delayed-ref.h"
0012 #include "ctree.h"
0013 
0014 enum btrfs_trans_state {
0015     TRANS_STATE_RUNNING,
0016     TRANS_STATE_COMMIT_START,
0017     TRANS_STATE_COMMIT_DOING,
0018     TRANS_STATE_UNBLOCKED,
0019     TRANS_STATE_SUPER_COMMITTED,
0020     TRANS_STATE_COMPLETED,
0021     TRANS_STATE_MAX,
0022 };
0023 
0024 #define BTRFS_TRANS_HAVE_FREE_BGS   0
0025 #define BTRFS_TRANS_DIRTY_BG_RUN    1
0026 #define BTRFS_TRANS_CACHE_ENOSPC    2
0027 
0028 struct btrfs_transaction {
0029     u64 transid;
0030     /*
0031      * total external writers(USERSPACE/START/ATTACH) in this
0032      * transaction, it must be zero before the transaction is
0033      * being committed
0034      */
0035     atomic_t num_extwriters;
0036     /*
0037      * total writers in this transaction, it must be zero before the
0038      * transaction can end
0039      */
0040     atomic_t num_writers;
0041     refcount_t use_count;
0042 
0043     unsigned long flags;
0044 
0045     /* Be protected by fs_info->trans_lock when we want to change it. */
0046     enum btrfs_trans_state state;
0047     int aborted;
0048     struct list_head list;
0049     struct extent_io_tree dirty_pages;
0050     time64_t start_time;
0051     wait_queue_head_t writer_wait;
0052     wait_queue_head_t commit_wait;
0053     struct list_head pending_snapshots;
0054     struct list_head dev_update_list;
0055     struct list_head switch_commits;
0056     struct list_head dirty_bgs;
0057 
0058     /*
0059      * There is no explicit lock which protects io_bgs, rather its
0060      * consistency is implied by the fact that all the sites which modify
0061      * it do so under some form of transaction critical section, namely:
0062      *
0063      * - btrfs_start_dirty_block_groups - This function can only ever be
0064      *   run by one of the transaction committers. Refer to
0065      *   BTRFS_TRANS_DIRTY_BG_RUN usage in btrfs_commit_transaction
0066      *
0067      * - btrfs_write_dirty_blockgroups - this is called by
0068      *   commit_cowonly_roots from transaction critical section
0069      *   (TRANS_STATE_COMMIT_DOING)
0070      *
0071      * - btrfs_cleanup_dirty_bgs - called on transaction abort
0072      */
0073     struct list_head io_bgs;
0074     struct list_head dropped_roots;
0075     struct extent_io_tree pinned_extents;
0076 
0077     /*
0078      * we need to make sure block group deletion doesn't race with
0079      * free space cache writeout.  This mutex keeps them from stomping
0080      * on each other
0081      */
0082     struct mutex cache_write_mutex;
0083     spinlock_t dirty_bgs_lock;
0084     /* Protected by spin lock fs_info->unused_bgs_lock. */
0085     struct list_head deleted_bgs;
0086     spinlock_t dropped_roots_lock;
0087     struct btrfs_delayed_ref_root delayed_refs;
0088     struct btrfs_fs_info *fs_info;
0089 
0090     /*
0091      * Number of ordered extents the transaction must wait for before
0092      * committing. These are ordered extents started by a fast fsync.
0093      */
0094     atomic_t pending_ordered;
0095     wait_queue_head_t pending_wait;
0096 
0097     spinlock_t releasing_ebs_lock;
0098     struct list_head releasing_ebs;
0099 };
0100 
0101 #define __TRANS_FREEZABLE   (1U << 0)
0102 
0103 #define __TRANS_START       (1U << 9)
0104 #define __TRANS_ATTACH      (1U << 10)
0105 #define __TRANS_JOIN        (1U << 11)
0106 #define __TRANS_JOIN_NOLOCK (1U << 12)
0107 #define __TRANS_DUMMY       (1U << 13)
0108 #define __TRANS_JOIN_NOSTART    (1U << 14)
0109 
0110 #define TRANS_START     (__TRANS_START | __TRANS_FREEZABLE)
0111 #define TRANS_ATTACH        (__TRANS_ATTACH)
0112 #define TRANS_JOIN      (__TRANS_JOIN | __TRANS_FREEZABLE)
0113 #define TRANS_JOIN_NOLOCK   (__TRANS_JOIN_NOLOCK)
0114 #define TRANS_JOIN_NOSTART  (__TRANS_JOIN_NOSTART)
0115 
0116 #define TRANS_EXTWRITERS    (__TRANS_START | __TRANS_ATTACH)
0117 
0118 struct btrfs_trans_handle {
0119     u64 transid;
0120     u64 bytes_reserved;
0121     u64 chunk_bytes_reserved;
0122     unsigned long delayed_ref_updates;
0123     struct btrfs_transaction *transaction;
0124     struct btrfs_block_rsv *block_rsv;
0125     struct btrfs_block_rsv *orig_rsv;
0126     /* Set by a task that wants to create a snapshot. */
0127     struct btrfs_pending_snapshot *pending_snapshot;
0128     refcount_t use_count;
0129     unsigned int type;
0130     /*
0131      * Error code of transaction abort, set outside of locks and must use
0132      * the READ_ONCE/WRITE_ONCE access
0133      */
0134     short aborted;
0135     bool adding_csums;
0136     bool allocating_chunk;
0137     bool removing_chunk;
0138     bool reloc_reserved;
0139     bool in_fsync;
0140     struct btrfs_fs_info *fs_info;
0141     struct list_head new_bgs;
0142 };
0143 
0144 /*
0145  * The abort status can be changed between calls and is not protected by locks.
0146  * This accepts btrfs_transaction and btrfs_trans_handle as types. Once it's
0147  * set to a non-zero value it does not change, so the macro should be in checks
0148  * but is not necessary for further reads of the value.
0149  */
0150 #define TRANS_ABORTED(trans)        (unlikely(READ_ONCE((trans)->aborted)))
0151 
0152 struct btrfs_pending_snapshot {
0153     struct dentry *dentry;
0154     struct inode *dir;
0155     struct btrfs_root *root;
0156     struct btrfs_root_item *root_item;
0157     struct btrfs_root *snap;
0158     struct btrfs_qgroup_inherit *inherit;
0159     struct btrfs_path *path;
0160     /* block reservation for the operation */
0161     struct btrfs_block_rsv block_rsv;
0162     /* extra metadata reservation for relocation */
0163     int error;
0164     /* Preallocated anonymous block device number */
0165     dev_t anon_dev;
0166     bool readonly;
0167     struct list_head list;
0168 };
0169 
0170 static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
0171                           struct btrfs_inode *inode)
0172 {
0173     spin_lock(&inode->lock);
0174     inode->last_trans = trans->transaction->transid;
0175     inode->last_sub_trans = inode->root->log_transid;
0176     inode->last_log_commit = inode->last_sub_trans - 1;
0177     spin_unlock(&inode->lock);
0178 }
0179 
0180 /*
0181  * Make qgroup codes to skip given qgroupid, means the old/new_roots for
0182  * qgroup won't contain the qgroupid in it.
0183  */
0184 static inline void btrfs_set_skip_qgroup(struct btrfs_trans_handle *trans,
0185                      u64 qgroupid)
0186 {
0187     struct btrfs_delayed_ref_root *delayed_refs;
0188 
0189     delayed_refs = &trans->transaction->delayed_refs;
0190     WARN_ON(delayed_refs->qgroup_to_skip);
0191     delayed_refs->qgroup_to_skip = qgroupid;
0192 }
0193 
0194 static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans)
0195 {
0196     struct btrfs_delayed_ref_root *delayed_refs;
0197 
0198     delayed_refs = &trans->transaction->delayed_refs;
0199     WARN_ON(!delayed_refs->qgroup_to_skip);
0200     delayed_refs->qgroup_to_skip = 0;
0201 }
0202 
0203 int btrfs_end_transaction(struct btrfs_trans_handle *trans);
0204 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
0205                            unsigned int num_items);
0206 struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
0207                     struct btrfs_root *root,
0208                     unsigned int num_items);
0209 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
0210 struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root);
0211 struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root);
0212 struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
0213 struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
0214                     struct btrfs_root *root);
0215 int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
0216 
0217 void btrfs_add_dead_root(struct btrfs_root *root);
0218 int btrfs_defrag_root(struct btrfs_root *root);
0219 void btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info);
0220 int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info);
0221 int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
0222 void btrfs_commit_transaction_async(struct btrfs_trans_handle *trans);
0223 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
0224 bool btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
0225 void btrfs_throttle(struct btrfs_fs_info *fs_info);
0226 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
0227                 struct btrfs_root *root);
0228 int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
0229                 struct extent_io_tree *dirty_pages, int mark);
0230 int btrfs_wait_tree_log_extents(struct btrfs_root *root, int mark);
0231 int btrfs_transaction_blocked(struct btrfs_fs_info *info);
0232 int btrfs_transaction_in_commit(struct btrfs_fs_info *info);
0233 void btrfs_put_transaction(struct btrfs_transaction *transaction);
0234 void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info);
0235 void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
0236                 struct btrfs_root *root);
0237 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
0238 
0239 #endif