Debian Codegraph @

src/plugins/mpath.c

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Vratislav Podzimek <[email protected]>
18 */
19
20 #include <glib.h>
21 /* provides major and minor macros */
22 #include <sys/sysmacros.h>
23 #include <libdevmapper.h>
24 #include <unistd.h>
25 #include <blockdev/utils.h>
26
27 #include "mpath.h"
28 #include "check_deps.h"
29
30 #define MULTIPATH_MIN_VERSION "0.4.9"
31
32 /**
33 * SECTION: mpath
34 * @short_description: plugin for basic operations with multipath devices
35 * @title: Mpath
36 * @include: mpath.h
37 *
38 * A plugin for basic operations with multipath devices.
39 */
40
41 /**
42 * bd_mpath_error_quark: (skip)
43 */
44 GQuark bd_mpath_error_quark (void)
45 {
46 return g_quark_from_static_string ("g-bd-mpath-error-quark");
47 }
48
49 static volatile guint avail_deps = 0;
50 static GMutex deps_check_lock;
51
52 #define DEPS_MPATH 0
53 #define DEPS_MPATH_MASK (1 << DEPS_MPATH)
54 #define DEPS_MPATHCONF 1
55 #define DEPS_MPATHCONF_MASK (1 << DEPS_MPATHCONF)
56 #define DEPS_LAST 2
57
58 static const UtilDep deps[DEPS_LAST] = {
59 {"multipath", MULTIPATH_MIN_VERSION, NULL, "multipath-tools v([\\d\\.]+)"},
60 {"mpathconf", NULL, NULL, NULL},
61 };
62
63
64 /**
65 * bd_mpath_init:
66 *
67 * Initializes the plugin. **This function is called automatically by the
68 * library's initialization functions.**
69 *
70 */
71 gboolean bd_mpath_init (void) {
72 /* nothing to do here */
73 return TRUE;
74 };
75
76 /**
77 * bd_mpath_close:
78 *
79 * Cleans up after the plugin. **This function is called automatically by the
80 * library's functions that unload it.**
81 *
82 */
83 void bd_mpath_close (void) {
84 g_atomic_int_set (&avail_deps, 0);
85 }
86
87 /**
88 * bd_mpath_is_tech_avail:
89 * @tech: the queried tech
90 * @mode: a bit mask of queried modes of operation for @tech
91 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
92 *
93 * Returns: whether the @tech-@mode combination is available -- supported by the
94 * plugin implementation and having all the runtime dependencies available
95 */
96 gboolean bd_mpath_is_tech_avail (BDMpathTech tech, guint64 mode, GError **error) {
97 switch (tech) {
98 case BD_MPATH_TECH_BASE:
99 return check_deps (&avail_deps, DEPS_MPATH_MASK, deps, DEPS_LAST, &deps_check_lock, error);
100 case BD_MPATH_TECH_FRIENDLY_NAMES:
101 if (mode & ~BD_MPATH_TECH_MODE_MODIFY) {
102 g_set_error_literal (error, BD_MPATH_ERROR, BD_MPATH_ERROR_TECH_UNAVAIL,
103 "Only 'modify' (setting) supported for friendly names");
104 return FALSE;
105 } else if (mode & BD_MPATH_TECH_MODE_MODIFY)
106 return check_deps (&avail_deps, DEPS_MPATHCONF_MASK, deps, DEPS_LAST, &deps_check_lock, error);
107 else {
108 g_set_error_literal (error, BD_MPATH_ERROR, BD_MPATH_ERROR_TECH_UNAVAIL,
109 "Unknown mode");
110 return FALSE;
111 }
112 default:
113 g_set_error_literal (error, BD_MPATH_ERROR, BD_MPATH_ERROR_TECH_UNAVAIL, "Unknown technology");
114 return FALSE;
115 }
116 }
117
118
119 /**
120 * bd_mpath_flush_mpaths:
121 * @error: (out) (optional): place to store error (if any)
122 *
123 * Returns: whether multipath device maps were successfully flushed or not
124 *
125 * Flushes all unused multipath device maps.
126 *
127 * Tech category: %BD_MPATH_TECH_BASE-%BD_MPATH_TECH_MODE_MODIFY
128 */
129 gboolean bd_mpath_flush_mpaths (GError **error) {
130 const gchar *argv[3] = {"multipath", "-F", NULL};
131 gboolean success = FALSE;
132 gchar *output = NULL;
133
134 if (!check_deps (&avail_deps, DEPS_MPATH_MASK, deps, DEPS_LAST, &deps_check_lock, error))
135 return FALSE;
136
137 /* try to flush the device maps */
138 success = bd_utils_exec_and_report_error (argv, NULL, error);
139 if (!success)
140 return FALSE;
141
142 /* list devices (there should be none) */
143 argv[1] = "-ll";
144 success = bd_utils_exec_and_capture_output (argv, NULL, &output, NULL);
145 if (success && output && (g_strcmp0 (output, "") != 0)) {
146 g_set_error (error, BD_MPATH_ERROR, BD_MPATH_ERROR_FLUSH,
147 "Some device cannot be flushed: %s", output);
148 g_free (output);
149 return FALSE;
150 }
151
152 g_free (output);
153 return TRUE;
154 }
155
156 static gchar* get_device_name (const gchar *major_minor, GError **error) {
157 gchar *path = NULL;
158 gchar *link = NULL;
159 gchar *ret = NULL;
160
161 path = g_strdup_printf ("/dev/block/%s", major_minor);
162 link = g_file_read_link (path, error);
163 g_free (path);
164 if (!link) {
165 g_prefix_error (error, "Failed to determine device name for '%s'",
166 major_minor);
167 return NULL;
168 }
169
170 /* 'link' should be something like "../sda" */
171 /* get the last '/' */
172 ret = strrchr (link, '/');
173 if (!ret) {
174 g_set_error (error, BD_MPATH_ERROR, BD_MPATH_ERROR_INVAL,
175 "Failed to determine device name for '%s'",
176 major_minor);
177 g_free (link);
178 return NULL;
179 }
180 /* move right after the last '/' */
181 ret++;
182
183 /* create a new copy and free the whole link path */
184 ret = g_strdup (ret);
185 g_free (link);
186
187 return ret;
188 }
189
190 static gboolean map_is_multipath (const gchar *map_name, GError **error) {
191 struct dm_task *task = NULL;
192 struct dm_info info;
193 guint64 start = 0;
194 guint64 length = 0;
195 gchar *type = NULL;
196 gchar *params = NULL;
197 gboolean ret = FALSE;
198
199 task = dm_task_create (DM_DEVICE_STATUS);
200 if (!task) {
201 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to create DM task");
202 g_set_error_literal (error, BD_MPATH_ERROR, BD_MPATH_ERROR_DM_ERROR,
203 "Failed to create DM task");
204 return FALSE;
205 }
206
207 if (dm_task_set_name (task, map_name) == 0) {
208 g_set_error_literal (error, BD_MPATH_ERROR, BD_MPATH_ERROR_DM_ERROR,
209 "Failed to create DM task");
210 dm_task_destroy (task);
211 return FALSE;
212 }
213
214 if (dm_task_run (task) == 0) {
215 g_set_error_literal (error, BD_MPATH_ERROR, BD_MPATH_ERROR_DM_ERROR,
216 "Failed to run DM task");
217 dm_task_destroy (task);
218 return FALSE;
219 }
220
221 if (dm_task_get_info (task, &info) == 0) {
222 g_set_error_literal (error, BD_MPATH_ERROR, BD_MPATH_ERROR_DM_ERROR,
223 "Failed to get task info");
224 dm_task_destroy (task);
225 return FALSE;
226 }
227
228 dm_get_next_target (task, NULL, &start, &length, &type, &params);
229 if (g_strcmp0 (type, "multipath") == 0)
230 ret = TRUE;
231 else
232 ret = FALSE;
233 dm_task_destroy (task);
234
235 return ret;
236 }
237
238 static gchar** get_map_deps (const gchar *map_name, guint64 *n_deps, GError **error) {
239 struct dm_task *task;
240 struct dm_deps *deps;
241 guint64 dev_major = 0;
242 guint64 dev_minor = 0;
243 guint64 i = 0;
244 gchar **dep_devs = NULL;
245 gchar *major_minor = NULL;
246 GError *l_error = NULL;
247
248 task = dm_task_create (DM_DEVICE_DEPS);
249 if (!task) {
250 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to create DM task");
251 g_set_error_literal (error, BD_MPATH_ERROR, BD_MPATH_ERROR_DM_ERROR,
252 "Failed to create DM task");
253 return NULL;
254 }
255
256 if (dm_task_set_name (task, map_name) == 0) {
257 g_set_error_literal (error, BD_MPATH_ERROR, BD_MPATH_ERROR_DM_ERROR,
258 "Failed to create DM task");
259 dm_task_destroy (task);
260 return NULL;
261 }
262
263 if (dm_task_run (task) == 0) {
264 g_set_error_literal (error, BD_MPATH_ERROR, BD_MPATH_ERROR_DM_ERROR,
265 "Failed to run DM task");
266 dm_task_destroy (task);
267 return NULL;
268 }
269
270 deps = dm_task_get_deps (task);
271 if (!deps) {
272 g_set_error_literal (error, BD_MPATH_ERROR, BD_MPATH_ERROR_DM_ERROR,
273 "Failed to get device dependencies");
274 dm_task_destroy (task);
275 return NULL;
276 }
277
278 /* allocate space for the dependencies */
279 dep_devs = g_new0 (gchar*, deps->count + 1);
280
281 for (i = 0; i < deps->count; i++) {
282 dev_major = (guint64) major (deps->device[i]);
283 dev_minor = (guint64) minor (deps->device[i]);
284 major_minor = g_strdup_printf ("%"G_GUINT64_FORMAT":%"G_GUINT64_FORMAT, dev_major, dev_minor);
285 dep_devs[i] = get_device_name (major_minor, &l_error);
286 if (l_error) {
287 g_propagate_prefixed_error (error, l_error, "Failed to resolve '%s' to device name",
288 major_minor);
289 g_strfreev (dep_devs);
290 g_free (major_minor);
291 return NULL;
292 }
293 g_free (major_minor);
294 }
295 dep_devs[deps->count] = NULL;
296 if (n_deps)
297 *n_deps = deps->count;
298
299 dm_task_destroy (task);
300 return dep_devs;
301 }
302
303 /**
304 * bd_mpath_is_mpath_member:
305 * @device: device to test
306 * @error: (out) (optional): place to store error (if any)
307 *
308 * Returns: %TRUE if the device is a multipath member, %FALSE if not or an error
309 * appeared when queried (@error is set in those cases)
310 *
311 * Tech category: %BD_MPATH_TECH_BASE-%BD_MPATH_TECH_MODE_QUERY
312 */
313 gboolean bd_mpath_is_mpath_member (const gchar *device, GError **error) {
314 struct dm_task *task_names = NULL;
315 struct dm_names *names = NULL;
316 gchar *dev_path = NULL;
317 guint64 next = 0;
318 gchar **deps = NULL;
319 gchar **dev_name = NULL;
320 gboolean ret = FALSE;
321 GError *l_error = NULL;
322
323 /* we check if the 'device' is a dependency of any multipath map */
324 /* get maps */
325 task_names = dm_task_create (DM_DEVICE_LIST);
326 if (!task_names) {
327 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to create DM task");
328 g_set_error_literal (error, BD_MPATH_ERROR, BD_MPATH_ERROR_DM_ERROR,
329 "Failed to create DM task");
330 return FALSE;
331 }
332
333 dm_task_run (task_names);
334 names = dm_task_get_names (task_names);
335
336 if (!names || !names->dev) {
337 dm_task_destroy (task_names);
338 return FALSE;
339 }
340
341 /* in case the device is dev_path, we need to resolve it because maps's deps
342 are devices and not their dev_paths */
343 if (g_str_has_prefix (device, "/dev/mapper/") || g_str_has_prefix (device, "/dev/md/")) {
344 dev_path = bd_utils_resolve_device (device, NULL);
345 if (!dev_path) {
346 /* the device doesn't exist and thus is not an mpath member */
347 dm_task_destroy (task_names);
348 return FALSE;
349 }
350
351 /* the dev_path starts with "../" */
352 device = dev_path + 3;
353 }
354
355 if (g_str_has_prefix (device, "/dev/"))
356 device += 5;
357
358 /* check all maps */
359 do {
360 names = (void *)names + next;
361 next = names->next;
362
363 /* we are only interested in multipath maps */
364 if (map_is_multipath (names->name, &l_error)) {
365 deps = get_map_deps (names->name, NULL, &l_error);
366 if (!deps) {
367 if (l_error)
368 g_propagate_prefixed_error (error, l_error, "Failed to determine deps for '%s'",
369 names->name);
370 else
371 g_set_error (error, BD_MPATH_ERROR, BD_MPATH_ERROR_NOT_ROOT,
372 "No deps found for '%s'", names->name);
373 g_free (dev_path);
374 dm_task_destroy (task_names);
375 g_strfreev (deps);
376 return FALSE;
377 }
378 for (dev_name = deps; !ret && *dev_name; dev_name++)
379 ret = (g_strcmp0 (*dev_name, device) == 0);
380 g_strfreev (deps);
381 } else if (l_error) {
382 g_propagate_prefixed_error (error, l_error, "Failed to determine map's target for '%s'",
383 names->name);
384 g_free (dev_path);
385 dm_task_destroy (task_names);
386 return FALSE;
387 }
388 } while (!ret && next);
389
390 g_free (dev_path);
391 dm_task_destroy (task_names);
392 return ret;
393 }
394
395 /**
396 * bd_mpath_get_mpath_members:
397 * @error: (out) (optional): place to store error (if any)
398 *
399 * Returns: (transfer full) (array zero-terminated=1): list of names of all devices that are
400 * members of the mpath mappings
401 * (or %NULL in case of error)
402 *
403 * Tech category: %BD_MPATH_TECH_BASE-%BD_MPATH_TECH_MODE_QUERY
404 */
405 gchar** bd_mpath_get_mpath_members (GError **error) {
406 struct dm_task *task_names = NULL;
407 struct dm_names *names = NULL;
408 guint64 next = 0;
409 gchar **deps = NULL;
410 gchar **dev_name = NULL;
411 guint64 n_deps = 0;
412 guint64 n_devs = 0;
413 guint64 top_dev = 0;
414 gchar **ret = NULL;
415 guint64 progress_id = 0;
416 GError *l_error = NULL;
417
418 progress_id = bd_utils_report_started ("Started getting mpath members");
419
420 /* we check if the 'device' is a dependency of any multipath map */
421 /* get maps */
422 task_names = dm_task_create (DM_DEVICE_LIST);
423 if (!task_names) {
424 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to create DM task");
425 g_set_error_literal (&l_error, BD_MPATH_ERROR, BD_MPATH_ERROR_DM_ERROR,
426 "Failed to create DM task");
427 bd_utils_report_finished (progress_id, l_error->message);
428 g_propagate_error (error, l_error);
429 return NULL;
430 }
431
432 dm_task_run (task_names);
433 names = dm_task_get_names (task_names);
434
435 if (!names || !names->dev) {
436 dm_task_destroy (task_names);
437 bd_utils_report_finished (progress_id, "Completed");
438 return NULL;
439 }
440
441 ret = g_new0 (gchar*, 1);
442 n_devs = 1;
443
444 /* check all maps */
445 do {
446 names = (void *)names + next;
447 next = names->next;
448
449 /* we are only interested in multipath maps */
450 if (map_is_multipath (names->name, NULL)) {
451 deps = get_map_deps (names->name, &n_deps, &l_error);
452 if (l_error) {
453 g_prefix_error (&l_error, "Failed to determine deps for '%s'", names->name);
454 dm_task_destroy (task_names);
455 bd_utils_report_finished (progress_id, l_error->message);
456 g_propagate_error (error, l_error);
457 g_free (deps);
458 g_free (ret);
459 return NULL;
460 }
461 if (deps) {
462 n_devs += n_deps;
463 ret = g_renew (gchar*, ret, n_devs);
464 for (dev_name=deps; *dev_name; dev_name++) {
465 ret[top_dev] = *dev_name;
466 top_dev += 1;
467 }
468 g_free (deps);
469 }
470 }
471 } while (next);
472
473 ret[top_dev] = NULL;
474 bd_utils_report_finished (progress_id, "Completed");
475
476 return ret;
477 }
478
479
480 /**
481 * bd_mpath_set_friendly_names:
482 * @enabled: whether friendly names should be enabled or not
483 * @error: (out) (optional): place to store error (if any)
484 *
485 * Returns: if successfully set or not
486 *
487 * Tech category: %BD_MPATH_TECH_FRIENDLY_NAMES-%BD_MPATH_TECH_MODE_MODIFY
488 */
489 gboolean bd_mpath_set_friendly_names (gboolean enabled, GError **error) {
490 const gchar *argv[8] = {"mpathconf", "--find_multipaths", "y", "--user_friendly_names", NULL, "--with_multipathd", "y", NULL};
491 argv[4] = enabled ? "y" : "n";
492
493 if (!check_deps (&avail_deps, DEPS_MPATHCONF_MASK, deps, DEPS_LAST, &deps_check_lock, error))
494 return FALSE;
495
496 return bd_utils_exec_and_report_error (argv, NULL, error);
497 }