Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/fs/btrfs/delayed-ref.h
26278 views
1
/* SPDX-License-Identifier: GPL-2.0 */
2
/*
3
* Copyright (C) 2008 Oracle. All rights reserved.
4
*/
5
6
#ifndef BTRFS_DELAYED_REF_H
7
#define BTRFS_DELAYED_REF_H
8
9
#include <linux/types.h>
10
#include <linux/refcount.h>
11
#include <linux/list.h>
12
#include <linux/rbtree.h>
13
#include <linux/mutex.h>
14
#include <linux/spinlock.h>
15
#include <linux/slab.h>
16
#include <uapi/linux/btrfs_tree.h>
17
#include "fs.h"
18
#include "messages.h"
19
20
struct btrfs_trans_handle;
21
struct btrfs_fs_info;
22
23
/* these are the possible values of struct btrfs_delayed_ref_node->action */
24
enum btrfs_delayed_ref_action {
25
/* Add one backref to the tree */
26
BTRFS_ADD_DELAYED_REF = 1,
27
/* Delete one backref from the tree */
28
BTRFS_DROP_DELAYED_REF,
29
/* Record a full extent allocation */
30
BTRFS_ADD_DELAYED_EXTENT,
31
/* Not changing ref count on head ref */
32
BTRFS_UPDATE_DELAYED_HEAD,
33
} __packed;
34
35
struct btrfs_data_ref {
36
/* For EXTENT_DATA_REF */
37
38
/* Inode which refers to this data extent */
39
u64 objectid;
40
41
/*
42
* file_offset - extent_offset
43
*
44
* file_offset is the key.offset of the EXTENT_DATA key.
45
* extent_offset is btrfs_file_extent_offset() of the EXTENT_DATA data.
46
*/
47
u64 offset;
48
};
49
50
struct btrfs_tree_ref {
51
/*
52
* Level of this tree block.
53
*
54
* Shared for skinny (TREE_BLOCK_REF) and normal tree ref.
55
*/
56
int level;
57
58
/* For non-skinny metadata, no special member needed */
59
};
60
61
struct btrfs_delayed_ref_node {
62
struct rb_node ref_node;
63
/*
64
* If action is BTRFS_ADD_DELAYED_REF, also link this node to
65
* ref_head->ref_add_list, then we do not need to iterate the
66
* refs rbtree in the corresponding delayed ref head
67
* (struct btrfs_delayed_ref_head::ref_tree).
68
*/
69
struct list_head add_list;
70
71
/* the starting bytenr of the extent */
72
u64 bytenr;
73
74
/* the size of the extent */
75
u64 num_bytes;
76
77
/* seq number to keep track of insertion order */
78
u64 seq;
79
80
/* The ref_root for this ref */
81
u64 ref_root;
82
83
/*
84
* The parent for this ref, if this isn't set the ref_root is the
85
* reference owner.
86
*/
87
u64 parent;
88
89
/* ref count on this data structure */
90
refcount_t refs;
91
92
/*
93
* how many refs is this entry adding or deleting. For
94
* head refs, this may be a negative number because it is keeping
95
* track of the total mods done to the reference count.
96
* For individual refs, this will always be a positive number
97
*
98
* It may be more than one, since it is possible for a single
99
* parent to have more than one ref on an extent
100
*/
101
int ref_mod;
102
103
unsigned int action:8;
104
unsigned int type:8;
105
106
union {
107
struct btrfs_tree_ref tree_ref;
108
struct btrfs_data_ref data_ref;
109
};
110
};
111
112
struct btrfs_delayed_extent_op {
113
struct btrfs_disk_key key;
114
bool update_key;
115
bool update_flags;
116
u64 flags_to_set;
117
};
118
119
/*
120
* the head refs are used to hold a lock on a given extent, which allows us
121
* to make sure that only one process is running the delayed refs
122
* at a time for a single extent. They also store the sum of all the
123
* reference count modifications we've queued up.
124
*/
125
struct btrfs_delayed_ref_head {
126
u64 bytenr;
127
u64 num_bytes;
128
/*
129
* the mutex is held while running the refs, and it is also
130
* held when checking the sum of reference modifications.
131
*/
132
struct mutex mutex;
133
134
refcount_t refs;
135
136
/* Protects 'ref_tree' and 'ref_add_list'. */
137
spinlock_t lock;
138
struct rb_root_cached ref_tree;
139
/* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */
140
struct list_head ref_add_list;
141
142
struct btrfs_delayed_extent_op *extent_op;
143
144
/*
145
* This is used to track the final ref_mod from all the refs associated
146
* with this head ref, this is not adjusted as delayed refs are run,
147
* this is meant to track if we need to do the csum accounting or not.
148
*/
149
int total_ref_mod;
150
151
/*
152
* This is the current outstanding mod references for this bytenr. This
153
* is used with lookup_extent_info to get an accurate reference count
154
* for a bytenr, so it is adjusted as delayed refs are run so that any
155
* on disk reference count + ref_mod is accurate.
156
*/
157
int ref_mod;
158
159
/*
160
* The root that triggered the allocation when must_insert_reserved is
161
* set to true.
162
*/
163
u64 owning_root;
164
165
/*
166
* Track reserved bytes when setting must_insert_reserved. On success
167
* or cleanup, we will need to free the reservation.
168
*/
169
u64 reserved_bytes;
170
171
/* Tree block level, for metadata only. */
172
u8 level;
173
174
/*
175
* when a new extent is allocated, it is just reserved in memory
176
* The actual extent isn't inserted into the extent allocation tree
177
* until the delayed ref is processed. must_insert_reserved is
178
* used to flag a delayed ref so the accounting can be updated
179
* when a full insert is done.
180
*
181
* It is possible the extent will be freed before it is ever
182
* inserted into the extent allocation tree. In this case
183
* we need to update the in ram accounting to properly reflect
184
* the free has happened.
185
*/
186
bool must_insert_reserved;
187
188
bool is_data;
189
bool is_system;
190
bool processing;
191
/*
192
* Indicate if it's currently in the data structure that tracks head
193
* refs (struct btrfs_delayed_ref_root::head_refs).
194
*/
195
bool tracked;
196
};
197
198
enum btrfs_delayed_ref_flags {
199
/* Indicate that we are flushing delayed refs for the commit */
200
BTRFS_DELAYED_REFS_FLUSHING,
201
};
202
203
struct btrfs_delayed_ref_root {
204
/*
205
* Track head references.
206
* The keys correspond to the logical address of the extent ("bytenr")
207
* right shifted by fs_info->sectorsize_bits. This is both to get a more
208
* dense index space (optimizes xarray structure) and because indexes in
209
* xarrays are of "unsigned long" type, meaning they are 32 bits wide on
210
* 32 bits platforms, limiting the extent range to 4G which is too low
211
* and makes it unusable (truncated index values) on 32 bits platforms.
212
* Protected by the spinlock 'lock' defined below.
213
*/
214
struct xarray head_refs;
215
216
/*
217
* Track dirty extent records.
218
* The keys correspond to the logical address of the extent ("bytenr")
219
* right shifted by fs_info->sectorsize_bits, for same reasons as above.
220
*/
221
struct xarray dirty_extents;
222
223
/*
224
* Protects the xarray head_refs, its entries and the following fields:
225
* num_heads, num_heads_ready, pending_csums and run_delayed_start.
226
*/
227
spinlock_t lock;
228
229
/* Total number of head refs, protected by the spinlock 'lock'. */
230
unsigned long num_heads;
231
232
/*
233
* Total number of head refs ready for processing, protected by the
234
* spinlock 'lock'.
235
*/
236
unsigned long num_heads_ready;
237
238
/*
239
* Track space reserved for deleting csums of data extents.
240
* Protected by the spinlock 'lock'.
241
*/
242
u64 pending_csums;
243
244
unsigned long flags;
245
246
/*
247
* Track from which bytenr to start searching ref heads.
248
* Protected by the spinlock 'lock'.
249
*/
250
u64 run_delayed_start;
251
252
/*
253
* To make qgroup to skip given root.
254
* This is for snapshot, as btrfs_qgroup_inherit() will manually
255
* modify counters for snapshot and its source, so we should skip
256
* the snapshot in new_root/old_roots or it will get calculated twice
257
*/
258
u64 qgroup_to_skip;
259
};
260
261
enum btrfs_ref_type {
262
BTRFS_REF_NOT_SET,
263
BTRFS_REF_DATA,
264
BTRFS_REF_METADATA,
265
} __packed;
266
267
struct btrfs_ref {
268
enum btrfs_ref_type type;
269
enum btrfs_delayed_ref_action action;
270
271
/*
272
* Whether this extent should go through qgroup record.
273
*
274
* Normally false, but for certain cases like delayed subtree scan,
275
* setting this flag can hugely reduce qgroup overhead.
276
*/
277
bool skip_qgroup;
278
279
#ifdef CONFIG_BTRFS_FS_REF_VERIFY
280
/* Through which root is this modification. */
281
u64 real_root;
282
#endif
283
u64 bytenr;
284
u64 num_bytes;
285
u64 owning_root;
286
287
/*
288
* The root that owns the reference for this reference, this will be set
289
* or ->parent will be set, depending on what type of reference this is.
290
*/
291
u64 ref_root;
292
293
/* Bytenr of the parent tree block */
294
u64 parent;
295
union {
296
struct btrfs_data_ref data_ref;
297
struct btrfs_tree_ref tree_ref;
298
};
299
};
300
301
extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
302
extern struct kmem_cache *btrfs_delayed_ref_node_cachep;
303
extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
304
305
int __init btrfs_delayed_ref_init(void);
306
void __cold btrfs_delayed_ref_exit(void);
307
308
static inline u64 btrfs_calc_delayed_ref_bytes(const struct btrfs_fs_info *fs_info,
309
int num_delayed_refs)
310
{
311
u64 num_bytes;
312
313
num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_delayed_refs);
314
315
/*
316
* We have to check the mount option here because we could be enabling
317
* the free space tree for the first time and don't have the compat_ro
318
* option set yet.
319
*
320
* We need extra reservations if we have the free space tree because
321
* we'll have to modify that tree as well.
322
*/
323
if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
324
num_bytes *= 2;
325
326
return num_bytes;
327
}
328
329
static inline u64 btrfs_calc_delayed_ref_csum_bytes(const struct btrfs_fs_info *fs_info,
330
int num_csum_items)
331
{
332
/*
333
* Deleting csum items does not result in new nodes/leaves and does not
334
* require changing the free space tree, only the csum tree, so this is
335
* all we need.
336
*/
337
return btrfs_calc_metadata_size(fs_info, num_csum_items);
338
}
339
340
void btrfs_init_tree_ref(struct btrfs_ref *generic_ref, int level, u64 mod_root,
341
bool skip_qgroup);
342
void btrfs_init_data_ref(struct btrfs_ref *generic_ref, u64 ino, u64 offset,
343
u64 mod_root, bool skip_qgroup);
344
345
static inline struct btrfs_delayed_extent_op *
346
btrfs_alloc_delayed_extent_op(void)
347
{
348
return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);
349
}
350
351
static inline void
352
btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)
353
{
354
if (op)
355
kmem_cache_free(btrfs_delayed_extent_op_cachep, op);
356
}
357
358
void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref);
359
360
static inline u64 btrfs_ref_head_to_space_flags(
361
struct btrfs_delayed_ref_head *head_ref)
362
{
363
if (head_ref->is_data)
364
return BTRFS_BLOCK_GROUP_DATA;
365
else if (head_ref->is_system)
366
return BTRFS_BLOCK_GROUP_SYSTEM;
367
return BTRFS_BLOCK_GROUP_METADATA;
368
}
369
370
static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head)
371
{
372
if (refcount_dec_and_test(&head->refs))
373
kmem_cache_free(btrfs_delayed_ref_head_cachep, head);
374
}
375
376
int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
377
struct btrfs_ref *generic_ref,
378
struct btrfs_delayed_extent_op *extent_op);
379
int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
380
struct btrfs_ref *generic_ref,
381
u64 reserved);
382
int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
383
u64 bytenr, u64 num_bytes, u8 level,
384
struct btrfs_delayed_extent_op *extent_op);
385
void btrfs_merge_delayed_refs(struct btrfs_fs_info *fs_info,
386
struct btrfs_delayed_ref_root *delayed_refs,
387
struct btrfs_delayed_ref_head *head);
388
389
struct btrfs_delayed_ref_head *
390
btrfs_find_delayed_ref_head(const struct btrfs_fs_info *fs_info,
391
struct btrfs_delayed_ref_root *delayed_refs,
392
u64 bytenr);
393
static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
394
{
395
mutex_unlock(&head->mutex);
396
}
397
void btrfs_delete_ref_head(const struct btrfs_fs_info *fs_info,
398
struct btrfs_delayed_ref_root *delayed_refs,
399
struct btrfs_delayed_ref_head *head);
400
401
struct btrfs_delayed_ref_head *btrfs_select_ref_head(
402
const struct btrfs_fs_info *fs_info,
403
struct btrfs_delayed_ref_root *delayed_refs);
404
void btrfs_unselect_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
405
struct btrfs_delayed_ref_head *head);
406
struct btrfs_delayed_ref_node *btrfs_select_delayed_ref(struct btrfs_delayed_ref_head *head);
407
408
int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);
409
410
void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr_refs, int nr_csums);
411
void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);
412
void btrfs_inc_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info);
413
void btrfs_dec_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info);
414
void btrfs_inc_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info);
415
void btrfs_dec_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info);
416
int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
417
enum btrfs_reserve_flush_enum flush);
418
bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);
419
bool btrfs_find_delayed_tree_ref(struct btrfs_delayed_ref_head *head,
420
u64 root, u64 parent);
421
void btrfs_destroy_delayed_refs(struct btrfs_transaction *trans);
422
423
static inline u64 btrfs_delayed_ref_owner(const struct btrfs_delayed_ref_node *node)
424
{
425
if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
426
node->type == BTRFS_SHARED_DATA_REF_KEY)
427
return node->data_ref.objectid;
428
return node->tree_ref.level;
429
}
430
431
static inline u64 btrfs_delayed_ref_offset(const struct btrfs_delayed_ref_node *node)
432
{
433
if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
434
node->type == BTRFS_SHARED_DATA_REF_KEY)
435
return node->data_ref.offset;
436
return 0;
437
}
438
439
static inline u8 btrfs_ref_type(const struct btrfs_ref *ref)
440
{
441
ASSERT(ref->type == BTRFS_REF_DATA || ref->type == BTRFS_REF_METADATA);
442
443
if (ref->type == BTRFS_REF_DATA) {
444
if (ref->parent)
445
return BTRFS_SHARED_DATA_REF_KEY;
446
else
447
return BTRFS_EXTENT_DATA_REF_KEY;
448
} else {
449
if (ref->parent)
450
return BTRFS_SHARED_BLOCK_REF_KEY;
451
else
452
return BTRFS_TREE_BLOCK_REF_KEY;
453
}
454
455
return 0;
456
}
457
458
#endif
459
460