Blame view

fs/btrfs/backref.h 10.4 KB
9888c3402   David Sterba   btrfs: replace GP...
1
  /* SPDX-License-Identifier: GPL-2.0 */
a542ad1ba   Jan Schmidt   btrfs: added help...
2
3
  /*
   * Copyright (C) 2011 STRATO.  All rights reserved.
a542ad1ba   Jan Schmidt   btrfs: added help...
4
   */
9888c3402   David Sterba   btrfs: replace GP...
5
6
  #ifndef BTRFS_BACKREF_H
  #define BTRFS_BACKREF_H
a542ad1ba   Jan Schmidt   btrfs: added help...
7

55e301fd5   Filipe Brandenburger   Btrfs: move fs/bt...
8
  #include <linux/btrfs.h>
8da6d5815   Jan Schmidt   Btrfs: added btrf...
9
  #include "ulist.h"
741188d3a   Qu Wenruo   btrfs: backref: r...
10
  #include "disk-io.h"
91cb916ca   Alexander Block   Btrfs: make iref_...
11
  #include "extent_io.h"
a542ad1ba   Jan Schmidt   btrfs: added help...
12
13
14
15
16
17
18
19
20
  
  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);
a542ad1ba   Jan Schmidt   btrfs: added help...
21

a542ad1ba   Jan Schmidt   btrfs: added help...
22
  int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
69917e431   Liu Bo   Btrfs: fix a bug ...
23
24
  			struct btrfs_path *path, struct btrfs_key *found_key,
  			u64 *flags);
a542ad1ba   Jan Schmidt   btrfs: added help...
25
26
  
  int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
6eda71d0c   Liu Bo   Btrfs: fix scrub_...
27
28
  			    struct btrfs_key *key, struct btrfs_extent_item *ei,
  			    u32 item_size, u64 *out_root, u8 *out_level);
a542ad1ba   Jan Schmidt   btrfs: added help...
29
30
  
  int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
a542ad1ba   Jan Schmidt   btrfs: added help...
31
  				u64 extent_item_objectid,
7a3ae2f8c   Jan Schmidt   Btrfs: fix regres...
32
  				u64 extent_offset, int search_commit_root,
c995ab3cd   Zygo Blaxell   btrfs: add a flag...
33
34
  				iterate_extent_inodes_t *iterate, void *ctx,
  				bool ignore_offset);
a542ad1ba   Jan Schmidt   btrfs: added help...
35
36
37
  
  int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
  				struct btrfs_path *path,
c995ab3cd   Zygo Blaxell   btrfs: add a flag...
38
39
  				iterate_extent_inodes_t *iterate, void *ctx,
  				bool ignore_offset);
a542ad1ba   Jan Schmidt   btrfs: added help...
40
41
  
  int paths_from_inode(u64 inum, struct inode_fs_paths *ipath);
19b546d7a   Qu Wenruo   btrfs: relocation...
42
43
44
45
  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);
8da6d5815   Jan Schmidt   Btrfs: added btrf...
46
  int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
fcebe4562   Josef Bacik   Btrfs: rework qgr...
47
  			 struct btrfs_fs_info *fs_info, u64 bytenr,
c995ab3cd   Zygo Blaxell   btrfs: add a flag...
48
  			 u64 time_seq, struct ulist **roots, bool ignore_offset);
96b5bd777   Jan Schmidt   Btrfs: extended i...
49
50
51
52
  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);
8da6d5815   Jan Schmidt   Btrfs: added btrf...
53

a542ad1ba   Jan Schmidt   btrfs: added help...
54
55
56
57
  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);
f186373fe   Mark Fasheh   btrfs: extended i...
58
59
60
61
  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);
5911c8fe0   David Sterba   btrfs: fiemap: pr...
62
63
  int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr,
  		struct ulist *roots, struct ulist *tmp_ulist);
f186373fe   Mark Fasheh   btrfs: extended i...
64

b9e9a6cbc   Wang Shilong   Btrfs: allocate p...
65
  int __init btrfs_prelim_ref_init(void);
e67c718b5   David Sterba   btrfs: add more _...
66
  void __cold btrfs_prelim_ref_exit(void);
00142756e   Jeff Mahoney   btrfs: backref, a...
67
68
69
70
71
72
73
74
75
76
77
  
  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;
  };
a37f232b7   Qu Wenruo   btrfs: backref: i...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  /*
   * 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);
  }
c39c2ddc6   Qu Wenruo   btrfs: backref: i...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  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;
  }
a37f232b7   Qu Wenruo   btrfs: backref: i...
125
  int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr);
c39c2ddc6   Qu Wenruo   btrfs: backref: i...
126
127
128
129
130
131
132
133
134
135
  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;
  }
a37f232b7   Qu Wenruo   btrfs: backref: i...
136
137
138
139
140
141
142
143
144
  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));
  }
705354414   Qu Wenruo   btrfs: backref: m...
145
146
147
148
149
150
151
152
153
154
155
  /*
   * 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 {
e9a28dc52   Qu Wenruo   btrfs: rename tre...
156
157
158
159
  	struct {
  		struct rb_node rb_node;
  		u64 bytenr;
  	}; /* Use rb_simple_node for search/insert */
705354414   Qu Wenruo   btrfs: backref: m...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  
  	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;
92a7cc425   Qu Wenruo   btrfs: rename BTR...
178
  	/* Is the block in a non-shareable tree */
705354414   Qu Wenruo   btrfs: backref: m...
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  	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;
  };
584fb1218   Qu Wenruo   btrfs: backref: r...
262
263
  void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
  			      struct btrfs_backref_cache *cache, int is_reloc);
b1818dab9   Qu Wenruo   btrfs: backref: r...
264
265
  struct btrfs_backref_node *btrfs_backref_alloc_node(
  		struct btrfs_backref_cache *cache, u64 bytenr, int level);
47254d07f   Qu Wenruo   btrfs: backref: r...
266
267
  struct btrfs_backref_edge *btrfs_backref_alloc_edge(
  		struct btrfs_backref_cache *cache);
584fb1218   Qu Wenruo   btrfs: backref: r...
268

f39911e55   Qu Wenruo   btrfs: backref: r...
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
  #define		LINK_LOWER	(1 << 0)
  #define		LINK_UPPER	(1 << 1)
  static inline void btrfs_backref_link_edge(struct btrfs_backref_edge *edge,
  					   struct btrfs_backref_node *lower,
  					   struct btrfs_backref_node *upper,
  					   int link_which)
  {
  	ASSERT(upper && lower && upper->level == lower->level + 1);
  	edge->node[LOWER] = lower;
  	edge->node[UPPER] = upper;
  	if (link_which & LINK_LOWER)
  		list_add_tail(&edge->list[LOWER], &lower->upper);
  	if (link_which & LINK_UPPER)
  		list_add_tail(&edge->list[UPPER], &upper->lower);
  }
741188d3a   Qu Wenruo   btrfs: backref: r...
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  static inline void btrfs_backref_free_node(struct btrfs_backref_cache *cache,
  					   struct btrfs_backref_node *node)
  {
  	if (node) {
  		cache->nr_nodes--;
  		btrfs_put_root(node->root);
  		kfree(node);
  	}
  }
  
  static inline void btrfs_backref_free_edge(struct btrfs_backref_cache *cache,
  					   struct btrfs_backref_edge *edge)
  {
  	if (edge) {
  		cache->nr_edges--;
  		kfree(edge);
  	}
  }
b0fe7078d   Qu Wenruo   btrfs: backref: r...
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
  static inline void btrfs_backref_unlock_node_buffer(
  		struct btrfs_backref_node *node)
  {
  	if (node->locked) {
  		btrfs_tree_unlock(node->eb);
  		node->locked = 0;
  	}
  }
  
  static inline void btrfs_backref_drop_node_buffer(
  		struct btrfs_backref_node *node)
  {
  	if (node->eb) {
  		btrfs_backref_unlock_node_buffer(node);
  		free_extent_buffer(node->eb);
  		node->eb = NULL;
  	}
  }
  
  /*
   * Drop the backref node from cache without cleaning up its children
   * edges.
   *
   * This can only be called on node without parent edges.
   * The children edges are still kept as is.
   */
  static inline void btrfs_backref_drop_node(struct btrfs_backref_cache *tree,
  					   struct btrfs_backref_node *node)
  {
  	BUG_ON(!list_empty(&node->upper));
  
  	btrfs_backref_drop_node_buffer(node);
  	list_del(&node->list);
  	list_del(&node->lower);
  	if (!RB_EMPTY_NODE(&node->rb_node))
  		rb_erase(&node->rb_node, &tree->rb_root);
  	btrfs_backref_free_node(tree, node);
  }
023acb07b   Qu Wenruo   btrfs: backref: r...
340
341
  void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache,
  				struct btrfs_backref_node *node);
13fe1bdb2   Qu Wenruo   btrfs: backref: r...
342
  void btrfs_backref_release_cache(struct btrfs_backref_cache *cache);
982c92cbd   Qu Wenruo   btrfs: backref: r...
343
344
345
346
347
348
349
  static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info,
  				       u64 bytenr, int errno)
  {
  	btrfs_panic(fs_info, errno,
  		    "Inconsistency in backref cache found at offset %llu",
  		    bytenr);
  }
1b60d2ec9   Qu Wenruo   btrfs: backref: r...
350
351
352
353
354
  int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache,
  				struct btrfs_path *path,
  				struct btrfs_backref_iter *iter,
  				struct btrfs_key *node_key,
  				struct btrfs_backref_node *cur);
fc997ed05   Qu Wenruo   btrfs: backref: r...
355
356
  int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
  				     struct btrfs_backref_node *start);
1b23ea180   Qu Wenruo   btrfs: reloc: mov...
357
358
  void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache,
  				 struct btrfs_backref_node *node);
a542ad1ba   Jan Schmidt   btrfs: added help...
359
  #endif