Debian Codegraph @

daemons/dmeventd/plugins/snapshot/dmeventd_snapshot.c

raw
1 /*
2 * Copyright (C) 2007-2015 Red Hat, Inc. All rights reserved.
3 *
4 * This file is part of LVM2.
5 *
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU Lesser General Public License v.2.1.
9 *
10 * You should have received a copy of the GNU Lesser General Public License
11 * along with this program; if not, write to the Free Software Foundation,
12 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
13 */
14
15 #include "lib/misc/lib.h"
16 #include "daemons/dmeventd/plugins/lvm2/dmeventd_lvm.h"
17 #include "daemons/dmeventd/libdevmapper-event.h"
18
19 #include <sys/sysmacros.h>
20 #include <sys/wait.h>
21 #include <stdarg.h>
22 #include <pthread.h>
23
24 /* First warning when snapshot is 80% full. */
25 #define WARNING_THRESH (DM_PERCENT_1 * 80)
26 /* Run a check every 5%. */
27 #define CHECK_STEP (DM_PERCENT_1 * 5)
28 /* Do not bother checking snapshots less than 50% full. */
29 #define CHECK_MINIMUM (DM_PERCENT_1 * 50)
30
31 #define UMOUNT_COMMAND "/bin/umount"
32
33 struct dso_state {
34 struct dm_pool *mem;
35 dm_percent_t percent_check;
36 uint64_t known_size;
37 char cmd_lvextend[512];
38 };
39
40 DM_EVENT_LOG_FN("snap")
41
42 static int _run(const char *cmd, ...)
43 {
44 va_list ap;
45 int argc = 1; /* for argv[0], i.e. cmd */
46 int i = 0;
47 const char **argv;
48 pid_t pid = fork();
49 int status;
50
51 if (pid == 0) { /* child */
52 va_start(ap, cmd);
53 while (va_arg(ap, const char *))
54 ++ argc;
55 va_end(ap);
56
57 /* + 1 for the terminating NULL */
58 argv = alloca(sizeof(const char *) * (argc + 1));
59
60 argv[0] = cmd;
61 va_start(ap, cmd);
62 while ((argv[++i] = va_arg(ap, const char *)));
63 va_end(ap);
64
65 execvp(cmd, (char **)argv);
66 log_sys_error("exec", cmd);
67 exit(127);
68 }
69
70 if (pid > 0) { /* parent */
71 if (waitpid(pid, &status, 0) != pid)
72 return 0; /* waitpid failed */
73 if (!WIFEXITED(status) || WEXITSTATUS(status))
74 return 0; /* the child failed */
75 }
76
77 if (pid < 0)
78 return 0; /* fork failed */
79
80 return 1; /* all good */
81 }
82
83 static int _extend(const char *cmd)
84 {
85 log_debug("Extending snapshot via %s.", cmd);
86 return dmeventd_lvm2_run_with_lock(cmd);
87 }
88
89 #ifdef SNAPSHOT_REMOVE
90 /* Remove invalid snapshot from dm-table */
91 /* Experimental for now and not used by default */
92 static int _remove(const char *uuid)
93 {
94 int r = 1;
95 uint32_t cookie = 0;
96 struct dm_task *dmt;
97
98 if (!(dmt = dm_task_create(DM_DEVICE_REMOVE)))
99 return 0;
100
101 if (!dm_task_set_uuid(dmt, uuid)) {
102 r = 0;
103 goto_out;
104 }
105
106 dm_task_retry_remove(dmt);
107
108 if (!dm_task_set_cookie(dmt, &cookie, 0)) {
109 r = 0;
110 goto_out;
111 }
112
113 if (!dm_task_run(dmt)) {
114 r = 0;
115 goto_out;
116 }
117 out:
118 dm_task_destroy(dmt);
119
120 return r;
121 }
122 #endif /* SNAPSHOT_REMOVE */
123
124 static void _umount(const char *device, int major, int minor)
125 {
126 FILE *mounts;
127 char buffer[4096];
128 char *words[3];
129 struct stat st;
130 const char procmounts[] = "/proc/mounts";
131
132 if (!(mounts = fopen(procmounts, "r"))) {
133 log_sys_error("fopen", procmounts);
134 log_error("Not umounting %s.", device);
135 return;
136 }
137
138 while (!feof(mounts)) {
139 /* read a line of /proc/mounts */
140 if (!fgets(buffer, sizeof(buffer), mounts))
141 break; /* eof, likely */
142
143 /* words[0] is the mount point and words[1] is the device path */
144 if (dm_split_words(buffer, 3, 0, words) < 2)
145 continue;
146
147 /* find the major/minor of the device */
148 if (stat(words[0], &st))
149 continue; /* can't stat, skip this one */
150
151 if (S_ISBLK(st.st_mode) &&
152 (int) major(st.st_rdev) == major &&
153 (int) minor(st.st_rdev) == minor) {
154 log_error("Unmounting invalid snapshot %s from %s.", device, words[1]);
155 if (!_run(UMOUNT_COMMAND, "-fl", words[1], NULL))
156 log_error("Failed to umount snapshot %s from %s: %s.",
157 device, words[1], strerror(errno));
158 }
159 }
160
161 if (fclose(mounts))
162 log_sys_error("close", procmounts);
163 }
164
165 void process_event(struct dm_task *dmt,
166 enum dm_event_mask evmask __attribute__((unused)),
167 void **user)
168 {
169 struct dso_state *state = *user;
170 void *next = NULL;
171 uint64_t start, length;
172 char *target_type = NULL;
173 char *params;
174 struct dm_status_snapshot *status = NULL;
175 const char *device = dm_task_get_name(dmt);
176 int percent;
177 struct dm_info info;
178 int ret;
179
180 /* No longer monitoring, waiting for remove */
181 if (!state->percent_check)
182 return;
183
184 dm_get_next_target(dmt, next, &start, &length, &target_type, &params);
185 if (!target_type || strcmp(target_type, "snapshot")) {
186 log_error("Target %s is not snapshot.", target_type);
187 return;
188 }
189
190 if (!dm_get_status_snapshot(state->mem, params, &status)) {
191 log_error("Cannot parse snapshot %s state: %s.", device, params);
192 return;
193 }
194
195 /*
196 * If the snapshot has been invalidated or we failed to parse
197 * the status string. Report the full status string to syslog.
198 */
199 if (status->invalid || status->overflow || !status->total_sectors) {
200 log_warn("WARNING: Snapshot %s changed state to: %s and should be removed.",
201 device, params);
202 state->percent_check = 0;
203 if (dm_task_get_info(dmt, &info))
204 _umount(device, info.major, info.minor);
205 #ifdef SNAPSHOT_REMOVE
206 /* Maybe configurable ? */
207 _remove(dm_task_get_uuid(dmt));
208 #endif
209 if ((ret = pthread_kill(pthread_self(), SIGALRM)) && (ret != ESRCH))
210 log_sys_error("pthread_kill", "self");
211 goto out;
212 }
213
214 if (length <= (status->used_sectors - status->metadata_sectors)) {
215 /* TODO eventually recognize earlier when room is enough */
216 log_info("Dropping monitoring of fully provisioned snapshot %s.",
217 device);
218 if ((ret = pthread_kill(pthread_self(), SIGALRM)) && (ret != ESRCH))
219 log_sys_error("pthread_kill", "self");
220 goto out;
221 }
222
223 /* Snapshot size had changed. Clear the threshold. */
224 if (state->known_size != status->total_sectors) {
225 state->percent_check = CHECK_MINIMUM;
226 state->known_size = status->total_sectors;
227 }
228
229 percent = dm_make_percent(status->used_sectors, status->total_sectors);
230 if (percent >= state->percent_check) {
231 /* Usage has raised more than CHECK_STEP since the last
232 time. Run actions. */
233 state->percent_check = (percent / CHECK_STEP) * CHECK_STEP + CHECK_STEP;
234
235 if (percent >= WARNING_THRESH) /* Print a warning to syslog. */
236 log_warn("WARNING: Snapshot %s is now %.2f%% full.",
237 device, dm_percent_to_round_float(percent, 2));
238
239 /* Try to extend the snapshot, in accord with user-set policies */
240 if (!_extend(state->cmd_lvextend))
241 log_error("Failed to extend snapshot %s.", device);
242 }
243 out:
244 dm_pool_free(state->mem, status);
245 }
246
247 int register_device(const char *device,
248 const char *uuid __attribute__((unused)),
249 int major __attribute__((unused)),
250 int minor __attribute__((unused)),
251 void **user)
252 {
253 struct dso_state *state;
254
255 if (!dmeventd_lvm2_init_with_pool("snapshot_state", state))
256 goto_bad;
257
258 if (!dmeventd_lvm2_command(state->mem, state->cmd_lvextend,
259 sizeof(state->cmd_lvextend),
260 "lvextend --use-policies", device))
261 goto_bad;
262
263 state->percent_check = CHECK_MINIMUM;
264 *user = state;
265
266 log_info("Monitoring snapshot %s.", device);
267
268 return 1;
269 bad:
270 log_error("Failed to monitor snapshot %s.", device);
271
272 if (state)
273 dmeventd_lvm2_exit_with_pool(state);
274
275 return 0;
276 }
277
278 int unregister_device(const char *device,
279 const char *uuid __attribute__((unused)),
280 int major __attribute__((unused)),
281 int minor __attribute__((unused)),
282 void **user)
283 {
284 struct dso_state *state = *user;
285
286 dmeventd_lvm2_exit_with_pool(state);
287 log_info("No longer monitoring snapshot %s.", device);
288
289 return 1;
290 }