2018-04-04 01:16:55 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2011-06-14 01:52:59 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 STRATO. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2018-04-04 01:16:55 +08:00
|
|
|
#ifndef BTRFS_BACKREF_H
|
|
|
|
#define BTRFS_BACKREF_H
|
2011-06-14 01:52:59 +08:00
|
|
|
|
2013-01-29 14:04:50 +08:00
|
|
|
#include <linux/btrfs.h>
|
2011-11-24 01:55:04 +08:00
|
|
|
#include "ulist.h"
|
2012-06-03 20:23:23 +08:00
|
|
|
#include "extent_io.h"
|
2011-06-14 01:52:59 +08:00
|
|
|
|
|
|
|
struct inode_fs_paths {
|
|
|
|
struct btrfs_path *btrfs_path;
|
|
|
|
struct btrfs_root *fs_root;
|
|
|
|
struct btrfs_data_container *fspath;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef int (iterate_extent_inodes_t)(u64 inum, u64 offset, u64 root,
|
|
|
|
void *ctx);
|
|
|
|
|
|
|
|
int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
|
2012-09-08 10:01:28 +08:00
|
|
|
struct btrfs_path *path, struct btrfs_key *found_key,
|
|
|
|
u64 *flags);
|
2011-06-14 01:52:59 +08:00
|
|
|
|
|
|
|
int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
|
2014-06-09 10:54:07 +08:00
|
|
|
struct btrfs_key *key, struct btrfs_extent_item *ei,
|
|
|
|
u32 item_size, u64 *out_root, u8 *out_level);
|
2011-06-14 01:52:59 +08:00
|
|
|
|
|
|
|
int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
|
|
|
|
u64 extent_item_objectid,
|
2012-03-24 00:32:28 +08:00
|
|
|
u64 extent_offset, int search_commit_root,
|
btrfs: add a flag to iterate_inodes_from_logical to find all extent refs for uncompressed extents
The LOGICAL_INO ioctl provides a backward mapping from extent bytenr and
offset (encoded as a single logical address) to a list of extent refs.
LOGICAL_INO complements TREE_SEARCH, which provides the forward mapping
(extent ref -> extent bytenr and offset, or logical address). These are
useful capabilities for programs that manipulate extents and extent
references from userspace (e.g. dedup and defrag utilities).
When the extents are uncompressed (and not encrypted and not other),
check_extent_in_eb performs filtering of the extent refs to remove any
extent refs which do not contain the same extent offset as the 'logical'
parameter's extent offset. This prevents LOGICAL_INO from returning
references to more than a single block.
To find the set of extent references to an uncompressed extent from [a, b),
userspace has to run a loop like this pseudocode:
for (i = a; i < b; ++i)
extent_ref_set += LOGICAL_INO(i);
At each iteration of the loop (up to 32768 iterations for a 128M extent),
data we are interested in is collected in the kernel, then deleted by
the filter in check_extent_in_eb.
When the extents are compressed (or encrypted or other), the 'logical'
parameter must be an extent bytenr (the 'a' parameter in the loop).
No filtering by extent offset is done (or possible?) so the result is
the complete set of extent refs for the entire extent. This removes
the need for the loop, since we get all the extent refs in one call.
Add an 'ignore_offset' argument to iterate_inodes_from_logical,
[...several levels of function call graph...], and check_extent_in_eb, so
that we can disable the extent offset filtering for uncompressed extents.
This flag can be set by an improved version of the LOGICAL_INO ioctl to
get either behavior as desired.
There is no functional change in this patch. The new flag is always
false.
Signed-off-by: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
Reviewed-by: David Sterba <dsterba@suse.com>
[ minor coding style fixes ]
Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-23 01:58:45 +08:00
|
|
|
iterate_extent_inodes_t *iterate, void *ctx,
|
|
|
|
bool ignore_offset);
|
2011-06-14 01:52:59 +08:00
|
|
|
|
|
|
|
int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
|
|
|
|
struct btrfs_path *path,
|
btrfs: add a flag to iterate_inodes_from_logical to find all extent refs for uncompressed extents
The LOGICAL_INO ioctl provides a backward mapping from extent bytenr and
offset (encoded as a single logical address) to a list of extent refs.
LOGICAL_INO complements TREE_SEARCH, which provides the forward mapping
(extent ref -> extent bytenr and offset, or logical address). These are
useful capabilities for programs that manipulate extents and extent
references from userspace (e.g. dedup and defrag utilities).
When the extents are uncompressed (and not encrypted and not other),
check_extent_in_eb performs filtering of the extent refs to remove any
extent refs which do not contain the same extent offset as the 'logical'
parameter's extent offset. This prevents LOGICAL_INO from returning
references to more than a single block.
To find the set of extent references to an uncompressed extent from [a, b),
userspace has to run a loop like this pseudocode:
for (i = a; i < b; ++i)
extent_ref_set += LOGICAL_INO(i);
At each iteration of the loop (up to 32768 iterations for a 128M extent),
data we are interested in is collected in the kernel, then deleted by
the filter in check_extent_in_eb.
When the extents are compressed (or encrypted or other), the 'logical'
parameter must be an extent bytenr (the 'a' parameter in the loop).
No filtering by extent offset is done (or possible?) so the result is
the complete set of extent refs for the entire extent. This removes
the need for the loop, since we get all the extent refs in one call.
Add an 'ignore_offset' argument to iterate_inodes_from_logical,
[...several levels of function call graph...], and check_extent_in_eb, so
that we can disable the extent offset filtering for uncompressed extents.
This flag can be set by an improved version of the LOGICAL_INO ioctl to
get either behavior as desired.
There is no functional change in this patch. The new flag is always
false.
Signed-off-by: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
Reviewed-by: David Sterba <dsterba@suse.com>
[ minor coding style fixes ]
Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-23 01:58:45 +08:00
|
|
|
iterate_extent_inodes_t *iterate, void *ctx,
|
|
|
|
bool ignore_offset);
|
2011-06-14 01:52:59 +08:00
|
|
|
|
|
|
|
int paths_from_inode(u64 inum, struct inode_fs_paths *ipath);
|
|
|
|
|
2020-03-10 16:14:15 +08:00
|
|
|
int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
|
|
|
|
struct btrfs_fs_info *fs_info, u64 bytenr,
|
|
|
|
u64 time_seq, struct ulist **leafs,
|
|
|
|
const u64 *extent_item_pos, bool ignore_offset);
|
2011-11-24 01:55:04 +08:00
|
|
|
int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
|
2014-05-14 08:30:47 +08:00
|
|
|
struct btrfs_fs_info *fs_info, u64 bytenr,
|
btrfs: add a flag to iterate_inodes_from_logical to find all extent refs for uncompressed extents
The LOGICAL_INO ioctl provides a backward mapping from extent bytenr and
offset (encoded as a single logical address) to a list of extent refs.
LOGICAL_INO complements TREE_SEARCH, which provides the forward mapping
(extent ref -> extent bytenr and offset, or logical address). These are
useful capabilities for programs that manipulate extents and extent
references from userspace (e.g. dedup and defrag utilities).
When the extents are uncompressed (and not encrypted and not other),
check_extent_in_eb performs filtering of the extent refs to remove any
extent refs which do not contain the same extent offset as the 'logical'
parameter's extent offset. This prevents LOGICAL_INO from returning
references to more than a single block.
To find the set of extent references to an uncompressed extent from [a, b),
userspace has to run a loop like this pseudocode:
for (i = a; i < b; ++i)
extent_ref_set += LOGICAL_INO(i);
At each iteration of the loop (up to 32768 iterations for a 128M extent),
data we are interested in is collected in the kernel, then deleted by
the filter in check_extent_in_eb.
When the extents are compressed (or encrypted or other), the 'logical'
parameter must be an extent bytenr (the 'a' parameter in the loop).
No filtering by extent offset is done (or possible?) so the result is
the complete set of extent refs for the entire extent. This removes
the need for the loop, since we get all the extent refs in one call.
Add an 'ignore_offset' argument to iterate_inodes_from_logical,
[...several levels of function call graph...], and check_extent_in_eb, so
that we can disable the extent offset filtering for uncompressed extents.
This flag can be set by an improved version of the LOGICAL_INO ioctl to
get either behavior as desired.
There is no functional change in this patch. The new flag is always
false.
Signed-off-by: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
Reviewed-by: David Sterba <dsterba@suse.com>
[ minor coding style fixes ]
Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-23 01:58:45 +08:00
|
|
|
u64 time_seq, struct ulist **roots, bool ignore_offset);
|
2012-10-15 16:30:45 +08:00
|
|
|
char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
|
|
|
|
u32 name_len, unsigned long name_off,
|
|
|
|
struct extent_buffer *eb_in, u64 parent,
|
|
|
|
char *dest, u32 size);
|
2011-11-24 01:55:04 +08:00
|
|
|
|
2011-06-14 01:52:59 +08:00
|
|
|
struct btrfs_data_container *init_data_container(u32 total_bytes);
|
|
|
|
struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
|
|
|
|
struct btrfs_path *path);
|
|
|
|
void free_ipath(struct inode_fs_paths *ipath);
|
|
|
|
|
2012-08-09 02:32:27 +08:00
|
|
|
int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
|
|
|
|
u64 start_off, struct btrfs_path *path,
|
|
|
|
struct btrfs_inode_extref **ret_extref,
|
|
|
|
u64 *found_off);
|
2019-05-15 21:31:04 +08:00
|
|
|
int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr,
|
|
|
|
struct ulist *roots, struct ulist *tmp_ulist);
|
2012-08-09 02:32:27 +08:00
|
|
|
|
2013-08-09 13:25:36 +08:00
|
|
|
int __init btrfs_prelim_ref_init(void);
|
2018-02-20 00:24:18 +08:00
|
|
|
void __cold btrfs_prelim_ref_exit(void);
|
2017-07-13 06:20:08 +08:00
|
|
|
|
|
|
|
struct prelim_ref {
|
|
|
|
struct rb_node rbnode;
|
|
|
|
u64 root_id;
|
|
|
|
struct btrfs_key key_for_search;
|
|
|
|
int level;
|
|
|
|
int count;
|
|
|
|
struct extent_inode_elem *inode_list;
|
|
|
|
u64 parent;
|
|
|
|
u64 wanted_disk_byte;
|
|
|
|
};
|
|
|
|
|
2020-02-13 14:11:04 +08:00
|
|
|
/*
|
|
|
|
* Iterate backrefs of one extent.
|
|
|
|
*
|
|
|
|
* Now it only supports iteration of tree block in commit root.
|
|
|
|
*/
|
|
|
|
struct btrfs_backref_iter {
|
|
|
|
u64 bytenr;
|
|
|
|
struct btrfs_path *path;
|
|
|
|
struct btrfs_fs_info *fs_info;
|
|
|
|
struct btrfs_key cur_key;
|
|
|
|
u32 item_ptr;
|
|
|
|
u32 cur_ptr;
|
|
|
|
u32 end_ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct btrfs_backref_iter *btrfs_backref_iter_alloc(
|
|
|
|
struct btrfs_fs_info *fs_info, gfp_t gfp_flag);
|
|
|
|
|
|
|
|
static inline void btrfs_backref_iter_free(struct btrfs_backref_iter *iter)
|
|
|
|
{
|
|
|
|
if (!iter)
|
|
|
|
return;
|
|
|
|
btrfs_free_path(iter->path);
|
|
|
|
kfree(iter);
|
|
|
|
}
|
|
|
|
|
2020-02-13 15:04:04 +08:00
|
|
|
static inline struct extent_buffer *btrfs_backref_get_eb(
|
|
|
|
struct btrfs_backref_iter *iter)
|
|
|
|
{
|
|
|
|
if (!iter)
|
|
|
|
return NULL;
|
|
|
|
return iter->path->nodes[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data
|
|
|
|
* is btrfs_tree_block_info, without a btrfs_extent_inline_ref header.
|
|
|
|
*
|
|
|
|
* This helper determines if that's the case.
|
|
|
|
*/
|
|
|
|
static inline bool btrfs_backref_has_tree_block_info(
|
|
|
|
struct btrfs_backref_iter *iter)
|
|
|
|
{
|
|
|
|
if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY &&
|
|
|
|
iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-13 14:11:04 +08:00
|
|
|
int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr);
|
|
|
|
|
2020-02-13 15:04:04 +08:00
|
|
|
int btrfs_backref_iter_next(struct btrfs_backref_iter *iter);
|
|
|
|
|
|
|
|
static inline bool btrfs_backref_iter_is_inline_ref(
|
|
|
|
struct btrfs_backref_iter *iter)
|
|
|
|
{
|
|
|
|
if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY ||
|
|
|
|
iter->cur_key.type == BTRFS_METADATA_ITEM_KEY)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-13 14:11:04 +08:00
|
|
|
static inline void btrfs_backref_iter_release(struct btrfs_backref_iter *iter)
|
|
|
|
{
|
|
|
|
iter->bytenr = 0;
|
|
|
|
iter->item_ptr = 0;
|
|
|
|
iter->cur_ptr = 0;
|
|
|
|
iter->end_ptr = 0;
|
|
|
|
btrfs_release_path(iter->path);
|
|
|
|
memset(&iter->cur_key, 0, sizeof(iter->cur_key));
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:03:56 +08:00
|
|
|
/*
|
|
|
|
* Backref cache related structures
|
|
|
|
*
|
|
|
|
* The whole objective of backref_cache is to build a bi-directional map
|
|
|
|
* of tree blocks (represented by backref_node) and all their parents.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Represent a tree block in the backref cache
|
|
|
|
*/
|
|
|
|
struct btrfs_backref_node {
|
2020-03-26 14:11:09 +08:00
|
|
|
struct {
|
|
|
|
struct rb_node rb_node;
|
|
|
|
u64 bytenr;
|
|
|
|
}; /* Use rb_simple_node for search/insert */
|
2020-03-23 15:03:56 +08:00
|
|
|
|
|
|
|
u64 new_bytenr;
|
|
|
|
/* Objectid of tree block owner, can be not uptodate */
|
|
|
|
u64 owner;
|
|
|
|
/* Link to pending, changed or detached list */
|
|
|
|
struct list_head list;
|
|
|
|
|
|
|
|
/* List of upper level edges, which link this node to its parents */
|
|
|
|
struct list_head upper;
|
|
|
|
/* List of lower level edges, which link this node to its children */
|
|
|
|
struct list_head lower;
|
|
|
|
|
|
|
|
/* NULL if this node is not tree root */
|
|
|
|
struct btrfs_root *root;
|
|
|
|
/* Extent buffer got by COWing the block */
|
|
|
|
struct extent_buffer *eb;
|
|
|
|
/* Level of the tree block */
|
|
|
|
unsigned int level:8;
|
|
|
|
/* Is the block in non-reference counted tree */
|
|
|
|
unsigned int cowonly:1;
|
|
|
|
/* 1 if no child node is in the cache */
|
|
|
|
unsigned int lowest:1;
|
|
|
|
/* Is the extent buffer locked */
|
|
|
|
unsigned int locked:1;
|
|
|
|
/* Has the block been processed */
|
|
|
|
unsigned int processed:1;
|
|
|
|
/* Have backrefs of this block been checked */
|
|
|
|
unsigned int checked:1;
|
|
|
|
/*
|
|
|
|
* 1 if corresponding block has been COWed but some upper level block
|
|
|
|
* pointers may not point to the new location
|
|
|
|
*/
|
|
|
|
unsigned int pending:1;
|
|
|
|
/* 1 if the backref node isn't connected to any other backref node */
|
|
|
|
unsigned int detached:1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For generic purpose backref cache, where we only care if it's a reloc
|
|
|
|
* root, doesn't care the source subvolid.
|
|
|
|
*/
|
|
|
|
unsigned int is_reloc_root:1;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define LOWER 0
|
|
|
|
#define UPPER 1
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Represent an edge connecting upper and lower backref nodes.
|
|
|
|
*/
|
|
|
|
struct btrfs_backref_edge {
|
|
|
|
/*
|
|
|
|
* list[LOWER] is linked to btrfs_backref_node::upper of lower level
|
|
|
|
* node, and list[UPPER] is linked to btrfs_backref_node::lower of
|
|
|
|
* upper level node.
|
|
|
|
*
|
|
|
|
* Also, build_backref_tree() uses list[UPPER] for pending edges, before
|
|
|
|
* linking list[UPPER] to its upper level nodes.
|
|
|
|
*/
|
|
|
|
struct list_head list[2];
|
|
|
|
|
|
|
|
/* Two related nodes */
|
|
|
|
struct btrfs_backref_node *node[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct btrfs_backref_cache {
|
|
|
|
/* Red black tree of all backref nodes in the cache */
|
|
|
|
struct rb_root rb_root;
|
|
|
|
/* For passing backref nodes to btrfs_reloc_cow_block */
|
|
|
|
struct btrfs_backref_node *path[BTRFS_MAX_LEVEL];
|
|
|
|
/*
|
|
|
|
* List of blocks that have been COWed but some block pointers in upper
|
|
|
|
* level blocks may not reflect the new location
|
|
|
|
*/
|
|
|
|
struct list_head pending[BTRFS_MAX_LEVEL];
|
|
|
|
/* List of backref nodes with no child node */
|
|
|
|
struct list_head leaves;
|
|
|
|
/* List of blocks that have been COWed in current transaction */
|
|
|
|
struct list_head changed;
|
|
|
|
/* List of detached backref node. */
|
|
|
|
struct list_head detached;
|
|
|
|
|
|
|
|
u64 last_trans;
|
|
|
|
|
|
|
|
int nr_nodes;
|
|
|
|
int nr_edges;
|
|
|
|
|
|
|
|
/* List of unchecked backref edges during backref cache build */
|
|
|
|
struct list_head pending_edge;
|
|
|
|
|
|
|
|
/* List of useless backref nodes during backref cache build */
|
|
|
|
struct list_head useless_node;
|
|
|
|
|
|
|
|
struct btrfs_fs_info *fs_info;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Whether this cache is for relocation
|
|
|
|
*
|
|
|
|
* Reloction backref cache require more info for reloc root compared
|
|
|
|
* to generic backref cache.
|
|
|
|
*/
|
|
|
|
unsigned int is_reloc;
|
|
|
|
};
|
|
|
|
|
2020-03-03 13:14:41 +08:00
|
|
|
void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
|
|
|
|
struct btrfs_backref_cache *cache, int is_reloc);
|
2020-03-03 13:21:30 +08:00
|
|
|
struct btrfs_backref_node *btrfs_backref_alloc_node(
|
|
|
|
struct btrfs_backref_cache *cache, u64 bytenr, int level);
|
2020-03-03 13:22:57 +08:00
|
|
|
struct btrfs_backref_edge *btrfs_backref_alloc_edge(
|
|
|
|
struct btrfs_backref_cache *cache);
|
2020-03-03 13:14:41 +08:00
|
|
|
|
2011-06-14 01:52:59 +08:00
|
|
|
#endif
|