2022-10-22 19:59:53 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2019-03-08 00:46:10 +00:00
|
|
|
#ifndef _BCACHEFS_BTREE_KEY_CACHE_H
|
|
|
|
#define _BCACHEFS_BTREE_KEY_CACHE_H
|
|
|
|
|
2020-11-20 00:54:40 +00:00
|
|
|
static inline size_t bch2_nr_btree_keys_need_flush(struct bch_fs *c)
|
|
|
|
{
|
2021-03-25 03:37:33 +00:00
|
|
|
size_t nr_dirty = atomic_long_read(&c->btree_key_cache.nr_dirty);
|
|
|
|
size_t nr_keys = atomic_long_read(&c->btree_key_cache.nr_keys);
|
2020-12-06 02:03:57 +00:00
|
|
|
size_t max_dirty = 1024 + nr_keys / 2;
|
2020-11-20 00:54:40 +00:00
|
|
|
|
|
|
|
return max_t(ssize_t, 0, nr_dirty - max_dirty);
|
|
|
|
}
|
|
|
|
|
2020-11-20 02:40:03 +00:00
|
|
|
static inline bool bch2_btree_key_cache_must_wait(struct bch_fs *c)
|
|
|
|
{
|
2021-03-25 03:37:33 +00:00
|
|
|
size_t nr_dirty = atomic_long_read(&c->btree_key_cache.nr_dirty);
|
|
|
|
size_t nr_keys = atomic_long_read(&c->btree_key_cache.nr_keys);
|
2020-11-20 02:40:03 +00:00
|
|
|
size_t max_dirty = 4096 + (nr_keys * 3) / 4;
|
|
|
|
|
2021-12-28 04:10:06 +00:00
|
|
|
return nr_dirty > max_dirty;
|
2020-11-20 02:40:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 01:44:55 +00:00
|
|
|
int bch2_btree_key_cache_journal_flush(struct journal *,
|
|
|
|
struct journal_entry_pin *, u64);
|
|
|
|
|
2019-09-22 23:10:21 +00:00
|
|
|
struct bkey_cached *
|
|
|
|
bch2_btree_key_cache_find(struct bch_fs *, enum btree_id, struct bpos);
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
int bch2_btree_path_traverse_cached(struct btree_trans *, struct btree_path *,
|
|
|
|
unsigned);
|
2019-03-08 00:46:10 +00:00
|
|
|
|
2023-02-09 18:22:12 +00:00
|
|
|
bool bch2_btree_insert_key_cached(struct btree_trans *, unsigned,
|
bcachefs: don't bump key cache journal seq on nojournal commits
fstest generic/388 occasionally reproduces corruptions where an
inode has extents beyond i_size. This is a deliberate crash and
recovery test, and the post crash+recovery characteristics are
usually the same: the inode exists on disk in an early (i.e. just
allocated) state based on the journal sequence number associated
with the inode. Subsequent inode updates exist in the journal at
higher sequence numbers, but the inode hadn't been written back
before the associated crash and the post-crash recovery processes a
set of journal sequence numbers that doesn't include updates to the
inode. In fact, the sequence with the most recent inode key update
always happens to be the sequence just before the front of the
journal processed by recovery.
This last bit is a significant hint that the problem relates to an
on-disk journal update of the front of the journal. The root cause
of this problem is basically that the inode is updated (multiple
times) in-core and in the key cache, each time bumping the key cache
sequence number used to control the cache flush. The cache flush
skips one or more times, bumping the associated key cache journal
pin to the key cache seq value. This has a side effect of holding
the inode in memory a bit longer than normal, which helps exacerbate
this problem, but is also unsafe in certain cases where the key
cache seq may have been updated by a transaction commit that didn't
journal the associated key.
For example, consider an inode that has been allocated, updated
several times in the key cache, journaled, but not yet written back.
At this stage, everything should be consistent if the fs happens to
crash because the latest update has been journal. Now consider a key
update via bch2_extent_update_i_size_sectors() that uses the
BTREE_UPDATE_NOJOURNAL flag. While this update may not change inode
state, it can have the side effect of bumping ck->seq in
bch2_btree_insert_key_cached(). In turn, if a subsequent key cache
flush skips due to seq not matching the former, the ck->journal pin
is updated to ck->seq even though the most recent key update was not
journaled. If this pin happens to reside at the front (tail) of the
journal, this means a subsequent journal write can update last_seq
to a value beyond that which includes the most recent update to the
inode. If this occurs and the fs happens to crash before the inode
happens to flush, recovery will see the latest last_seq, fail to
recover the inode and leave the inode in the inconsistent state
described above.
To avoid this problem, skip the key cache seq update on NOJOURNAL
commits, except on initial pin add. Pass the insert entry directly
to bch2_btree_insert_key_cached() to make the associated flag
available and be consistent with btree_insert_key_leaf().
Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2023-03-02 14:03:37 +00:00
|
|
|
struct btree_insert_entry *);
|
2022-01-12 06:14:47 +00:00
|
|
|
void bch2_btree_key_cache_drop(struct btree_trans *,
|
|
|
|
struct btree_path *);
|
2019-03-08 00:46:10 +00:00
|
|
|
|
|
|
|
void bch2_fs_btree_key_cache_exit(struct btree_key_cache *);
|
|
|
|
void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *);
|
|
|
|
int bch2_fs_btree_key_cache_init(struct btree_key_cache *);
|
|
|
|
|
2020-06-15 23:53:46 +00:00
|
|
|
void bch2_btree_key_cache_to_text(struct printbuf *, struct btree_key_cache *);
|
|
|
|
|
2020-11-18 19:09:33 +00:00
|
|
|
void bch2_btree_key_cache_exit(void);
|
|
|
|
int __init bch2_btree_key_cache_init(void);
|
|
|
|
|
2019-03-08 00:46:10 +00:00
|
|
|
#endif /* _BCACHEFS_BTREE_KEY_CACHE_H */
|