Tizen 2.1 base
[external/device-mapper.git] / lib / snapshot / snapshot.c
1 /*
2  * Copyright (C) 2003-2004 Sistina Software, Inc. All rights reserved.
3  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
4  *
5  * This file is part of LVM2.
6  *
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.
10  *
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
14  */
15
16 #include "lib.h"
17 #include "toolcontext.h"
18 #include "metadata.h"
19 #include "segtype.h"
20 #include "text_export.h"
21 #include "config.h"
22 #include "activate.h"
23 #include "str_list.h"
24 #include "defaults.h"
25
26 static const char *_snap_name(const struct lv_segment *seg)
27 {
28         return seg->segtype->name;
29 }
30
31 static const char *_snap_target_name(const struct lv_segment *seg)
32 {
33         if (seg->status & MERGING)
34                 return "snapshot-merge";
35
36         return _snap_name(seg);
37 }
38
39 static int _snap_text_import(struct lv_segment *seg, const struct config_node *sn,
40                         struct dm_hash_table *pv_hash __attribute__((unused)))
41 {
42         uint32_t chunk_size;
43         const char *org_name, *cow_name;
44         struct logical_volume *org, *cow;
45         int old_suppress, merge = 0;
46
47         if (!get_config_uint32(sn, "chunk_size", &chunk_size)) {
48                 log_error("Couldn't read chunk size for snapshot.");
49                 return 0;
50         }
51
52         old_suppress = log_suppress(1);
53
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.");
58                         return 0;
59                 }
60                 merge = 1;
61         }
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.");
65                 return 0;
66         }
67
68         if (!(org_name = find_config_str(sn, "origin", NULL))) {
69                 log_suppress(old_suppress);
70                 log_error("Snapshot origin not specified.");
71                 return 0;
72         }
73
74         log_suppress(old_suppress);
75
76         if (!(cow = find_lv(seg->lv->vg, cow_name))) {
77                 log_error("Unknown logical volume specified for "
78                           "snapshot cow store.");
79                 return 0;
80         }
81
82         if (!(org = find_lv(seg->lv->vg, org_name))) {
83                 log_error("Unknown logical volume specified for "
84                           "snapshot origin.");
85                 return 0;
86         }
87
88         init_snapshot_seg(seg, org, cow, chunk_size, merge);
89
90         return 1;
91 }
92
93 static int _snap_text_export(const struct lv_segment *seg, struct formatter *f)
94 {
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);
99         else
100                 outf(f, "merging_store = \"%s\"", seg->cow->name);
101
102         return 1;
103 }
104
105 static int _snap_target_status_compatible(const char *type)
106 {
107         return (strcmp(type, "snapshot-merge") == 0);
108 }
109
110 #ifdef DEVMAPPER_SUPPORT
111 static int _snap_target_percent(void **target_state __attribute__((unused)),
112                                 percent_t *percent,
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)
118 {
119         uint64_t total_sectors, sectors_allocated, metadata_sectors;
120         int r;
121
122         /*
123          * snapshot target's percent format:
124          * <= 1.7.0: <sectors_allocated>/<total_sectors>
125          * >= 1.8.0: <sectors_allocated>/<total_sectors> <metadata_sectors>
126          */
127         r = sscanf(params, "%" PRIu64 "/%" PRIu64 " %" PRIu64,
128                    &sectors_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;
136                 else
137                         *percent = make_percent(*total_numerator, *total_denominator);
138         } else if (!strcmp(params, "Invalid") ||
139                    !strcmp(params, "Merge failed"))
140                 *percent = PERCENT_INVALID;
141         else
142                 return 0;
143
144         return 1;
145 }
146
147 static int _snap_target_present(struct cmd_context *cmd,
148                                 const struct lv_segment *seg,
149                                 unsigned *attributes __attribute__((unused)))
150 {
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;
155
156         if (!_snap_checked) {
157                 _snap_present = target_present(cmd, "snapshot", 1) &&
158                     target_present(cmd, "snapshot-origin", 0);
159                 _snap_checked = 1;
160         }
161
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;
166         }
167
168         return _snap_present;
169 }
170
171 #ifdef DMEVENTD
172
173 static const char *_get_snapshot_dso_path(struct cmd_context *cmd)
174 {
175         return get_monitor_dso_path(cmd, find_config_tree_str(cmd, "dmeventd/snapshot_library",
176                                                               DEFAULT_DMEVENTD_SNAPSHOT_LIB));
177 }
178
179 /* FIXME Cache this */
180 static int _target_registered(struct lv_segment *seg, int *pending)
181 {
182         return target_registered_with_dmeventd(seg->lv->vg->cmd, _get_snapshot_dso_path(seg->lv->vg->cmd),
183                                                seg->cow, pending);
184 }
185
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)
188 {
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);
192 }
193
194 static int _target_register_events(struct lv_segment *seg,
195                                    int events)
196 {
197         return _target_set_events(seg, events, 1);
198 }
199
200 static int _target_unregister_events(struct lv_segment *seg,
201                                      int events)
202 {
203         return _target_set_events(seg, events, 0);
204 }
205
206 #endif /* DMEVENTD */
207 #endif
208
209 static int _snap_modules_needed(struct dm_pool *mem,
210                                 const struct lv_segment *seg __attribute__((unused)),
211                                 struct dm_list *modules)
212 {
213         if (!str_list_add(mem, modules, "snapshot")) {
214                 log_error("snapshot string list allocation failed");
215                 return 0;
216         }
217
218         return 1;
219 }
220
221 static void _snap_destroy(struct segment_type *segtype)
222 {
223         dm_free(segtype);
224 }
225
226 static struct segtype_handler _snapshot_ops = {
227         .name = _snap_name,
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,
235 #ifdef DMEVENTD
236         .target_monitored = _target_registered,
237         .target_monitor_events = _target_register_events,
238         .target_unmonitor_events = _target_unregister_events,
239 #endif
240 #endif
241         .modules_needed = _snap_modules_needed,
242         .destroy = _snap_destroy,
243 };
244
245 #ifdef SNAPSHOT_INTERNAL
246 struct segment_type *init_snapshot_segtype(struct cmd_context *cmd)
247 #else                           /* Shared */
248 struct segment_type *init_segtype(struct cmd_context *cmd);
249 struct segment_type *init_segtype(struct cmd_context *cmd)
250 #endif
251 {
252         struct segment_type *segtype = dm_malloc(sizeof(*segtype));
253
254         if (!segtype)
255                 return_NULL;
256
257         segtype->cmd = cmd;
258         segtype->ops = &_snapshot_ops;
259         segtype->name = "snapshot";
260         segtype->private = NULL;
261         segtype->flags = SEG_SNAPSHOT;
262
263 #ifdef DMEVENTD
264         if (_get_snapshot_dso_path(cmd))
265                 segtype->flags |= SEG_MONITORED;
266 #endif
267         log_very_verbose("Initialised segtype: %s", segtype->name);
268
269         return segtype;
270 }