summaryrefslogtreecommitdiffstats
path: root/lib/metadata/snapshot_manip.c
blob: 6140a61fc0222b4a5ec85dcb0b97d7111c16500a (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) 2002 Sistina Software (UK) Limited.
 *
 * This file is released under the LGPL.
 */

#include "lib.h"
#include "metadata.h"
#include "toolcontext.h"

int lv_is_origin(const struct logical_volume *lv)
{
	struct list *slh;
	struct snapshot *s;

	list_iterate(slh, &lv->vg->snapshots) {
		s = list_item(slh, struct snapshot_list)->snapshot;
		if (s->origin == lv)
			return 1;
	}

	return 0;
}

int lv_is_cow(const struct logical_volume *lv)
{
	struct list *slh;
	struct snapshot *s;

	list_iterate(slh, &lv->vg->snapshots) {
		s = list_item(slh, struct snapshot_list)->snapshot;
		if (s->cow == lv)
			return 1;
	}

	return 0;
}

struct snapshot *find_origin(const struct logical_volume *lv)
{
	struct list *slh;
	struct snapshot *s;

	list_iterate(slh, &lv->vg->snapshots) {
		s = list_item(slh, struct snapshot_list)->snapshot;
		if (s->origin == lv)
			return s;
	}

	return NULL;
}

struct snapshot *find_cow(const struct logical_volume *lv)
{
	struct list *slh;
	struct snapshot *s;

	list_iterate(slh, &lv->vg->snapshots) {
		s = list_item(slh, struct snapshot_list)->snapshot;
		if (s->cow == lv)
			return s;
	}

	return NULL;
}

struct list *find_snapshots(const struct logical_volume *lv)
{
	struct list *slh;
	struct list *snaplist;
	struct snapshot *s;
	struct snapshot_list *newsl;
	struct pool *mem = lv->vg->cmd->mem;

	if (!(snaplist = pool_alloc(mem, sizeof(*snaplist)))) {
		log_error("snapshot name list allocation failed");
		return NULL;
	}

	list_init(snaplist);

	list_iterate(slh, &lv->vg->snapshots) {
		s = list_item(slh, struct snapshot_list)->snapshot;
		if (!(s->origin == lv))
			continue;
		if (!(newsl = pool_alloc(mem, sizeof(*newsl)))) {
			log_error("snapshot_list structure allocation failed");
			pool_free(mem, snaplist);
			return NULL;
		}
		newsl->snapshot = s;
		list_add(snaplist, &newsl->list);
	}

	return snaplist;
}

int vg_add_snapshot(struct logical_volume *origin,
		    struct logical_volume *cow,
		    int persistent, struct id *id, uint32_t chunk_size)
{
	struct snapshot *s;
	struct snapshot_list *sl;
	struct pool *mem = origin->vg->cmd->mem;

	/*
	 * Is the cow device already being used ?
	 */
	if (lv_is_cow(cow)) {
		log_err("'%s' is already in use as a snapshot.", cow->name);
		return 0;
	}

	if (!(s = pool_alloc(mem, sizeof(*s)))) {
		stack;
		return 0;
	}

	s->persistent = persistent;
	s->chunk_size = chunk_size;
	s->origin = origin;
	s->cow = cow;

	if (id)
		s->id = *id;
	else if (!id_create(&s->id)) {
		log_error("Snapshot UUID creation failed");
		return 0;
	}

	if (!(sl = pool_alloc(mem, sizeof(*sl)))) {
		stack;
		pool_free(mem, s);
		return 0;
	}

	cow->status &= ~VISIBLE_LV;
	sl->snapshot = s;
	list_add(&origin->vg->snapshots, &sl->list);
	origin->vg->snapshot_count++;

	return 1;
}

int vg_remove_snapshot(struct volume_group *vg, struct logical_volume *cow)
{
	struct list *slh;
	struct snapshot_list *sl;

	list_iterate(slh, &vg->snapshots) {
		sl = list_item(slh, struct snapshot_list);

		if (sl->snapshot->cow == cow) {
			list_del(slh);
			vg->snapshot_count--;
			return 1;
		}
	}

	/* fail */
	log_err("Asked to remove an unknown snapshot.");
	return 0;
}