2019-07-10 23:12:15 +03:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2017-10-06 01:41:44 +03:00
|
|
|
#ifndef _BCACHEFS_BTREE_IO_H
|
|
|
|
#define _BCACHEFS_BTREE_IO_H
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2020-03-30 21:34:20 +03:00
|
|
|
#include "bkey_methods.h"
|
2018-03-01 01:56:28 +03:00
|
|
|
#include "bset.h"
|
2019-01-14 04:36:38 +03:00
|
|
|
#include "btree_locking.h"
|
2017-04-24 08:56:57 +03:00
|
|
|
#include "extents.h"
|
2018-01-11 14:41:59 +03:00
|
|
|
#include "io_types.h"
|
2017-04-24 08:56:57 +03:00
|
|
|
|
2017-03-11 00:40:01 +03:00
|
|
|
struct bch_fs;
|
2017-01-08 12:13:18 +03:00
|
|
|
struct btree_write;
|
|
|
|
struct btree;
|
|
|
|
struct btree_iter;
|
|
|
|
|
2017-04-24 08:56:57 +03:00
|
|
|
struct btree_read_bio {
|
|
|
|
struct bch_fs *c;
|
2017-06-14 04:06:05 +03:00
|
|
|
u64 start_time;
|
2018-05-04 21:04:31 +03:00
|
|
|
unsigned have_ioref:1;
|
2018-11-04 03:11:29 +03:00
|
|
|
struct extent_ptr_decoded pick;
|
2017-04-24 08:56:57 +03:00
|
|
|
struct work_struct work;
|
|
|
|
struct bio bio;
|
|
|
|
};
|
|
|
|
|
2018-01-11 14:41:59 +03:00
|
|
|
struct btree_write_bio {
|
|
|
|
void *data;
|
|
|
|
struct work_struct work;
|
|
|
|
struct bch_write_bio wbio;
|
|
|
|
};
|
|
|
|
|
2017-01-08 12:13:18 +03:00
|
|
|
static inline void btree_node_io_unlock(struct btree *b)
|
|
|
|
{
|
|
|
|
EBUG_ON(!btree_node_write_in_flight(b));
|
|
|
|
clear_btree_node_write_in_flight(b);
|
|
|
|
wake_up_bit(&b->flags, BTREE_NODE_write_in_flight);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void btree_node_io_lock(struct btree *b)
|
|
|
|
{
|
|
|
|
wait_on_bit_lock_io(&b->flags, BTREE_NODE_write_in_flight,
|
|
|
|
TASK_UNINTERRUPTIBLE);
|
|
|
|
}
|
|
|
|
|
2017-04-10 07:11:10 +03:00
|
|
|
static inline void btree_node_wait_on_io(struct btree *b)
|
|
|
|
{
|
|
|
|
wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight,
|
|
|
|
TASK_UNINTERRUPTIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btree_node_may_write(struct btree *b)
|
|
|
|
{
|
2017-04-15 07:38:49 +03:00
|
|
|
return list_empty_careful(&b->write_blocked) &&
|
2019-01-14 04:36:38 +03:00
|
|
|
(!b->written || !b->will_make_reachable);
|
2017-04-10 07:11:10 +03:00
|
|
|
}
|
|
|
|
|
2017-01-08 12:13:18 +03:00
|
|
|
enum compact_mode {
|
|
|
|
COMPACT_LAZY,
|
2019-12-16 22:42:09 +03:00
|
|
|
COMPACT_ALL,
|
2017-01-08 12:13:18 +03:00
|
|
|
};
|
|
|
|
|
2019-12-16 22:42:09 +03:00
|
|
|
bool bch2_compact_whiteouts(struct bch_fs *, struct btree *,
|
|
|
|
enum compact_mode);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2019-12-16 22:42:09 +03:00
|
|
|
static inline bool should_compact_bset_lazy(struct btree *b,
|
|
|
|
struct bset_tree *t)
|
2018-02-05 08:26:03 +03:00
|
|
|
{
|
2019-11-04 20:53:59 +03:00
|
|
|
unsigned total_u64s = bset_u64s(t);
|
2019-12-16 22:42:09 +03:00
|
|
|
unsigned dead_u64s = bset_dead_u64s(b, t);
|
2018-02-05 08:26:03 +03:00
|
|
|
|
2019-11-04 20:53:59 +03:00
|
|
|
return dead_u64s > 64 && dead_u64s * 3 > total_u64s;
|
2018-02-05 08:26:03 +03:00
|
|
|
}
|
|
|
|
|
2017-03-20 02:56:34 +03:00
|
|
|
static inline bool bch2_maybe_compact_whiteouts(struct bch_fs *c, struct btree *b)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
|
|
|
struct bset_tree *t;
|
|
|
|
|
2018-02-05 08:26:03 +03:00
|
|
|
for_each_bset(b, t)
|
|
|
|
if (should_compact_bset_lazy(b, t))
|
2019-12-16 22:42:09 +03:00
|
|
|
return bch2_compact_whiteouts(c, b, COMPACT_LAZY);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-20 02:56:34 +03:00
|
|
|
void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-03-20 02:56:34 +03:00
|
|
|
void bch2_btree_build_aux_trees(struct btree *);
|
|
|
|
void bch2_btree_init_next(struct bch_fs *, struct btree *,
|
2017-01-08 12:13:18 +03:00
|
|
|
struct btree_iter *);
|
|
|
|
|
2017-11-11 10:18:52 +03:00
|
|
|
int bch2_btree_node_read_done(struct bch_fs *, struct btree *, bool);
|
2017-04-24 08:56:57 +03:00
|
|
|
void bch2_btree_node_read(struct bch_fs *, struct btree *, bool);
|
2017-03-20 02:56:34 +03:00
|
|
|
int bch2_btree_root_read(struct bch_fs *, enum btree_id,
|
2017-06-14 04:06:05 +03:00
|
|
|
const struct bkey_i *, unsigned);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-03-20 02:56:34 +03:00
|
|
|
void bch2_btree_complete_write(struct bch_fs *, struct btree *,
|
2017-01-08 12:13:18 +03:00
|
|
|
struct btree_write *);
|
2017-05-13 05:45:15 +03:00
|
|
|
void bch2_btree_write_error_work(struct work_struct *);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-03-20 02:56:34 +03:00
|
|
|
void __bch2_btree_node_write(struct bch_fs *, struct btree *,
|
2018-01-21 20:16:32 +03:00
|
|
|
enum six_lock_type);
|
2017-03-20 02:56:34 +03:00
|
|
|
bool bch2_btree_post_write_cleanup(struct bch_fs *, struct btree *);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-03-20 02:56:34 +03:00
|
|
|
void bch2_btree_node_write(struct bch_fs *, struct btree *,
|
2018-01-21 20:16:32 +03:00
|
|
|
enum six_lock_type);
|
|
|
|
|
2020-03-25 22:56:38 +03:00
|
|
|
static inline void btree_node_write_if_need(struct bch_fs *c, struct btree *b,
|
|
|
|
enum six_lock_type lock_held)
|
2018-01-21 20:16:32 +03:00
|
|
|
{
|
2019-01-14 04:36:38 +03:00
|
|
|
while (b->written &&
|
|
|
|
btree_node_need_write(b) &&
|
|
|
|
btree_node_may_write(b)) {
|
|
|
|
if (!btree_node_write_in_flight(b)) {
|
2020-03-25 22:56:38 +03:00
|
|
|
bch2_btree_node_write(c, b, lock_held);
|
2019-01-14 04:36:38 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-14 02:31:45 +03:00
|
|
|
six_unlock_type(&b->c.lock, lock_held);
|
2019-01-14 04:36:38 +03:00
|
|
|
btree_node_wait_on_io(b);
|
2020-03-25 22:56:38 +03:00
|
|
|
btree_node_lock_type(c, b, lock_held);
|
2019-01-14 04:36:38 +03:00
|
|
|
}
|
2018-01-21 20:16:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define bch2_btree_node_write_cond(_c, _b, cond) \
|
2017-04-10 07:11:10 +03:00
|
|
|
do { \
|
2019-01-14 04:36:38 +03:00
|
|
|
unsigned long old, new, v = READ_ONCE((_b)->flags); \
|
|
|
|
\
|
|
|
|
do { \
|
|
|
|
old = new = v; \
|
2017-04-10 07:11:10 +03:00
|
|
|
\
|
2019-01-14 04:36:38 +03:00
|
|
|
if (!(old & (1 << BTREE_NODE_dirty)) || !(cond)) \
|
2017-04-10 07:11:10 +03:00
|
|
|
break; \
|
|
|
|
\
|
2019-01-14 04:36:38 +03:00
|
|
|
new |= (1 << BTREE_NODE_need_write); \
|
|
|
|
} while ((v = cmpxchg(&(_b)->flags, old, new)) != old); \
|
|
|
|
\
|
2020-03-25 22:56:38 +03:00
|
|
|
btree_node_write_if_need(_c, _b, SIX_LOCK_read); \
|
2017-04-10 07:11:10 +03:00
|
|
|
} while (0)
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2018-02-05 08:26:03 +03:00
|
|
|
void bch2_btree_flush_all_reads(struct bch_fs *);
|
|
|
|
void bch2_btree_flush_all_writes(struct bch_fs *);
|
2017-04-11 08:19:15 +03:00
|
|
|
void bch2_btree_verify_flushed(struct bch_fs *);
|
2018-02-05 08:26:03 +03:00
|
|
|
ssize_t bch2_dirty_btree_nodes_print(struct bch_fs *, char *);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2020-03-30 21:34:20 +03:00
|
|
|
static inline void compat_bformat(unsigned level, enum btree_id btree_id,
|
|
|
|
unsigned version, unsigned big_endian,
|
|
|
|
int write, struct bkey_format *f)
|
|
|
|
{
|
|
|
|
if (version < bcachefs_metadata_version_inode_btree_change &&
|
|
|
|
btree_id == BTREE_ID_INODES) {
|
|
|
|
swap(f->bits_per_field[BKEY_FIELD_INODE],
|
|
|
|
f->bits_per_field[BKEY_FIELD_OFFSET]);
|
|
|
|
swap(f->field_offset[BKEY_FIELD_INODE],
|
|
|
|
f->field_offset[BKEY_FIELD_OFFSET]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void compat_bpos(unsigned level, enum btree_id btree_id,
|
|
|
|
unsigned version, unsigned big_endian,
|
|
|
|
int write, struct bpos *p)
|
|
|
|
{
|
|
|
|
if (big_endian != CPU_BIG_ENDIAN)
|
|
|
|
bch2_bpos_swab(p);
|
|
|
|
|
|
|
|
if (version < bcachefs_metadata_version_inode_btree_change &&
|
|
|
|
btree_id == BTREE_ID_INODES)
|
|
|
|
swap(p->inode, p->offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void compat_btree_node(unsigned level, enum btree_id btree_id,
|
|
|
|
unsigned version, unsigned big_endian,
|
|
|
|
int write,
|
|
|
|
struct btree_node *bn)
|
|
|
|
{
|
|
|
|
if (version < bcachefs_metadata_version_inode_btree_change &&
|
|
|
|
btree_node_type_is_extents(btree_id) &&
|
|
|
|
bkey_cmp(bn->min_key, POS_MIN) &&
|
|
|
|
write)
|
|
|
|
bn->min_key = bkey_predecessor(bn->min_key);
|
|
|
|
|
|
|
|
compat_bpos(level, btree_id, version, big_endian, write, &bn->min_key);
|
|
|
|
compat_bpos(level, btree_id, version, big_endian, write, &bn->max_key);
|
|
|
|
|
|
|
|
if (version < bcachefs_metadata_version_inode_btree_change &&
|
|
|
|
btree_node_type_is_extents(btree_id) &&
|
|
|
|
bkey_cmp(bn->min_key, POS_MIN) &&
|
|
|
|
!write)
|
|
|
|
bn->min_key = bkey_successor(bn->min_key);
|
|
|
|
}
|
|
|
|
|
2017-10-06 01:41:44 +03:00
|
|
|
#endif /* _BCACHEFS_BTREE_IO_H */
|