mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-12-18 12:26:54 +07:00
1418bae1c2
[BUG]
Btrfs/139 will fail with a high probability if the testing machine (VM)
has only 2G RAM.
Resulting the final write success while it should fail due to EDQUOT,
and the fs will have quota exceeding the limit by 16K.
The simplified reproducer will be: (needs a 2G ram VM)
$ mkfs.btrfs -f $dev
$ mount $dev $mnt
$ btrfs subv create $mnt/subv
$ btrfs quota enable $mnt
$ btrfs quota rescan -w $mnt
$ btrfs qgroup limit -e 1G $mnt/subv
$ for i in $(seq -w 1 8); do
xfs_io -f -c "pwrite 0 128M" $mnt/subv/file_$i > /dev/null
echo "file $i written" > /dev/kmsg
done
$ sync
$ btrfs qgroup show -pcre --raw $mnt
The last pwrite will not trigger EDQUOT and final 'qgroup show' will
show something like:
qgroupid rfer excl max_rfer max_excl parent child
-------- ---- ---- -------- -------- ------ -----
0/5 16384 16384 none none --- ---
0/256 1073758208 1073758208 none 1073741824 --- ---
And 1073758208 is larger than
> 1073741824.
[CAUSE]
It's a bug in btrfs qgroup data reserved space management.
For quota limit, we must ensure that:
reserved (data + metadata) + rfer/excl <= limit
Since rfer/excl is only updated at transaction commmit time, reserved
space needs to be taken special care.
One important part of reserved space is data, and for a new data extent
written to disk, we still need to take the reserved space until
rfer/excl numbers get updated.
Originally when an ordered extent finishes, we migrate the reserved
qgroup data space from extent_io tree to delayed ref head of the data
extent, expecting delayed ref will only be cleaned up at commit
transaction time.
However for small RAM machine, due to memory pressure dirty pages can be
flushed back to disk without committing a transaction.
The related events will be something like:
file 1 written
btrfs_finish_ordered_io: ino=258 ordered offset=0 len=54947840
btrfs_finish_ordered_io: ino=258 ordered offset=54947840 len=5636096
btrfs_finish_ordered_io: ino=258 ordered offset=61153280 len=57344
btrfs_finish_ordered_io: ino=258 ordered offset=61210624 len=8192
btrfs_finish_ordered_io: ino=258 ordered offset=60583936 len=569344
cleanup_ref_head: num_bytes=54947840
cleanup_ref_head: num_bytes=5636096
cleanup_ref_head: num_bytes=569344
cleanup_ref_head: num_bytes=57344
cleanup_ref_head: num_bytes=8192
^^^^^^^^^^^^^^^^ This will free qgroup data reserved space
file 2 written
...
file 8 written
cleanup_ref_head: num_bytes=8192
...
btrfs_commit_transaction <<< the only transaction committed during
the test
When file 2 is written, we have already freed 128M reserved qgroup data
space for ino 258. Thus later write won't trigger EDQUOT.
This allows us to write more data beyond qgroup limit.
In my 2G ram VM, it could reach about 1.2G before hitting EDQUOT.
[FIX]
By moving reserved qgroup data space from btrfs_delayed_ref_head to
btrfs_qgroup_extent_record, we can ensure that reserved qgroup data
space won't be freed half way before commit transaction, thus fix the
problem.
Fixes: f64d5ca868
("btrfs: delayed_ref: Add new function to record reserved space into delayed ref")
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
277 lines
7.9 KiB
C
277 lines
7.9 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
/*
|
|
* Copyright (C) 2008 Oracle. All rights reserved.
|
|
*/
|
|
|
|
#ifndef BTRFS_DELAYED_REF_H
|
|
#define BTRFS_DELAYED_REF_H
|
|
|
|
#include <linux/refcount.h>
|
|
|
|
/* these are the possible values of struct btrfs_delayed_ref_node->action */
|
|
#define BTRFS_ADD_DELAYED_REF 1 /* add one backref to the tree */
|
|
#define BTRFS_DROP_DELAYED_REF 2 /* delete one backref from the tree */
|
|
#define BTRFS_ADD_DELAYED_EXTENT 3 /* record a full extent allocation */
|
|
#define BTRFS_UPDATE_DELAYED_HEAD 4 /* not changing ref count on head ref */
|
|
|
|
struct btrfs_delayed_ref_node {
|
|
struct rb_node ref_node;
|
|
/*
|
|
* If action is BTRFS_ADD_DELAYED_REF, also link this node to
|
|
* ref_head->ref_add_list, then we do not need to iterate the
|
|
* whole ref_head->ref_list to find BTRFS_ADD_DELAYED_REF nodes.
|
|
*/
|
|
struct list_head add_list;
|
|
|
|
/* the starting bytenr of the extent */
|
|
u64 bytenr;
|
|
|
|
/* the size of the extent */
|
|
u64 num_bytes;
|
|
|
|
/* seq number to keep track of insertion order */
|
|
u64 seq;
|
|
|
|
/* ref count on this data structure */
|
|
refcount_t refs;
|
|
|
|
/*
|
|
* how many refs is this entry adding or deleting. For
|
|
* head refs, this may be a negative number because it is keeping
|
|
* track of the total mods done to the reference count.
|
|
* For individual refs, this will always be a positive number
|
|
*
|
|
* It may be more than one, since it is possible for a single
|
|
* parent to have more than one ref on an extent
|
|
*/
|
|
int ref_mod;
|
|
|
|
unsigned int action:8;
|
|
unsigned int type:8;
|
|
/* is this node still in the rbtree? */
|
|
unsigned int is_head:1;
|
|
unsigned int in_tree:1;
|
|
};
|
|
|
|
struct btrfs_delayed_extent_op {
|
|
struct btrfs_disk_key key;
|
|
u8 level;
|
|
bool update_key;
|
|
bool update_flags;
|
|
bool is_data;
|
|
u64 flags_to_set;
|
|
};
|
|
|
|
/*
|
|
* the head refs are used to hold a lock on a given extent, which allows us
|
|
* to make sure that only one process is running the delayed refs
|
|
* at a time for a single extent. They also store the sum of all the
|
|
* reference count modifications we've queued up.
|
|
*/
|
|
struct btrfs_delayed_ref_head {
|
|
u64 bytenr;
|
|
u64 num_bytes;
|
|
refcount_t refs;
|
|
/*
|
|
* the mutex is held while running the refs, and it is also
|
|
* held when checking the sum of reference modifications.
|
|
*/
|
|
struct mutex mutex;
|
|
|
|
spinlock_t lock;
|
|
struct rb_root_cached ref_tree;
|
|
/* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */
|
|
struct list_head ref_add_list;
|
|
|
|
struct rb_node href_node;
|
|
|
|
struct btrfs_delayed_extent_op *extent_op;
|
|
|
|
/*
|
|
* This is used to track the final ref_mod from all the refs associated
|
|
* with this head ref, this is not adjusted as delayed refs are run,
|
|
* this is meant to track if we need to do the csum accounting or not.
|
|
*/
|
|
int total_ref_mod;
|
|
|
|
/*
|
|
* This is the current outstanding mod references for this bytenr. This
|
|
* is used with lookup_extent_info to get an accurate reference count
|
|
* for a bytenr, so it is adjusted as delayed refs are run so that any
|
|
* on disk reference count + ref_mod is accurate.
|
|
*/
|
|
int ref_mod;
|
|
|
|
/*
|
|
* when a new extent is allocated, it is just reserved in memory
|
|
* The actual extent isn't inserted into the extent allocation tree
|
|
* until the delayed ref is processed. must_insert_reserved is
|
|
* used to flag a delayed ref so the accounting can be updated
|
|
* when a full insert is done.
|
|
*
|
|
* It is possible the extent will be freed before it is ever
|
|
* inserted into the extent allocation tree. In this case
|
|
* we need to update the in ram accounting to properly reflect
|
|
* the free has happened.
|
|
*/
|
|
unsigned int must_insert_reserved:1;
|
|
unsigned int is_data:1;
|
|
unsigned int is_system:1;
|
|
unsigned int processing:1;
|
|
};
|
|
|
|
struct btrfs_delayed_tree_ref {
|
|
struct btrfs_delayed_ref_node node;
|
|
u64 root;
|
|
u64 parent;
|
|
int level;
|
|
};
|
|
|
|
struct btrfs_delayed_data_ref {
|
|
struct btrfs_delayed_ref_node node;
|
|
u64 root;
|
|
u64 parent;
|
|
u64 objectid;
|
|
u64 offset;
|
|
};
|
|
|
|
struct btrfs_delayed_ref_root {
|
|
/* head ref rbtree */
|
|
struct rb_root_cached href_root;
|
|
|
|
/* dirty extent records */
|
|
struct rb_root dirty_extent_root;
|
|
|
|
/* this spin lock protects the rbtree and the entries inside */
|
|
spinlock_t lock;
|
|
|
|
/* how many delayed ref updates we've queued, used by the
|
|
* throttling code
|
|
*/
|
|
atomic_t num_entries;
|
|
|
|
/* total number of head nodes in tree */
|
|
unsigned long num_heads;
|
|
|
|
/* total number of head nodes ready for processing */
|
|
unsigned long num_heads_ready;
|
|
|
|
u64 pending_csums;
|
|
|
|
/*
|
|
* set when the tree is flushing before a transaction commit,
|
|
* used by the throttling code to decide if new updates need
|
|
* to be run right away
|
|
*/
|
|
int flushing;
|
|
|
|
u64 run_delayed_start;
|
|
|
|
/*
|
|
* To make qgroup to skip given root.
|
|
* This is for snapshot, as btrfs_qgroup_inherit() will manually
|
|
* modify counters for snapshot and its source, so we should skip
|
|
* the snapshot in new_root/old_roots or it will get calculated twice
|
|
*/
|
|
u64 qgroup_to_skip;
|
|
};
|
|
|
|
extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
|
|
extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
|
|
extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
|
|
extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
|
|
|
|
int __init btrfs_delayed_ref_init(void);
|
|
void __cold btrfs_delayed_ref_exit(void);
|
|
|
|
static inline struct btrfs_delayed_extent_op *
|
|
btrfs_alloc_delayed_extent_op(void)
|
|
{
|
|
return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);
|
|
}
|
|
|
|
static inline void
|
|
btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)
|
|
{
|
|
if (op)
|
|
kmem_cache_free(btrfs_delayed_extent_op_cachep, op);
|
|
}
|
|
|
|
static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
|
|
{
|
|
WARN_ON(refcount_read(&ref->refs) == 0);
|
|
if (refcount_dec_and_test(&ref->refs)) {
|
|
WARN_ON(ref->in_tree);
|
|
switch (ref->type) {
|
|
case BTRFS_TREE_BLOCK_REF_KEY:
|
|
case BTRFS_SHARED_BLOCK_REF_KEY:
|
|
kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
|
|
break;
|
|
case BTRFS_EXTENT_DATA_REF_KEY:
|
|
case BTRFS_SHARED_DATA_REF_KEY:
|
|
kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
|
|
break;
|
|
default:
|
|
BUG();
|
|
}
|
|
}
|
|
}
|
|
|
|
static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head)
|
|
{
|
|
if (refcount_dec_and_test(&head->refs))
|
|
kmem_cache_free(btrfs_delayed_ref_head_cachep, head);
|
|
}
|
|
|
|
int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
|
|
u64 bytenr, u64 num_bytes, u64 parent,
|
|
u64 ref_root, int level, int action,
|
|
struct btrfs_delayed_extent_op *extent_op,
|
|
int *old_ref_mod, int *new_ref_mod);
|
|
int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
|
|
u64 bytenr, u64 num_bytes,
|
|
u64 parent, u64 ref_root,
|
|
u64 owner, u64 offset, u64 reserved, int action,
|
|
int *old_ref_mod, int *new_ref_mod);
|
|
int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
|
|
struct btrfs_trans_handle *trans,
|
|
u64 bytenr, u64 num_bytes,
|
|
struct btrfs_delayed_extent_op *extent_op);
|
|
void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
|
|
struct btrfs_delayed_ref_root *delayed_refs,
|
|
struct btrfs_delayed_ref_head *head);
|
|
|
|
struct btrfs_delayed_ref_head *
|
|
btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
|
|
u64 bytenr);
|
|
int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
|
|
struct btrfs_delayed_ref_head *head);
|
|
static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
|
|
{
|
|
mutex_unlock(&head->mutex);
|
|
}
|
|
void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
|
|
struct btrfs_delayed_ref_head *head);
|
|
|
|
struct btrfs_delayed_ref_head *btrfs_select_ref_head(
|
|
struct btrfs_delayed_ref_root *delayed_refs);
|
|
|
|
int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);
|
|
|
|
/*
|
|
* helper functions to cast a node into its container
|
|
*/
|
|
static inline struct btrfs_delayed_tree_ref *
|
|
btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
|
|
{
|
|
return container_of(node, struct btrfs_delayed_tree_ref, node);
|
|
}
|
|
|
|
static inline struct btrfs_delayed_data_ref *
|
|
btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
|
|
{
|
|
return container_of(node, struct btrfs_delayed_data_ref, node);
|
|
}
|
|
|
|
#endif
|