[multipathd] sweets
[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
16 /*
17  * libsysfs
18  */
19 #include <sysfs/libsysfs.h>
20 #include <sysfs/dlist.h>
21
22 /*
23  * libcheckers
24  */
25 #include <checkers.h>
26
27 /*
28  * libmultipath
29  */
30 #include <parser.h>
31 #include <vector.h>
32 #include <memory.h>
33 #include <config.h>
34 #include <util.h>
35 #include <hwtable.h>
36 #include <defaults.h>
37 #include <structs.h>
38 #include <callout.h>
39 #include <blacklist.h>
40 #include <structs_vec.h>
41 #include <dmparser.h>
42 #include <devmapper.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
52 #include "main.h"
53 #include "pidfile.h"
54 #include "uxlsnr.h"
55 #include "uxclnt.h"
56 #include "cli.h"
57 #include "cli_handlers.h"
58 #include "lock.h"
59 #include "waiter.h"
60
61 #define FILE_NAME_SIZE 256
62 #define CMDSIZE 160
63
64 #define LOG_MSG(a,b) \
65         if (strlen(b)) condlog(a, "%s: %s", pp->dev, b);
66
67 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
68 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
69
70 /*
71  * global copy of vecs for use in sig handlers
72  */
73 struct vectors * gvecs;
74
75 static int
76 need_switch_pathgroup (struct multipath * mpp, int refresh)
77 {
78         struct pathgroup * pgp;
79         struct path * pp;
80         unsigned int i, j;
81
82         if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
83                 return 0;
84
85         /*
86          * Refresh path priority values
87          */
88         if (refresh)
89                 vector_foreach_slot (mpp->pg, pgp, i)
90                         vector_foreach_slot (pgp->paths, pp, j)
91                                 pathinfo(pp, conf->hwtable, DI_PRIO);
92
93         mpp->bestpg = select_path_group(mpp);
94
95         if (mpp->bestpg != mpp->nextpg)
96                 return 1;
97
98         return 0;
99 }
100
101 static void
102 switch_pathgroup (struct multipath * mpp)
103 {
104         mpp->stat_switchgroup++;
105         dm_switchgroup(mpp->alias, mpp->bestpg);
106         condlog(2, "%s: switch to path group #%i",
107                  mpp->alias, mpp->bestpg);
108 }
109
110 static int
111 coalesce_maps(struct vectors *vecs, vector nmpv)
112 {
113         struct multipath * ompp;
114         vector ompv = vecs->mpvec;
115         unsigned int i;
116         int j;
117
118         vector_foreach_slot (ompv, ompp, i) {
119                 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
120                         /*
121                          * remove all current maps not allowed by the
122                          * current configuration
123                          */
124                         if (dm_flush_map(ompp->alias, DEFAULT_TARGET)) {
125                                 condlog(0, "%s: unable to flush devmap",
126                                         ompp->alias);
127                                 /*
128                                  * may be just because the device is open
129                                  */
130                                 if (!vector_alloc_slot(nmpv))
131                                         return 1;
132
133                                 vector_set_slot(nmpv, ompp);
134                                 setup_multipath(vecs, ompp);
135
136                                 if ((j = find_slot(ompv, (void *)ompp)) != -1)
137                                         vector_del_slot(ompv, j);
138
139                                 continue;
140                         }
141                         else {
142                                 dm_lib_release();
143                                 condlog(3, "%s devmap removed", ompp->alias);
144                         }
145                 }
146         }
147         return 0;
148 }
149
150 static void
151 sync_map_state(struct multipath *mpp)
152 {
153         struct pathgroup *pgp;
154         struct path *pp;
155         unsigned int i, j;
156
157         vector_foreach_slot (mpp->pg, pgp, i){
158                 vector_foreach_slot (pgp->paths, pp, j){
159                         if (pp->state <= PATH_UNCHECKED)
160                                 continue;
161                         if ((pp->dmstate == PSTATE_FAILED ||
162                              pp->dmstate == PSTATE_UNDEF) &&
163                             (pp->state == PATH_UP || pp->state == PATH_GHOST))
164                                 dm_reinstate_path(mpp->alias, pp->dev_t);
165                         else if ((pp->dmstate == PSTATE_ACTIVE ||
166                                   pp->dmstate == PSTATE_UNDEF) &&
167                                  (pp->state == PATH_DOWN ||
168                                   pp->state == PATH_SHAKY))
169                                 dm_fail_path(mpp->alias, pp->dev_t);
170                 }
171         }
172 }
173
174 static void
175 sync_maps_state(vector mpvec)
176 {
177         unsigned int i;
178         struct multipath *mpp;
179
180         vector_foreach_slot (mpvec, mpp, i) 
181                 sync_map_state(mpp);
182 }
183
184 static int
185 flush_map(struct multipath * mpp, struct vectors * vecs)
186 {
187         /*
188          * clear references to this map before flushing so we can ignore
189          * the spurious uevent we may generate with the dm_flush_map call below
190          */
191         if (dm_flush_map(mpp->alias, DEFAULT_TARGET)) {
192                 /*
193                  * May not really be an error -- if the map was already flushed
194                  * from the device mapper by dmsetup(8) for instance.
195                  */
196                 condlog(0, "%s: can't flush", mpp->alias);
197                 return 1;
198         }
199         else {
200                 dm_lib_release();
201                 condlog(3, "%s: devmap removed", mpp->alias);
202         }
203
204         orphan_paths(vecs->pathvec, mpp);
205         remove_map(mpp, vecs, stop_waiter_thread, 1);
206
207         return 0;
208 }
209
210 static int
211 uev_add_map (char * devname, struct vectors * vecs)
212 {
213         condlog(2, "%s: add map (uevent)", devname);
214         return ev_add_map(devname, vecs);
215 }
216
217 int
218 ev_add_map (char * devname, struct vectors * vecs)
219 {
220         int major, minor;
221         char dev_t[BLK_DEV_SIZE];
222         char * alias;
223         char * refwwid;
224         struct multipath * mpp;
225         int map_present;
226         int r = 1;
227
228         if (sscanf(devname, "dm-%d", &minor) == 1 &&
229             !sysfs_get_dev(sysfs_path, devname, dev_t, BLK_DEV_SIZE) &&
230             sscanf(dev_t, "%d:%d", &major, &minor) == 2)
231                 alias = dm_mapname(major, minor);
232         else
233                 alias = STRDUP(devname);
234                 
235         if (!alias)
236                 return 1;
237         
238         map_present = dm_map_present(alias);
239
240         if (map_present && dm_type(alias, DEFAULT_TARGET) <= 0) {
241                 condlog(4, "%s: not a multipath map", alias);
242                 FREE(alias);
243                 return 0;
244         }
245
246         mpp = find_mp_by_alias(vecs->mpvec, alias);
247
248         if (mpp) {
249                 /*
250                  * Not really an error -- we generate our own uevent
251                  * if we create a multipath mapped device as a result
252                  * of uev_add_path
253                  */
254                 condlog(0, "%s: devmap already registered",
255                         devname);
256                 FREE(alias);
257                 return 0;
258         }
259
260         /*
261          * now we can register the map
262          */
263         if (map_present && (mpp = add_map_without_path(vecs, minor, alias,
264                                         start_waiter_thread))) {
265                 sync_map_state(mpp);
266                 condlog(3, "%s: devmap %s added", alias, devname);
267                 return 0;
268         }
269         refwwid = get_refwwid(devname, DEV_DEVMAP, vecs->pathvec);
270
271         if (refwwid) {
272                 r = coalesce_paths(vecs, NULL, refwwid);
273                 dm_lib_release();
274         }
275         
276         if (!r)
277                 condlog(3, "%s: devmap %s added", alias, devname);
278         else
279                 condlog(0, "%s: uev_add_map %s failed", alias, devname);
280
281         FREE(refwwid);
282         FREE(alias);
283         return r;
284 }
285
286 static int
287 uev_remove_map (char * devname, struct vectors * vecs)
288 {
289         condlog(2, "%s: remove map (uevent)", devname);
290         return ev_remove_map(devname, vecs);
291 }
292
293 int
294 ev_remove_map (char * devname, struct vectors * vecs)
295 {
296         struct multipath * mpp;
297
298         mpp = find_mp_by_str(vecs->mpvec, devname);
299
300         if (!mpp) {
301                 condlog(3, "%s: devmap not registered, can't remove",
302                         devname);
303                 return 0;
304         }
305         flush_map(mpp, vecs);
306
307         return 0;
308 }
309
310 static int
311 uev_umount_map (char * devname, struct vectors * vecs)
312 {
313         struct multipath * mpp;
314
315         condlog(2, "%s: umount map (uevent)", devname);
316
317         mpp = find_mp_by_str(vecs->mpvec, devname);
318
319         if (!mpp)
320                 return 0;
321
322         update_mpp_paths(mpp, vecs->pathvec);
323         verify_paths(mpp, vecs, NULL);
324
325         if (!VECTOR_SIZE(mpp->paths))
326                 flush_map(mpp, vecs);
327
328         return 0;
329 }
330         
331 static int
332 uev_add_path (char * devname, struct vectors * vecs)
333 {
334         condlog(2, "%s: add path (uevent)", devname);
335         return (ev_add_path(devname, vecs) != 1)? 0 : 1;
336 }
337
338
339 /*
340  * returns:
341  * 0: added
342  * 1: error
343  * 2: blacklisted
344  */
345 int
346 ev_add_path (char * devname, struct vectors * vecs)
347 {
348         struct multipath * mpp;
349         struct path * pp;
350         char empty_buff[WWID_SIZE] = {0};
351
352         pp = find_path_by_dev(vecs->pathvec, devname);
353
354         if (pp) {
355                 condlog(0, "%s: spurious uevent, path already in pathvec",
356                         devname);
357                 if (pp->mpp)
358                         return 0;
359         }
360         else {
361                 /*
362                  * get path vital state
363                  */
364                 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
365                       devname, DI_ALL))) {
366                         condlog(0, "%s: failed to store path info", devname);
367                         return 1;
368                 }
369                 pp->checkint = conf->checkint;
370         }
371
372         /*
373          * need path UID to go any further
374          */
375         if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
376                 condlog(0, "%s: failed to get path uid", devname);
377                 return 1; /* leave path added to pathvec */
378         }
379         if (blacklist_path(conf, pp)){
380                 int i = find_slot(vecs->pathvec, (void *)pp);
381                 if (i != -1)
382                         vector_del_slot(vecs->pathvec, i);
383                 free_path(pp);
384                 return 2;
385         }       
386         mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
387 rescan:
388         if (mpp) {
389                 if (adopt_paths(vecs->pathvec, mpp))
390                         return 1; /* leave path added to pathvec */
391
392                 verify_paths(mpp, vecs, NULL);
393                 mpp->action = ACT_RELOAD;
394         }
395         else {
396                 if ((mpp = add_map_with_path(vecs, pp, 1)))
397                         mpp->action = ACT_CREATE;
398                 else
399                         return 1; /* leave path added to pathvec */
400         }
401
402         /*
403          * push the map to the device-mapper
404          */
405         if (setup_map(mpp)) {
406                 condlog(0, "%s: failed to setup map for addition of new "
407                         "path %s", mpp->alias, devname);
408                 goto out;
409         }
410         /*
411          * reload the map for the multipath mapped device
412          */
413         if (domap(mpp) <= 0) {
414                 condlog(0, "%s: failed in domap for addition of new "
415                         "path %s", mpp->alias, devname);
416                 /*
417                  * deal with asynchronous uevents :((
418                  */
419                 if (mpp->action == ACT_RELOAD) {
420                         condlog(0, "%s: uev_add_path sleep", mpp->alias);
421                         sleep(1);
422                         update_mpp_paths(mpp, vecs->pathvec);
423                         goto rescan;
424                 }
425                 else
426                         goto out;
427         }
428         dm_lib_release();
429
430         /*
431          * update our state from kernel regardless of create or reload
432          */
433         if (setup_multipath(vecs, mpp))
434                 goto out;
435
436         sync_map_state(mpp);
437
438         if (mpp->action == ACT_CREATE &&
439             start_waiter_thread(mpp, vecs))
440                         goto out;
441
442         condlog(3, "%s path added to devmap %s", devname, mpp->alias);
443         return 0;
444
445 out:
446         remove_map(mpp, vecs, NULL, 1);
447         return 1;
448 }
449
450 static int
451 uev_remove_path (char * devname, struct vectors * vecs)
452 {
453         condlog(2, "%s: remove path (uevent)", devname);
454         return ev_remove_path(devname, vecs);
455 }
456
457 int
458 ev_remove_path (char * devname, struct vectors * vecs)
459 {
460         struct multipath * mpp;
461         struct path * pp;
462         int i;
463         int rm_path = 1;
464
465         pp = find_path_by_dev(vecs->pathvec, devname);
466
467         if (!pp) {
468                 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
469                 return 1;
470         }
471
472         /*
473          * avoid referring to the map of an orphanned path
474          */
475         if ((mpp = pp->mpp)) {
476
477                 /*
478                  * remove the map IFF removing the last path
479                  */
480                 if (pathcount(mpp, PATH_WILD) > 1) {
481                         vector rpvec = vector_alloc();
482
483                         /*
484                          * transform the mp->pg vector of vectors of paths
485                          * into a mp->params string to feed the device-mapper
486                          */
487                         update_mpp_paths(mpp, vecs->pathvec);
488                         if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
489                                 vector_del_slot(mpp->paths, i);
490
491                         if (VECTOR_SIZE(mpp->paths) == 0) {
492                                 char alias[WWID_SIZE];
493
494                                 /*
495                                  * flush_map will fail if the device is open
496                                  */
497                                 strncpy(alias, mpp->alias, WWID_SIZE);
498                                 if (flush_map(mpp, vecs))
499                                         rm_path = 0;
500                                 else
501                                         condlog(3, "%s: removed map after removing"
502                                                 " multiple paths", alias);
503                         }
504                         else {
505                                 if (setup_map(mpp)) {
506                                         condlog(0, "%s: failed to setup map for"
507                                                 " removal of path %s", mpp->alias, devname);
508                                         free_pathvec(rpvec, KEEP_PATHS);
509                                         goto out;
510                                 }
511                                 /*
512                                  * reload the map
513                                  */
514                                 mpp->action = ACT_RELOAD;
515                                 if (domap(mpp) <= 0) {
516                                         condlog(0, "%s: failed in domap for "
517                                                 "removal of path %s",
518                                                 mpp->alias, devname);
519                                         /*
520                                          * Delete path from pathvec so that
521                                          * update_mpp_paths wont find it later
522                                          * when/if another path is removed.
523                                          */
524                                         if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
525                                                 vector_del_slot(vecs->pathvec, i);
526                                         free_path(pp);
527                                         return 1;
528                                 }
529                                 /*
530                                  * update our state from kernel
531                                  */
532                                 if (setup_multipath(vecs, mpp)) {
533                                         free_pathvec(rpvec, KEEP_PATHS);
534                                         goto out;
535                                 }
536                                 sync_map_state(mpp);
537
538                                 condlog(3, "%s: path removed from map %s",
539                                         devname, mpp->alias);
540                         }
541                         free_pathvec(rpvec, KEEP_PATHS);
542                 }
543                 else {
544                         char alias[WWID_SIZE];
545
546                         /*
547                          * flush_map will fail if the device is open
548                          */
549                         strncpy(alias, mpp->alias, WWID_SIZE);
550                         if (flush_map(mpp, vecs))
551                                 rm_path = 0;
552                         else
553                                 condlog(3, "%s: removed map", alias);
554                 }
555         }
556
557         if (rm_path) {
558                 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
559                         vector_del_slot(vecs->pathvec, i);
560                 free_path(pp);
561         }
562
563         return 0;
564
565 out:
566         remove_map(mpp, vecs, stop_waiter_thread, 1);
567         return 1;
568 }
569
570 static int
571 map_discovery (struct vectors * vecs)
572 {
573         struct multipath * mpp;
574         unsigned int i;
575
576         if (dm_get_maps(vecs->mpvec, "multipath"))
577                 return 1;
578
579         vector_foreach_slot (vecs->mpvec, mpp, i)
580                 if (setup_multipath(vecs, mpp))
581                         return 1;
582
583         return 0;
584 }
585
586 int
587 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
588 {
589         struct vectors * vecs;
590         int r;
591         
592         *reply = NULL;
593         *len = 0;
594         vecs = (struct vectors *)trigger_data;
595
596         pthread_cleanup_push(cleanup_lock, vecs->lock);
597         lock(vecs->lock);
598
599         r = parse_cmd(str, reply, len, vecs);
600
601         if (r > 0) {
602                 *reply = STRDUP("fail\n");
603                 *len = strlen(*reply) + 1;
604                 r = 1;
605         }
606         else if (!r && *len == 0) {
607                 *reply = STRDUP("ok\n");
608                 *len = strlen(*reply) + 1;
609                 r = 0;
610         }
611         /* else if (r < 0) leave *reply alone */
612
613         lock_cleanup_pop(vecs->lock);
614         return r;
615 }
616
617 static int
618 uev_discard(char * devpath)
619 {
620         char a[10], b[10];
621
622         /*
623          * keep only block devices, discard partitions
624          */
625         if (sscanf(devpath, "/block/%10s", a) != 1 ||
626             sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
627                 condlog(4, "discard event on %s", devpath);
628                 return 1;
629         }
630         return 0;
631 }
632
633 int 
634 uev_trigger (struct uevent * uev, void * trigger_data)
635 {
636         int r = 0;
637         char devname[32];
638         struct vectors * vecs;
639
640         vecs = (struct vectors *)trigger_data;
641
642         if (uev_discard(uev->devpath))
643                 return 0;
644
645         basename(uev->devpath, devname);
646         lock(vecs->lock);
647
648         /*
649          * device map add/remove event
650          */
651         if (!strncmp(devname, "dm-", 3)) {
652                 if (!strncmp(uev->action, "add", 3)) {
653                         r = uev_add_map(devname, vecs);
654                         goto out;
655                 }
656                 if (!strncmp(uev->action, "remove", 6)) {
657                         r = uev_remove_map(devname, vecs);
658                         goto out;
659                 }
660                 if (!strncmp(uev->action, "umount", 6)) {
661                         r = uev_umount_map(devname, vecs);
662                         goto out;
663                 }
664                 goto out;
665         }
666         
667         /*
668          * path add/remove event
669          */
670         if (blacklist(conf->blist_devnode, devname))
671                 goto out;
672
673         if (!strncmp(uev->action, "add", 3)) {
674                 r = uev_add_path(devname, vecs);
675                 goto out;
676         }
677         if (!strncmp(uev->action, "remove", 6)) {
678                 r = uev_remove_path(devname, vecs);
679                 goto out;
680         }
681
682 out:
683         unlock(vecs->lock);
684         return r;
685 }
686
687 static void *
688 ueventloop (void * ap)
689 {
690         if (uevent_listen(&uev_trigger, ap))
691                 fprintf(stderr, "error starting uevent listener");
692                 
693         return NULL;
694 }
695
696 static void *
697 uxlsnrloop (void * ap)
698 {
699         if (load_keys())
700                 return NULL;
701         
702         if (alloc_handlers())
703                 return NULL;
704
705         add_handler(LIST+PATHS, cli_list_paths);
706         add_handler(LIST+MAPS, cli_list_maps);
707         add_handler(LIST+MAPS+STATUS, cli_list_maps_status);
708         add_handler(LIST+MAPS+STATS, cli_list_maps_stats);
709         add_handler(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
710         add_handler(LIST+TOPOLOGY, cli_list_maps_topology);
711         add_handler(LIST+MAP+TOPOLOGY, cli_list_map_topology);
712         add_handler(LIST+CONFIG, cli_list_config);
713         add_handler(ADD+PATH, cli_add_path);
714         add_handler(DEL+PATH, cli_del_path);
715         add_handler(ADD+MAP, cli_add_map);
716         add_handler(DEL+MAP, cli_del_map);
717         add_handler(SWITCH+MAP+GROUP, cli_switch_group);
718         add_handler(RECONFIGURE, cli_reconfigure);
719         add_handler(SUSPEND+MAP, cli_suspend);
720         add_handler(RESUME+MAP, cli_resume);
721         add_handler(REINSTATE+PATH, cli_reinstate);
722         add_handler(FAIL+PATH, cli_fail);
723
724         uxsock_listen(&uxsock_trigger, ap);
725
726         return NULL;
727 }
728
729 static int
730 exit_daemon (int status)
731 {
732         if (status != 0)
733                 fprintf(stderr, "bad exit status. see daemon.log\n");
734
735         condlog(3, "unlink pidfile");
736         unlink(DEFAULT_PIDFILE);
737
738         lock(&exit_mutex);
739         pthread_cond_signal(&exit_cond);
740         unlock(&exit_mutex);
741
742         return status;
743 }
744
745 static void
746 fail_path (struct path * pp, int del_active)
747 {
748         if (!pp->mpp)
749                 return;
750
751         condlog(2, "checker failed path %s in map %s",
752                  pp->dev_t, pp->mpp->alias);
753
754         dm_fail_path(pp->mpp->alias, pp->dev_t);
755         if (del_active)
756                 update_queue_mode_del_path(pp->mpp);
757 }
758
759 /*
760  * caller must have locked the path list before calling that function
761  */
762 static void
763 reinstate_path (struct path * pp, int add_active)
764 {
765         if (!pp->mpp)
766                 return;
767
768         if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
769                 condlog(0, "%s: reinstate failed", pp->dev_t);
770         else {
771                 condlog(2, "%s: reinstated", pp->dev_t);
772                 if (add_active)
773                         update_queue_mode_add_path(pp->mpp);
774         }
775 }
776
777 static void
778 enable_group(struct path * pp)
779 {
780         struct pathgroup * pgp;
781
782         /*
783          * if path is added through uev_add_path, pgindex can be unset.
784          * next update_strings() will set it, upon map reload event.
785          *
786          * we can safely return here, because upon map reload, all
787          * PG will be enabled.
788          */
789         if (!pp->mpp->pg || !pp->pgindex)
790                 return;
791
792         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
793         
794         if (pgp->status == PGSTATE_DISABLED) {
795                 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
796                 dm_enablegroup(pp->mpp->alias, pp->pgindex);
797         }
798 }
799
800 static void
801 mpvec_garbage_collector (struct vectors * vecs)
802 {
803         struct multipath * mpp;
804         unsigned int i;
805
806         vector_foreach_slot (vecs->mpvec, mpp, i) {
807                 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
808                         condlog(2, "%s: remove dead map", mpp->alias);
809                         remove_map(mpp, vecs, stop_waiter_thread, 1);
810                         i--;
811                 }
812         }
813 }
814
815 static void
816 defered_failback_tick (vector mpvec)
817 {
818         struct multipath * mpp;
819         unsigned int i;
820
821         vector_foreach_slot (mpvec, mpp, i) {
822                 /*
823                  * defered failback getting sooner
824                  */
825                 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
826                         mpp->failback_tick--;
827
828                         if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
829                                 switch_pathgroup(mpp);
830                 }
831         }
832 }
833
834 static void
835 retry_count_tick(vector mpvec)
836 {
837         struct multipath *mpp;
838         unsigned int i;
839
840         vector_foreach_slot (mpvec, mpp, i) {
841                 if (mpp->retry_tick) {
842                         mpp->stat_total_queueing_time++;
843                         condlog(4, "%s: Retrying.. No active path", mpp->alias);
844                         if(--mpp->retry_tick == 0) {
845                                 dm_queue_if_no_path(mpp->alias, 0);
846                                 condlog(2, "%s: Disable queueing", mpp->alias);
847                         }
848                 }
849         }
850 }
851
852 static void *
853 checkerloop (void *ap)
854 {
855         struct vectors *vecs;
856         struct path *pp;
857         int count = 0;
858         int newstate;
859         unsigned int i;
860
861         mlockall(MCL_CURRENT | MCL_FUTURE);
862         vecs = (struct vectors *)ap;
863         condlog(2, "path checkers start up");
864
865         /*
866          * init the path check interval
867          */
868         vector_foreach_slot (vecs->pathvec, pp, i) {
869                 pp->checkint = conf->checkint;
870         }
871
872         while (1) {
873                 pthread_cleanup_push(cleanup_lock, vecs->lock);
874                 lock(vecs->lock);
875                 condlog(4, "tick");
876
877                 vector_foreach_slot (vecs->pathvec, pp, i) {
878                         if (!pp->mpp)
879                                 continue;
880
881                         if (pp->tick && --pp->tick)
882                                 continue; /* don't check this path yet */
883
884                         /*
885                          * provision a next check soonest,
886                          * in case we exit abnormaly from here
887                          */
888                         pp->tick = conf->checkint;
889                         
890                         if (!checker_selected(&pp->checker)) {
891                                 pathinfo(pp, conf->hwtable, DI_SYSFS);
892                                 select_checker(pp);
893                         }
894
895                         if (!checker_selected(&pp->checker)) {
896                                 condlog(0, "%s: checker is not set", pp->dev);
897                                 continue;
898                         }
899                         newstate = checker_check(&pp->checker);
900                         
901                         if (newstate < 0) {
902                                 condlog(2, "%s: unusable path", pp->dev);
903                                 pathinfo(pp, conf->hwtable, 0);
904                                 continue;
905                         }
906
907                         if (newstate != pp->state) {
908                                 int oldstate = pp->state;
909                                 pp->state = newstate;
910                                 LOG_MSG(1, checker_message(&pp->checker));
911
912                                 /*
913                                  * upon state change, reset the checkint
914                                  * to the shortest delay
915                                  */
916                                 pp->checkint = conf->checkint;
917
918                                 if (newstate == PATH_DOWN ||
919                                     newstate == PATH_SHAKY ||
920                                     update_multipath_strings(pp->mpp,
921                                                              vecs->pathvec)) {
922                                         /*
923                                          * proactively fail path in the DM
924                                          */
925                                         if (oldstate == PATH_UP ||
926                                             oldstate == PATH_GHOST)
927                                                 fail_path(pp, 1);
928                                         else
929                                                 fail_path(pp, 0);
930
931                                         /*
932                                          * cancel scheduled failback
933                                          */
934                                         pp->mpp->failback_tick = 0;
935
936                                         pp->mpp->stat_path_failures++;
937                                         continue;
938                                 }
939
940                                 /*
941                                  * reinstate this path
942                                  */
943                                 if (oldstate != PATH_UP &&
944                                     oldstate != PATH_GHOST)
945                                         reinstate_path(pp, 1);
946                                 else
947                                         reinstate_path(pp, 0);
948
949                                 /*
950                                  * schedule [defered] failback
951                                  */
952                                 if (pp->mpp->pgfailback > 0)
953                                         pp->mpp->failback_tick =
954                                                 pp->mpp->pgfailback + 1;
955                                 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
956                                     need_switch_pathgroup(pp->mpp, 1))
957                                         switch_pathgroup(pp->mpp);
958
959                                 /*
960                                  * if at least one path is up in a group, and
961                                  * the group is disabled, re-enable it
962                                  */
963                                 if (newstate == PATH_UP)
964                                         enable_group(pp);
965                         }
966                         else if (newstate == PATH_UP || newstate == PATH_GHOST) {
967                                 LOG_MSG(4, checker_message(&pp->checker));
968                                 /*
969                                  * double the next check delay.
970                                  * max at conf->max_checkint
971                                  */
972                                 if (pp->checkint < (conf->max_checkint / 2))
973                                         pp->checkint = 2 * pp->checkint;
974                                 else
975                                         pp->checkint = conf->max_checkint;
976
977                                 pp->tick = pp->checkint;
978                                 condlog(4, "%s: delay next check %is",
979                                                 pp->dev_t, pp->tick);
980                         }
981                         else if (newstate == PATH_DOWN)
982                                 LOG_MSG(2, checker_message(&pp->checker));
983
984                         pp->state = newstate;
985
986                         /*
987                          * path prio refreshing
988                          */
989                         condlog(4, "path prio refresh");
990                         pathinfo(pp, conf->hwtable, DI_PRIO);
991
992                         if (need_switch_pathgroup(pp->mpp, 0)) {
993                                 if (pp->mpp->pgfailback > 0 &&
994                                     pp->mpp->failback_tick <= 0)
995                                         pp->mpp->failback_tick =
996                                                 pp->mpp->pgfailback + 1;
997                                 else if (pp->mpp->pgfailback ==
998                                                 -FAILBACK_IMMEDIATE)
999                                         switch_pathgroup(pp->mpp);
1000                         }
1001                 }
1002                 defered_failback_tick(vecs->mpvec);
1003                 retry_count_tick(vecs->mpvec);
1004
1005                 if (count)
1006                         count--;
1007                 else {
1008                         condlog(4, "map garbage collection");
1009                         mpvec_garbage_collector(vecs);
1010                         count = MAPGCINT;
1011                 }
1012                 
1013                 lock_cleanup_pop(vecs->lock);
1014                 sleep(1);
1015         }
1016         return NULL;
1017 }
1018
1019 int
1020 configure (struct vectors * vecs, int start_waiters)
1021 {
1022         struct multipath * mpp;
1023         struct path * pp;
1024         vector mpvec;
1025         int i;
1026
1027         if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1028                 return 1;
1029         
1030         if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1031                 return 1;
1032         
1033         if (!(mpvec = vector_alloc()))
1034                 return 1;
1035
1036         /*
1037          * probe for current path (from sysfs) and map (from dm) sets
1038          */
1039         path_discovery(vecs->pathvec, conf, DI_ALL);
1040
1041         vector_foreach_slot (vecs->pathvec, pp, i){
1042                 if (blacklist_path(conf, pp)){
1043                         vector_del_slot(vecs->pathvec, i);
1044                         free_path(pp);
1045                         i--;
1046                 }       
1047                 else
1048                         pp->checkint = conf->checkint;
1049         }
1050         if (map_discovery(vecs))
1051                 return 1;
1052
1053         /*
1054          * create new set of maps & push changed ones into dm
1055          */
1056         if (coalesce_paths(vecs, mpvec, NULL))
1057                 return 1;
1058
1059         /*
1060          * may need to remove some maps which are no longer relevant
1061          * e.g., due to blacklist changes in conf file
1062          */
1063         if (coalesce_maps(vecs, mpvec))
1064                 return 1;
1065
1066         dm_lib_release();
1067
1068         sync_maps_state(mpvec);
1069
1070         if (conf->verbosity > 2)
1071                 vector_foreach_slot(mpvec, mpp, i)
1072                         print_map(mpp);
1073
1074         /*
1075          * purge dm of old maps
1076          */
1077         remove_maps(vecs, NULL);
1078
1079         /*
1080          * save new set of maps formed by considering current path state
1081          */
1082         vector_free(vecs->mpvec);
1083         vecs->mpvec = mpvec;
1084
1085         /*
1086          * start dm event waiter threads for these new maps
1087          */
1088         vector_foreach_slot(vecs->mpvec, mpp, i) {
1089                 if (setup_multipath(vecs, mpp))
1090                         return 1;
1091                 if (start_waiters)
1092                         if (start_waiter_thread(mpp, vecs))
1093                                 return 1;
1094         }
1095         return 0;
1096 }
1097
1098 int
1099 reconfigure (struct vectors * vecs)
1100 {
1101         struct config * old = conf;
1102
1103         /*
1104          * free old map and path vectors ... they use old conf state
1105          */
1106         if (VECTOR_SIZE(vecs->mpvec))
1107                 remove_maps(vecs, stop_waiter_thread);
1108
1109         if (VECTOR_SIZE(vecs->pathvec))
1110                 free_pathvec(vecs->pathvec, FREE_PATHS);
1111
1112         vecs->pathvec = NULL;
1113         conf = NULL;
1114
1115         if (load_config(DEFAULT_CONFIGFILE))
1116                 return 1;
1117
1118         conf->verbosity = old->verbosity;
1119
1120         if (!conf->checkint) {
1121                 conf->checkint = DEFAULT_CHECKINT;
1122                 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1123         }
1124         configure(vecs, 1);
1125         free_config(old);
1126         return 0;
1127 }
1128
1129 static struct vectors *
1130 init_vecs (void)
1131 {
1132         struct vectors * vecs;
1133
1134         vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1135
1136         if (!vecs)
1137                 return NULL;
1138
1139         vecs->lock = 
1140                 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1141
1142         if (!vecs->lock)
1143                 goto out;
1144
1145         pthread_mutex_init(vecs->lock, NULL);
1146
1147         return vecs;
1148
1149 out:
1150         FREE(vecs);
1151         condlog(0, "failed to init paths");
1152         return NULL;
1153 }
1154
1155 static void *
1156 signal_set(int signo, void (*func) (int))
1157 {
1158         int r;
1159         struct sigaction sig;
1160         struct sigaction osig;
1161
1162         sig.sa_handler = func;
1163         sigemptyset(&sig.sa_mask);
1164         sig.sa_flags = 0;
1165
1166         r = sigaction(signo, &sig, &osig);
1167
1168         if (r < 0)
1169                 return (SIG_ERR);
1170         else
1171                 return (osig.sa_handler);
1172 }
1173
1174 static void
1175 sighup (int sig)
1176 {
1177         condlog(2, "reconfigure (SIGHUP)");
1178
1179         lock(gvecs->lock);
1180         reconfigure(gvecs);
1181         unlock(gvecs->lock);
1182
1183 #ifdef _DEBUG_
1184         dbg_free_final(NULL);
1185 #endif
1186 }
1187
1188 static void
1189 sigend (int sig)
1190 {
1191         exit_daemon(0);
1192 }
1193
1194 static void
1195 sigusr1 (int sig)
1196 {
1197         condlog(3, "SIGUSR1 received");
1198 }
1199
1200 static void
1201 signal_init(void)
1202 {
1203         signal_set(SIGHUP, sighup);
1204         signal_set(SIGUSR1, sigusr1);
1205         signal_set(SIGINT, sigend);
1206         signal_set(SIGTERM, sigend);
1207         signal(SIGPIPE, SIG_IGN);
1208 }
1209
1210 static void
1211 setscheduler (void)
1212 {
1213         int res;
1214         static struct sched_param sched_param = {
1215                 .sched_priority = 99
1216         };
1217
1218         res = sched_setscheduler (0, SCHED_RR, &sched_param);
1219
1220         if (res == -1)
1221                 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1222         return;
1223 }
1224
1225 static void
1226 set_oom_adj (int val)
1227 {
1228         FILE *fp;
1229
1230         fp = fopen("/proc/self/oom_adj", "w");
1231
1232         if (!fp)
1233                 return;
1234
1235         fprintf(fp, "%i", val);
1236         fclose(fp);
1237 }
1238         
1239 static int
1240 child (void * param)
1241 {
1242         pthread_t check_thr, uevent_thr, uxlsnr_thr;
1243         pthread_attr_t attr;
1244         struct vectors * vecs;
1245
1246         mlockall(MCL_CURRENT | MCL_FUTURE);
1247
1248         if (logsink)
1249                 log_thread_start();
1250
1251         condlog(2, "--------start up--------");
1252         condlog(2, "read " DEFAULT_CONFIGFILE);
1253
1254         if (load_config(DEFAULT_CONFIGFILE))
1255                 exit(1);
1256
1257         setlogmask(LOG_UPTO(conf->verbosity + 3));
1258
1259         /*
1260          * fill the voids left in the config file
1261          */
1262         if (!conf->checkint) {
1263                 conf->checkint = DEFAULT_CHECKINT;
1264                 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1265         }
1266
1267         if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1268                 if (logsink)
1269                         log_thread_stop();
1270
1271                 exit(1);
1272         }
1273         signal_init();
1274         setscheduler();
1275         set_oom_adj(-16);
1276         vecs = gvecs = init_vecs();
1277
1278         if (!vecs)
1279                 exit(1);
1280
1281         if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1282                 condlog(0, "can not find sysfs mount point");
1283                 exit(1);
1284         }
1285
1286         /*
1287          * fetch and configure both paths and multipaths
1288          */
1289         if (configure(vecs, 1)) {
1290                 condlog(0, "failure during configuration");
1291                 exit(1);
1292         }
1293
1294         /*
1295          * start threads
1296          */
1297         pthread_attr_init(&attr);
1298         pthread_attr_setstacksize(&attr, 64 * 1024);
1299         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1300         
1301         pthread_create(&check_thr, &attr, checkerloop, vecs);
1302         pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1303         pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1304
1305         pthread_cond_wait(&exit_cond, &exit_mutex);
1306
1307         /*
1308          * exit path
1309          */
1310         lock(vecs->lock);
1311         remove_maps(vecs, stop_waiter_thread);
1312         free_pathvec(vecs->pathvec, FREE_PATHS);
1313
1314         pthread_cancel(check_thr);
1315         pthread_cancel(uevent_thr);
1316         pthread_cancel(uxlsnr_thr);
1317
1318         free_keys(keys);
1319         keys = NULL;
1320         free_handlers(handlers);
1321         handlers = NULL;
1322         free_polls();
1323
1324         unlock(vecs->lock);
1325         pthread_mutex_destroy(vecs->lock);
1326         FREE(vecs->lock);
1327         vecs->lock = NULL;
1328         FREE(vecs);
1329         vecs = NULL;
1330         free_config(conf);
1331         conf = NULL;
1332
1333         condlog(2, "--------shut down-------");
1334         
1335         if (logsink)
1336                 log_thread_stop();
1337
1338         dm_lib_release();
1339         dm_lib_exit();
1340
1341 #ifdef _DEBUG_
1342         dbg_free_final(NULL);
1343 #endif
1344
1345         exit(0);
1346 }
1347
1348 static int
1349 daemonize(void)
1350 {
1351         int pid;
1352         int in_fd, out_fd;
1353
1354         if( (pid = fork()) < 0){
1355                 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1356                 return -1;
1357         }
1358         else if (pid != 0)
1359                 return pid;
1360
1361         setsid();
1362
1363         if ( (pid = fork()) < 0)
1364                 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1365         else if (pid != 0)
1366                 _exit(0);
1367
1368         in_fd = open("/dev/null", O_RDONLY);
1369         if (in_fd < 0){
1370                 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1371                         strerror(errno));
1372                 _exit(0);
1373         }
1374         out_fd = open("/dev/console", O_WRONLY);
1375         if (out_fd < 0){
1376                 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1377                         strerror(errno));
1378                 _exit(0);
1379         }
1380
1381         close(STDIN_FILENO);
1382         dup(in_fd);
1383         close(STDOUT_FILENO);
1384         dup(out_fd);
1385         close(STDERR_FILENO);
1386         dup(out_fd);
1387
1388         close(in_fd);
1389         close(out_fd);
1390         chdir("/");
1391         umask(0);
1392         return 0;
1393 }
1394
1395 int
1396 main (int argc, char *argv[])
1397 {
1398         extern char *optarg;
1399         extern int optind;
1400         int arg;
1401         int err;
1402         
1403         logsink = 1;
1404
1405         if (getuid() != 0) {
1406                 fprintf(stderr, "need to be root\n");
1407                 exit(1);
1408         }
1409
1410         /* make sure we don't lock any path */
1411         chdir("/");
1412         umask(umask(077) | 022);
1413
1414         conf = alloc_config();
1415
1416         if (!conf)
1417                 exit(1);
1418
1419         while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1420         switch(arg) {
1421                 case 'd':
1422                         logsink = 0;
1423                         //debug=1; /* ### comment me out ### */
1424                         break;
1425                 case 'v':
1426                         if (sizeof(optarg) > sizeof(char *) ||
1427                             !isdigit(optarg[0]))
1428                                 exit(1);
1429
1430                         conf->verbosity = atoi(optarg);
1431                         break;
1432                 case 'k':
1433                         uxclnt(optarg);
1434                         exit(0);
1435                 default:
1436                         ;
1437                 }
1438         }
1439
1440         if (!logsink)
1441                 err = 0;
1442         else
1443                 err = daemonize();
1444         
1445         if (err < 0)
1446                 /* error */
1447                 exit(1);
1448         else if (err > 0)
1449                 /* parent dies */
1450                 exit(0);
1451         else
1452                 /* child lives */
1453                 return (child(NULL));
1454 }
1455