summaryrefslogtreecommitdiff
path: root/drivers/md/bcache/snapshot.h
blob: 168f52271f050bb57b04d74ba745f01eceff05b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef _BCACHE_SNAPSHOT_H
#define _BCACHE_SNAPSHOT_H

#include <linux/rhashtable.h>

/* Reference to an open snapshot: */
struct snapshot {
	struct list_head	list;
	atomic_long_t		count;
	u32			id;
	struct dentry		*root;
	char			name[];
};

struct snapshot_cached_entry {
	struct rhash_head	hash;
	u32			id;
	u32			parent;
	u64			start;
	u64			end;
};

static const struct rhashtable_params bch_snapshot_cache_params = {
	.head_offset	= offsetof(struct snapshot_cached_entry, hash),
	.key_offset	= offsetof(struct snapshot_cached_entry, id),
	.key_len	= sizeof(u32),
};

struct snapshot_cached_entry *__bch_snapshot_entry(struct cache_set *, u32);

static inline struct snapshot_cached_entry *
bch_snapshot_entry(struct cache_set *c, u32 id)
{
	return rhashtable_lookup_fast(&c->snapshot_cache, &id,
				      bch_snapshot_cache_params)
		?: __bch_snapshot_entry(c, id);
}

static inline bool bch_snapshot_is_descendant(struct cache_set *c,
					      struct snapshot *snapshot,
					      u32 ancestor_id)
{
	struct snapshot_cached_entry *child, *ancestor;
	u32 child_id = snapshot->id;
	bool ret;

	if (likely(child_id == ancestor_id))
		return true;

	mutex_lock(&c->snapshot_lock);
	rcu_read_lock();
	child = bch_snapshot_entry(c, child_id);
	if (unlikely(!child)) {
	}

	ancestor = bch_snapshot_entry(c, ancestor_id);
	if (unlikely(!ancestor)) {
	}

	ret = child->start >= ancestor->start &&
	      child->end   <= ancestor->end;
	rcu_read_unlock();
	mutex_unlock(&c->snapshot_lock);

	return ret;
}

int bch_snapshot_create(struct cache_set *, struct snapshot *, const char *);
int bch_snapshot_create_initial(struct cache_set *);

void bch_snapshot_close(struct cache_set *, struct snapshot *);
struct snapshot *bch_snapshot_open(struct cache_set *, struct qstr);

extern const struct btree_keys_ops bch_snapshot_name_ops;
extern const struct bkey_ops bch_bkey_snapshot_name_ops;
extern const struct btree_keys_ops bch_snapshot_tree_ops;
extern const struct bkey_ops bch_bkey_snapshot_tree_ops;

void bch_snapshots_exit(struct cache_set *);
int bch_snapshots_init(struct cache_set *);

#endif /* _BCACHE_SNAPSHOT_H */