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