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