blob: 0a1f296b1f264987c9170522a53a83ed3401465e [file] [log] [blame]
David Sterba9888c342018-04-03 19:16:55 +02001/* SPDX-License-Identifier: GPL-2.0 */
Jan Schmidta542ad12011-06-13 19:52:59 +02002/*
3 * Copyright (C) 2011 STRATO. All rights reserved.
Jan Schmidta542ad12011-06-13 19:52:59 +02004 */
5
David Sterba9888c342018-04-03 19:16:55 +02006#ifndef BTRFS_BACKREF_H
7#define BTRFS_BACKREF_H
Jan Schmidta542ad12011-06-13 19:52:59 +02008
Filipe Brandenburger55e301f2013-01-29 06:04:50 +00009#include <linux/btrfs.h>
Jan Schmidt8da6d582011-11-23 18:55:04 +010010#include "ulist.h"
Qu Wenruo741188d2020-03-03 13:26:12 +080011#include "disk-io.h"
Alexander Block91cb9162012-06-03 14:23:23 +020012#include "extent_io.h"
Jan Schmidta542ad12011-06-13 19:52:59 +020013
14struct inode_fs_paths {
15 struct btrfs_path *btrfs_path;
16 struct btrfs_root *fs_root;
17 struct btrfs_data_container *fspath;
18};
19
20typedef int (iterate_extent_inodes_t)(u64 inum, u64 offset, u64 root,
21 void *ctx);
Jan Schmidta542ad12011-06-13 19:52:59 +020022
Jan Schmidta542ad12011-06-13 19:52:59 +020023int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
Liu Bo69917e42012-09-07 20:01:28 -060024 struct btrfs_path *path, struct btrfs_key *found_key,
25 u64 *flags);
Jan Schmidta542ad12011-06-13 19:52:59 +020026
27int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
Liu Bo6eda71d2014-06-09 10:54:07 +080028 struct btrfs_key *key, struct btrfs_extent_item *ei,
29 u32 item_size, u64 *out_root, u8 *out_level);
Jan Schmidta542ad12011-06-13 19:52:59 +020030
31int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
Jan Schmidta542ad12011-06-13 19:52:59 +020032 u64 extent_item_objectid,
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +010033 u64 extent_offset, int search_commit_root,
Zygo Blaxellc995ab32017-09-22 13:58:45 -040034 iterate_extent_inodes_t *iterate, void *ctx,
35 bool ignore_offset);
Jan Schmidta542ad12011-06-13 19:52:59 +020036
37int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
38 struct btrfs_path *path,
Zygo Blaxellc995ab32017-09-22 13:58:45 -040039 iterate_extent_inodes_t *iterate, void *ctx,
40 bool ignore_offset);
Jan Schmidta542ad12011-06-13 19:52:59 +020041
42int paths_from_inode(u64 inum, struct inode_fs_paths *ipath);
43
Qu Wenruo19b546d2020-03-10 16:14:15 +080044int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
45 struct btrfs_fs_info *fs_info, u64 bytenr,
46 u64 time_seq, struct ulist **leafs,
47 const u64 *extent_item_pos, bool ignore_offset);
Jan Schmidt8da6d582011-11-23 18:55:04 +010048int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
Josef Bacikfcebe452014-05-13 17:30:47 -070049 struct btrfs_fs_info *fs_info, u64 bytenr,
Zygo Blaxellc995ab32017-09-22 13:58:45 -040050 u64 time_seq, struct ulist **roots, bool ignore_offset);
Jan Schmidt96b5bd72012-10-15 08:30:45 +000051char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
52 u32 name_len, unsigned long name_off,
53 struct extent_buffer *eb_in, u64 parent,
54 char *dest, u32 size);
Jan Schmidt8da6d582011-11-23 18:55:04 +010055
Jan Schmidta542ad12011-06-13 19:52:59 +020056struct btrfs_data_container *init_data_container(u32 total_bytes);
57struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
58 struct btrfs_path *path);
59void free_ipath(struct inode_fs_paths *ipath);
60
Mark Fashehf1863732012-08-08 11:32:27 -070061int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
62 u64 start_off, struct btrfs_path *path,
63 struct btrfs_inode_extref **ret_extref,
64 u64 *found_off);
David Sterba5911c8f2019-05-15 15:31:04 +020065int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr,
66 struct ulist *roots, struct ulist *tmp_ulist);
Mark Fashehf1863732012-08-08 11:32:27 -070067
Wang Shilongb9e9a6c2013-08-09 13:25:36 +080068int __init btrfs_prelim_ref_init(void);
David Sterbae67c7182018-02-19 17:24:18 +010069void __cold btrfs_prelim_ref_exit(void);
Jeff Mahoney00142752017-07-12 16:20:08 -060070
71struct prelim_ref {
72 struct rb_node rbnode;
73 u64 root_id;
74 struct btrfs_key key_for_search;
75 int level;
76 int count;
77 struct extent_inode_elem *inode_list;
78 u64 parent;
79 u64 wanted_disk_byte;
80};
81
Qu Wenruoa37f2322020-02-13 14:11:04 +080082/*
83 * Iterate backrefs of one extent.
84 *
85 * Now it only supports iteration of tree block in commit root.
86 */
87struct btrfs_backref_iter {
88 u64 bytenr;
89 struct btrfs_path *path;
90 struct btrfs_fs_info *fs_info;
91 struct btrfs_key cur_key;
92 u32 item_ptr;
93 u32 cur_ptr;
94 u32 end_ptr;
95};
96
97struct btrfs_backref_iter *btrfs_backref_iter_alloc(
98 struct btrfs_fs_info *fs_info, gfp_t gfp_flag);
99
100static inline void btrfs_backref_iter_free(struct btrfs_backref_iter *iter)
101{
102 if (!iter)
103 return;
104 btrfs_free_path(iter->path);
105 kfree(iter);
106}
107
Qu Wenruoc39c2dd2020-02-13 15:04:04 +0800108static inline struct extent_buffer *btrfs_backref_get_eb(
109 struct btrfs_backref_iter *iter)
110{
111 if (!iter)
112 return NULL;
113 return iter->path->nodes[0];
114}
115
116/*
117 * For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data
118 * is btrfs_tree_block_info, without a btrfs_extent_inline_ref header.
119 *
120 * This helper determines if that's the case.
121 */
122static inline bool btrfs_backref_has_tree_block_info(
123 struct btrfs_backref_iter *iter)
124{
125 if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY &&
126 iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item))
127 return true;
128 return false;
129}
130
Qu Wenruoa37f2322020-02-13 14:11:04 +0800131int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr);
132
Qu Wenruoc39c2dd2020-02-13 15:04:04 +0800133int btrfs_backref_iter_next(struct btrfs_backref_iter *iter);
134
135static inline bool btrfs_backref_iter_is_inline_ref(
136 struct btrfs_backref_iter *iter)
137{
138 if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY ||
139 iter->cur_key.type == BTRFS_METADATA_ITEM_KEY)
140 return true;
141 return false;
142}
143
Qu Wenruoa37f2322020-02-13 14:11:04 +0800144static inline void btrfs_backref_iter_release(struct btrfs_backref_iter *iter)
145{
146 iter->bytenr = 0;
147 iter->item_ptr = 0;
148 iter->cur_ptr = 0;
149 iter->end_ptr = 0;
150 btrfs_release_path(iter->path);
151 memset(&iter->cur_key, 0, sizeof(iter->cur_key));
152}
153
Qu Wenruo70535442020-03-23 15:03:56 +0800154/*
155 * Backref cache related structures
156 *
157 * The whole objective of backref_cache is to build a bi-directional map
158 * of tree blocks (represented by backref_node) and all their parents.
159 */
160
161/*
162 * Represent a tree block in the backref cache
163 */
164struct btrfs_backref_node {
Qu Wenruoe9a28dc2020-03-26 14:11:09 +0800165 struct {
166 struct rb_node rb_node;
167 u64 bytenr;
168 }; /* Use rb_simple_node for search/insert */
Qu Wenruo70535442020-03-23 15:03:56 +0800169
170 u64 new_bytenr;
171 /* Objectid of tree block owner, can be not uptodate */
172 u64 owner;
173 /* Link to pending, changed or detached list */
174 struct list_head list;
175
176 /* List of upper level edges, which link this node to its parents */
177 struct list_head upper;
178 /* List of lower level edges, which link this node to its children */
179 struct list_head lower;
180
181 /* NULL if this node is not tree root */
182 struct btrfs_root *root;
183 /* Extent buffer got by COWing the block */
184 struct extent_buffer *eb;
185 /* Level of the tree block */
186 unsigned int level:8;
187 /* Is the block in non-reference counted tree */
188 unsigned int cowonly:1;
189 /* 1 if no child node is in the cache */
190 unsigned int lowest:1;
191 /* Is the extent buffer locked */
192 unsigned int locked:1;
193 /* Has the block been processed */
194 unsigned int processed:1;
195 /* Have backrefs of this block been checked */
196 unsigned int checked:1;
197 /*
198 * 1 if corresponding block has been COWed but some upper level block
199 * pointers may not point to the new location
200 */
201 unsigned int pending:1;
202 /* 1 if the backref node isn't connected to any other backref node */
203 unsigned int detached:1;
204
205 /*
206 * For generic purpose backref cache, where we only care if it's a reloc
207 * root, doesn't care the source subvolid.
208 */
209 unsigned int is_reloc_root:1;
210};
211
212#define LOWER 0
213#define UPPER 1
214
215/*
216 * Represent an edge connecting upper and lower backref nodes.
217 */
218struct btrfs_backref_edge {
219 /*
220 * list[LOWER] is linked to btrfs_backref_node::upper of lower level
221 * node, and list[UPPER] is linked to btrfs_backref_node::lower of
222 * upper level node.
223 *
224 * Also, build_backref_tree() uses list[UPPER] for pending edges, before
225 * linking list[UPPER] to its upper level nodes.
226 */
227 struct list_head list[2];
228
229 /* Two related nodes */
230 struct btrfs_backref_node *node[2];
231};
232
233struct btrfs_backref_cache {
234 /* Red black tree of all backref nodes in the cache */
235 struct rb_root rb_root;
236 /* For passing backref nodes to btrfs_reloc_cow_block */
237 struct btrfs_backref_node *path[BTRFS_MAX_LEVEL];
238 /*
239 * List of blocks that have been COWed but some block pointers in upper
240 * level blocks may not reflect the new location
241 */
242 struct list_head pending[BTRFS_MAX_LEVEL];
243 /* List of backref nodes with no child node */
244 struct list_head leaves;
245 /* List of blocks that have been COWed in current transaction */
246 struct list_head changed;
247 /* List of detached backref node. */
248 struct list_head detached;
249
250 u64 last_trans;
251
252 int nr_nodes;
253 int nr_edges;
254
255 /* List of unchecked backref edges during backref cache build */
256 struct list_head pending_edge;
257
258 /* List of useless backref nodes during backref cache build */
259 struct list_head useless_node;
260
261 struct btrfs_fs_info *fs_info;
262
263 /*
264 * Whether this cache is for relocation
265 *
266 * Reloction backref cache require more info for reloc root compared
267 * to generic backref cache.
268 */
269 unsigned int is_reloc;
270};
271
Qu Wenruo584fb122020-03-03 13:14:41 +0800272void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
273 struct btrfs_backref_cache *cache, int is_reloc);
Qu Wenruob1818da2020-03-03 13:21:30 +0800274struct btrfs_backref_node *btrfs_backref_alloc_node(
275 struct btrfs_backref_cache *cache, u64 bytenr, int level);
Qu Wenruo47254d02020-03-03 13:22:57 +0800276struct btrfs_backref_edge *btrfs_backref_alloc_edge(
277 struct btrfs_backref_cache *cache);
Qu Wenruo584fb122020-03-03 13:14:41 +0800278
Qu Wenruof39911e52020-03-03 13:24:06 +0800279#define LINK_LOWER (1 << 0)
280#define LINK_UPPER (1 << 1)
281static inline void btrfs_backref_link_edge(struct btrfs_backref_edge *edge,
282 struct btrfs_backref_node *lower,
283 struct btrfs_backref_node *upper,
284 int link_which)
285{
286 ASSERT(upper && lower && upper->level == lower->level + 1);
287 edge->node[LOWER] = lower;
288 edge->node[UPPER] = upper;
289 if (link_which & LINK_LOWER)
290 list_add_tail(&edge->list[LOWER], &lower->upper);
291 if (link_which & LINK_UPPER)
292 list_add_tail(&edge->list[UPPER], &upper->lower);
293}
294
Qu Wenruo741188d2020-03-03 13:26:12 +0800295static inline void btrfs_backref_free_node(struct btrfs_backref_cache *cache,
296 struct btrfs_backref_node *node)
297{
298 if (node) {
299 cache->nr_nodes--;
300 btrfs_put_root(node->root);
301 kfree(node);
302 }
303}
304
305static inline void btrfs_backref_free_edge(struct btrfs_backref_cache *cache,
306 struct btrfs_backref_edge *edge)
307{
308 if (edge) {
309 cache->nr_edges--;
310 kfree(edge);
311 }
312}
313
Jan Schmidta542ad12011-06-13 19:52:59 +0200314#endif