#include <linux/bsearch.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/sort.h>
#include <linux/mount.h>
#include <linux/xattr.h>
#include <linux/posix_acl_xattr.h>
#include <linux/radix-tree.h>
#include <linux/vmalloc.h>
#include <linux/string.h>
#include <linux/compat.h>
#include <linux/crc32c.h>
#include <linux/fsverity.h>
#include "send.h"
#include "ctree.h"
#include "backref.h"
#include "locking.h"
#include "disk-io.h"
#include "btrfs_inode.h"
#include "transaction.h"
#include "compression.h"
#include "print-tree.h"
#include "accessors.h"
#include "dir-item.h"
#include "file-item.h"
#include "ioctl.h"
#include "verity.h"
#include "lru_cache.h"
#define SEND_MAX_EXTENT_REFS …
struct fs_path { … };
#define FS_PATH_INLINE_SIZE …
struct clone_root { … };
#define SEND_MAX_NAME_CACHE_SIZE …
#define SEND_MAX_BACKREF_CACHE_ROOTS …
#define SEND_MAX_BACKREF_CACHE_SIZE …
struct backref_cache_entry { … };
static_assert(…);
#define SEND_MAX_DIR_CREATED_CACHE_SIZE …
#define SEND_MAX_DIR_UTIMES_CACHE_SIZE …
struct send_ctx { … };
struct pending_dir_move { … };
struct waiting_dir_move { … };
struct orphan_dir_info { … };
struct name_cache_entry { … };
static_assert(…);
#define ADVANCE …
#define ADVANCE_ONLY_NEXT …
enum btrfs_compare_tree_result { … };
__cold
static void inconsistent_snapshot_error(struct send_ctx *sctx,
enum btrfs_compare_tree_result result,
const char *what)
{ … }
__maybe_unused
static bool proto_cmd_ok(const struct send_ctx *sctx, int cmd)
{ … }
static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
static struct waiting_dir_move *
get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
static int need_send_hole(struct send_ctx *sctx)
{ … }
static void fs_path_reset(struct fs_path *p)
{ … }
static struct fs_path *fs_path_alloc(void)
{ … }
static struct fs_path *fs_path_alloc_reversed(void)
{ … }
static void fs_path_free(struct fs_path *p)
{ … }
static int fs_path_len(struct fs_path *p)
{ … }
static int fs_path_ensure_buf(struct fs_path *p, int len)
{ … }
static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
char **prepared)
{ … }
static int fs_path_add(struct fs_path *p, const char *name, int name_len)
{ … }
static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
{ … }
static int fs_path_add_from_extent_buffer(struct fs_path *p,
struct extent_buffer *eb,
unsigned long off, int len)
{ … }
static int fs_path_copy(struct fs_path *p, struct fs_path *from)
{ … }
static void fs_path_unreverse(struct fs_path *p)
{ … }
static struct btrfs_path *alloc_path_for_send(void)
{ … }
static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
{ … }
static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
{ … }
#define TLV_PUT_DEFINE_INT(bits) …
TLV_PUT_DEFINE_INT(…)
TLV_PUT_DEFINE_INT(…)
TLV_PUT_DEFINE_INT(…)
static int tlv_put_string(struct send_ctx *sctx, u16 attr,
const char *str, int len)
{ … }
static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
const u8 *uuid)
{ … }
static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
struct extent_buffer *eb,
struct btrfs_timespec *ts)
{ … }
#define TLV_PUT(sctx, attrtype, data, attrlen) …
#define TLV_PUT_INT(sctx, attrtype, bits, value) …
#define TLV_PUT_U8(sctx, attrtype, data) …
#define TLV_PUT_U16(sctx, attrtype, data) …
#define TLV_PUT_U32(sctx, attrtype, data) …
#define TLV_PUT_U64(sctx, attrtype, data) …
#define TLV_PUT_STRING(sctx, attrtype, str, len) …
#define TLV_PUT_PATH(sctx, attrtype, p) …
#define TLV_PUT_UUID(sctx, attrtype, uuid) …
#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) …
static int send_header(struct send_ctx *sctx)
{ … }
static int begin_cmd(struct send_ctx *sctx, int cmd)
{ … }
static int send_cmd(struct send_ctx *sctx)
{ … }
static int send_rename(struct send_ctx *sctx,
struct fs_path *from, struct fs_path *to)
{ … }
static int send_link(struct send_ctx *sctx,
struct fs_path *path, struct fs_path *lnk)
{ … }
static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
{ … }
static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
{ … }
struct btrfs_inode_info { … };
static int get_inode_info(struct btrfs_root *root, u64 ino,
struct btrfs_inode_info *info)
{ … }
static int get_inode_gen(struct btrfs_root *root, u64 ino, u64 *gen)
{ … }
iterate_inode_ref_t;
static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
struct btrfs_key *found_key, int resolve,
iterate_inode_ref_t iterate, void *ctx)
{ … }
iterate_dir_item_t;
static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
iterate_dir_item_t iterate, void *ctx)
{ … }
static int __copy_first_ref(int num, u64 dir, int index,
struct fs_path *p, void *ctx)
{ … }
static int get_inode_path(struct btrfs_root *root,
u64 ino, struct fs_path *path)
{ … }
struct backref_ctx { … };
static int __clone_root_cmp_bsearch(const void *key, const void *elt)
{ … }
static int __clone_root_cmp_sort(const void *e1, const void *e2)
{ … }
static int iterate_backrefs(u64 ino, u64 offset, u64 num_bytes, u64 root_id,
void *ctx_)
{ … }
static bool lookup_backref_cache(u64 leaf_bytenr, void *ctx,
const u64 **root_ids_ret, int *root_count_ret)
{ … }
static void store_backref_cache(u64 leaf_bytenr, const struct ulist *root_ids,
void *ctx)
{ … }
static int check_extent_item(u64 bytenr, const struct btrfs_extent_item *ei,
const struct extent_buffer *leaf, void *ctx)
{ … }
static bool skip_self_data_ref(u64 root, u64 ino, u64 offset, void *ctx)
{ … }
static int find_extent_clone(struct send_ctx *sctx,
struct btrfs_path *path,
u64 ino, u64 data_offset,
u64 ino_size,
struct clone_root **found)
{ … }
static int read_symlink(struct btrfs_root *root,
u64 ino,
struct fs_path *dest)
{ … }
static int gen_unique_name(struct send_ctx *sctx,
u64 ino, u64 gen,
struct fs_path *dest)
{ … }
enum inode_state { … };
static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen,
u64 *send_gen, u64 *parent_gen)
{ … }
static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen,
u64 *send_gen, u64 *parent_gen)
{ … }
static int lookup_dir_item_inode(struct btrfs_root *root,
u64 dir, const char *name, int name_len,
u64 *found_inode)
{ … }
static int get_first_ref(struct btrfs_root *root, u64 ino,
u64 *dir, u64 *dir_gen, struct fs_path *name)
{ … }
static int is_first_ref(struct btrfs_root *root,
u64 ino, u64 dir,
const char *name, int name_len)
{ … }
static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
const char *name, int name_len,
u64 *who_ino, u64 *who_gen, u64 *who_mode)
{ … }
static int did_overwrite_ref(struct send_ctx *sctx,
u64 dir, u64 dir_gen,
u64 ino, u64 ino_gen,
const char *name, int name_len)
{ … }
static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
{ … }
static inline struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
u64 ino, u64 gen)
{ … }
static int __get_cur_name_and_parent(struct send_ctx *sctx,
u64 ino, u64 gen,
u64 *parent_ino,
u64 *parent_gen,
struct fs_path *dest)
{ … }
static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
struct fs_path *dest)
{ … }
static int send_subvol_begin(struct send_ctx *sctx)
{ … }
static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
{ … }
static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
{ … }
static int send_fileattr(struct send_ctx *sctx, u64 ino, u64 gen, u64 fileattr)
{ … }
static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
{ … }
static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
{ … }
static int cache_dir_utimes(struct send_ctx *sctx, u64 dir, u64 gen)
{ … }
static int trim_dir_utimes_cache(struct send_ctx *sctx)
{ … }
static int send_create_inode(struct send_ctx *sctx, u64 ino)
{ … }
static void cache_dir_created(struct send_ctx *sctx, u64 dir)
{ … }
static int did_create_dir(struct send_ctx *sctx, u64 dir)
{ … }
static int send_create_inode_if_needed(struct send_ctx *sctx)
{ … }
struct recorded_ref { … };
static struct recorded_ref *recorded_ref_alloc(void)
{ … }
static void recorded_ref_free(struct recorded_ref *ref)
{ … }
static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
{ … }
static int dup_ref(struct recorded_ref *ref, struct list_head *list)
{ … }
static void __free_recorded_refs(struct list_head *head)
{ … }
static void free_recorded_refs(struct send_ctx *sctx)
{ … }
static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
struct fs_path *path)
{ … }
static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx,
u64 dir_ino, u64 dir_gen)
{ … }
static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx,
u64 dir_ino, u64 gen)
{ … }
static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen)
{ … }
static void free_orphan_dir_info(struct send_ctx *sctx,
struct orphan_dir_info *odi)
{ … }
static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen)
{ … }
static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
{ … }
static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
{ … }
static struct waiting_dir_move *
get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
{ … }
static void free_waiting_dir_move(struct send_ctx *sctx,
struct waiting_dir_move *dm)
{ … }
static int add_pending_dir_move(struct send_ctx *sctx,
u64 ino,
u64 ino_gen,
u64 parent_ino,
struct list_head *new_refs,
struct list_head *deleted_refs,
const bool is_orphan)
{ … }
static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
u64 parent_ino)
{ … }
static int path_loop(struct send_ctx *sctx, struct fs_path *name,
u64 ino, u64 gen, u64 *ancestor_ino)
{ … }
static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
{ … }
static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
{ … }
static void tail_append_pending_moves(struct send_ctx *sctx,
struct pending_dir_move *moves,
struct list_head *stack)
{ … }
static int apply_children_dir_moves(struct send_ctx *sctx)
{ … }
static int wait_for_dest_dir_move(struct send_ctx *sctx,
struct recorded_ref *parent_ref,
const bool is_orphan)
{ … }
static int check_ino_in_path(struct btrfs_root *root,
const u64 ino1,
const u64 ino1_gen,
const u64 ino2,
const u64 ino2_gen,
struct fs_path *fs_path)
{ … }
static int is_ancestor(struct btrfs_root *root,
const u64 ino1,
const u64 ino1_gen,
const u64 ino2,
struct fs_path *fs_path)
{ … }
static int wait_for_parent_move(struct send_ctx *sctx,
struct recorded_ref *parent_ref,
const bool is_orphan)
{ … }
static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
{ … }
static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
{ … }
static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
{ … }
static int rbtree_ref_comp(const void *k, const struct rb_node *node)
{ … }
static bool rbtree_ref_less(struct rb_node *node, const struct rb_node *parent)
{ … }
static int record_ref_in_tree(struct rb_root *root, struct list_head *refs,
struct fs_path *name, u64 dir, u64 dir_gen,
struct send_ctx *sctx)
{ … }
static int record_new_ref_if_needed(int num, u64 dir, int index,
struct fs_path *name, void *ctx)
{ … }
static int record_deleted_ref_if_needed(int num, u64 dir, int index,
struct fs_path *name, void *ctx)
{ … }
static int record_new_ref(struct send_ctx *sctx)
{ … }
static int record_deleted_ref(struct send_ctx *sctx)
{ … }
static int record_changed_ref(struct send_ctx *sctx)
{ … }
static int process_all_refs(struct send_ctx *sctx,
enum btrfs_compare_tree_result cmd)
{ … }
static int send_set_xattr(struct send_ctx *sctx,
struct fs_path *path,
const char *name, int name_len,
const char *data, int data_len)
{ … }
static int send_remove_xattr(struct send_ctx *sctx,
struct fs_path *path,
const char *name, int name_len)
{ … }
static int __process_new_xattr(int num, struct btrfs_key *di_key,
const char *name, int name_len, const char *data,
int data_len, void *ctx)
{ … }
static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
const char *name, int name_len,
const char *data, int data_len, void *ctx)
{ … }
static int process_new_xattr(struct send_ctx *sctx)
{ … }
static int process_deleted_xattr(struct send_ctx *sctx)
{ … }
struct find_xattr_ctx { … };
static int __find_xattr(int num, struct btrfs_key *di_key, const char *name,
int name_len, const char *data, int data_len, void *vctx)
{ … }
static int find_xattr(struct btrfs_root *root,
struct btrfs_path *path,
struct btrfs_key *key,
const char *name, int name_len,
char **data, int *data_len)
{ … }
static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
const char *name, int name_len,
const char *data, int data_len,
void *ctx)
{ … }
static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
const char *name, int name_len,
const char *data, int data_len,
void *ctx)
{ … }
static int process_changed_xattr(struct send_ctx *sctx)
{ … }
static int process_all_new_xattrs(struct send_ctx *sctx)
{ … }
static int send_verity(struct send_ctx *sctx, struct fs_path *path,
struct fsverity_descriptor *desc)
{ … }
static int process_verity(struct send_ctx *sctx)
{ … }
static inline u64 max_send_read_size(const struct send_ctx *sctx)
{ … }
static int put_data_header(struct send_ctx *sctx, u32 len)
{ … }
static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)
{ … }
static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
{ … }
static int send_clone(struct send_ctx *sctx,
u64 offset, u32 len,
struct clone_root *clone_root)
{ … }
static int send_update_extent(struct send_ctx *sctx,
u64 offset, u32 len)
{ … }
static int send_hole(struct send_ctx *sctx, u64 end)
{ … }
static int send_encoded_inline_extent(struct send_ctx *sctx,
struct btrfs_path *path, u64 offset,
u64 len)
{ … }
static int send_encoded_extent(struct send_ctx *sctx, struct btrfs_path *path,
u64 offset, u64 len)
{ … }
static int send_extent_data(struct send_ctx *sctx, struct btrfs_path *path,
const u64 offset, const u64 len)
{ … }
static int send_capabilities(struct send_ctx *sctx)
{ … }
static int clone_range(struct send_ctx *sctx, struct btrfs_path *dst_path,
struct clone_root *clone_root, const u64 disk_byte,
u64 data_offset, u64 offset, u64 len)
{ … }
static int send_write_or_clone(struct send_ctx *sctx,
struct btrfs_path *path,
struct btrfs_key *key,
struct clone_root *clone_root)
{ … }
static int is_extent_unchanged(struct send_ctx *sctx,
struct btrfs_path *left_path,
struct btrfs_key *ekey)
{ … }
static int get_last_extent(struct send_ctx *sctx, u64 offset)
{ … }
static int range_is_hole_in_parent(struct send_ctx *sctx,
const u64 start,
const u64 end)
{ … }
static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
struct btrfs_key *key)
{ … }
static int process_extent(struct send_ctx *sctx,
struct btrfs_path *path,
struct btrfs_key *key)
{ … }
static int process_all_extents(struct send_ctx *sctx)
{ … }
static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
int *pending_move,
int *refs_processed)
{ … }
static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
{ … }
static void close_current_inode(struct send_ctx *sctx)
{ … }
static int changed_inode(struct send_ctx *sctx,
enum btrfs_compare_tree_result result)
{ … }
static int changed_ref(struct send_ctx *sctx,
enum btrfs_compare_tree_result result)
{ … }
static int changed_xattr(struct send_ctx *sctx,
enum btrfs_compare_tree_result result)
{ … }
static int changed_extent(struct send_ctx *sctx,
enum btrfs_compare_tree_result result)
{ … }
static int changed_verity(struct send_ctx *sctx, enum btrfs_compare_tree_result result)
{ … }
static int dir_changed(struct send_ctx *sctx, u64 dir)
{ … }
static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
struct btrfs_key *key)
{ … }
static int changed_cb(struct btrfs_path *left_path,
struct btrfs_path *right_path,
struct btrfs_key *key,
enum btrfs_compare_tree_result result,
struct send_ctx *sctx)
{ … }
static int search_key_again(const struct send_ctx *sctx,
struct btrfs_root *root,
struct btrfs_path *path,
const struct btrfs_key *key)
{ … }
static int full_send_tree(struct send_ctx *sctx)
{ … }
static int replace_node_with_clone(struct btrfs_path *path, int level)
{ … }
static int tree_move_down(struct btrfs_path *path, int *level, u64 reada_min_gen)
{ … }
static int tree_move_next_or_upnext(struct btrfs_path *path,
int *level, int root_level)
{ … }
static int tree_advance(struct btrfs_path *path,
int *level, int root_level,
int allow_down,
struct btrfs_key *key,
u64 reada_min_gen)
{ … }
static int tree_compare_item(struct btrfs_path *left_path,
struct btrfs_path *right_path,
char *tmp_buf)
{ … }
static int restart_after_relocation(struct btrfs_path *left_path,
struct btrfs_path *right_path,
const struct btrfs_key *left_key,
const struct btrfs_key *right_key,
int left_level,
int right_level,
const struct send_ctx *sctx)
{ … }
static int btrfs_compare_trees(struct btrfs_root *left_root,
struct btrfs_root *right_root, struct send_ctx *sctx)
{ … }
static int send_subvol(struct send_ctx *sctx)
{ … }
static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
{ … }
static int flush_delalloc_roots(struct send_ctx *sctx)
{ … }
static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
{ … }
static void dedupe_in_progress_warn(const struct btrfs_root *root)
{ … }
long btrfs_ioctl_send(struct btrfs_inode *inode, const struct btrfs_ioctl_send_args *arg)
{ … }