162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2008 Oracle.  All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef BTRFS_TREE_LOG_H
762306a36Sopenharmony_ci#define BTRFS_TREE_LOG_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include "messages.h"
1062306a36Sopenharmony_ci#include "ctree.h"
1162306a36Sopenharmony_ci#include "transaction.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/* return value for btrfs_log_dentry_safe that means we don't need to log it at all */
1462306a36Sopenharmony_ci#define BTRFS_NO_LOG_SYNC 256
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*
1762306a36Sopenharmony_ci * We can't use the tree log for whatever reason, force a transaction commit.
1862306a36Sopenharmony_ci * We use a negative value because there are functions through the logging code
1962306a36Sopenharmony_ci * that need to return an error (< 0 value), false (0) or true (1). Any negative
2062306a36Sopenharmony_ci * value will do, as it will cause the log to be marked for a full sync.
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci#define BTRFS_LOG_FORCE_COMMIT				(-(MAX_ERRNO + 1))
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistruct btrfs_log_ctx {
2562306a36Sopenharmony_ci	int log_ret;
2662306a36Sopenharmony_ci	int log_transid;
2762306a36Sopenharmony_ci	bool log_new_dentries;
2862306a36Sopenharmony_ci	bool logging_new_name;
2962306a36Sopenharmony_ci	bool logging_new_delayed_dentries;
3062306a36Sopenharmony_ci	/* Indicate if the inode being logged was logged before. */
3162306a36Sopenharmony_ci	bool logged_before;
3262306a36Sopenharmony_ci	struct inode *inode;
3362306a36Sopenharmony_ci	struct list_head list;
3462306a36Sopenharmony_ci	/* Only used for fast fsyncs. */
3562306a36Sopenharmony_ci	struct list_head ordered_extents;
3662306a36Sopenharmony_ci	struct list_head conflict_inodes;
3762306a36Sopenharmony_ci	int num_conflict_inodes;
3862306a36Sopenharmony_ci	bool logging_conflict_inodes;
3962306a36Sopenharmony_ci};
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistatic inline void btrfs_init_log_ctx(struct btrfs_log_ctx *ctx,
4262306a36Sopenharmony_ci				      struct inode *inode)
4362306a36Sopenharmony_ci{
4462306a36Sopenharmony_ci	ctx->log_ret = 0;
4562306a36Sopenharmony_ci	ctx->log_transid = 0;
4662306a36Sopenharmony_ci	ctx->log_new_dentries = false;
4762306a36Sopenharmony_ci	ctx->logging_new_name = false;
4862306a36Sopenharmony_ci	ctx->logging_new_delayed_dentries = false;
4962306a36Sopenharmony_ci	ctx->logged_before = false;
5062306a36Sopenharmony_ci	ctx->inode = inode;
5162306a36Sopenharmony_ci	INIT_LIST_HEAD(&ctx->list);
5262306a36Sopenharmony_ci	INIT_LIST_HEAD(&ctx->ordered_extents);
5362306a36Sopenharmony_ci	INIT_LIST_HEAD(&ctx->conflict_inodes);
5462306a36Sopenharmony_ci	ctx->num_conflict_inodes = 0;
5562306a36Sopenharmony_ci	ctx->logging_conflict_inodes = false;
5662306a36Sopenharmony_ci}
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_cistatic inline void btrfs_release_log_ctx_extents(struct btrfs_log_ctx *ctx)
5962306a36Sopenharmony_ci{
6062306a36Sopenharmony_ci	struct btrfs_ordered_extent *ordered;
6162306a36Sopenharmony_ci	struct btrfs_ordered_extent *tmp;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	ASSERT(inode_is_locked(ctx->inode));
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
6662306a36Sopenharmony_ci		list_del_init(&ordered->log_list);
6762306a36Sopenharmony_ci		btrfs_put_ordered_extent(ordered);
6862306a36Sopenharmony_ci	}
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistatic inline void btrfs_set_log_full_commit(struct btrfs_trans_handle *trans)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	WRITE_ONCE(trans->fs_info->last_trans_log_full_commit, trans->transid);
7462306a36Sopenharmony_ci}
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_cistatic inline int btrfs_need_log_full_commit(struct btrfs_trans_handle *trans)
7762306a36Sopenharmony_ci{
7862306a36Sopenharmony_ci	return READ_ONCE(trans->fs_info->last_trans_log_full_commit) ==
7962306a36Sopenharmony_ci		trans->transid;
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ciint btrfs_sync_log(struct btrfs_trans_handle *trans,
8362306a36Sopenharmony_ci		   struct btrfs_root *root, struct btrfs_log_ctx *ctx);
8462306a36Sopenharmony_ciint btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root);
8562306a36Sopenharmony_ciint btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
8662306a36Sopenharmony_ci			     struct btrfs_fs_info *fs_info);
8762306a36Sopenharmony_ciint btrfs_recover_log_trees(struct btrfs_root *tree_root);
8862306a36Sopenharmony_ciint btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
8962306a36Sopenharmony_ci			  struct dentry *dentry,
9062306a36Sopenharmony_ci			  struct btrfs_log_ctx *ctx);
9162306a36Sopenharmony_civoid btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
9262306a36Sopenharmony_ci				  struct btrfs_root *root,
9362306a36Sopenharmony_ci				  const struct fscrypt_str *name,
9462306a36Sopenharmony_ci				  struct btrfs_inode *dir, u64 index);
9562306a36Sopenharmony_civoid btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
9662306a36Sopenharmony_ci				struct btrfs_root *root,
9762306a36Sopenharmony_ci				const struct fscrypt_str *name,
9862306a36Sopenharmony_ci				struct btrfs_inode *inode, u64 dirid);
9962306a36Sopenharmony_civoid btrfs_end_log_trans(struct btrfs_root *root);
10062306a36Sopenharmony_civoid btrfs_pin_log_trans(struct btrfs_root *root);
10162306a36Sopenharmony_civoid btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
10262306a36Sopenharmony_ci			     struct btrfs_inode *dir, struct btrfs_inode *inode,
10362306a36Sopenharmony_ci			     bool for_rename);
10462306a36Sopenharmony_civoid btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
10562306a36Sopenharmony_ci				   struct btrfs_inode *dir);
10662306a36Sopenharmony_civoid btrfs_log_new_name(struct btrfs_trans_handle *trans,
10762306a36Sopenharmony_ci			struct dentry *old_dentry, struct btrfs_inode *old_dir,
10862306a36Sopenharmony_ci			u64 old_dir_index, struct dentry *parent);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci#endif
111