Debian Codegraph @

src/plugins/s390.c

raw
1 /*
2 * Copyright (C) 2015 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: Samantha N. Bueno <[email protected]>
18 */
19
20 #include <glib.h>
21 #include <glob.h>
22 #include <linux/fs.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <blockdev/utils.h>
26 #include <asm/dasd.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <sys/ioctl.h>
30
31 #include "s390.h"
32 #include "check_deps.h"
33
34 /**
35 * SECTION: s390
36 * @short_description: plugin for operations with s390
37 * @title: s390
38 * @include: s390.h
39 *
40 * A plugin for operations with s390 devices.
41 */
42
43 /**
44 * bd_s390_error_quark: (skip)
45 */
46 GQuark bd_s390_error_quark (void) {
47 return g_quark_from_static_string ("g-bd-s390-error-quark");
48 }
49
50
51 static volatile guint avail_deps = 0;
52 static GMutex deps_check_lock;
53
54 #define DEPS_DASDFMT 0
55 #define DEPS_DASDFMT_MASK (1 << DEPS_DASDFMT)
56 #define DEPS_LAST 1
57
58 static const UtilDep deps[DEPS_LAST] = {
59 /* dasdfmt doesn't return version info */
60 {"dasdfmt", NULL, NULL, NULL},
61 };
62
63
64 /**
65 * bd_s390_init:
66 *
67 * Initializes the plugin. **This function is called automatically by the
68 * library's initialization functions.**
69 *
70 */
71 gboolean bd_s390_init (void) {
72 /* nothing to do here */
73 return TRUE;
74 };
75
76 /**
77 * bd_s390_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_s390_close (void) {
84 g_atomic_int_set (&avail_deps, 0);
85 }
86
87 /**
88 * bd_s390_is_tech_avail:
89 * @tech: the queried tech
90 * @mode: a bit mask of queried modes of operation (#BDS390TechMode) 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_s390_is_tech_avail (BDS390Tech tech, guint64 mode, GError **error) {
97 switch (tech) {
98 case BD_S390_TECH_ZFCP:
99 /* all ZFCP-mode combinations are supported by this implementation of the
100 * plugin, nothing extra is needed */
101 return TRUE;
102 case BD_S390_TECH_DASD:
103 if (mode & BD_S390_TECH_MODE_MODIFY)
104 return check_deps (&avail_deps, DEPS_DASDFMT_MASK, deps, DEPS_LAST, &deps_check_lock, error);
105 else
106 return TRUE;
107 default:
108 g_set_error_literal (error, BD_S390_ERROR, BD_S390_ERROR_TECH_UNAVAIL, "Unknown technology");
109 return FALSE;
110 }
111 }
112
113 /**
114 * bd_s390_dasd_format:
115 * @dasd: dasd to format
116 * @extra: (nullable) (array zero-terminated=1): extra options for the formatting (right now
117 * passed to the 'dasdfmt' utility)
118 * @error: (out) (optional): place to store error (if any)
119 *
120 * Returns: whether dasdfmt was successful or not
121 *
122 * Tech category: %BD_S390_TECH_DASD-%BD_S390_TECH_MODE_MODIFY
123 */
124 gboolean bd_s390_dasd_format (const gchar *dasd, const BDExtraArg **extra, GError **error) {
125 gboolean rc = FALSE;
126 const gchar *argv[8] = {"dasdfmt", "-y", "-d", "cdl", "-b", "4096", NULL, NULL};
127
128 if (!check_deps (&avail_deps, DEPS_DASDFMT_MASK, deps, DEPS_LAST, &deps_check_lock, error))
129 return FALSE;
130
131 argv[6] = g_strdup_printf ("/dev/%s", dasd);
132
133 rc = bd_utils_exec_and_report_error (argv, extra, error);
134 g_free ((gchar *) argv[6]);
135 return rc;
136 }
137
138 /**
139 * bd_s390_dasd_needs_format:
140 * @dasd: dasd to check, given as device number
141 * @error: (out) (optional): place to store error (if any)
142 *
143 * Returns: whether a dasd needs dasdfmt run against it
144 *
145 * Tech category: %BD_S390_TECH_DASD-%BD_S390_TECH_MODE_QUERY
146 */
147 gboolean bd_s390_dasd_needs_format (const gchar *dasd, GError **error) {
148 gchar status[12];
149 gchar *path = NULL;
150 gchar *rc = NULL;
151 FILE *fd = NULL;
152
153 path = g_strdup_printf ("/sys/bus/ccw/drivers/dasd-eckd/%s/status", dasd);
154 fd = fopen (path, "r");
155 g_free (path);
156 if (!fd) {
157 g_set_error (error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
158 "Error checking status of device %s; device may not exist,"
159 " or status can not be read.", dasd);
160 return FALSE;
161 }
162
163 /* read 'status' value; will either be 'unformatted' or 'online' */
164 memset (status, 0, sizeof (status));
165 rc = fgets (status, 12, fd);
166 fclose (fd);
167
168 if (!rc) {
169 g_set_error (error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
170 "Error checking status of device %s.", dasd);
171 return FALSE;
172 }
173
174 if (g_ascii_strncasecmp (status, "unformatted", strlen (status)) == 0) {
175 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Device %s status is %s, needs dasdfmt.", dasd, status);
176 return TRUE;
177 }
178
179 return FALSE;
180 }
181
182 /**
183 * bd_s390_dasd_online:
184 * @dasd: dasd to switch online, given as device number
185 * @error: (out) (optional): place to store error (if any)
186 *
187 * Returns: whether a dasd was successfully switched online
188 *
189 * Tech category: %BD_S390_TECH_DASD-%BD_S390_TECH_MODE_MODIFY
190 */
191 gboolean bd_s390_dasd_online (const gchar *dasd, GError **error) {
192 gboolean rc = FALSE;
193 gint wrc = 0;
194 gint online = 0;
195 gchar *path = NULL;
196 FILE *fd = NULL;
197 const gchar *argv[4] = {"dasd_cio_free", "-d", dasd, NULL};
198 guint64 progress_id = 0;
199 gchar *msg = NULL;
200 GError *l_error = NULL;
201
202 msg = g_strdup_printf ("Started switching '%s' online", dasd);
203 progress_id = bd_utils_report_started (msg);
204 g_free (msg);
205
206 path = g_strdup_printf ("/sys/bus/ccw/drivers/dasd-eckd/%s/online", dasd);
207 fd = fopen (path, "r+");
208 if (!fd) {
209 /* DASD might be in device ignore list; try to rm it */
210 rc = bd_utils_exec_and_report_error_no_progress (argv, NULL, &l_error);
211 if (!rc) {
212 g_free (path);
213 bd_utils_report_finished (progress_id, l_error->message);
214 g_propagate_error (error, l_error);
215 return rc;
216 }
217 /* fd is NULL at this point, so try to open it */
218 fd = fopen (path, "r+");
219 g_free (path);
220
221 if (!fd) {
222 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
223 "Could not open device %s even after removing it from"
224 " the device ignore list.", dasd);
225 bd_utils_report_finished (progress_id, l_error->message);
226 g_propagate_error (error, l_error);
227 return FALSE;
228 }
229 }
230 else {
231 g_free (path);
232 }
233
234 /* check whether our DASD is online; if not, set it */
235 online = fgetc (fd);
236
237 if (online == EOF) {
238 /* there was some error checking the 'online' status */
239 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
240 "Error checking if device %s is online", dasd);
241 fclose (fd);
242 bd_utils_report_finished (progress_id, l_error->message);
243 g_propagate_error (error, l_error);
244 return FALSE;
245 }
246 if (online == '1') {
247 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
248 "DASD device %s is already online.", dasd);
249 fclose (fd);
250 bd_utils_report_finished (progress_id, l_error->message);
251 g_propagate_error (error, l_error);
252 return FALSE;
253 }
254 else {
255 /* reset file cursor before writing to it */
256 rewind (fd);
257 wrc = fputs ("1", fd);
258 }
259
260 fclose (fd);
261
262 if (wrc == EOF) {
263 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
264 "Could not set DASD device %s online", dasd);
265 bd_utils_report_finished (progress_id, l_error->message);
266 g_propagate_error (error, l_error);
267 return FALSE;
268 }
269
270 bd_utils_report_finished (progress_id, "Completed");
271 return TRUE;
272 }
273
274 /**
275 * bd_s390_dasd_is_ldl:
276 * @dasd: dasd to check, whether it is LDL formatted
277 * @error: (out) (optional): place to store error (if any)
278 *
279 * Returns: whether a dasd is LDL formatted
280 *
281 * Tech category: %BD_S390_TECH_DASD-%BD_S390_TECH_MODE_QUERY
282 */
283 gboolean bd_s390_dasd_is_ldl (const gchar *dasd, GError **error) {
284 gchar *devname = NULL;
285 gint f = 0;
286 gint blksize = 0;
287 dasd_information2_t dasd_info;
288
289 memset (&dasd_info, 0, sizeof (dasd_info));
290
291 /* complete the device name */
292 if (g_str_has_prefix (dasd, "/dev/")) {
293 devname = g_strdup (dasd);
294 }
295 else {
296 devname = g_strdup_printf ("/dev/%s", dasd);
297 }
298
299 /* open the device */
300 if ((f = open (devname, O_RDONLY)) == -1) {
301 g_set_error (error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
302 "Unable to open device %s", devname);
303 g_free (devname);
304 return FALSE;
305 }
306
307 g_free (devname);
308
309 /* check if this is a block device */
310 if (ioctl (f, BLKSSZGET, &blksize) != 0) {
311 close (f);
312 return FALSE;
313 }
314
315 /* get some info about DASD */
316 if (ioctl (f, BIODASDINFO2, &dasd_info) != 0) {
317 close (f);
318 return FALSE;
319 }
320
321 close (f);
322
323 /* check we're not on an FBA DASD, since dasdfmt can't run on them */
324 if (strncmp (dasd_info.type, "FBA", 3) == 0) {
325 return FALSE;
326 }
327
328 /* check dasd format */
329 return dasd_info.format == DASD_FORMAT_LDL;
330 }
331
332 /**
333 * bd_s390_dasd_is_fba:
334 * @dasd: dasd to check, whether it is FBA
335 * @error: (out) (optional): place to store error (if any)
336 *
337 * Returns: whether a dasd is FBA
338 *
339 * Tech category: %BD_S390_TECH_DASD-%BD_S390_TECH_MODE_QUERY
340 */
341 gboolean bd_s390_dasd_is_fba (const gchar *dasd, GError **error) {
342 gchar *devname = NULL;
343 gint f = 0;
344 gint blksize = 0;
345 dasd_information2_t dasd_info;
346
347 memset (&dasd_info, 0, sizeof (dasd_info));
348
349 /* complete the device name */
350 if (g_str_has_prefix (dasd, "/dev/")) {
351 devname = g_strdup (dasd);
352 }
353 else {
354 devname = g_strdup_printf ("/dev/%s", dasd);
355 }
356
357 /* open the device */
358 if ((f = open (devname, O_RDONLY)) == -1) {
359 g_set_error (error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
360 "Unable to open device %s", devname);
361 g_free (devname);
362 return FALSE;
363 }
364
365 g_free (devname);
366
367 /* check if this is a block device */
368 if (ioctl (f, BLKSSZGET, &blksize) != 0) {
369 close (f);
370 return FALSE;
371 }
372
373 /* get some info about DASD */
374 if (ioctl (f, BIODASDINFO2, &dasd_info) != 0) {
375 close (f);
376 return FALSE;
377 }
378
379 close (f);
380
381 /* check if we're on an FBA DASD */
382 return strncmp (dasd_info.type, "FBA", 3) == 0;
383 }
384
385 /**
386 * bd_s390_sanitize_dev_input:
387 * @dev: a DASD or zFCP device number
388 * @error: (out) (optional): place to store error (if any)
389 *
390 * Returns: (transfer full): a synthesized dasd or zfcp device number
391 *
392 * Tech category: always available
393 */
394 gchar* bd_s390_sanitize_dev_input (const gchar *dev, GError **error) {
395 gchar *tok = NULL;
396 gchar *tmptok = NULL;
397 gchar *prepend = NULL;
398 gchar *ret = NULL;
399 gchar *lcdev = NULL;
400
401 /* first make sure we're not being played */
402 if ((dev == NULL) || (!*dev)) {
403 g_set_error_literal (error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
404 "Device number not specified or invalid");
405 return NULL;
406 }
407
408 /* convert everything to lowercase */
409 lcdev = g_ascii_strdown (dev, -1);
410
411 /* we only care about the last piece of the device number, since
412 * that's the only part which will need formatting */
413 tok = g_strrstr (lcdev, ".");
414
415 if (tok) {
416 tmptok = tok + 1; /* want to ignore the delimiter char */
417 }
418 else {
419 tmptok = lcdev;
420 }
421
422 /* calculate if/how much to pad tok with */
423 if (strlen (tmptok) < 4)
424 prepend = g_strnfill ((4 - strlen (tmptok)), '0');
425
426 /* combine it all together */
427 if (prepend == NULL) {
428 ret = g_strdup_printf ("0.0.%s", tmptok);
429 }
430 else {
431 ret = g_strdup_printf ("0.0.%s%s", prepend, tmptok);
432 }
433 g_free (prepend);
434 g_free (lcdev);
435
436 return ret;
437 }
438
439 /**
440 * bd_s390_zfcp_sanitize_wwpn_input:
441 * @wwpn: a zFCP WWPN identifier
442 * @error: (out) (optional): place to store error (if any)
443 *
444 * Returns: (transfer full): a synthesized zFCP WWPN
445 *
446 * Tech category: always available
447 */
448 gchar* bd_s390_zfcp_sanitize_wwpn_input (const gchar *wwpn, GError **error) {
449 gchar *fullwwpn = NULL;
450 gchar *lcwwpn = NULL;
451
452 /* first make sure we're not being played */
453 if ((wwpn == NULL) || (!*wwpn) || (strlen (wwpn) < 2)) {
454 g_set_error_literal (error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
455 "WWPN not specified or invalid");
456 return NULL;
457 }
458
459 /* convert everything to lowercase */
460 lcwwpn = g_ascii_strdown (wwpn, -1);
461
462 if (strncmp (lcwwpn, "0x", 2) == 0) {
463 /* user entered a properly formatted wwpn */
464 fullwwpn = g_strdup_printf ("%s", lcwwpn);
465 }
466 else {
467 fullwwpn = g_strdup_printf ("0x%s", lcwwpn);
468 }
469 g_free (lcwwpn);
470 return fullwwpn;
471 }
472
473 /**
474 * bd_s390_zfcp_sanitize_lun_input:
475 * @lun: a zFCP LUN identifier
476 * @error: (out) (optional): place to store error (if any)
477 *
478 * Returns: (transfer full): a synthesized zFCP LUN
479 *
480 * Tech category: always available
481 */
482 gchar* bd_s390_zfcp_sanitize_lun_input (const gchar *lun, GError **error) {
483 gchar *lclun = NULL;
484 gchar *tmplun = NULL;
485 gchar *fulllun = NULL;
486 gchar *prepend = NULL;
487 gchar *append = NULL;
488
489 /* first make sure we're not being played */
490 if ((lun == NULL) || (!*lun) || (strlen (lun) > 18)) {
491 g_set_error_literal (error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
492 "LUN not specified or invalid");
493 return NULL;
494 }
495
496 /* convert everything to lowercase */
497 lclun = g_ascii_strdown (lun, -1);
498
499 if ((g_str_has_prefix (lclun, "0x")) && (strlen (lclun) == 18)) {
500 /* user entered a properly formatted lun */
501 fulllun = g_strdup_printf ("%s", lclun);
502 }
503 else {
504 /* we need to mangle the input to make it proper. ugh. */
505 if (g_str_has_prefix (lclun, "0x")) {
506 /* this may seem odd, but it makes the math easier a ways down */
507 tmplun = lclun + 2;
508 }
509 else {
510 tmplun = lclun;
511 }
512
513 if (strlen (tmplun) < 4) {
514 /* check if/how many zeros we pad to the left */
515 prepend = g_strnfill ((4 - strlen (tmplun)), '0');
516 /* check if/how many zeros we pad to the right */
517 append = g_strnfill ((16 - (strlen (tmplun) + strlen (prepend))), '0');
518 }
519 else {
520 /* didn't need to pad anything on the left; so just check if/how
521 * many zeros we pad to the right */
522 append = g_strnfill ((16 - (strlen (tmplun))), '0');
523 }
524
525 /* now combine everything together */
526 if (prepend == NULL) {
527 fulllun = g_strdup_printf ("0x%s%s", tmplun, append);
528 }
529 else {
530 fulllun = g_strdup_printf ("0x%s%s%s", prepend, tmplun, append);
531 }
532 }
533 g_free (lclun);
534 g_free (prepend);
535 g_free (append);
536
537 return fulllun;
538 }
539
540 /**
541 * bd_s390_zfcp_online:
542 * @devno: zfcp device number
543 * @wwpn: zfcp WWPN (World Wide Port Number)
544 * @lun: zfcp LUN (Logical Unit Number)
545 * @error: (out) (optional): place to store error (if any)
546 *
547 * Returns: whether a zfcp device was successfully switched online
548 *
549 * Tech category: %BD_S390_TECH_ZFCP-%BD_S390_TECH_MODE_MODIFY
550 */
551 gboolean bd_s390_zfcp_online (const gchar *devno, const gchar *wwpn, const gchar *lun, GError **error) {
552 gboolean boolrc = FALSE;
553 gint rc = 0;
554 FILE *fd = NULL;
555 DIR *pdfd = NULL;
556 const gchar *zfcp_cio_free[4] = {"zfcp_cio_free", "-d", devno, NULL};
557 const gchar *chccwdev[4] = {"chccwdev", "-e", devno, NULL};
558
559 gchar *zfcpsysfs = "/sys/bus/ccw/drivers/zfcp";
560 gchar *online = g_strdup_printf ("%s/%s/online", zfcpsysfs, devno);
561 gchar *portdir = NULL;
562 gchar *unitadd = NULL;
563 gchar *failed = NULL;
564 guint64 progress_id = 0;
565 gchar *msg = NULL;
566 GError *l_error = NULL;
567
568 msg = g_strdup_printf ("Started switching zfcp '%s' online", devno);
569 progress_id = bd_utils_report_started (msg);
570 g_free (msg);
571
572 /* part 01: make sure device is available/not on device ignore list */
573 fd = fopen (online, "r");
574 if (!fd) {
575 boolrc = bd_utils_exec_and_report_error_no_progress (zfcp_cio_free, NULL, NULL);
576 if (!boolrc) {
577 g_free (online);
578 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
579 "Could not remove device %s from device ignore list.", devno);
580 bd_utils_report_finished (progress_id, l_error->message);
581 g_propagate_error (error, l_error);
582 return FALSE;
583 }
584 /* fd is NULL at this point, so try to open it again */
585 fd = fopen (online, "r");
586
587 /* still no luck, fail */
588 if (!fd) {
589 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
590 "Could not open device %s even after removing it from" " the device ignore list.", devno);
591 g_free (online);
592 bd_utils_report_finished (progress_id, l_error->message);
593 g_propagate_error (error, l_error);
594 return FALSE;
595 }
596 }
597 g_free (online);
598
599 /* part 02: check to make sure/turn device online */
600 rc = fgetc (fd);
601 if (rc == EOF) {
602 /* there was some error checking the 'online' status */
603 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_IO,
604 "Error checking if device %s is online", devno);
605 fclose (fd);
606 bd_utils_report_finished (progress_id, l_error->message);
607 g_propagate_error (error, l_error);
608 return FALSE;
609 }
610 if (rc == '1') {
611 /* otherwise device's status indicates that it's already online, so
612 just close the fd and proceed; we don't return because although 'online'
613 status may be correct, the device may not be completely online and ready
614 to use just yet, so just throw a warning. */
615 fclose (fd);
616 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Device %s is already online", devno);
617 }
618 else {
619 /* offline */
620 fclose (fd);
621 boolrc = bd_utils_exec_and_report_error_no_progress (chccwdev, NULL, NULL);
622 if (!boolrc) {
623 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
624 "Could not set zFCP device %s online", devno);
625 bd_utils_report_finished (progress_id, l_error->message);
626 g_propagate_error (error, l_error);
627 return FALSE;
628 }
629 }
630
631 /* part 03: set other properties to use the device */
632 /* check this dir exists */
633 portdir = g_strdup_printf ("%s/%s/%s", zfcpsysfs, devno, wwpn);
634 pdfd = opendir (portdir);
635 if (!pdfd) {
636 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
637 "WWPN %s not found for zFCP device %s", wwpn, devno);
638 g_free (portdir);
639 bd_utils_report_finished (progress_id, l_error->message);
640 g_propagate_error (error, l_error);
641 return FALSE;
642 }
643 closedir (pdfd);
644
645 unitadd = g_strdup_printf ("%s/unit_add", portdir);
646 fd = fopen (unitadd, "w");
647 if (!fd) {
648 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_IO,
649 "Could not open %s", unitadd);
650 g_free (unitadd);
651 g_free (portdir);
652 bd_utils_report_finished (progress_id, l_error->message);
653 g_propagate_error (error, l_error);
654 return FALSE;
655 }
656 rc = fputs (lun, fd);
657 if (rc == EOF) {
658 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_IO,
659 "Could not add LUN %s to WWPN %s on zFCP device %s", lun, wwpn, devno);
660 g_free (unitadd);
661 g_free (portdir);
662 fclose (fd);
663 bd_utils_report_finished (progress_id, l_error->message);
664 g_propagate_error (error, l_error);
665 return FALSE;
666 }
667 g_free (unitadd);
668 fclose (fd);
669
670 /* part 04: other error checking to verify device turned on properly */
671 failed = g_strdup_printf ("%s/%s/failed", portdir, lun);
672 fd = fopen (failed, "r");
673 if (!fd) {
674 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_IO,
675 "Could not open %s", failed);
676 g_free (failed);
677 g_free (portdir);
678 bd_utils_report_finished (progress_id, l_error->message);
679 g_propagate_error (error, l_error);
680 return FALSE;
681 }
682
683 rc = fgetc (fd);
684 if (rc == EOF) {
685 /* there was some error checking the 'failed' status */
686 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_IO,
687 "Could not read failed attribute of LUN %s at WWPN %s on" " zFCP device %s", lun, wwpn, devno);
688 g_free (failed);
689 g_free (portdir);
690 fclose (fd);
691 bd_utils_report_finished (progress_id, l_error->message);
692 g_propagate_error (error, l_error);
693 return FALSE;
694 }
695 /* read value here is either 0 or 1; fgetc casts this from char->int, so
696 subtract '0' here to get the literal read value */
697 rc -= '0';
698 if (rc != 0) {
699 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
700 "Failed LUN %s at WWPN %s on zFCP device %s removed again", lun, wwpn, devno);
701 g_free (failed);
702 g_free (portdir);
703 fclose (fd);
704 bd_utils_report_finished (progress_id, l_error->message);
705 g_propagate_error (error, l_error);
706 return FALSE;
707 }
708 /* if you haven't failed yet, you deserve this */
709 g_free (failed);
710 g_free (portdir);
711 fclose (fd);
712 bd_utils_report_finished (progress_id, "Completed");
713 return TRUE;
714 }
715
716 /**
717 * bd_s390_zfcp_scsi_offline:
718 * @devno: zfcp device number
719 * @wwpn: zfcp WWPN (World Wide Port Number)
720 * @lun: zfcp LUN (Logical Unit Number)
721 * @error: (out) (optional): place to store error (if any)
722 *
723 * Returns: whether a LUN was successfully removed from its associated WWPN
724 *
725 * This function looks through /proc/scsi/scsi and manually removes LUNs from
726 * associated WWPNs. zFCP devices are SCSI devices accessible over FCP protocol.
727 * In z/OS the IODF (I/O definition file) contains basic information about the
728 * I/O config, but WWPN and LUN configuration is done at the OS level, hence
729 * this function becomes necessary when switching the device offline. This
730 * particular sequence of actions is for some reason unnecessary when switching
731 * the device online. Chalk it up to s390x being s390x.
732 *
733 * Tech category: %BD_S390_TECH_ZFCP-%BD_S390_TECH_MODE_MODIFY
734 */
735 gboolean bd_s390_zfcp_scsi_offline (const gchar *devno, const gchar *wwpn, const gchar *lun, GError **error) {
736 FILE *scsifd = NULL;
737 FILE *fd = NULL;
738 size_t len = 0;
739 ssize_t read, rc;
740
741 const gchar *delim = " ";
742 gchar *channel = "0";
743 gchar *devid = "0";
744 gchar *path = "/proc/scsi/scsi";
745 gchar *scsidevsysfs = "/sys/bus/scsi/devices";
746
747 gchar *line = NULL;
748 gchar *fcphbasysfs = NULL;
749 gchar *fcpwwpnsysfs = NULL;
750 gchar *fcplunsysfs = NULL;
751 gchar *hba_path = NULL;
752 gchar *wwpn_path = NULL;
753 gchar *lun_path = NULL;
754 gchar *scsidev = NULL;
755 gchar *fcpsysfs = NULL;
756 gchar *scsidel = NULL;
757 gchar **tokens = NULL;
758 guint64 progress_id = 0;
759 gchar *msg = NULL;
760 GError *l_error = NULL;
761
762 msg = g_strdup_printf ("Started switching zfcp scsi '%s' offline", devno);
763 progress_id = bd_utils_report_started (msg);
764 g_free (msg);
765
766 scsifd = fopen (path, "r");
767 if (!scsifd) {
768 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
769 "Failed to open path to SCSI device: %s", path);
770 bd_utils_report_finished (progress_id, l_error->message);
771 g_propagate_error (error, l_error);
772 return FALSE;
773 }
774
775 while ((read = getline (&line, &len, scsifd)) != -1) {
776 if (!g_str_has_prefix (line, "Host")) {
777 continue;
778 }
779
780 /* tokenize line and assign certain values we'll need later */
781 tokens = g_strsplit (line, delim, 8);
782
783 scsidev = g_strdup_printf ("%s:%s:%s:%s", tokens[1] /* host */ + 4, channel, devid, tokens[7] /* fcplun */);
784 scsidev = g_strchomp (scsidev);
785 fcpsysfs = g_strdup_printf ("%s/%s", scsidevsysfs, scsidev);
786 fcpsysfs = g_strchomp (fcpsysfs);
787 g_strfreev (tokens);
788
789 /* get HBA path value (same as device number) */
790 hba_path = g_strdup_printf ("%s/hba_id", fcpsysfs);
791 len = 0; /* should be zero, but re-set it just in case */
792 fd = fopen (hba_path, "r");
793 if (!fd) {
794 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
795 "Failed to open %s: %m", hba_path);
796 g_free (hba_path);
797 g_free (fcpsysfs);
798 g_free (scsidev);
799 fclose (scsifd);
800 g_free (line);
801 bd_utils_report_finished (progress_id, l_error->message);
802 g_propagate_error (error, l_error);
803 return FALSE;
804 }
805 rc = getline (&fcphbasysfs, &len, fd);
806 if (rc == -1) {
807 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
808 "Failed to read value from %s: %m", hba_path);
809 fclose (fd);
810 g_free (hba_path);
811 g_free (fcpsysfs);
812 g_free (scsidev);
813 fclose (scsifd);
814 g_free (line);
815 bd_utils_report_finished (progress_id, l_error->message);
816 g_propagate_error (error, l_error);
817 return FALSE;
818 }
819 g_strchomp (fcphbasysfs);
820 fclose (fd);
821 g_free (hba_path);
822
823 /* get WWPN value */
824 wwpn_path = g_strdup_printf ("%s/wwpn", fcpsysfs);
825 len = 0;
826 fd = fopen (wwpn_path, "r");
827 if (!fd) {
828 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
829 "Failed to open %s: %m", wwpn_path);
830 g_free (wwpn_path);
831 g_free (fcphbasysfs);
832 g_free (fcpsysfs);
833 g_free (scsidev);
834 fclose (scsifd);
835 g_free (line);
836 bd_utils_report_finished (progress_id, l_error->message);
837 g_propagate_error (error, l_error);
838 return FALSE;
839 }
840 rc = getline (&fcpwwpnsysfs, &len, fd);
841 if (rc == -1) {
842 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
843 "Failed to read value from %s", wwpn_path);
844 g_free (wwpn_path);
845 g_free (fcphbasysfs);
846 g_free (fcpsysfs);
847 g_free (scsidev);
848 fclose (fd);
849 fclose (scsifd);
850 g_free (line);
851 bd_utils_report_finished (progress_id, l_error->message);
852 g_propagate_error (error, l_error);
853 return FALSE;
854 }
855 g_strchomp (fcpwwpnsysfs);
856 fclose (fd);
857 g_free (wwpn_path);
858
859 /* read LUN value */
860 lun_path = g_strdup_printf ("%s/fcp_lun", fcpsysfs);
861 len = 0;
862 fd = fopen (lun_path, "r");
863 if (!fd) {
864 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
865 "Failed to open %s: %m", lun_path);
866 g_free (lun_path);
867 g_free (fcpwwpnsysfs);
868 g_free (fcphbasysfs);
869 g_free (fcpsysfs);
870 g_free (scsidev);
871 fclose (scsifd);
872 g_free (line);
873 bd_utils_report_finished (progress_id, l_error->message);
874 g_propagate_error (error, l_error);
875 return FALSE;
876 }
877 rc = getline (&fcplunsysfs, &len, fd);
878 if (rc == -1) {
879 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
880 "Failed to read value from %s", lun_path);
881 fclose (fd);
882 g_free (lun_path);
883 g_free (fcpwwpnsysfs);
884 g_free (fcphbasysfs);
885 g_free (fcpsysfs);
886 g_free (scsidev);
887 fclose (scsifd);
888 g_free (line);
889 bd_utils_report_finished (progress_id, l_error->message);
890 g_propagate_error (error, l_error);
891 return FALSE;
892 }
893 g_strchomp (fcplunsysfs);
894 fclose (fd);
895 g_free (lun_path);
896 g_free (fcpsysfs);
897
898 /* make sure read values align with expected values */
899 scsidel = g_strdup_printf ("%s/%s/delete", scsidevsysfs, scsidev);
900 scsidel = g_strchomp (scsidel);
901 if (g_strcmp0 (fcphbasysfs, devno) == 0 && g_strcmp0 (fcpwwpnsysfs, wwpn) == 0 && g_strcmp0 (fcplunsysfs, lun) == 0) {
902 fd = fopen (scsidel, "w");
903 if (!fd) {
904 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
905 "Failed to open %s", scsidel);
906 g_free (scsidel);
907 g_free (fcplunsysfs);
908 g_free (fcpwwpnsysfs);
909 g_free (fcphbasysfs);
910 g_free (scsidev);
911 fclose (scsifd);
912 g_free (line);
913 bd_utils_report_finished (progress_id, l_error->message);
914 g_propagate_error (error, l_error);
915 return FALSE;
916 }
917 rc = fputs ("1", fd);
918 if (rc == EOF) {
919 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
920 "Could not write to %s", scsidel);
921 fclose (fd);
922 g_free (scsidel);
923 g_free (fcplunsysfs);
924 g_free (fcpwwpnsysfs);
925 g_free (fcphbasysfs);
926 g_free (scsidev);
927 fclose (scsifd);
928 g_free (line);
929 bd_utils_report_finished (progress_id, l_error->message);
930 g_propagate_error (error, l_error);
931 return FALSE;
932 }
933 fclose (fd);
934 }
935
936 g_free (scsidel);
937 scsidel = NULL;
938 g_free (scsidev);
939 scsidev = NULL;
940 g_free (fcplunsysfs);
941 fcplunsysfs = NULL;
942 g_free (fcpwwpnsysfs);
943 fcpwwpnsysfs = NULL;
944 g_free (fcphbasysfs);
945 fcphbasysfs = NULL;
946 }
947 fclose (scsifd);
948 g_free (line);
949 bd_utils_report_finished (progress_id, "Completed");
950 return TRUE;
951 }
952
953 /**
954 * bd_s390_zfcp_offline:
955 * @devno: zfcp device number
956 * @wwpn: zfcp WWPN (World Wide Port Number)
957 * @lun: zfcp LUN (Logical Unit Number)
958 * @error: (out) (optional): place to store error (if any)
959 *
960 * Returns: whether a zfcp device was successfully switched offline
961 *
962 * Tech category: %BD_S390_TECH_ZFCP-%BD_S390_TECH_MODE_MODIFY
963 */
964 gboolean bd_s390_zfcp_offline (const gchar *devno, const gchar *wwpn, const gchar *lun, GError **error) {
965 gboolean success = FALSE;
966 gint rc = 0;
967 FILE *fd = NULL;
968 glob_t luns;
969
970 gchar *zfcpsysfs = "/sys/bus/ccw/drivers/zfcp";
971 gchar *offline = NULL;
972 gchar *unitrm = NULL;
973 gchar *pattern = NULL;
974 const gchar *chccwdev[4] = {"chccwdev", "-d", devno, NULL};
975 guint64 progress_id = 0;
976 gchar *msg = NULL;
977 GError *l_error = NULL;
978
979 msg = g_strdup_printf ("Started switching zfcp '%s' offline", devno);
980 progress_id = bd_utils_report_started (msg);
981 g_free (msg);
982
983 success = bd_s390_zfcp_scsi_offline (devno, wwpn, lun, NULL);
984 if (!success) {
985 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
986 "Could not correctly delete SCSI device of zFCP %s with WWPN %s, LUN %s", devno, wwpn, lun);
987 bd_utils_report_finished (progress_id, l_error->message);
988 g_propagate_error (error, l_error);
989 return FALSE;
990 }
991
992 /* remove lun */
993 unitrm = g_strdup_printf ("%s/%s/%s/unit_remove", zfcpsysfs, devno, wwpn);
994 fd = fopen (unitrm, "w");
995 if (!fd) {
996 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
997 "Failed to open %s", unitrm);
998 g_free (unitrm);
999 bd_utils_report_finished (progress_id, l_error->message);
1000 g_propagate_error (error, l_error);
1001 return FALSE;
1002 }
1003 rc = fputs (lun, fd);
1004 if (rc == EOF) {
1005 fclose (fd);
1006 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
1007 "Could not remove LUN %s at WWPN %s on zFCP device %s", lun, wwpn, devno);
1008 g_free (unitrm);
1009 bd_utils_report_finished (progress_id, l_error->message);
1010 g_propagate_error (error, l_error);
1011 return FALSE;
1012 }
1013 fclose (fd);
1014 g_free (unitrm);
1015 rc = 0;
1016
1017 /* gather the luns */
1018 pattern = g_strdup_printf ("%s/0x??????????????\?\?/0x????????????????", zfcpsysfs);
1019 rc = glob (pattern, GLOB_ONLYDIR, NULL, &luns);
1020 if (rc == GLOB_ABORTED || rc == GLOB_NOSPACE) {
1021 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
1022 "An error occurred trying to determine if other LUNs are still associated with WWPN %s", wwpn);
1023 globfree (&luns);
1024 g_free (pattern);
1025 bd_utils_report_finished (progress_id, l_error->message);
1026 g_propagate_error (error, l_error);
1027 return FALSE;
1028 }
1029 /* check if we have any matches found; if so, bail */
1030 if (luns.gl_pathc > 0) {
1031 g_set_error_literal (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
1032 "Not setting zFCP device offline since it still has other LUNs");
1033 globfree (&luns);
1034 g_free (pattern);
1035 bd_utils_report_finished (progress_id, l_error->message);
1036 g_propagate_error (error, l_error);
1037 return FALSE;
1038 }
1039 globfree (&luns);
1040 g_free (pattern);
1041 rc = 0;
1042
1043 /* offline */
1044 offline = g_strdup_printf ("%s/%s/online", zfcpsysfs, devno);
1045 success = bd_utils_exec_and_report_error_no_progress (chccwdev, NULL, NULL);
1046 g_free (offline);
1047 if (!success) {
1048 g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE,
1049 "Could not set zFCP device %s offline", devno);
1050 bd_utils_report_finished (progress_id, l_error->message);
1051 g_propagate_error (error, l_error);
1052 return FALSE;
1053 }
1054
1055 bd_utils_report_finished (progress_id, "Completed");
1056 return TRUE;
1057 }