multipathd: remove references to sysfs_device
[platform/upstream/multipath-tools.git] / multipathd / main.c
1 /*
2  * Copyright (c) 2004, 2005 Christophe Varoqui
3  * Copyright (c) 2005 Kiyoshi Ueda, NEC
4  * Copyright (c) 2005 Benjamin Marzinski, Redhat
5  * Copyright (c) 2005 Edward Goggin, EMC
6  */
7 #include <unistd.h>
8 #include <sys/stat.h>
9 #include <libdevmapper.h>
10 #include <wait.h>
11 #include <sys/mman.h>
12 #include <sys/types.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <sys/time.h>
16 #include <sys/resource.h>
17 #include <limits.h>
18 #include <linux/oom.h>
19 #include <mpath_persist.h>
20
21 /*
22  * libcheckers
23  */
24 #include <checkers.h>
25
26 /*
27  * libmultipath
28  */
29 #include <parser.h>
30 #include <vector.h>
31 #include <memory.h>
32 #include <config.h>
33 #include <util.h>
34 #include <hwtable.h>
35 #include <defaults.h>
36 #include <structs.h>
37 #include <callout.h>
38 #include <blacklist.h>
39 #include <structs_vec.h>
40 #include <dmparser.h>
41 #include <devmapper.h>
42 #include <sysfs.h>
43 #include <dict.h>
44 #include <discovery.h>
45 #include <debug.h>
46 #include <propsel.h>
47 #include <uevent.h>
48 #include <switchgroup.h>
49 #include <print.h>
50 #include <configure.h>
51 #include <prio.h>
52 #include <pgpolicies.h>
53 #include <uevent.h>
54
55 #include "main.h"
56 #include "pidfile.h"
57 #include "uxlsnr.h"
58 #include "uxclnt.h"
59 #include "cli.h"
60 #include "cli_handlers.h"
61 #include "lock.h"
62 #include "waiter.h"
63
64 #define FILE_NAME_SIZE 256
65 #define CMDSIZE 160
66
67 #define LOG_MSG(a, b) \
68 do { \
69         if (pp->offline) \
70                 condlog(a, "%s: %s - path offline", pp->mpp->alias, pp->dev); \
71         else if (strlen(b)) \
72                 condlog(a, "%s: %s - %s", pp->mpp->alias, pp->dev, b); \
73 } while(0)
74
75 struct mpath_event_param
76 {
77         char * devname;
78         struct multipath *mpp;
79 };
80
81 unsigned int mpath_mx_alloc_len;
82
83 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
84 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
85
86 int logsink;
87 enum daemon_status running_state;
88 pid_t daemon_pid;
89
90 /*
91  * global copy of vecs for use in sig handlers
92  */
93 struct vectors * gvecs;
94
95 static int
96 need_switch_pathgroup (struct multipath * mpp, int refresh)
97 {
98         struct pathgroup * pgp;
99         struct path * pp;
100         unsigned int i, j;
101
102         if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
103                 return 0;
104
105         /*
106          * Refresh path priority values
107          */
108         if (refresh)
109                 vector_foreach_slot (mpp->pg, pgp, i)
110                         vector_foreach_slot (pgp->paths, pp, j)
111                                 pathinfo(pp, conf->hwtable, DI_PRIO);
112
113         mpp->bestpg = select_path_group(mpp);
114
115         if (mpp->bestpg != mpp->nextpg)
116                 return 1;
117
118         return 0;
119 }
120
121 static void
122 switch_pathgroup (struct multipath * mpp)
123 {
124         mpp->stat_switchgroup++;
125         dm_switchgroup(mpp->alias, mpp->bestpg);
126         condlog(2, "%s: switch to path group #%i",
127                  mpp->alias, mpp->bestpg);
128 }
129
130 static int
131 coalesce_maps(struct vectors *vecs, vector nmpv)
132 {
133         struct multipath * ompp;
134         vector ompv = vecs->mpvec;
135         unsigned int i;
136         int j;
137
138         vector_foreach_slot (ompv, ompp, i) {
139                 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
140                         /*
141                          * remove all current maps not allowed by the
142                          * current configuration
143                          */
144                         if (dm_flush_map(ompp->alias)) {
145                                 condlog(0, "%s: unable to flush devmap",
146                                         ompp->alias);
147                                 /*
148                                  * may be just because the device is open
149                                  */
150                                 if (!vector_alloc_slot(nmpv))
151                                         return 1;
152
153                                 vector_set_slot(nmpv, ompp);
154                                 setup_multipath(vecs, ompp);
155
156                                 if ((j = find_slot(ompv, (void *)ompp)) != -1)
157                                         vector_del_slot(ompv, j);
158
159                                 continue;
160                         }
161                         else {
162                                 dm_lib_release();
163                                 condlog(2, "%s devmap removed", ompp->alias);
164                         }
165                 } else if (conf->reassign_maps) {
166                         condlog(3, "%s: Reassign existing device-mapper"
167                                 " devices", ompp->alias);
168                         dm_reassign(ompp->alias);
169                 }
170         }
171         return 0;
172 }
173
174 void
175 sync_map_state(struct multipath *mpp)
176 {
177         struct pathgroup *pgp;
178         struct path *pp;
179         unsigned int i, j;
180
181         if (!mpp->pg)
182                 return;
183
184         vector_foreach_slot (mpp->pg, pgp, i){
185                 vector_foreach_slot (pgp->paths, pp, j){
186                         if (pp->state == PATH_UNCHECKED || 
187                             pp->state == PATH_WILD)
188                                 continue;
189                         if ((pp->dmstate == PSTATE_FAILED ||
190                              pp->dmstate == PSTATE_UNDEF) &&
191                             (pp->state == PATH_UP || pp->state == PATH_GHOST))
192                                 dm_reinstate_path(mpp->alias, pp->dev_t);
193                         else if ((pp->dmstate == PSTATE_ACTIVE ||
194                                   pp->dmstate == PSTATE_UNDEF) &&
195                                  (pp->state == PATH_DOWN ||
196                                   pp->state == PATH_SHAKY))
197                                 dm_fail_path(mpp->alias, pp->dev_t);
198                 }
199         }
200 }
201
202 static void
203 sync_maps_state(vector mpvec)
204 {
205         unsigned int i;
206         struct multipath *mpp;
207
208         vector_foreach_slot (mpvec, mpp, i)
209                 sync_map_state(mpp);
210 }
211
212 static int
213 flush_map(struct multipath * mpp, struct vectors * vecs)
214 {
215         /*
216          * clear references to this map before flushing so we can ignore
217          * the spurious uevent we may generate with the dm_flush_map call below
218          */
219         if (dm_flush_map(mpp->alias)) {
220                 /*
221                  * May not really be an error -- if the map was already flushed
222                  * from the device mapper by dmsetup(8) for instance.
223                  */
224                 condlog(0, "%s: can't flush", mpp->alias);
225                 return 1;
226         }
227         else {
228                 dm_lib_release();
229                 condlog(2, "%s: devmap removed", mpp->alias);
230         }
231
232         orphan_paths(vecs->pathvec, mpp);
233         remove_map_and_stop_waiter(mpp, vecs, 1);
234
235         return 0;
236 }
237
238 static int
239 uev_add_map (struct uevent * uev, struct vectors * vecs)
240 {
241         char *alias;
242         int major = -1, minor = -1, rc;
243
244         condlog(3, "%s: add map (uevent)", uev->kernel);
245         alias = uevent_get_dm_name(uev);
246         if (!alias) {
247                 condlog(3, "%s: No DM_NAME in uevent", uev->kernel);
248                 major = uevent_get_major(uev);
249                 minor = uevent_get_minor(uev);
250                 alias = dm_mapname(major, minor);
251                 if (!alias) {
252                         condlog(2, "%s: mapname not found for %d:%d",
253                                 uev->kernel, major, minor);
254                         return 1;
255                 }
256         }
257         rc = ev_add_map(uev->kernel, alias, vecs);
258         FREE(alias);
259         return rc;
260 }
261
262 int
263 ev_add_map (char * dev, char * alias, struct vectors * vecs)
264 {
265         char * refwwid;
266         struct multipath * mpp;
267         int map_present;
268         int r = 1;
269
270         map_present = dm_map_present(alias);
271
272         if (map_present && dm_type(alias, TGT_MPATH) <= 0) {
273                 condlog(4, "%s: not a multipath map", alias);
274                 return 0;
275         }
276
277         mpp = find_mp_by_alias(vecs->mpvec, alias);
278
279         if (mpp) {
280                 /*
281                  * Not really an error -- we generate our own uevent
282                  * if we create a multipath mapped device as a result
283                  * of uev_add_path
284                  */
285                 if (conf->reassign_maps) {
286                         condlog(3, "%s: Reassign existing device-mapper devices",
287                                 alias);
288                         dm_reassign(alias);
289                 }
290                 return 0;
291         }
292         condlog(2, "%s: adding map", alias);
293
294         /*
295          * now we can register the map
296          */
297         if (map_present && (mpp = add_map_without_path(vecs, alias))) {
298                 sync_map_state(mpp);
299                 condlog(2, "%s: devmap %s registered", alias, dev);
300                 return 0;
301         }
302         refwwid = get_refwwid(dev, DEV_DEVMAP, vecs->pathvec);
303
304         if (refwwid) {
305                 r = coalesce_paths(vecs, NULL, refwwid, 0);
306                 dm_lib_release();
307         }
308
309         if (!r)
310                 condlog(2, "%s: devmap %s added", alias, dev);
311         else
312                 condlog(0, "%s: uev_add_map %s failed", alias, dev);
313
314         FREE(refwwid);
315         return r;
316 }
317
318 static int
319 uev_remove_map (struct uevent * uev, struct vectors * vecs)
320 {
321         char *alias;
322         int minor;
323         struct multipath *mpp;
324
325         condlog(2, "%s: remove map (uevent)", uev->kernel);
326         alias = uevent_get_dm_name(uev);
327         if (!alias) {
328                 condlog(3, "%s: No DM_NAME in uevent, ignoring", uev->kernel);
329                 return 0;
330         }
331         minor = uevent_get_minor(uev);
332         mpp = find_mp_by_minor(vecs->mpvec, minor);
333
334         if (!mpp) {
335                 condlog(2, "%s: devmap not registered, can't remove",
336                         uev->kernel);
337                 goto out;
338         }
339         if (strcmp(mpp->alias, alias)) {
340                 condlog(2, "%s: minor number mismatch (map %d, event %d)",
341                         mpp->alias, mpp->dmi->minor, minor);
342                 goto out;
343         }
344
345         orphan_paths(vecs->pathvec, mpp);
346         remove_map_and_stop_waiter(mpp, vecs, 1);
347 out:
348         FREE(alias);
349         return 0;
350 }
351
352 int
353 ev_remove_map (char * devname, char * alias, int minor, struct vectors * vecs)
354 {
355         struct multipath * mpp;
356
357         mpp = find_mp_by_minor(vecs->mpvec, minor);
358
359         if (!mpp) {
360                 condlog(2, "%s: devmap not registered, can't remove",
361                         devname);
362                 return 0;
363         }
364         if (strcmp(mpp->alias, alias)) {
365                 condlog(2, "%s: minor number mismatch (map %d, event %d)",
366                         mpp->alias, mpp->dmi->minor, minor);
367                 return 0;
368         }
369         return flush_map(mpp, vecs);
370 }
371
372 static int
373 uev_add_path (struct uevent *uev, struct vectors * vecs)
374 {
375         condlog(2, "%s: add path (uevent)", uev->kernel);
376         return (ev_add_path(dev->kernel, vecs) != 1)? 0 : 1;
377 }
378
379 /*
380  * returns:
381  * 0: added
382  * 1: error
383  * 2: blacklisted
384  */
385 int
386 ev_add_path (char * devname, struct vectors * vecs)
387 {
388         struct multipath * mpp;
389         struct path * pp;
390         char empty_buff[WWID_SIZE] = {0};
391         char params[PARAMS_SIZE] = {0};
392         int retries = 3;
393         int start_waiter = 0;
394
395         if (strstr(devname, "..") != NULL) {
396                 /*
397                  * Don't allow relative device names in the pathvec
398                  */
399                 condlog(0, "%s: path name is invalid", devname);
400                 return 1;
401         }
402
403         pp = find_path_by_dev(vecs->pathvec, devname);
404
405         if (pp) {
406                 condlog(0, "%s: spurious uevent, path already in pathvec",
407                         devname);
408                 if (pp->mpp)
409                         return 0;
410         }
411         else {
412                 /*
413                  * get path vital state
414                  */
415                 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
416                       devname, DI_ALL))) {
417                         condlog(0, "%s: failed to store path info", devname);
418                         return 1;
419                 }
420                 pp->checkint = conf->checkint;
421         }
422
423         /*
424          * need path UID to go any further
425          */
426         if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
427                 condlog(0, "%s: failed to get path uid", devname);
428                 goto fail; /* leave path added to pathvec */
429         }
430         if (filter_path(conf, pp) > 0){
431                 int i = find_slot(vecs->pathvec, (void *)pp);
432                 if (i != -1)
433                         vector_del_slot(vecs->pathvec, i);
434                 free_path(pp);
435                 return 2;
436         }
437         mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
438 rescan:
439         if (mpp) {
440                 if ((!pp->size) || (mpp->size != pp->size)) {
441                         if (!pp->size)
442                                 condlog(0, "%s: failed to add new path %s, "
443                                         "device size is 0",
444                                         devname, pp->dev);
445                         else
446                                 condlog(0, "%s: failed to add new path %s, "
447                                         "device size mismatch",
448                                         devname, pp->dev);
449                         int i = find_slot(vecs->pathvec, (void *)pp);
450                         if (i != -1)
451                                 vector_del_slot(vecs->pathvec, i);
452                         free_path(pp);
453                         return 1;
454                 }
455
456                 condlog(4,"%s: adopting all paths for path %s",
457                         mpp->alias, pp->dev);
458                 if (adopt_paths(vecs->pathvec, mpp, 1))
459                         goto fail; /* leave path added to pathvec */
460
461                 verify_paths(mpp, vecs, NULL);
462                 mpp->flush_on_last_del = FLUSH_UNDEF;
463                 mpp->action = ACT_RELOAD;
464         }
465         else {
466                 if (!pp->size) {
467                         condlog(0, "%s: failed to create new map,"
468                                 " %s device size is 0 ", devname, pp->dev);
469                         int i = find_slot(vecs->pathvec, (void *)pp);
470                         if (i != -1)
471                                 vector_del_slot(vecs->pathvec, i);
472                         free_path(pp);
473                         return 1;
474                 }
475
476                 condlog(4,"%s: creating new map", pp->dev);
477                 if ((mpp = add_map_with_path(vecs, pp, 1))) {
478                         mpp->action = ACT_CREATE;
479                         /*
480                          * We don't depend on ACT_CREATE, as domap will
481                          * set it to ACT_NOTHING when complete.
482                          */
483                         start_waiter = 1;
484                 }
485                 else
486                         goto fail; /* leave path added to pathvec */
487         }
488
489         /* persistent reseravtion check*/
490         mpath_pr_event_handle(pp);      
491
492         /*
493          * push the map to the device-mapper
494          */
495         if (setup_map(mpp, params, PARAMS_SIZE)) {
496                 condlog(0, "%s: failed to setup map for addition of new "
497                         "path %s", mpp->alias, devname);
498                 goto fail_map;
499         }
500         /*
501          * reload the map for the multipath mapped device
502          */
503         if (domap(mpp, params) <= 0) {
504                 condlog(0, "%s: failed in domap for addition of new "
505                         "path %s", mpp->alias, devname);
506                 /*
507                  * deal with asynchronous uevents :((
508                  */
509                 if (mpp->action == ACT_RELOAD && retries-- > 0) {
510                         condlog(0, "%s: uev_add_path sleep", mpp->alias);
511                         sleep(1);
512                         update_mpp_paths(mpp, vecs->pathvec);
513                         goto rescan;
514                 }
515                 else if (mpp->action == ACT_RELOAD)
516                         condlog(0, "%s: giving up reload", mpp->alias);
517                 else
518                         goto fail_map;
519         }
520         dm_lib_release();
521
522         /*
523          * update our state from kernel regardless of create or reload
524          */
525         if (setup_multipath(vecs, mpp))
526                 goto fail; /* if setup_multipath fails, it removes the map */
527
528         sync_map_state(mpp);
529
530         if ((mpp->action == ACT_CREATE ||
531              (mpp->action == ACT_NOTHING && start_waiter && !mpp->waiter)) &&
532             start_waiter_thread(mpp, vecs))
533                         goto fail_map;
534
535         if (retries >= 0) {
536                 condlog(2, "%s path added to devmap %s", devname, mpp->alias);
537                 return 0;
538         }
539         else
540                 return 1;
541
542 fail_map:
543         remove_map(mpp, vecs, 1);
544 fail:
545         orphan_path(pp);
546         return 1;
547 }
548
549 static int
550 uev_remove_path (struct uevent *uev, struct vectors * vecs)
551 {
552         int retval;
553
554         condlog(2, "%s: remove path (uevent)", uev->kernel);
555         return ev_remove_path(uev->kernel, vecs);
556 }
557
558 int
559 ev_remove_path (char * devname, struct vectors * vecs)
560 {
561         struct multipath * mpp;
562         struct path * pp;
563         int i, retval = 0;
564         char params[PARAMS_SIZE] = {0};
565
566         pp = find_path_by_dev(vecs->pathvec, devname);
567
568         if (!pp) {
569                 /* Not an error; path might have been purged earlier */
570                 condlog(0, "%s: path already removed", devname);
571                 return 0;
572         }
573
574         /*
575          * avoid referring to the map of an orphaned path
576          */
577         if ((mpp = pp->mpp)) {
578                 /*
579                  * transform the mp->pg vector of vectors of paths
580                  * into a mp->params string to feed the device-mapper
581                  */
582                 if (update_mpp_paths(mpp, vecs->pathvec)) {
583                         condlog(0, "%s: failed to update paths",
584                                 mpp->alias);
585                         goto fail;
586                 }
587                 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
588                         vector_del_slot(mpp->paths, i);
589
590                 /*
591                  * remove the map IFF removing the last path
592                  */
593                 if (VECTOR_SIZE(mpp->paths) == 0) {
594                         char alias[WWID_SIZE];
595
596                         /*
597                          * flush_map will fail if the device is open
598                          */
599                         strncpy(alias, mpp->alias, WWID_SIZE);
600                         if (mpp->flush_on_last_del == FLUSH_ENABLED) {
601                                 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
602                                 mpp->retry_tick = 0;
603                                 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
604                                 mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
605                                 dm_queue_if_no_path(mpp->alias, 0);
606                         }
607                         if (!flush_map(mpp, vecs)) {
608                                 condlog(2, "%s: removed map after"
609                                         " removing all paths",
610                                         alias);
611                                 retval = 0;
612                                 goto out;
613                         }
614                         /*
615                          * Not an error, continue
616                          */
617                 }
618
619                 if (setup_map(mpp, params, PARAMS_SIZE)) {
620                         condlog(0, "%s: failed to setup map for"
621                                 " removal of path %s", mpp->alias,
622                                 devname);
623                         goto fail;
624                 }
625                 /*
626                  * reload the map
627                  */
628                 mpp->action = ACT_RELOAD;
629                 if (domap(mpp, params) <= 0) {
630                         condlog(0, "%s: failed in domap for "
631                                 "removal of path %s",
632                                 mpp->alias, devname);
633                         retval = 1;
634                 } else {
635                         /*
636                          * update our state from kernel
637                          */
638                         if (setup_multipath(vecs, mpp)) {
639                                 goto fail;
640                         }
641                         sync_map_state(mpp);
642
643                         condlog(2, "%s: path removed from map %s",
644                                 devname, mpp->alias);
645                 }
646         }
647
648 out:
649         if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
650                 vector_del_slot(vecs->pathvec, i);
651
652         free_path(pp);
653
654         return retval;
655
656 fail:
657         remove_map_and_stop_waiter(mpp, vecs, 1);
658         return 1;
659 }
660
661 static int
662 uev_update_path (struct uevent *uev, struct vectors * vecs)
663 {
664         int retval, ro;
665
666         ro = uevent_get_disk_ro(uev);
667
668         if (ro >= 0) {
669                 struct path * pp;
670
671                 condlog(2, "%s: update path write_protect to '%d' (uevent)",
672                         uev->kernel, ro);
673                 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
674                 if (!pp) {
675                         condlog(0, "%s: spurious uevent, path not found",
676                                 uev->kernel);
677                         return 1;
678                 }
679                 if (pp->mpp)
680                         retval = reload_map(vecs, pp->mpp);
681
682                 condlog(2, "%s: map %s reloaded (retval %d)",
683                         uev->kernel, pp->mpp->alias, retval);
684
685         }
686
687         return retval;
688 }
689
690 static int
691 map_discovery (struct vectors * vecs)
692 {
693         struct multipath * mpp;
694         unsigned int i;
695
696         if (dm_get_maps(vecs->mpvec))
697                 return 1;
698
699         vector_foreach_slot (vecs->mpvec, mpp, i)
700                 if (setup_multipath(vecs, mpp))
701                         return 1;
702
703         return 0;
704 }
705
706 int
707 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
708 {
709         struct vectors * vecs;
710         int r;
711
712         *reply = NULL;
713         *len = 0;
714         vecs = (struct vectors *)trigger_data;
715
716         pthread_cleanup_push(cleanup_lock, &vecs->lock);
717         lock(vecs->lock);
718         pthread_testcancel();
719
720         r = parse_cmd(str, reply, len, vecs);
721
722         if (r > 0) {
723                 *reply = STRDUP("fail\n");
724                 *len = strlen(*reply) + 1;
725                 r = 1;
726         }
727         else if (!r && *len == 0) {
728                 *reply = STRDUP("ok\n");
729                 *len = strlen(*reply) + 1;
730                 r = 0;
731         }
732         /* else if (r < 0) leave *reply alone */
733
734         lock_cleanup_pop(vecs->lock);
735         return r;
736 }
737
738 static int
739 uev_discard(char * devpath)
740 {
741         char *tmp;
742         char a[11], b[11];
743
744         /*
745          * keep only block devices, discard partitions
746          */
747         tmp = strstr(devpath, "/block/");
748         if (tmp == NULL){
749                 condlog(4, "no /block/ in '%s'", devpath);
750                 return 1;
751         }
752         if (sscanf(tmp, "/block/%10s", a) != 1 ||
753             sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
754                 condlog(4, "discard event on %s", devpath);
755                 return 1;
756         }
757         return 0;
758 }
759
760 int
761 uev_trigger (struct uevent * uev, void * trigger_data)
762 {
763         int r = 0;
764         struct vectors * vecs;
765
766         vecs = (struct vectors *)trigger_data;
767
768         if (uev_discard(uev->devpath))
769                 return 0;
770
771         pthread_cleanup_push(cleanup_lock, &vecs->lock);
772         lock(vecs->lock);
773         pthread_testcancel();
774
775         /*
776          * device map event
777          * Add events are ignored here as the tables
778          * are not fully initialised then.
779          */
780         if (!strncmp(uev->kernel, "dm-", 3)) {
781                 if (!strncmp(uev->action, "change", 6)) {
782                         r = uev_add_map(uev, vecs);
783                         goto out;
784                 }
785                 if (!strncmp(uev->action, "remove", 6)) {
786                         r = uev_remove_map(uev, vecs);
787                         goto out;
788                 }
789                 goto out;
790         }
791
792         /*
793          * path add/remove event
794          */
795         if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
796                            uev->kernel) > 0)
797                 goto out;
798
799         if (!strncmp(uev->action, "add", 3)) {
800                 r = uev_add_path(uev, vecs);
801                 goto out;
802         }
803         if (!strncmp(uev->action, "remove", 6)) {
804                 r = uev_remove_path(uev, vecs);
805                 goto out;
806         }
807         if (!strncmp(uev->action, "change", 6)) {
808                 r = uev_update_path(uev, vecs);
809                 goto out;
810         }
811
812 out:
813         lock_cleanup_pop(vecs->lock);
814         return r;
815 }
816
817 static void *
818 ueventloop (void * ap)
819 {
820         block_signal(SIGUSR1, NULL);
821         block_signal(SIGHUP, NULL);
822
823         if (uevent_listen())
824                 condlog(0, "error starting uevent listener");
825
826         return NULL;
827 }
828
829 static void *
830 uevqloop (void * ap)
831 {
832         block_signal(SIGUSR1, NULL);
833         block_signal(SIGHUP, NULL);
834
835         if (uevent_dispatch(&uev_trigger, ap))
836                 condlog(0, "error starting uevent dispatcher");
837
838         return NULL;
839 }
840 static void *
841 uxlsnrloop (void * ap)
842 {
843         block_signal(SIGUSR1, NULL);
844         block_signal(SIGHUP, NULL);
845
846         if (cli_init())
847                 return NULL;
848
849         set_handler_callback(LIST+PATHS, cli_list_paths);
850         set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
851         set_handler_callback(LIST+MAPS, cli_list_maps);
852         set_handler_callback(LIST+STATUS, cli_list_status);
853         set_handler_callback(LIST+DAEMON, cli_list_daemon);
854         set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
855         set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
856         set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
857         set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
858         set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
859         set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
860         set_handler_callback(LIST+CONFIG, cli_list_config);
861         set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
862         set_handler_callback(LIST+DEVICES, cli_list_devices);
863         set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
864         set_handler_callback(ADD+PATH, cli_add_path);
865         set_handler_callback(DEL+PATH, cli_del_path);
866         set_handler_callback(ADD+MAP, cli_add_map);
867         set_handler_callback(DEL+MAP, cli_del_map);
868         set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
869         set_handler_callback(RECONFIGURE, cli_reconfigure);
870         set_handler_callback(SUSPEND+MAP, cli_suspend);
871         set_handler_callback(RESUME+MAP, cli_resume);
872         set_handler_callback(RESIZE+MAP, cli_resize);
873         set_handler_callback(RELOAD+MAP, cli_reload);
874         set_handler_callback(RESET+MAP, cli_reassign);
875         set_handler_callback(REINSTATE+PATH, cli_reinstate);
876         set_handler_callback(FAIL+PATH, cli_fail);
877         set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
878         set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
879         set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
880         set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
881         set_handler_callback(QUIT, cli_quit);
882         set_handler_callback(SHUTDOWN, cli_shutdown);
883         set_handler_callback(GETPRSTATUS+MAP, cli_getprstatus);
884         set_handler_callback(SETPRSTATUS+MAP, cli_setprstatus);
885         set_handler_callback(UNSETPRSTATUS+MAP, cli_unsetprstatus);
886
887         umask(077);
888         uxsock_listen(&uxsock_trigger, ap);
889
890         return NULL;
891 }
892
893 int
894 exit_daemon (int status)
895 {
896         if (status != 0)
897                 fprintf(stderr, "bad exit status. see daemon.log\n");
898
899         condlog(3, "unlink pidfile");
900         unlink(DEFAULT_PIDFILE);
901
902         pthread_mutex_lock(&exit_mutex);
903         pthread_cond_signal(&exit_cond);
904         pthread_mutex_unlock(&exit_mutex);
905
906         return status;
907 }
908
909 const char *
910 daemon_status(void)
911 {
912         switch (running_state) {
913         case DAEMON_INIT:
914                 return "init";
915         case DAEMON_START:
916                 return "startup";
917         case DAEMON_CONFIGURE:
918                 return "configure";
919         case DAEMON_RUNNING:
920                 return "running";
921         case DAEMON_SHUTDOWN:
922                 return "shutdown";
923         }
924         return NULL;
925 }
926
927 static void
928 fail_path (struct path * pp, int del_active)
929 {
930         if (!pp->mpp)
931                 return;
932
933         condlog(2, "checker failed path %s in map %s",
934                  pp->dev_t, pp->mpp->alias);
935
936         dm_fail_path(pp->mpp->alias, pp->dev_t);
937         if (del_active)
938                 update_queue_mode_del_path(pp->mpp);
939 }
940
941 /*
942  * caller must have locked the path list before calling that function
943  */
944 static void
945 reinstate_path (struct path * pp, int add_active)
946 {
947         if (!pp->mpp)
948                 return;
949
950         if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
951                 condlog(0, "%s: reinstate failed", pp->dev_t);
952         else {
953                 condlog(2, "%s: reinstated", pp->dev_t);
954                 if (add_active)
955                         update_queue_mode_add_path(pp->mpp);
956         }
957 }
958
959 static void
960 enable_group(struct path * pp)
961 {
962         struct pathgroup * pgp;
963
964         /*
965          * if path is added through uev_add_path, pgindex can be unset.
966          * next update_strings() will set it, upon map reload event.
967          *
968          * we can safely return here, because upon map reload, all
969          * PG will be enabled.
970          */
971         if (!pp->mpp->pg || !pp->pgindex)
972                 return;
973
974         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
975
976         if (pgp->status == PGSTATE_DISABLED) {
977                 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
978                 dm_enablegroup(pp->mpp->alias, pp->pgindex);
979         }
980 }
981
982 static void
983 mpvec_garbage_collector (struct vectors * vecs)
984 {
985         struct multipath * mpp;
986         unsigned int i;
987
988         if (!vecs->mpvec)
989                 return;
990
991         vector_foreach_slot (vecs->mpvec, mpp, i) {
992                 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
993                         condlog(2, "%s: remove dead map", mpp->alias);
994                         remove_map_and_stop_waiter(mpp, vecs, 1);
995                         i--;
996                 }
997         }
998 }
999
1000 static void
1001 defered_failback_tick (vector mpvec)
1002 {
1003         struct multipath * mpp;
1004         unsigned int i;
1005
1006         vector_foreach_slot (mpvec, mpp, i) {
1007                 /*
1008                  * defered failback getting sooner
1009                  */
1010                 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1011                         mpp->failback_tick--;
1012
1013                         if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1014                                 switch_pathgroup(mpp);
1015                 }
1016         }
1017 }
1018
1019 static void
1020 retry_count_tick(vector mpvec)
1021 {
1022         struct multipath *mpp;
1023         unsigned int i;
1024
1025         vector_foreach_slot (mpvec, mpp, i) {
1026                 if (mpp->retry_tick) {
1027                         mpp->stat_total_queueing_time++;
1028                         condlog(4, "%s: Retrying.. No active path", mpp->alias);
1029                         if(--mpp->retry_tick == 0) {
1030                                 dm_queue_if_no_path(mpp->alias, 0);
1031                                 condlog(2, "%s: Disable queueing", mpp->alias);
1032                         }
1033                 }
1034         }
1035 }
1036
1037 int update_prio(struct path *pp, int refresh_all)
1038 {
1039         int oldpriority;
1040         struct path *pp1;
1041         struct pathgroup * pgp;
1042         int i, j, changed = 0;
1043
1044         if (refresh_all) {
1045                 vector_foreach_slot (pp->mpp->pg, pgp, i) {
1046                         vector_foreach_slot (pgp->paths, pp1, j) {
1047                                 oldpriority = pp1->priority;
1048                                 pathinfo(pp1, conf->hwtable, DI_PRIO);
1049                                 if (pp1->priority != oldpriority)
1050                                         changed = 1;
1051                         }
1052                 }
1053                 return changed;
1054         }
1055         oldpriority = pp->priority;
1056         pathinfo(pp, conf->hwtable, DI_PRIO);
1057
1058         if (pp->priority == oldpriority)
1059                 return 0;
1060         return 1;
1061 }
1062
1063 int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
1064 {
1065         int i;
1066         struct path * pp;
1067         char params[PARAMS_SIZE];
1068
1069         update_mpp_paths(mpp, vecs->pathvec);
1070         if (refresh) {
1071                 vector_foreach_slot (mpp->paths, pp, i)
1072                         pathinfo(pp, conf->hwtable, DI_PRIO);
1073         }
1074         params[0] = '\0';
1075         if (setup_map(mpp, params, PARAMS_SIZE))
1076                 return 1;
1077
1078         mpp->action = ACT_RELOAD;
1079         if (domap(mpp, params) <= 0) {
1080                 condlog(0, "%s: failed to update map : %s", mpp->alias,
1081                         strerror(errno));
1082                 return 1;
1083         }
1084         dm_lib_release();
1085         if (setup_multipath(vecs, mpp) != 0)
1086                 return 1;
1087         sync_map_state(mpp);
1088
1089         return 0;
1090 }
1091
1092 void
1093 check_path (struct vectors * vecs, struct path * pp)
1094 {
1095         int newstate;
1096         int new_path_up = 0;
1097
1098         if (!pp->mpp)
1099                 return;
1100
1101         if (pp->tick && --pp->tick)
1102                 return; /* don't check this path yet */
1103
1104         /*
1105          * provision a next check soonest,
1106          * in case we exit abnormaly from here
1107          */
1108         pp->tick = conf->checkint;
1109
1110         newstate = path_offline(pp);
1111         if (newstate == PATH_UP)
1112                 newstate = get_state(pp, 1);
1113
1114         if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
1115                 condlog(2, "%s: unusable path", pp->dev);
1116                 pathinfo(pp, conf->hwtable, 0);
1117                 return;
1118         }
1119         /*
1120          * Async IO in flight. Keep the previous path state
1121          * and reschedule as soon as possible
1122          */
1123         if (newstate == PATH_PENDING) {
1124                 pp->tick = 1;
1125                 return;
1126         }
1127         /*
1128          * Synchronize with kernel state
1129          */
1130         if (update_multipath_strings(pp->mpp, vecs->pathvec)) {
1131                 condlog(1, "%s: Could not synchronize with kernel state\n",
1132                         pp->dev);
1133                 pp->dmstate = PSTATE_UNDEF;
1134         }
1135         if (newstate != pp->state) {
1136                 int oldstate = pp->state;
1137                 pp->state = newstate;
1138                 LOG_MSG(1, checker_message(&pp->checker));
1139
1140                 /*
1141                  * upon state change, reset the checkint
1142                  * to the shortest delay
1143                  */
1144                 pp->checkint = conf->checkint;
1145
1146                 if (newstate == PATH_DOWN || newstate == PATH_SHAKY) {
1147                         /*
1148                          * proactively fail path in the DM
1149                          */
1150                         if (oldstate == PATH_UP ||
1151                             oldstate == PATH_GHOST)
1152                                 fail_path(pp, 1);
1153                         else
1154                                 fail_path(pp, 0);
1155
1156                         /*
1157                          * cancel scheduled failback
1158                          */
1159                         pp->mpp->failback_tick = 0;
1160
1161                         pp->mpp->stat_path_failures++;
1162                         return;
1163                 }
1164
1165                 if(newstate == PATH_UP || newstate == PATH_GHOST){
1166                         if ( pp->mpp && pp->mpp->prflag ){
1167                                 /*
1168                                  * Check Persistent Reservation.
1169                                  */
1170                         condlog(2, "%s: checking persistent reservation "
1171                                 "registration", pp->dev);
1172                         mpath_pr_event_handle(pp);
1173                         }
1174                 }
1175
1176                 /*
1177                  * reinstate this path
1178                  */
1179                 if (oldstate != PATH_UP &&
1180                     oldstate != PATH_GHOST)
1181                         reinstate_path(pp, 1);
1182                 else
1183                         reinstate_path(pp, 0);
1184
1185                 new_path_up = 1;
1186
1187                 /*
1188                  * if at least one path is up in a group, and
1189                  * the group is disabled, re-enable it
1190                  */
1191                 if (newstate == PATH_UP)
1192                         enable_group(pp);
1193         }
1194         else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1195                 if (pp->dmstate == PSTATE_FAILED ||
1196                     pp->dmstate == PSTATE_UNDEF) {
1197                         /* Clear IO errors */
1198                         reinstate_path(pp, 0);
1199                 } else {
1200                         LOG_MSG(4, checker_message(&pp->checker));
1201                         /*
1202                          * double the next check delay.
1203                          * max at conf->max_checkint
1204                          */
1205                         if (pp->checkint < (conf->max_checkint / 2))
1206                                 pp->checkint = 2 * pp->checkint;
1207                         else
1208                                 pp->checkint = conf->max_checkint;
1209
1210                         pp->tick = pp->checkint;
1211                         condlog(4, "%s: delay next check %is",
1212                                 pp->dev_t, pp->tick);
1213                 }
1214         }
1215         else if (newstate == PATH_DOWN) {
1216                 if (conf->log_checker_err == LOG_CHKR_ERR_ONCE)
1217                         LOG_MSG(3, checker_message(&pp->checker));
1218                 else
1219                         LOG_MSG(2, checker_message(&pp->checker));
1220         }
1221
1222         pp->state = newstate;
1223
1224         /*
1225          * path prio refreshing
1226          */
1227         condlog(4, "path prio refresh");
1228
1229         if (update_prio(pp, new_path_up) &&
1230             (pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio) &&
1231              pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
1232                 update_path_groups(pp->mpp, vecs, !new_path_up);
1233         else if (need_switch_pathgroup(pp->mpp, 0)) {
1234                 if (pp->mpp->pgfailback > 0 &&
1235                     (new_path_up || pp->mpp->failback_tick <= 0))
1236                         pp->mpp->failback_tick =
1237                                 pp->mpp->pgfailback + 1;
1238                 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
1239                         switch_pathgroup(pp->mpp);
1240         }
1241 }
1242
1243 static void *
1244 checkerloop (void *ap)
1245 {
1246         struct vectors *vecs;
1247         struct path *pp;
1248         int count = 0;
1249         unsigned int i;
1250         sigset_t old;
1251
1252         mlockall(MCL_CURRENT | MCL_FUTURE);
1253         vecs = (struct vectors *)ap;
1254         condlog(2, "path checkers start up");
1255
1256         /*
1257          * init the path check interval
1258          */
1259         vector_foreach_slot (vecs->pathvec, pp, i) {
1260                 pp->checkint = conf->checkint;
1261         }
1262
1263         while (1) {
1264                 block_signal(SIGHUP, &old);
1265                 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1266                 lock(vecs->lock);
1267                 pthread_testcancel();
1268                 condlog(4, "tick");
1269
1270                 if (vecs->pathvec) {
1271                         vector_foreach_slot (vecs->pathvec, pp, i) {
1272                                 check_path(vecs, pp);
1273                         }
1274                 }
1275                 if (vecs->mpvec) {
1276                         defered_failback_tick(vecs->mpvec);
1277                         retry_count_tick(vecs->mpvec);
1278                 }
1279                 if (count)
1280                         count--;
1281                 else {
1282                         condlog(4, "map garbage collection");
1283                         mpvec_garbage_collector(vecs);
1284                         count = MAPGCINT;
1285                 }
1286
1287                 lock_cleanup_pop(vecs->lock);
1288                 pthread_sigmask(SIG_SETMASK, &old, NULL);
1289                 sleep(1);
1290         }
1291         return NULL;
1292 }
1293
1294 int
1295 configure (struct vectors * vecs, int start_waiters)
1296 {
1297         struct multipath * mpp;
1298         struct path * pp;
1299         vector mpvec;
1300         int i;
1301
1302         if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1303                 return 1;
1304
1305         if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1306                 return 1;
1307
1308         if (!(mpvec = vector_alloc()))
1309                 return 1;
1310
1311         /*
1312          * probe for current path (from sysfs) and map (from dm) sets
1313          */
1314         path_discovery(vecs->pathvec, conf, DI_ALL);
1315
1316         vector_foreach_slot (vecs->pathvec, pp, i){
1317                 if (filter_path(conf, pp) > 0){
1318                         vector_del_slot(vecs->pathvec, i);
1319                         free_path(pp);
1320                         i--;
1321                 }
1322                 else
1323                         pp->checkint = conf->checkint;
1324         }
1325         if (map_discovery(vecs))
1326                 return 1;
1327
1328         /*
1329          * create new set of maps & push changed ones into dm
1330          */
1331         if (coalesce_paths(vecs, mpvec, NULL, 1))
1332                 return 1;
1333
1334         /*
1335          * may need to remove some maps which are no longer relevant
1336          * e.g., due to blacklist changes in conf file
1337          */
1338         if (coalesce_maps(vecs, mpvec))
1339                 return 1;
1340
1341         dm_lib_release();
1342
1343         sync_maps_state(mpvec);
1344         vector_foreach_slot(mpvec, mpp, i){
1345                 update_map_pr(mpp);
1346         }
1347
1348         /*
1349          * purge dm of old maps
1350          */
1351         remove_maps(vecs);
1352
1353         /*
1354          * save new set of maps formed by considering current path state
1355          */
1356         vector_free(vecs->mpvec);
1357         vecs->mpvec = mpvec;
1358
1359         /*
1360          * start dm event waiter threads for these new maps
1361          */
1362         vector_foreach_slot(vecs->mpvec, mpp, i) {
1363                 if (setup_multipath(vecs, mpp))
1364                         return 1;
1365                 if (start_waiters)
1366                         if (start_waiter_thread(mpp, vecs))
1367                                 return 1;
1368         }
1369         return 0;
1370 }
1371
1372 int
1373 reconfigure (struct vectors * vecs)
1374 {
1375         struct config * old = conf;
1376         int retval = 1;
1377
1378         /*
1379          * free old map and path vectors ... they use old conf state
1380          */
1381         if (VECTOR_SIZE(vecs->mpvec))
1382                 remove_maps_and_stop_waiters(vecs);
1383
1384         if (VECTOR_SIZE(vecs->pathvec))
1385                 free_pathvec(vecs->pathvec, FREE_PATHS);
1386
1387         vecs->pathvec = NULL;
1388         conf = NULL;
1389
1390         if (!load_config(DEFAULT_CONFIGFILE)) {
1391                 conf->verbosity = old->verbosity;
1392                 conf->daemon = 1;
1393                 configure(vecs, 1);
1394                 free_config(old);
1395                 retval = 0;
1396         }
1397
1398         return retval;
1399 }
1400
1401 static struct vectors *
1402 init_vecs (void)
1403 {
1404         struct vectors * vecs;
1405
1406         vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1407
1408         if (!vecs)
1409                 return NULL;
1410
1411         vecs->lock.mutex =
1412                 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1413
1414         if (!vecs->lock.mutex)
1415                 goto out;
1416
1417         pthread_mutex_init(vecs->lock.mutex, NULL);
1418         vecs->lock.depth = 0;
1419
1420         return vecs;
1421
1422 out:
1423         FREE(vecs);
1424         condlog(0, "failed to init paths");
1425         return NULL;
1426 }
1427
1428 static void *
1429 signal_set(int signo, void (*func) (int))
1430 {
1431         int r;
1432         struct sigaction sig;
1433         struct sigaction osig;
1434
1435         sig.sa_handler = func;
1436         sigemptyset(&sig.sa_mask);
1437         sig.sa_flags = 0;
1438
1439         r = sigaction(signo, &sig, &osig);
1440
1441         if (r < 0)
1442                 return (SIG_ERR);
1443         else
1444                 return (osig.sa_handler);
1445 }
1446
1447 static void
1448 sighup (int sig)
1449 {
1450         condlog(2, "reconfigure (SIGHUP)");
1451
1452         if (running_state != DAEMON_RUNNING)
1453                 return;
1454
1455         lock(gvecs->lock);
1456         reconfigure(gvecs);
1457         unlock(gvecs->lock);
1458
1459 #ifdef _DEBUG_
1460         dbg_free_final(NULL);
1461 #endif
1462 }
1463
1464 static void
1465 sigend (int sig)
1466 {
1467         exit_daemon(0);
1468 }
1469
1470 static void
1471 sigusr1 (int sig)
1472 {
1473         condlog(3, "SIGUSR1 received");
1474 }
1475
1476 static void
1477 signal_init(void)
1478 {
1479         signal_set(SIGHUP, sighup);
1480         signal_set(SIGUSR1, sigusr1);
1481         signal_set(SIGINT, sigend);
1482         signal_set(SIGTERM, sigend);
1483         signal(SIGPIPE, SIG_IGN);
1484 }
1485
1486 static void
1487 setscheduler (void)
1488 {
1489         int res;
1490         static struct sched_param sched_param = {
1491                 .sched_priority = 99
1492         };
1493
1494         res = sched_setscheduler (0, SCHED_RR, &sched_param);
1495
1496         if (res == -1)
1497                 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1498         return;
1499 }
1500
1501 static void
1502 set_oom_adj (void)
1503 {
1504         int retry = 1;
1505         char *file = "/proc/self/oom_score_adj";
1506         int score = OOM_SCORE_ADJ_MIN;
1507         FILE *fp;
1508         struct stat st;
1509
1510         do {
1511                 if (stat(file, &st) == 0){
1512                         fp = fopen(file, "w");
1513                         if (!fp) {
1514                                 condlog(0, "couldn't fopen %s : %s", file,
1515                                         strerror(errno));
1516                                 return;
1517                         }
1518                         fprintf(fp, "%i", score);
1519                         fclose(fp);
1520                         return;
1521                 }
1522                 if (errno != ENOENT) {
1523                         condlog(0, "couldn't stat %s : %s", file,
1524                                 strerror(errno));
1525                         return;
1526                 }
1527                 file = "/proc/self/oom_adj";
1528                 score = OOM_ADJUST_MIN;
1529         } while (retry--);
1530         condlog(0, "couldn't adjust oom score");
1531 }
1532
1533 static int
1534 child (void * param)
1535 {
1536         pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr;
1537         pthread_attr_t log_attr, misc_attr;
1538         struct vectors * vecs;
1539         struct multipath * mpp;
1540         int i;
1541         int rc;
1542
1543         mlockall(MCL_CURRENT | MCL_FUTURE);
1544
1545         setup_thread_attr(&misc_attr, 64 * 1024, 1);
1546         setup_thread_attr(&waiter_attr, 32 * 1024, 1);
1547
1548         if (logsink) {
1549                 setup_thread_attr(&log_attr, 64 * 1024, 0);
1550                 log_thread_start(&log_attr);
1551                 pthread_attr_destroy(&log_attr);
1552         }
1553
1554         running_state = DAEMON_START;
1555
1556         condlog(2, "--------start up--------");
1557         condlog(2, "read " DEFAULT_CONFIGFILE);
1558
1559         if (load_config(DEFAULT_CONFIGFILE))
1560                 exit(1);
1561
1562         if (init_checkers()) {
1563                 condlog(0, "failed to initialize checkers");
1564                 exit(1);
1565         }
1566         if (init_prio()) {
1567                 condlog(0, "failed to initialize prioritizers");
1568                 exit(1);
1569         }
1570
1571         setlogmask(LOG_UPTO(conf->verbosity + 3));
1572
1573         if (conf->max_fds) {
1574                 struct rlimit fd_limit;
1575
1576                 if (getrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
1577                         condlog(0, "can't get open fds limit: %s\n",
1578                                 strerror(errno));
1579                         fd_limit.rlim_cur = 0;
1580                         fd_limit.rlim_max = 0;
1581                 }
1582                 if (fd_limit.rlim_cur < conf->max_fds) {
1583                         fd_limit.rlim_cur = conf->max_fds;
1584                         if (fd_limit.rlim_max < conf->max_fds)
1585                                 fd_limit.rlim_max = conf->max_fds;
1586                         if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
1587                                 condlog(0, "can't set open fds limit to "
1588                                         "%lu/%lu : %s\n",
1589                                         fd_limit.rlim_cur, fd_limit.rlim_max,
1590                                         strerror(errno));
1591                         } else {
1592                                 condlog(3, "set open fds limit to %lu/%lu\n",
1593                                         fd_limit.rlim_cur, fd_limit.rlim_max);
1594                         }
1595                 }
1596
1597         }
1598
1599         signal_init();
1600         setscheduler();
1601         set_oom_adj();
1602         vecs = gvecs = init_vecs();
1603
1604         if (!vecs)
1605                 exit(1);
1606
1607         if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
1608                 condlog(0, "can not find sysfs mount point");
1609                 exit(1);
1610         }
1611         conf->daemon = 1;
1612         udev_set_sync_support(0);
1613         /*
1614          * Start uevent listener early to catch events
1615          */
1616         if ((rc = pthread_create(&uevent_thr, &misc_attr, ueventloop, vecs))) {
1617                 condlog(0, "failed to create uevent thread: %d", rc);
1618                 exit(1);
1619         }
1620         if ((rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs))) {
1621                 condlog(0, "failed to create cli listener: %d", rc);
1622                 exit(1);
1623         }
1624         /*
1625          * fetch and configure both paths and multipaths
1626          */
1627         lock(vecs->lock);
1628         running_state = DAEMON_CONFIGURE;
1629
1630         if (configure(vecs, 1)) {
1631                 unlock(vecs->lock);
1632                 condlog(0, "failure during configuration");
1633                 exit(1);
1634         }
1635         unlock(vecs->lock);
1636
1637         /*
1638          * start threads
1639          */
1640         if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
1641                 condlog(0,"failed to create checker loop thread: %d", rc);
1642                 exit(1);
1643         }
1644         if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
1645                 condlog(0, "failed to create uevent dispatcher: %d", rc);
1646                 exit(1);
1647         }
1648         pthread_attr_destroy(&misc_attr);
1649
1650         pthread_mutex_lock(&exit_mutex);
1651         /* Startup complete, create logfile */
1652         if (pidfile_create(DEFAULT_PIDFILE, daemon_pid))
1653                 /* Ignore errors, we can live without */
1654                 condlog(1, "failed to create pidfile");
1655
1656         running_state = DAEMON_RUNNING;
1657         pthread_cond_wait(&exit_cond, &exit_mutex);
1658
1659         /*
1660          * exit path
1661          */
1662         running_state = DAEMON_SHUTDOWN;
1663         block_signal(SIGHUP, NULL);
1664         lock(vecs->lock);
1665         if (conf->queue_without_daemon == QUE_NO_DAEMON_OFF)
1666                 vector_foreach_slot(vecs->mpvec, mpp, i)
1667                         dm_queue_if_no_path(mpp->alias, 0);
1668         remove_maps_and_stop_waiters(vecs);
1669         unlock(vecs->lock);
1670
1671         pthread_cancel(check_thr);
1672         pthread_cancel(uevent_thr);
1673         pthread_cancel(uxlsnr_thr);
1674         pthread_cancel(uevq_thr);
1675
1676         sysfs_cleanup();
1677
1678         lock(vecs->lock);
1679         free_pathvec(vecs->pathvec, FREE_PATHS);
1680         vecs->pathvec = NULL;
1681         unlock(vecs->lock);
1682         /* Now all the waitevent threads will start rushing in. */
1683         while (vecs->lock.depth > 0) {
1684                 sleep (1); /* This is weak. */
1685                 condlog(3,"Have %d wait event checkers threads to de-alloc, waiting..\n", vecs->lock.depth);
1686         }
1687         pthread_mutex_destroy(vecs->lock.mutex);
1688         FREE(vecs->lock.mutex);
1689         vecs->lock.depth = 0;
1690         vecs->lock.mutex = NULL;
1691         FREE(vecs);
1692         vecs = NULL;
1693
1694         cleanup_checkers();
1695         cleanup_prio();
1696
1697         dm_lib_release();
1698         dm_lib_exit();
1699
1700         /* We're done here */
1701         condlog(3, "unlink pidfile");
1702         unlink(DEFAULT_PIDFILE);
1703
1704         condlog(2, "--------shut down-------");
1705
1706         if (logsink)
1707                 log_thread_stop();
1708
1709         /*
1710          * Freeing config must be done after condlog() and dm_lib_exit(),
1711          * because logging functions like dlog() and dm_write_log()
1712          * reference the config.
1713          */
1714         free_config(conf);
1715         conf = NULL;
1716
1717 #ifdef _DEBUG_
1718         dbg_free_final(NULL);
1719 #endif
1720
1721         exit(0);
1722 }
1723
1724 static int
1725 daemonize(void)
1726 {
1727         int pid;
1728         int dev_null_fd;
1729
1730         if( (pid = fork()) < 0){
1731                 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1732                 return -1;
1733         }
1734         else if (pid != 0)
1735                 return pid;
1736
1737         setsid();
1738
1739         if ( (pid = fork()) < 0)
1740                 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1741         else if (pid != 0)
1742                 _exit(0);
1743
1744         if (chdir("/") < 0)
1745                 fprintf(stderr, "cannot chdir to '/', continuing\n");
1746
1747         dev_null_fd = open("/dev/null", O_RDWR);
1748         if (dev_null_fd < 0){
1749                 fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
1750                         strerror(errno));
1751                 _exit(0);
1752         }
1753
1754         close(STDIN_FILENO);
1755         dup(dev_null_fd);
1756         close(STDOUT_FILENO);
1757         dup(dev_null_fd);
1758         close(STDERR_FILENO);
1759         dup(dev_null_fd);
1760         close(dev_null_fd);
1761         daemon_pid = getpid();
1762         return 0;
1763 }
1764
1765 int
1766 main (int argc, char *argv[])
1767 {
1768         extern char *optarg;
1769         extern int optind;
1770         int arg;
1771         int err;
1772
1773         logsink = 1;
1774         running_state = DAEMON_INIT;
1775         dm_init();
1776
1777         if (getuid() != 0) {
1778                 fprintf(stderr, "need to be root\n");
1779                 exit(1);
1780         }
1781
1782         /* make sure we don't lock any path */
1783         chdir("/");
1784         umask(umask(077) | 022);
1785
1786         conf = alloc_config();
1787
1788         if (!conf)
1789                 exit(1);
1790
1791         while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1792         switch(arg) {
1793                 case 'd':
1794                         logsink = 0;
1795                         //debug=1; /* ### comment me out ### */
1796                         break;
1797                 case 'v':
1798                         if (sizeof(optarg) > sizeof(char *) ||
1799                             !isdigit(optarg[0]))
1800                                 exit(1);
1801
1802                         conf->verbosity = atoi(optarg);
1803                         break;
1804                 case 'k':
1805                         uxclnt(optarg);
1806                         exit(0);
1807                 default:
1808                         ;
1809                 }
1810         }
1811         if (optind < argc) {
1812                 char cmd[CMDSIZE];
1813                 char * s = cmd;
1814                 char * c = s;
1815
1816                 while (optind < argc) {
1817                         if (strchr(argv[optind], ' '))
1818                                 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
1819                         else
1820                                 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
1821                         optind++;
1822                 }
1823                 c += snprintf(c, s + CMDSIZE - c, "\n");
1824                 uxclnt(s);
1825                 exit(0);
1826         }
1827
1828         if (!logsink)
1829                 err = 0;
1830         else
1831                 err = daemonize();
1832
1833         if (err < 0)
1834                 /* error */
1835                 exit(1);
1836         else if (err > 0)
1837                 /* parent dies */
1838                 exit(0);
1839         else
1840                 /* child lives */
1841                 return (child(NULL));
1842 }
1843
1844 void *  mpath_pr_event_handler_fn (void * pathp )
1845 {
1846         struct multipath * mpp;
1847         int i,j, ret, isFound;
1848         struct path * pp = (struct path *)pathp;
1849         unsigned char *keyp;
1850         uint64_t prkey;
1851         struct prout_param_descriptor *param;
1852         struct prin_resp *resp;
1853
1854         mpp = pp->mpp;
1855
1856         resp = mpath_alloc_prin_response(MPATH_PRIN_RKEY_SA);
1857         if (!resp){
1858                 condlog(0,"%s Alloc failed for prin response \n", pp->dev);
1859                 return NULL;
1860         }
1861
1862         ret = prin_do_scsi_ioctl(pp->dev, MPATH_PRIN_RKEY_SA, resp, 0);
1863         if (ret != MPATH_PR_SUCCESS )
1864         {
1865                 condlog(0,"%s : pr in read keys service action failed. Error=%d\n", pp->dev, ret);
1866                 goto out;
1867         }
1868
1869         condlog(3, " event pr=%d addlen=%d\n",resp->prin_descriptor.prin_readkeys.prgeneration,
1870                         resp->prin_descriptor.prin_readkeys.additional_length );
1871
1872         if (resp->prin_descriptor.prin_readkeys.additional_length == 0 )
1873         {
1874                 condlog(1, "%s: No key found. Device may not be registered.", pp->dev);
1875                 ret = MPATH_PR_SUCCESS;
1876                 goto out;
1877         }
1878         prkey = 0;
1879         keyp = (unsigned char *)mpp->reservation_key;
1880         for (j = 0; j < 8; ++j) {
1881                 if (j > 0)
1882                         prkey <<= 8;
1883                 prkey |= *keyp;
1884                 ++keyp;
1885         }
1886         condlog(2, "Multipath  reservation_key: 0x%" PRIx64 " ", prkey);
1887
1888         isFound =0;
1889         for (i = 0; i < resp->prin_descriptor.prin_readkeys.additional_length/8; i++ )
1890         {
1891                 condlog(2, "PR IN READKEYS[%d]  reservation key:\n",i);
1892                 dumpHex((char *)&resp->prin_descriptor.prin_readkeys.key_list[i*8], 8 , -1);
1893                 if (!memcmp(mpp->reservation_key, &resp->prin_descriptor.prin_readkeys.key_list[i*8], 8))
1894                 {
1895                         condlog(2, "%s: pr key found in prin readkeys response", mpp->alias);
1896                         isFound =1;
1897                         break;
1898                 }
1899         }
1900         if (!isFound)
1901         {
1902                 condlog(0, "%s: Either device not registered or ", pp->dev);
1903                 condlog(0, "host is not authorised for registration. Skip path\n");
1904                 ret = MPATH_PR_OTHER;
1905                 goto out;
1906         }
1907
1908         param= malloc(sizeof(struct prout_param_descriptor));
1909         memset(param, 0 , sizeof(struct prout_param_descriptor));
1910
1911         for (j = 7; j >= 0; --j) {
1912                 param->sa_key[j] = (prkey & 0xff);
1913                 prkey >>= 8;
1914         }
1915         param->num_transportid = 0;
1916
1917         condlog(3, "device %s:%s \n", pp->dev, pp->mpp->wwid);
1918
1919         ret = prout_do_scsi_ioctl(pp->dev, MPATH_PROUT_REG_IGN_SA, 0, 0, param, 0);
1920         if (ret != MPATH_PR_SUCCESS )
1921         {
1922                 condlog(0,"%s: Reservation registration failed. Error: %d\n", pp->dev, ret);
1923         }
1924         mpp->prflag = 1;
1925
1926         free(param);
1927 out:
1928         free(resp);
1929         return NULL;
1930 }
1931
1932 int mpath_pr_event_handle(struct path *pp)
1933 {
1934         pthread_t thread;
1935         int rc;
1936         pthread_attr_t attr;
1937         struct multipath * mpp;
1938
1939         mpp = pp->mpp;
1940
1941         if (!mpp->reservation_key)
1942                 return -1;
1943
1944         pthread_attr_init(&attr);
1945         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1946
1947         rc = pthread_create(&thread, NULL , mpath_pr_event_handler_fn, pp);
1948         if (rc) {
1949                 condlog(0, "%s: ERROR; return code from pthread_create() is %d\n", pp->dev, rc);
1950                 return -1;
1951         }
1952         pthread_attr_destroy(&attr);
1953         rc = pthread_join(thread, NULL);
1954         return 0;
1955 }
1956