Debian Codegraph @

modules/lsm/lsm_data.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2015 Red Hat, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Author: Gris Ge <[email protected]>
20 *
21 */
22
23 #include "config.h"
24
25 #include "lsm_data.h"
26
27 #include <src/udisksdaemon.h>
28 #include <src/udisksdaemontypes.h>
29 #include <src/udiskslogging.h>
30 #include <src/udisksconfigmanager.h>
31 #include <glib.h>
32 #include <glib/gprintf.h>
33 #include <libconfig.h>
34 #include <string.h>
35 #include <stdint.h>
36
37 #define _STD_LSM_SIM_URI "sim://"
38 #define _STD_LSM_HPSA_URI "hpsa://"
39
40 #define _STD_LSM_CONF_PATH "modules.conf.d"
41 #define _STD_LSM_CONF_FILE "udisks2_lsm.conf"
42 #define _STD_LSM_CONF_REFRESH_KEYNAME "refresh_interval"
43 #define _STD_LSM_CONF_SIM_KEYNAME "enable_sim"
44 #define _STD_LSM_CONF_HPSA_KEYNAME "enable_hpsa"
45 #define _STD_LSM_CONF_EXT_URIS_KEYNAME "extra_uris"
46 #define _STD_LSM_CONF_EXT_PASS_KEYNAME "extra_passwords"
47 #define _STD_LSM_CONNECTION_DEFAULT_TMO 30000
48
49 /*
50 * _LsmUriSet is holding the URI and password string pointers.
51 */
52 struct _LsmUriSet
53 {
54 char *uri;
55 char *password;
56 };
57
58 /*
59 * _LsmConnData is holding lsm connection information for each
60 * volume.
61 */
62 struct _LsmConnData
63 {
64 lsm_connect *lsm_conn;
65 lsm_volume *lsm_vol;
66 char *pl_id;
67 };
68
69 /*
70 * _LsmPlData is holding the pool information.
71 * It's shared by all the volumes under the same pool.
72 */
73 struct _LsmPlData
74 {
75 gint64 last_refresh_time;
76 gboolean is_ok;
77 gboolean is_raid_degraded;
78 gboolean is_raid_error;
79 gboolean is_raid_verifying;
80 gboolean is_raid_reconstructing;
81 char *status_info;
82 };
83
84 /*
85 * _LsmVriData is holding the volume RAID information.
86 */
87 struct _LsmVriData
88 {
89 gint64 last_refresh_time;
90 char *raid_type_str;
91 uint32_t min_io_size;
92 uint32_t opt_io_size;
93 uint32_t raid_disk_count;
94 };
95
96 static GPtrArray *_conf_lsm_uri_sets = NULL;
97 static uint32_t _conf_refresh_interval = 30;
98 static gboolean _sys_id_supported = TRUE;
99 static GPtrArray *_all_lsm_conn_array = NULL;
100 static GHashTable *_supported_sys_id_hash = NULL;
101 static GHashTable *_vpd83_2_lsm_conn_data_hash = NULL;
102 static GHashTable *_pl_id_2_lsm_pl_data_hash = NULL;
103 static GHashTable *_vpd83_2_lsm_vri_data_hash = NULL;
104
105 static void _fill_lsm_pl_data (struct _LsmPlData *lsm_pl_data,
106 lsm_pool *lsm_pl,
107 gint64 last_refresh_time);
108
109 static void _free_lsm_uri_set (gpointer data);
110
111 static struct _LsmUriSet *
112 _lsm_uri_set_new (const char *uri, const char *pass)
113 {
114 struct _LsmUriSet *lsm_uri_set;
115
116 lsm_uri_set = (struct _LsmUriSet *) g_malloc (sizeof (struct _LsmUriSet));
117 lsm_uri_set->uri = g_strdup (uri);
118 lsm_uri_set->password = g_strdup (pass);
119 return lsm_uri_set;
120 }
121
122 /*
123 * Convert LSM error to GError.
124 *
125 */
126 static void
127 _handle_lsm_error (const char *msg,
128 lsm_connect *lsm_conn,
129 GError **error)
130 {
131 lsm_error *lsm_err = NULL;
132
133 lsm_err = lsm_error_last_get (lsm_conn);
134 if (lsm_err)
135 {
136 g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_FAILED,
137 "%s. Error code: %d, error message: %s",
138 msg, lsm_error_number_get (lsm_err), lsm_error_message_get (lsm_err));
139 lsm_error_free (lsm_err);
140 }
141 else
142 {
143 g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_FAILED,
144 "LSM: %s. But failed to retrieve error code and message.",
145 msg);
146 }
147 }
148
149 /*
150 * Convert lsm_volume_raid_type to 'RAID 1' like string.
151 * The memory of string does not require manually free.
152 */
153
154 static const char *
155 _lsm_raid_type_to_str (lsm_volume_raid_type raid_type)
156 {
157 switch (raid_type)
158 {
159 case LSM_VOLUME_RAID_TYPE_JBOD:
160 return "JBOD";
161 case LSM_VOLUME_RAID_TYPE_RAID0:
162 return "RAID 0";
163 case LSM_VOLUME_RAID_TYPE_RAID1:
164 return "RAID 1";
165 case LSM_VOLUME_RAID_TYPE_RAID5:
166 return "RAID 5";
167 case LSM_VOLUME_RAID_TYPE_RAID6:
168 return "RAID 6";
169 case LSM_VOLUME_RAID_TYPE_RAID10:
170 return "RAID 10";
171 case LSM_VOLUME_RAID_TYPE_RAID50:
172 return "RAID 50";
173 case LSM_VOLUME_RAID_TYPE_RAID60:
174 return "RAID 60";
175 default:
176 return "";
177 }
178 }
179
180 static char *
181 _lsm_get_conf_path (UDisksDaemon *daemon)
182 {
183 UDisksConfigManager *config_manager;
184
185 config_manager = udisks_daemon_get_config_manager (daemon);
186
187 /* This should give us '/etc/udisks2/modules.conf.d/udisks2_lsm.conf' */
188 return g_build_filename (udisks_config_manager_get_config_dir (config_manager),
189 _STD_LSM_CONF_PATH,
190 _STD_LSM_CONF_FILE,
191 NULL);
192 }
193
194 /*
195 * Use libconfig.h to read config file.
196 * Set these static variables
197 * GPtrArray _conf_lsm_uri_sets
198 * gint64 _conf_refresh_interval
199 * Memory should be freed by
200 */
201 static gboolean
202 _load_module_conf (UDisksDaemon *daemon, GError **error)
203 {
204 struct _LsmUriSet *lsm_uri_set = NULL;
205 config_t cfg;
206 int cfg_value_int = 0;
207 config_setting_t *ext_uris = NULL;
208 config_setting_t *ext_pass = NULL;
209 const char *uri = NULL;
210 const char *password = NULL;
211 char *conf_path;
212 int i;
213 gboolean ret = TRUE;
214
215 udisks_debug ("LSM: loading config file");
216
217 if (_conf_lsm_uri_sets)
218 {
219 g_ptr_array_unref (_conf_lsm_uri_sets);
220 _conf_lsm_uri_sets = NULL;
221 }
222
223 /* Get the abs config file path. */
224 conf_path = _lsm_get_conf_path (daemon);
225
226 config_init (&cfg);
227 if (config_read_file (&cfg, conf_path) != CONFIG_TRUE)
228 {
229 /* ignore the error and continue with defaults */
230 udisks_warning ("LSM: Failed to load config file %s, continuing with defaults. Error: %s at line %d",
231 conf_path, config_error_text (&cfg), config_error_line (&cfg));
232 }
233
234 config_lookup_int (&cfg, _STD_LSM_CONF_REFRESH_KEYNAME, &cfg_value_int);
235 if (cfg_value_int > 0)
236 {
237 _conf_refresh_interval = cfg_value_int & UINT32_MAX;
238 }
239
240 _conf_lsm_uri_sets = g_ptr_array_new_full (0, (GDestroyNotify) _free_lsm_uri_set);
241
242 cfg_value_int = 0; /* Disable simulator by default */
243 config_lookup_bool (&cfg, _STD_LSM_CONF_SIM_KEYNAME, &cfg_value_int);
244 if (cfg_value_int)
245 {
246 lsm_uri_set = _lsm_uri_set_new (_STD_LSM_SIM_URI, NULL);
247 g_ptr_array_add (_conf_lsm_uri_sets, lsm_uri_set);
248 }
249
250 cfg_value_int = 1; /* Enable HPSA by default. */
251 config_lookup_bool (&cfg, _STD_LSM_CONF_HPSA_KEYNAME, &cfg_value_int);
252 if (cfg_value_int)
253 {
254 lsm_uri_set = _lsm_uri_set_new (_STD_LSM_HPSA_URI, NULL);
255 g_ptr_array_add (_conf_lsm_uri_sets, lsm_uri_set);
256 }
257
258 /* Check extra_uris and extra_passwords */
259 ext_uris = config_lookup (&cfg, _STD_LSM_CONF_EXT_URIS_KEYNAME);
260 if (ext_uris && !config_setting_is_array (ext_uris))
261 {
262 g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_NOT_SUPPORTED,
263 "LSM: Invalid setting of '%s' in %s",
264 _STD_LSM_CONF_EXT_URIS_KEYNAME, conf_path);
265 ret = FALSE;
266 goto out;
267 }
268
269 ext_pass = config_lookup (&cfg, _STD_LSM_CONF_EXT_PASS_KEYNAME);
270 if (ext_pass && !config_setting_is_array (ext_pass))
271 {
272 g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_NOT_SUPPORTED,
273 "LSM: Invalid configure setting of '%s' in %s",
274 _STD_LSM_CONF_EXT_PASS_KEYNAME, conf_path);
275 ret = FALSE;
276 goto out;
277 }
278
279 if (ext_uris == NULL && ext_pass == NULL)
280 goto out;
281
282 if ((ext_uris && !ext_pass) || (!ext_uris && ext_pass))
283 {
284 g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_NOT_SUPPORTED,
285 "LSM: Invalid configure setting: '%s' and '%s' should be used in pair",
286 _STD_LSM_CONF_EXT_URIS_KEYNAME,
287 _STD_LSM_CONF_EXT_PASS_KEYNAME);
288 ret = FALSE;
289 goto out;
290 }
291
292 if (config_setting_length (ext_uris) != config_setting_length (ext_pass))
293 {
294 g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_NOT_SUPPORTED,
295 "LSM: Invalid configure setting: the element count of '%s' and '%s' does not match.",
296 _STD_LSM_CONF_EXT_URIS_KEYNAME,
297 _STD_LSM_CONF_EXT_PASS_KEYNAME);
298 ret = FALSE;
299 goto out;
300 }
301
302 for (i = 0; i < config_setting_length (ext_uris); ++i)
303 {
304 uri = config_setting_get_string_elem (ext_uris, i);
305 password = config_setting_get_string_elem (ext_pass, i);
306
307 if (strlen (uri) <= 0)
308 continue;
309 udisks_debug ("LSM: Found extra URI: %s", uri);
310
311 lsm_uri_set = _lsm_uri_set_new (uri, password);
312 g_ptr_array_add (_conf_lsm_uri_sets, lsm_uri_set);
313 }
314
315 out:
316 if (_conf_lsm_uri_sets && _conf_lsm_uri_sets->len == 0)
317 {
318 g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_FAILED,
319 "LSM: Error parsing config file %s: No URI found", conf_path);
320 g_ptr_array_unref (_conf_lsm_uri_sets);
321 _conf_lsm_uri_sets = NULL;
322 ret = FALSE;
323 }
324 config_destroy (&cfg);
325 g_free (conf_path);
326
327 return ret;
328 }
329
330 static lsm_connect *
331 _create_lsm_connect (struct _LsmUriSet *lsm_uri_set,
332 GError **error)
333 {
334 lsm_connect *lsm_conn = NULL;
335 lsm_error *lsm_err = NULL;
336 int lsm_rc;
337 const char *uri = NULL;
338 const char *password = NULL;
339
340 if (lsm_uri_set == NULL)
341 {
342 g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_FAILED,
343 "LSM: _create_lsm_connect (): Skip on NULL lsm_uri_set");
344 return NULL;
345 }
346 uri = lsm_uri_set->uri;
347 password = lsm_uri_set->password;
348
349 udisks_debug ("LSM: Connecting to URI: %s", uri);
350 lsm_rc = lsm_connect_password (uri, password, &lsm_conn,
351 _STD_LSM_CONNECTION_DEFAULT_TMO,
352 &lsm_err, LSM_CLIENT_FLAG_RSVD);
353 if (lsm_rc == LSM_ERR_DAEMON_NOT_RUNNING)
354 {
355 g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_FAILED,
356 "LSM: The libStorageMgmt daemon is not running (process name lsmd), try 'service libstoragemgmt start'");
357 lsm_error_free (lsm_err);
358 return NULL;
359 }
360 if (lsm_rc != LSM_ERR_OK)
361 {
362 g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_FAILED,
363 "LSM: Failed to connect plugin via URI '%s', error code: %d, error message: %s",
364 uri, lsm_error_number_get (lsm_err),
365 lsm_error_message_get (lsm_err));
366 lsm_error_free (lsm_err);
367 return NULL;
368 }
369 udisks_debug ("LSM: Plugin for URI '%s' connected", uri);
370 return lsm_conn;
371 }
372
373 /*
374 * Update _supported_sys_id_hash GHashTable when system is having
375 * LSM_CAP_VOLUMES and LSM_CAP_VOLUME_RAID_INFO capabilities:
376 * {
377 * system_id: TRUE;
378 * }
379 * Return TRUE when provided connection has supported system, or FALSE.
380 */
381 static gboolean
382 _fill_supported_system_id_hash (lsm_connect *lsm_conn,
383 GError **error)
384 {
385 lsm_storage_capabilities *lsm_cap = NULL;
386 lsm_system **lsm_syss = NULL;
387 uint32_t lsm_sys_count = 0;
388 int lsm_rc = 0;
389 uint32_t i = 0;
390 char *lsm_sys_id = NULL;
391 gboolean rc = FALSE;
392
393 lsm_rc = lsm_system_list (lsm_conn, &lsm_syss, &lsm_sys_count, LSM_CLIENT_FLAG_RSVD);
394
395 if (lsm_rc != LSM_ERR_OK)
396 {
397 _handle_lsm_error ("LSM: Failed to list systems", lsm_conn, error);
398 return FALSE;
399 }
400
401 if (lsm_sys_count == 0)
402 {
403 g_set_error_literal (error, UDISKS_ERROR, UDISKS_ERROR_FAILED,
404 "LSM: No system found in this lsm connection");
405 return FALSE;
406 }
407
408 for (i = 0; i < lsm_sys_count; ++i)
409 {
410 lsm_sys_id = (char *) lsm_system_id_get (lsm_syss[i]);
411 if (lsm_sys_id == NULL || strlen (lsm_sys_id) == 0)
412 {
413 udisks_debug ("LSM: BUG: got NULL system ID");
414 continue;
415 }
416 lsm_cap = NULL;
417 lsm_rc = lsm_capabilities (lsm_conn, lsm_syss[i], &lsm_cap, LSM_CLIENT_FLAG_RSVD);
418 if (lsm_rc != LSM_ERR_OK)
419 {
420 udisks_warning ("LSM: error on lsm_capabilities()");
421 continue;
422 }
423 if (lsm_capability_supported (lsm_cap, LSM_CAP_VOLUMES) &&
424 lsm_capability_supported (lsm_cap, LSM_CAP_VOLUME_RAID_INFO))
425 {
426 udisks_debug ("LSM: System '%s'(%s) is connected and supported.",
427 lsm_system_name_get (lsm_syss[i]), lsm_sys_id);
428 g_hash_table_insert (_supported_sys_id_hash, g_strdup (lsm_sys_id), &_sys_id_supported);
429 rc = TRUE;
430 }
431 else
432 {
433 udisks_debug ("LSM: System '%s'(%s) is not supporting LSM_CAP_VOLUMES or LSM_CAP_VOLUME_RAID_INFO.",
434 lsm_system_name_get (lsm_syss[i]), lsm_sys_id);
435 }
436 lsm_capability_record_free (lsm_cap);
437 }
438
439 lsm_system_record_array_free (lsm_syss, lsm_sys_count);
440
441 if (! rc)
442 {
443 g_set_error_literal (error, UDISKS_ERROR, UDISKS_ERROR_FAILED,
444 "LSM: Failed to get supported system id hash");
445 }
446
447 return rc;
448 }
449
450 static void
451 _free_lsm_volume_record (gpointer data)
452 {
453 lsm_volume_record_free ((lsm_volume *) data);
454 return;
455 }
456
457 /*
458 * Return an array of lsm_volume which system_id is in supported_sys_id_hash.
459 */
460 static GPtrArray *
461 _get_supported_lsm_volumes (lsm_connect *lsm_conn,
462 GError **error)
463 {
464 GPtrArray *lsm_vol_array = NULL;
465 lsm_volume **lsm_vols = NULL;
466 lsm_volume *lsm_vol_dup = NULL;
467 const char *lsm_sys_id = NULL;
468 uint32_t lsm_vol_count = 0;
469 int rc = 0;
470 uint32_t i = 0;
471 const char *lsm_vpd83;
472
473 rc = lsm_volume_list (lsm_conn, NULL, NULL, &lsm_vols, &lsm_vol_count, LSM_CLIENT_FLAG_RSVD);
474 if (rc != LSM_ERR_OK)
475 {
476 _handle_lsm_error ("LSM: Failed to list volumes", lsm_conn, error);
477 return NULL;
478 }
479
480 lsm_vol_array = g_ptr_array_new_full (0, (GDestroyNotify) _free_lsm_volume_record);
481
482 for (i = 0; i < lsm_vol_count; ++i)
483 {
484 lsm_vpd83 = lsm_volume_vpd83_get (lsm_vols[i]);
485 if (strlen (lsm_vpd83) == 0)
486 {
487 udisks_debug ("LSM: Volume %s(%s) has no VPD 83.",
488 lsm_volume_id_get (lsm_vols[i]),
489 lsm_volume_name_get (lsm_vols[i]));
490 continue;
491 }
492
493 lsm_sys_id = lsm_volume_system_id_get (lsm_vols[i]);
494 if (g_hash_table_lookup (_supported_sys_id_hash, lsm_sys_id) == NULL)
495 {
496 udisks_debug ("LSM: Volume VPD %s been rule out as its system is not supported", lsm_vpd83);
497 continue;
498 }
499
500 lsm_vol_dup = lsm_volume_record_copy (lsm_vols[i]);
501 g_assert (lsm_vol_dup != NULL);
502 g_ptr_array_add (lsm_vol_array, lsm_vol_dup);
503 }
504
505 lsm_volume_record_array_free (lsm_vols, lsm_vol_count);
506 if (lsm_vol_array->len == 0)
507 {
508 g_set_error_literal (error, UDISKS_ERROR, UDISKS_ERROR_FAILED,
509 "LSM: Failed to get supported LSM volumes");
510 g_ptr_array_unref (lsm_vol_array);
511 return NULL;
512 }
513 return lsm_vol_array;
514 }
515
516 static void
517 _free_lsm_pool_record (gpointer data)
518 {
519 lsm_pool_record_free ((lsm_pool *) data);
520 return;
521 }
522
523 /*
524 * Return an array of lsm_pool which system_id is in supported_sys_id_hash.
525 */
526 static GPtrArray *
527 _get_supported_lsm_pls (lsm_connect *lsm_conn,
528 GError **error)
529 {
530 GPtrArray *lsm_pl_array = NULL;
531 lsm_pool **lsm_pls = NULL;
532 lsm_pool *lsm_pl_dup = NULL;
533 const char *lsm_sys_id = NULL;
534 uint32_t lsm_pl_count = 0;
535 uint32_t i = 0;
536 int lsm_rc;
537
538 lsm_rc = lsm_pool_list (lsm_conn, NULL, NULL, &lsm_pls,
539 &lsm_pl_count, LSM_CLIENT_FLAG_RSVD);
540
541 if (lsm_rc != LSM_ERR_OK)
542 {
543 _handle_lsm_error ("LSM: Failed to list pools", lsm_conn, error);
544 return NULL;
545 }
546
547 lsm_pl_array = g_ptr_array_new_full (0, (GDestroyNotify) _free_lsm_pool_record);
548
549 for (i = 0; i < lsm_pl_count; ++i)
550 {
551
552 lsm_sys_id = lsm_pool_system_id_get (lsm_pls[i]);
553 if (g_hash_table_lookup (_supported_sys_id_hash, lsm_sys_id) == NULL)
554 {
555 udisks_debug ("LSM: Pool %s(%s) been rule out as its system is not supported",
556 lsm_pool_name_get (lsm_pls[i]),
557 lsm_pool_id_get (lsm_pls[i]));
558 continue;
559 }
560
561 lsm_pl_dup = lsm_pool_record_copy (lsm_pls[i]);
562 g_assert (lsm_pl_dup != NULL);
563 g_ptr_array_add (lsm_pl_array, lsm_pl_dup);
564 }
565 lsm_pool_record_array_free (lsm_pls, lsm_pl_count);
566 if (lsm_pl_array->len == 0)
567 {
568 g_set_error_literal (error, UDISKS_ERROR, UDISKS_ERROR_FAILED,
569 "LSM: Failed to get supported LSM pools");
570 g_ptr_array_unref (lsm_pl_array);
571 return NULL;
572 }
573 return lsm_pl_array;
574 }
575
576 static void
577 _fill_pl_id_2_lsm_pl_data_hash (GPtrArray *lsm_pl_array,
578 gint64 last_refresh_time)
579 {
580 struct _LsmPlData *lsm_pl_data = NULL;
581 lsm_pool *lsm_pl = NULL;
582 const char *pl_id = NULL;
583 const char *orig_pl_id = NULL;
584 struct _LsmPlData *orig_lsm_pl_data = NULL;
585 guint i;
586
587 for (i = 0; i < lsm_pl_array->len; ++i)
588 {
589 lsm_pl = g_ptr_array_index (lsm_pl_array, i);
590 pl_id = lsm_pool_id_get (lsm_pl);
591 if (pl_id == NULL || strlen (pl_id) == 0)
592 continue;
593
594 /* Override old data */
595 g_hash_table_lookup_extended (_pl_id_2_lsm_pl_data_hash, pl_id,
596 (gpointer *) &orig_pl_id,
597 (gpointer *) &orig_lsm_pl_data);
598 if (orig_pl_id != NULL)
599 g_hash_table_remove (_pl_id_2_lsm_pl_data_hash, (gconstpointer) orig_pl_id);
600
601 lsm_pl_data = (struct _LsmPlData *) g_malloc (sizeof (struct _LsmPlData));
602
603 _fill_lsm_pl_data (lsm_pl_data, lsm_pl, last_refresh_time);
604 g_hash_table_insert (_pl_id_2_lsm_pl_data_hash, g_strdup (pl_id), lsm_pl_data);
605 }
606 }
607
608 /*
609 * Use lsm_conn_data to fill in these hash tables to speed up the future
610 * search:
611 * _vpd83_2_lsm_conn_data_hash
612 * _pl_id_2_lsm_pl_data_hash
613 */
614 static void
615 _fill_vpd83_2_lsm_conn_data_hash (lsm_connect *lsm_conn,
616 GPtrArray *lsm_vol_array)
617 {
618 struct _LsmConnData *lsm_conn_data = NULL;
619 lsm_volume *lsm_vol = NULL;
620 const char *pl_id = NULL;
621 const char *vpd83 = NULL;
622 guint i;
623
624 for (i = 0; i < lsm_vol_array->len; ++i)
625 {
626 lsm_vol = g_ptr_array_index (lsm_vol_array, i);
627 if (lsm_vol == NULL)
628 continue;
629
630 vpd83 = lsm_volume_vpd83_get (lsm_vol);
631 if (vpd83 == NULL || strlen (vpd83) == 0)
632 continue;
633
634 pl_id = lsm_volume_pool_id_get (lsm_vol);
635
636 if (pl_id == NULL || strlen (pl_id) == 0)
637 continue;
638
639 lsm_conn_data = (struct _LsmConnData *) g_malloc (sizeof (struct _LsmConnData));
640
641 lsm_conn_data->lsm_conn = lsm_conn;
642 lsm_conn_data->lsm_vol = lsm_volume_record_copy (lsm_vol);
643 g_assert (lsm_conn_data->lsm_vol != NULL);
644 lsm_conn_data->pl_id = g_strdup (pl_id);
645
646 g_hash_table_insert (_vpd83_2_lsm_conn_data_hash, g_strdup (vpd83), lsm_conn_data);
647 }
648 }
649
650 static void
651 _fill_lsm_pl_data (struct _LsmPlData *lsm_pl_data, lsm_pool *lsm_pl,
652 gint64 last_refresh_time)
653 {
654 const char *lsm_pl_status_info = NULL;
655 uint64_t lsm_pl_status = 0;
656
657 lsm_pl_status = lsm_pool_status_get (lsm_pl);
658 lsm_pl_status_info = lsm_pool_status_info_get (lsm_pl);
659
660 lsm_pl_data->last_refresh_time = last_refresh_time;
661 lsm_pl_data->status_info = g_strdup (lsm_pl_status_info);
662
663 if (lsm_pl_status & LSM_POOL_STATUS_OK)
664 {
665 lsm_pl_data->is_ok = TRUE;
666 }
667 else
668 {
669 lsm_pl_data->is_ok = FALSE;
670 }
671
672 if (lsm_pl_status & LSM_POOL_STATUS_DEGRADED)
673 {
674 lsm_pl_data->is_raid_degraded = TRUE;
675 lsm_pl_data->is_ok = FALSE;
676 }
677 else
678 {
679 lsm_pl_data->is_raid_degraded = FALSE;
680 }
681
682 if (lsm_pl_status & LSM_POOL_STATUS_ERROR)
683 {
684 lsm_pl_data->is_raid_error = TRUE;
685 lsm_pl_data->is_ok = FALSE;
686 }
687 else
688 {
689 lsm_pl_data->is_raid_error = FALSE;
690 }
691
692 if (lsm_pl_status & LSM_POOL_STATUS_VERIFYING)
693 {
694 lsm_pl_data->is_raid_verifying = TRUE;
695 lsm_pl_data->is_ok = FALSE;
696 }
697 else
698 {
699 lsm_pl_data->is_raid_verifying = FALSE;
700 }
701
702 if (lsm_pl_status & LSM_POOL_STATUS_RECONSTRUCTING)
703 {
704 lsm_pl_data->is_raid_reconstructing = TRUE;
705 lsm_pl_data->is_ok = FALSE;
706 }
707 else
708 {
709 lsm_pl_data->is_raid_reconstructing = FALSE;
710 }
711 return;
712 }
713
714
715 /*
716 * Refresh _LsmVriData in _vpd83_2_lsm_vri_data_hash for certain VPD83.
717 * If volume has been delete, update _vpd83_2_lsm_conn_data_hash also to
718 * reflect that.
719 */
720 static struct _LsmVriData *
721 _refresh_lsm_vri_data (struct _LsmConnData *lsm_conn_data,
722 const char *vpd83)
723 {
724 struct _LsmVriData *lsm_vri_data = NULL;
725 const char *orig_vpd83 = NULL;
726 struct _LsmVriData *orig_lsm_vri_data = NULL;
727 struct _LsmConnData *orig_lsm_conn_data = NULL;
728 lsm_volume_raid_type raid_type;
729 uint32_t strip_size, disk_count, min_io_size, opt_io_size;
730 int lsm_rc;
731
732 /* Remove _vpd83_2_lsm_vri_data_hash old entry */
733 g_hash_table_lookup_extended (_vpd83_2_lsm_vri_data_hash, vpd83,
734 (gpointer *) &orig_vpd83,
735 (gpointer *) &orig_lsm_vri_data);
736 if (orig_vpd83 != NULL)
737 g_hash_table_remove (_vpd83_2_lsm_vri_data_hash, orig_vpd83);
738
739 lsm_rc = lsm_volume_raid_info (lsm_conn_data->lsm_conn,
740 lsm_conn_data->lsm_vol, &raid_type,
741 &strip_size, &disk_count, &min_io_size,
742 &opt_io_size, LSM_CLIENT_FLAG_RSVD);
743
744 if (lsm_rc != LSM_ERR_OK)
745 {
746 if (lsm_rc == LSM_ERR_NOT_FOUND_VOLUME)
747 udisks_debug ("LSM: Volume %s deleted", vpd83);
748 else
749 udisks_warning ("LSM: Failed to retrieve RAID information of volume");
750
751 /* Remove _vpd83_2_lsm_conn_data_hash entry */
752 g_hash_table_lookup_extended (_vpd83_2_lsm_conn_data_hash, vpd83,
753 (gpointer *) &orig_vpd83,
754 (gpointer *) &orig_lsm_conn_data);
755
756 if (orig_vpd83 != NULL)
757 g_hash_table_remove (_vpd83_2_lsm_conn_data_hash, orig_vpd83);
758
759 return NULL;
760 }
761
762 lsm_vri_data = (struct _LsmVriData *) g_malloc (sizeof (struct _LsmVriData));
763 lsm_vri_data->raid_type_str = g_strdup (_lsm_raid_type_to_str (raid_type));
764 lsm_vri_data->min_io_size = min_io_size;
765 lsm_vri_data->opt_io_size = opt_io_size;
766 lsm_vri_data->raid_disk_count = disk_count;
767 lsm_vri_data->last_refresh_time = g_get_monotonic_time ();
768
769 g_hash_table_insert (_vpd83_2_lsm_vri_data_hash, g_strdup (vpd83), lsm_vri_data);
770
771 return lsm_vri_data;
772 }
773
774 /*
775 * Check _pl_id_2_lsm_pl_data_hash and _vpd83_2_lsm_conn_data_hash hash table
776 * to find out the struct _LsmPlData.
777 * If data is outdated, try to update it.
778 */
779 static struct _LsmPlData *
780 _lsm_pl_data_lookup (const char *vpd83)
781 {
782 struct _LsmConnData *lsm_conn_data = NULL;
783 struct _LsmPlData *lsm_pl_data = NULL;
784 GPtrArray *new_lsm_pl_array = NULL;
785 gint64 current_time = 0;
786 gint64 refresh_interval = 0;
787 const char *orig_pl_id;
788 struct _LsmPlData *orig_lsm_pl_data;
789
790 refresh_interval = std_lsm_refresh_time_get ();
791
792 if ((_vpd83_2_lsm_conn_data_hash == NULL) ||
793 (_pl_id_2_lsm_pl_data_hash == NULL))
794 return NULL;
795
796 lsm_conn_data = g_hash_table_lookup (_vpd83_2_lsm_conn_data_hash, vpd83);
797
798 if ((lsm_conn_data == NULL) || (lsm_conn_data->pl_id == NULL))
799 return NULL;
800
801 lsm_pl_data = g_hash_table_lookup (_pl_id_2_lsm_pl_data_hash, lsm_conn_data->pl_id);
802
803 if (lsm_pl_data == NULL)
804 return NULL;
805
806 current_time = g_get_monotonic_time ();
807
808 if ((current_time - lsm_pl_data->last_refresh_time) / 1000000 < refresh_interval)
809 return lsm_pl_data;
810
811 /* Refresh data is required. */
812 udisks_debug ("LSM: Refreshing Pool(id %s) data", lsm_conn_data->pl_id);
813 new_lsm_pl_array = _get_supported_lsm_pls (lsm_conn_data->lsm_conn, NULL);
814 _fill_pl_id_2_lsm_pl_data_hash (new_lsm_pl_array, current_time);
815 g_ptr_array_unref (new_lsm_pl_array);
816
817 /* Search again */
818 lsm_pl_data = g_hash_table_lookup (_pl_id_2_lsm_pl_data_hash, lsm_conn_data->pl_id);
819
820 if (_pl_id_2_lsm_pl_data_hash == NULL)
821 /* Normally old data should not be delete yet. */
822 return NULL;
823
824 if (lsm_pl_data->last_refresh_time != current_time)
825 {
826 udisks_debug ("LSM: _lsm_pl_data_lookup: pool deleted");
827
828 /* Pool got deleted, we should delete the old data */
829 g_hash_table_lookup_extended (_pl_id_2_lsm_pl_data_hash,
830 lsm_conn_data->pl_id,
831 (gpointer *) &orig_pl_id,
832 (gpointer *) &orig_lsm_pl_data);
833 if (orig_pl_id != NULL)
834 g_hash_table_remove (_pl_id_2_lsm_pl_data_hash, orig_pl_id);
835 return NULL;
836 }
837 return lsm_pl_data;
838 }
839
840 /*
841 * Search _vpd83_2_lsm_vri_data_hash, if not found or outdated, update it.
842 */
843 static struct _LsmVriData *
844 _lsm_vri_data_lookup (const char *vpd83)
845 {
846 struct _LsmConnData *lsm_conn_data = NULL;
847 struct _LsmVriData *lsm_vri_data = NULL;
848 gint64 current_time = 0;
849 gint64 refresh_interval = 0;
850
851 refresh_interval = std_lsm_refresh_time_get ();
852
853 if (_vpd83_2_lsm_conn_data_hash == NULL)
854 return NULL;
855
856 lsm_conn_data = g_hash_table_lookup (_vpd83_2_lsm_conn_data_hash, vpd83);
857
858 if (lsm_conn_data == NULL)
859 return NULL;
860
861 lsm_vri_data = g_hash_table_lookup (_vpd83_2_lsm_vri_data_hash, vpd83);
862
863 current_time = g_get_monotonic_time ();
864
865 if (lsm_vri_data != NULL && (current_time - lsm_vri_data->last_refresh_time) / 1000000 < refresh_interval)
866 return lsm_vri_data;
867
868 /* Refresh data is required. */
869 udisks_debug ("LSM: Refreshing VRI data for %s", vpd83);
870 return _refresh_lsm_vri_data (lsm_conn_data, vpd83);
871 }
872
873 static void
874 _free_lsm_connect (gpointer data)
875 {
876 lsm_connect_close ((lsm_connect *) data, LSM_CLIENT_FLAG_RSVD);
877 }
878
879 static void
880 _free_lsm_uri_set (gpointer data)
881 {
882 struct _LsmUriSet *lsm_uri_set = (struct _LsmUriSet *) data;
883 if (lsm_uri_set != NULL)
884 {
885 g_free (lsm_uri_set->uri);
886 g_free (lsm_uri_set->password);
887 }
888 g_free (lsm_uri_set);
889 }
890
891 static void
892 _free_lsm_conn_data (gpointer data)
893 {
894 struct _LsmConnData *lsm_conn_data = (struct _LsmConnData *) data;
895
896 if (lsm_conn_data != NULL)
897 {
898 lsm_volume_record_free (lsm_conn_data->lsm_vol);
899 g_free (lsm_conn_data->pl_id);
900 g_free (lsm_conn_data);
901 }
902 }
903
904 static void
905 _free_lsm_pl_data (gpointer data)
906 {
907 struct _LsmPlData *lsm_pl_data = (struct _LsmPlData *) data;
908
909 if (lsm_pl_data != NULL)
910 {
911 g_free (lsm_pl_data->status_info);
912 g_free (lsm_pl_data);
913 }
914 }
915
916 static void
917 _free_lsm_vri_data (gpointer data)
918 {
919 struct _LsmVriData *lsm_vri_data = (struct _LsmVriData *) data;
920
921 if (lsm_vri_data != NULL)
922 {
923 g_free (lsm_vri_data->raid_type_str);
924 g_free (lsm_vri_data);
925 }
926 }
927
928 gboolean
929 std_lsm_data_init (UDisksDaemon *daemon,
930 GError **error)
931 {
932 struct _LsmUriSet *lsm_uri_set = NULL;
933 lsm_connect *lsm_conn = NULL;
934 GPtrArray *lsm_vol_array = NULL;
935 GPtrArray *lsm_pl_array = NULL;
936 guint i = 0;
937 gboolean success = FALSE;
938
939 if (! _load_module_conf (daemon, error))
940 return FALSE;
941
942 _all_lsm_conn_array = g_ptr_array_new_full (0, (GDestroyNotify) _free_lsm_connect);
943
944 _vpd83_2_lsm_conn_data_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
945 (GDestroyNotify) g_free,
946 (GDestroyNotify) _free_lsm_conn_data);
947
948 _pl_id_2_lsm_pl_data_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
949 (GDestroyNotify) g_free,
950 (GDestroyNotify) _free_lsm_pl_data);
951
952 _vpd83_2_lsm_vri_data_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
953 (GDestroyNotify) g_free,
954 (GDestroyNotify) _free_lsm_vri_data);
955
956 _supported_sys_id_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
957 (GDestroyNotify) g_free,
958 NULL);
959
960 /* fail globally in case none URI can be initialized */
961 for (i = 0; i < _conf_lsm_uri_sets->len; ++i)
962 {
963 gboolean rc = FALSE;
964 GError *local_error = NULL;
965
966 lsm_uri_set = g_ptr_array_index (_conf_lsm_uri_sets, i);
967 lsm_conn = _create_lsm_connect (lsm_uri_set, &local_error);
968 if (lsm_conn == NULL)
969 {
970 if (! success && ! *error)
971 {
972 /* propagate the error */
973 g_propagate_error (error, local_error);
974 local_error = NULL;
975 }
976 else
977 g_clear_error (&local_error);
978 continue;
979 }
980 rc = _fill_supported_system_id_hash (lsm_conn, &local_error);
981 if (rc != TRUE)
982 {
983 if (! success && ! *error)
984 {
985 /* propagate the error */
986 g_propagate_error (error, local_error);
987 local_error = NULL;
988 }
989 else
990 g_clear_error (&local_error);
991 lsm_connect_close (lsm_conn, LSM_CLIENT_FLAG_RSVD);
992 continue;
993 }
994 g_ptr_array_add (_all_lsm_conn_array, lsm_conn);
995
996 lsm_vol_array = _get_supported_lsm_volumes (lsm_conn, &local_error);
997 if (lsm_vol_array == NULL)
998 {
999 if (! success && ! *error)
1000 {
1001 /* propagate the error */
1002 g_propagate_error (error, local_error);
1003 local_error = NULL;
1004 }
1005 else
1006 g_clear_error (&local_error);
1007 lsm_connect_close (lsm_conn, LSM_CLIENT_FLAG_RSVD);
1008 continue;
1009 }
1010 lsm_pl_array = _get_supported_lsm_pls (lsm_conn, NULL);
1011
1012 _fill_pl_id_2_lsm_pl_data_hash (lsm_pl_array, g_get_monotonic_time ());
1013 _fill_vpd83_2_lsm_conn_data_hash (lsm_conn, lsm_vol_array);
1014 g_ptr_array_unref (lsm_vol_array);
1015 g_ptr_array_unref (lsm_pl_array);
1016
1017 success = TRUE;
1018 }
1019
1020 if (success)
1021 g_clear_error (error);
1022
1023 return success;
1024 }
1025
1026 uint32_t
1027 std_lsm_refresh_time_get (void)
1028 {
1029 return _conf_refresh_interval;
1030 }
1031
1032 /*
1033 * Return struct StdLsmVolData for given VPD83.
1034 * The memory should be freed by std_lsm_vol_data_free ().
1035 */
1036 struct StdLsmVolData *
1037 std_lsm_vol_data_get (const char *vpd83)
1038 {
1039 struct StdLsmVolData *std_lsm_vol_data = NULL;
1040 struct _LsmPlData *lsm_pl_data = NULL;
1041 struct _LsmVriData *lsm_vri_data = NULL;
1042
1043 lsm_pl_data = _lsm_pl_data_lookup (vpd83);
1044 if (lsm_pl_data == NULL)
1045 goto out;
1046
1047 lsm_vri_data = _lsm_vri_data_lookup (vpd83);
1048 if (lsm_vri_data == NULL)
1049 goto out;
1050
1051 std_lsm_vol_data = (struct StdLsmVolData *) g_malloc (sizeof (struct StdLsmVolData));
1052
1053 strncpy (std_lsm_vol_data->raid_type, lsm_vri_data->raid_type_str, _MAX_RAID_TYPE_LEN);
1054 std_lsm_vol_data->raid_type[_MAX_RAID_TYPE_LEN - 1] = '\0';
1055
1056 strncpy (std_lsm_vol_data->status_info, lsm_pl_data->status_info, _MAX_STATUS_INFO_LEN);
1057
1058 std_lsm_vol_data->status_info[_MAX_STATUS_INFO_LEN - 1] = '\0';
1059
1060 std_lsm_vol_data->is_raid_degraded = lsm_pl_data->is_raid_degraded;
1061 std_lsm_vol_data->is_raid_reconstructing = lsm_pl_data->is_raid_reconstructing;
1062 std_lsm_vol_data->is_raid_verifying = lsm_pl_data->is_raid_verifying;
1063 std_lsm_vol_data->is_raid_error = lsm_pl_data->is_raid_error;
1064 std_lsm_vol_data->is_ok = lsm_pl_data->is_ok;
1065 std_lsm_vol_data->min_io_size = lsm_vri_data->min_io_size;
1066 std_lsm_vol_data->opt_io_size = lsm_vri_data->opt_io_size;
1067 std_lsm_vol_data->raid_disk_count = lsm_vri_data->raid_disk_count;
1068
1069 out:
1070 return std_lsm_vol_data;
1071 }
1072
1073 void
1074 std_lsm_vol_data_free (struct StdLsmVolData *std_lsm_vol_data)
1075 {
1076 g_free (std_lsm_vol_data);
1077 }
1078
1079 void
1080 std_lsm_data_teardown (void)
1081 {
1082 if (_conf_lsm_uri_sets)
1083 {
1084 g_ptr_array_unref (_conf_lsm_uri_sets);
1085 _conf_lsm_uri_sets = NULL;
1086 }
1087
1088 if (_supported_sys_id_hash)
1089 {
1090 g_hash_table_unref (_supported_sys_id_hash);
1091 _supported_sys_id_hash = NULL;
1092 }
1093
1094 if (_all_lsm_conn_array)
1095 {
1096 g_ptr_array_unref (_all_lsm_conn_array);
1097 _all_lsm_conn_array = NULL;
1098 }
1099
1100 if (_vpd83_2_lsm_conn_data_hash)
1101 {
1102 g_hash_table_unref (_vpd83_2_lsm_conn_data_hash);
1103 _vpd83_2_lsm_conn_data_hash = NULL;
1104 }
1105
1106 if (_vpd83_2_lsm_vri_data_hash)
1107 {
1108 g_hash_table_unref (_vpd83_2_lsm_vri_data_hash);
1109 _vpd83_2_lsm_vri_data_hash = NULL;
1110 }
1111
1112 if (_pl_id_2_lsm_pl_data_hash)
1113 {
1114 g_hash_table_unref (_pl_id_2_lsm_pl_data_hash);
1115 _pl_id_2_lsm_pl_data_hash = NULL;
1116 }
1117 }
1118
1119 void
1120 std_lsm_vpd83_list_refresh (void)
1121 {
1122 lsm_connect *lsm_conn = NULL;
1123 GPtrArray *lsm_pl_array = NULL;
1124 GPtrArray *lsm_vol_array = NULL;
1125 guint i;
1126
1127 udisks_debug ("LSM: std_lsm_vpd83_list_refresh ()");
1128
1129 if (_all_lsm_conn_array == NULL )
1130 return;
1131
1132 /* free old data:
1133 * _vpd83_2_lsm_conn_data_hash
1134 * _pl_id_2_lsm_pl_data_hash
1135 */
1136 g_hash_table_remove_all (_vpd83_2_lsm_conn_data_hash);
1137 g_hash_table_remove_all (_pl_id_2_lsm_pl_data_hash);
1138
1139 for (i = 0; i < _all_lsm_conn_array->len; ++i)
1140 {
1141 lsm_conn = g_ptr_array_index (_all_lsm_conn_array, i);
1142 if (lsm_conn == NULL)
1143 continue;
1144
1145 lsm_vol_array = _get_supported_lsm_volumes (lsm_conn, NULL);
1146 if (lsm_vol_array == NULL)
1147 continue;
1148 lsm_pl_array = _get_supported_lsm_pls (lsm_conn, NULL);
1149
1150 _fill_pl_id_2_lsm_pl_data_hash (lsm_pl_array, g_get_monotonic_time ());
1151 _fill_vpd83_2_lsm_conn_data_hash (lsm_conn, lsm_vol_array);
1152 g_ptr_array_unref (lsm_vol_array);
1153 g_ptr_array_unref (lsm_pl_array);
1154 }
1155 }
1156
1157 gboolean
1158 std_lsm_vpd83_is_managed (const char *vpd83)
1159 {
1160 if (vpd83 != NULL && _vpd83_2_lsm_conn_data_hash != NULL &&
1161 g_hash_table_lookup (_vpd83_2_lsm_conn_data_hash, vpd83))
1162 return TRUE;
1163 return FALSE;
1164 }