2 * Copyright (C) 2003-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include "toolcontext.h"
20 #include "text_export.h"
26 static const char *_snap_name(const struct lv_segment *seg)
28 return seg->segtype->name;
31 static const char *_snap_target_name(const struct lv_segment *seg)
33 if (seg->status & MERGING)
34 return "snapshot-merge";
36 return _snap_name(seg);
39 static int _snap_text_import(struct lv_segment *seg, const struct config_node *sn,
40 struct dm_hash_table *pv_hash __attribute__((unused)))
43 const char *org_name, *cow_name;
44 struct logical_volume *org, *cow;
45 int old_suppress, merge = 0;
47 if (!get_config_uint32(sn, "chunk_size", &chunk_size)) {
48 log_error("Couldn't read chunk size for snapshot.");
52 old_suppress = log_suppress(1);
54 if ((cow_name = find_config_str(sn, "merging_store", NULL))) {
55 if (find_config_str(sn, "cow_store", NULL)) {
56 log_suppress(old_suppress);
57 log_error("Both snapshot cow and merging storage were specified.");
62 else if (!(cow_name = find_config_str(sn, "cow_store", NULL))) {
63 log_suppress(old_suppress);
64 log_error("Snapshot cow storage not specified.");
68 if (!(org_name = find_config_str(sn, "origin", NULL))) {
69 log_suppress(old_suppress);
70 log_error("Snapshot origin not specified.");
74 log_suppress(old_suppress);
76 if (!(cow = find_lv(seg->lv->vg, cow_name))) {
77 log_error("Unknown logical volume specified for "
78 "snapshot cow store.");
82 if (!(org = find_lv(seg->lv->vg, org_name))) {
83 log_error("Unknown logical volume specified for "
88 init_snapshot_seg(seg, org, cow, chunk_size, merge);
93 static int _snap_text_export(const struct lv_segment *seg, struct formatter *f)
95 outf(f, "chunk_size = %u", seg->chunk_size);
96 outf(f, "origin = \"%s\"", seg->origin->name);
97 if (!(seg->status & MERGING))
98 outf(f, "cow_store = \"%s\"", seg->cow->name);
100 outf(f, "merging_store = \"%s\"", seg->cow->name);
105 static int _snap_target_status_compatible(const char *type)
107 return (strcmp(type, "snapshot-merge") == 0);
110 #ifdef DEVMAPPER_SUPPORT
111 static int _snap_target_percent(void **target_state __attribute__((unused)),
113 struct dm_pool *mem __attribute__((unused)),
114 struct cmd_context *cmd __attribute__((unused)),
115 struct lv_segment *seg __attribute__((unused)),
116 char *params, uint64_t *total_numerator,
117 uint64_t *total_denominator)
119 uint64_t total_sectors, sectors_allocated, metadata_sectors;
123 * snapshot target's percent format:
124 * <= 1.7.0: <sectors_allocated>/<total_sectors>
125 * >= 1.8.0: <sectors_allocated>/<total_sectors> <metadata_sectors>
127 r = sscanf(params, "%" PRIu64 "/%" PRIu64 " %" PRIu64,
128 §ors_allocated, &total_sectors, &metadata_sectors);
129 if (r == 2 || r == 3) {
130 *total_numerator += sectors_allocated;
131 *total_denominator += total_sectors;
132 if (r == 3 && sectors_allocated == metadata_sectors)
133 *percent = PERCENT_0;
134 else if (sectors_allocated == total_sectors)
135 *percent = PERCENT_100;
137 *percent = make_percent(*total_numerator, *total_denominator);
138 } else if (!strcmp(params, "Invalid") ||
139 !strcmp(params, "Merge failed"))
140 *percent = PERCENT_INVALID;
147 static int _snap_target_present(struct cmd_context *cmd,
148 const struct lv_segment *seg,
149 unsigned *attributes __attribute__((unused)))
151 static int _snap_checked = 0;
152 static int _snap_merge_checked = 0;
153 static int _snap_present = 0;
154 static int _snap_merge_present = 0;
156 if (!_snap_checked) {
157 _snap_present = target_present(cmd, "snapshot", 1) &&
158 target_present(cmd, "snapshot-origin", 0);
162 if (!_snap_merge_checked && seg && (seg->status & MERGING)) {
163 _snap_merge_present = target_present(cmd, "snapshot-merge", 0);
164 _snap_merge_checked = 1;
165 return _snap_present && _snap_merge_present;
168 return _snap_present;
173 static const char *_get_snapshot_dso_path(struct cmd_context *cmd)
175 return get_monitor_dso_path(cmd, find_config_tree_str(cmd, "dmeventd/snapshot_library",
176 DEFAULT_DMEVENTD_SNAPSHOT_LIB));
179 /* FIXME Cache this */
180 static int _target_registered(struct lv_segment *seg, int *pending)
182 return target_registered_with_dmeventd(seg->lv->vg->cmd, _get_snapshot_dso_path(seg->lv->vg->cmd),
186 /* FIXME This gets run while suspended and performs banned operations. */
187 static int _target_set_events(struct lv_segment *seg, int evmask, int set)
189 /* FIXME Make timeout (10) configurable */
190 return target_register_events(seg->lv->vg->cmd, _get_snapshot_dso_path(seg->lv->vg->cmd),
191 seg->cow, evmask, set, 10);
194 static int _target_register_events(struct lv_segment *seg,
197 return _target_set_events(seg, events, 1);
200 static int _target_unregister_events(struct lv_segment *seg,
203 return _target_set_events(seg, events, 0);
206 #endif /* DMEVENTD */
209 static int _snap_modules_needed(struct dm_pool *mem,
210 const struct lv_segment *seg __attribute__((unused)),
211 struct dm_list *modules)
213 if (!str_list_add(mem, modules, "snapshot")) {
214 log_error("snapshot string list allocation failed");
221 static void _snap_destroy(struct segment_type *segtype)
226 static struct segtype_handler _snapshot_ops = {
228 .target_name = _snap_target_name,
229 .text_import = _snap_text_import,
230 .text_export = _snap_text_export,
231 .target_status_compatible = _snap_target_status_compatible,
232 #ifdef DEVMAPPER_SUPPORT
233 .target_percent = _snap_target_percent,
234 .target_present = _snap_target_present,
236 .target_monitored = _target_registered,
237 .target_monitor_events = _target_register_events,
238 .target_unmonitor_events = _target_unregister_events,
241 .modules_needed = _snap_modules_needed,
242 .destroy = _snap_destroy,
245 #ifdef SNAPSHOT_INTERNAL
246 struct segment_type *init_snapshot_segtype(struct cmd_context *cmd)
248 struct segment_type *init_segtype(struct cmd_context *cmd);
249 struct segment_type *init_segtype(struct cmd_context *cmd)
252 struct segment_type *segtype = dm_malloc(sizeof(*segtype));
258 segtype->ops = &_snapshot_ops;
259 segtype->name = "snapshot";
260 segtype->private = NULL;
261 segtype->flags = SEG_SNAPSHOT;
264 if (_get_snapshot_dso_path(cmd))
265 segtype->flags |= SEG_MONITORED;
267 log_very_verbose("Initialised segtype: %s", segtype->name);