Merge branch 'vendor/GCC50'
[dragonfly.git] / sbin / hammer2 / cmd_service.c
1 /*
2  * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include "hammer2.h"
37
38 #include <sys/xdiskioctl.h>
39 #include <machine/atomic.h>
40
41 struct hammer2_media_config {
42         hammer2_volconf_t       copy_run;
43         hammer2_volconf_t       copy_pend;
44         pthread_t               thread;
45         pthread_cond_t          cond;
46         int                     ctl;
47         int                     fd;
48         int                     pipefd[2];      /* signal stop */
49         dmsg_iocom_t            iocom;
50         pthread_t               iocom_thread;
51         enum { H2MC_STOPPED, H2MC_CONNECT, H2MC_RUNNING } state;
52 };
53
54 typedef struct hammer2_media_config hammer2_media_config_t;
55
56 #define H2CONFCTL_STOP          0x00000001
57 #define H2CONFCTL_UPDATE        0x00000002
58
59 struct diskcon {
60         TAILQ_ENTRY(diskcon) entry;
61         char    *disk;
62 };
63
64 struct service_node_opaque {
65         char    cl_label[64];
66         char    fs_label[64];
67         dmsg_media_block_t block;
68         int     attached;
69         int     servicing;
70         int     servicefd;
71 };
72
73 struct autoconn {
74         TAILQ_ENTRY(autoconn) entry;
75         char    *host;
76         int     stage;
77         int     stopme;
78         int     pipefd[2];      /* {read,write} */
79         enum { AUTOCONN_INACTIVE, AUTOCONN_ACTIVE } state;
80         pthread_t thread;
81 };
82
83 #define WS " \r\n"
84
85 TAILQ_HEAD(, diskcon) diskconq = TAILQ_HEAD_INITIALIZER(diskconq);
86 static pthread_mutex_t diskmtx;
87 static pthread_mutex_t confmtx;
88
89 static void *service_thread(void *data);
90 static void *udev_thread(void *data);
91 static void *autoconn_thread(void *data);
92 static void master_reconnect(const char *mntpt);
93 static void disk_reconnect(const char *disk);
94 static void disk_disconnect(void *handle);
95 static void udev_check_disks(void);
96 static void hammer2_usrmsg_handler(dmsg_msg_t *msg, int unmanaged);
97 static void *hammer2_volconf_thread(void *info);
98 static void hammer2_volconf_signal(dmsg_iocom_t *iocom);
99 static void hammer2_volconf_start(hammer2_media_config_t *conf,
100                         const char *hostname);
101 static void hammer2_volconf_stop(hammer2_media_config_t *conf);
102
103
104 static void xdisk_connect(void);
105
106 /*
107  * Start-up the master listener daemon for the machine.  This daemon runs
108  * a UDP discovery protocol, a TCP rendezvous, and scans certain files
109  * and directories for work.
110  *
111  * --
112  *
113  * The only purpose for the UDP discovery protocol is to determine what
114  * other IPs on the LAN are running the hammer2 service daemon.  DNS is not
115  * required to operate, but hostnames (if assigned) must be unique.  If
116  * no hostname is assigned the host's IP is used as the name.  This name
117  * is broadcast along with the mtime of the originator's private key.
118  *
119  * Receiving hammer2 service daemons which are able to match the label against
120  * /etc/hammer2/remote/<label>.pub will initiate a persistent connection
121  * to the target.  Removal of the file will cause a disconnection.  A failed
122  * public key negotiation stops further connection attempts until either the
123  * file is updated or the remote mtime is updated.
124  *
125  * Generally speaking this results in a web of connections, typically a
126  * combination of point-to-point for the more important links and relayed
127  * (spanning tree) for less important or filtered links.
128  *
129  * --
130  *
131  * The TCP listener serves as a rendezvous point in the cluster, accepting
132  * connections, performing registrations and authentications, maintaining
133  * the spanning tree, and keeping track of message state so disconnects can
134  * be handled properly.
135  *
136  * Once authenticated only low-level messaging protocols (which includes
137  * tracking persistent messages) are handled by this daemon.  This daemon
138  * does not run the higher level quorum or locking protocols.
139  *
140  * --
141  *
142  * The file /etc/hammer2/autoconn, if it exists, contains a list of targets
143  * to connect to (which do not have to be on the local lan).  This list will
144  * be retried until a connection can be established.  The file is not usually
145  * needed for linkages local to the LAN.
146  */
147 int
148 cmd_service(void)
149 {
150         struct sockaddr_in lsin;
151         int on;
152         int lfd;
153
154         /*
155          * Acquire socket and set options
156          */
157         if ((lfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
158                 fprintf(stderr, "master_listen: socket(): %s\n",
159                         strerror(errno));
160                 return 1;
161         }
162         on = 1;
163         setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
164
165         /*
166          * Setup listen port and try to bind.  If the bind fails we assume
167          * that a master listener process is already running and silently
168          * fail.
169          */
170         bzero(&lsin, sizeof(lsin));
171         lsin.sin_family = AF_INET;
172         lsin.sin_addr.s_addr = INADDR_ANY;
173         lsin.sin_port = htons(DMSG_LISTEN_PORT);
174         if (bind(lfd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
175                 close(lfd);
176                 if (QuietOpt == 0) {
177                         fprintf(stderr,
178                                 "master listen: daemon already running\n");
179                 }
180                 return 0;
181         }
182         if (QuietOpt == 0)
183                 fprintf(stderr, "master listen: startup\n");
184         listen(lfd, 50);
185
186         /*
187          * Fork and disconnect the controlling terminal and parent process,
188          * executing the specified function as a pthread.
189          *
190          * Returns to the original process which can then continue running.
191          * In debug mode this call will create the pthread without forking
192          * and set NormalExit to 0, instead of fork.
193          */
194         hammer2_demon(service_thread, (void *)(intptr_t)lfd);
195         if (NormalExit)
196                 close(lfd);
197         return 0;
198 }
199
200 /*
201  * Master listen/accept thread.  Accept connections on the master socket,
202  * starting a pthread for each one.
203  */
204 static
205 void *
206 service_thread(void *data)
207 {
208         struct sockaddr_in asin;
209         socklen_t alen;
210         pthread_t thread;
211         dmsg_master_service_info_t *info;
212         int lfd = (int)(intptr_t)data;
213         int fd;
214         int i;
215         int count;
216         int opt;
217         struct statfs *mntbuf = NULL;
218         struct statvfs *mntvbuf = NULL;
219
220         /*
221          * Nobody waits for us
222          */
223         setproctitle("hammer2 master listen");
224         pthread_detach(pthread_self());
225
226         /*
227          * Start up a thread to handle block device monitoring for
228          * export to the cluster.
229          */
230         thread = NULL;
231         pthread_create(&thread, NULL, udev_thread, NULL);
232
233         /*
234          * Start up a thread to tie /dev/xdisk into the cluster
235          * controller.
236          */
237         xdisk_connect();
238
239         /*
240          * Start thread to manage /etc/hammer2/autoconn
241          */
242         thread = NULL;
243         pthread_create(&thread, NULL, autoconn_thread, NULL);
244
245         /*
246          * Scan existing hammer2 mounts and reconnect to them using
247          * HAMMER2IOC_RECLUSTER.
248          */
249         count = getmntvinfo(&mntbuf, &mntvbuf, MNT_NOWAIT);
250         for (i = 0; i < count; ++i) {
251                 if (strcmp(mntbuf[i].f_fstypename, "hammer2") == 0)
252                         master_reconnect(mntbuf[i].f_mntonname);
253         }
254
255         /*
256          * Accept connections and create pthreads to handle them after
257          * validating the IP.
258          */
259         for (;;) {
260                 alen = sizeof(asin);
261                 fd = accept(lfd, (struct sockaddr *)&asin, &alen);
262                 if (fd < 0) {
263                         if (errno == EINTR)
264                                 continue;
265                         break;
266                 }
267                 opt = 1;
268                 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt);
269                 thread = NULL;
270                 fprintf(stderr, "service_thread: accept fd %d\n", fd);
271                 info = malloc(sizeof(*info));
272                 bzero(info, sizeof(*info));
273                 info->fd = fd;
274                 info->detachme = 1;
275                 info->usrmsg_callback = hammer2_usrmsg_handler;
276                 info->label = strdup("client");
277                 pthread_create(&thread, NULL, dmsg_master_service, info);
278         }
279         return (NULL);
280 }
281
282 /*
283  * Handle/Monitor the dmsg stream.  If unmanaged is set we are responsible
284  * for responding for the message, otherwise if it is not set libdmsg has
285  * already done some preprocessing and will respond to the message for us
286  * when we return.
287  *
288  * We primarily monitor for VOLCONFs
289  */
290 static
291 void
292 hammer2_usrmsg_handler(dmsg_msg_t *msg, int unmanaged)
293 {
294         dmsg_state_t *state;
295         hammer2_media_config_t *conf;
296         dmsg_lnk_hammer2_volconf_t *msgconf;
297         int i;
298
299         /*
300          * Only process messages which are part of a LNK_CONN stream
301          */
302         state = msg->state;
303         if (state == NULL ||
304             (state->rxcmd & DMSGF_BASECMDMASK) != DMSG_LNK_CONN) {
305                 hammer2_shell_parse(msg, unmanaged);
306                 return;
307         }
308
309         switch(msg->tcmd) {
310         case DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_DELETE:
311         case DMSG_LNK_CONN | DMSGF_DELETE:
312         case DMSG_LNK_ERROR | DMSGF_DELETE:
313                 /*
314                  * Deleting connection, clean out all volume configs
315                  */
316                 if (state->media == NULL || state->media->usrhandle == NULL)
317                         break;
318                 conf = state->media->usrhandle;
319                 fprintf(stderr, "Shutting down media spans\n");
320                 for (i = 0; i < HAMMER2_COPYID_COUNT; ++i) {
321                         if (conf[i].thread) {
322                                 conf[i].ctl = H2CONFCTL_STOP;
323                                 pthread_cond_signal(&conf[i].cond);
324                         }
325                 }
326                 for (i = 0; i < HAMMER2_COPYID_COUNT; ++i) {
327                         if (conf[i].thread) {
328                                 pthread_join(conf[i].thread, NULL);
329                                 conf->thread = NULL;
330                                 pthread_cond_destroy(&conf[i].cond);
331                         }
332                 }
333                 state->media->usrhandle = NULL;
334                 free(conf);
335                 break;
336         case DMSG_LNK_HAMMER2_VOLCONF:
337                 /*
338                  * One-way volume-configuration message is transmitted
339                  * over the open LNK_CONN transaction.
340                  */
341                 fprintf(stderr, "RECEIVED VOLCONF\n");
342
343                 if ((conf = state->media->usrhandle) == NULL) {
344                         conf = malloc(sizeof(*conf) * HAMMER2_COPYID_COUNT);
345                         bzero(conf, sizeof(*conf) * HAMMER2_COPYID_COUNT);
346                         state->media->usrhandle = conf;
347                 }
348                 msgconf = H2_LNK_VOLCONF(msg);
349
350                 if (msgconf->index < 0 ||
351                     msgconf->index >= HAMMER2_COPYID_COUNT) {
352                         fprintf(stderr,
353                                 "VOLCONF: ILLEGAL INDEX %d\n",
354                                 msgconf->index);
355                         break;
356                 }
357                 if (msgconf->copy.path[sizeof(msgconf->copy.path) - 1] != 0 ||
358                     msgconf->copy.path[0] == 0) {
359                         fprintf(stderr,
360                                 "VOLCONF: ILLEGAL PATH %d\n",
361                                 msgconf->index);
362                         break;
363                 }
364                 conf += msgconf->index;
365                 pthread_mutex_lock(&confmtx);
366                 conf->copy_pend = msgconf->copy;
367                 conf->ctl |= H2CONFCTL_UPDATE;
368                 pthread_mutex_unlock(&confmtx);
369                 if (conf->thread == NULL) {
370                         fprintf(stderr, "VOLCONF THREAD STARTED\n");
371                         pthread_cond_init(&conf->cond, NULL);
372                         pthread_create(&conf->thread, NULL,
373                                        hammer2_volconf_thread, (void *)conf);
374                 }
375                 pthread_cond_signal(&conf->cond);
376                 break;
377         default:
378                 if (unmanaged)
379                         dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
380                 break;
381         }
382 }
383
384 static void *
385 hammer2_volconf_thread(void *info)
386 {
387         hammer2_media_config_t *conf = info;
388
389         pthread_mutex_lock(&confmtx);
390         while ((conf->ctl & H2CONFCTL_STOP) == 0) {
391                 if (conf->ctl & H2CONFCTL_UPDATE) {
392                         fprintf(stderr, "VOLCONF UPDATE\n");
393                         conf->ctl &= ~H2CONFCTL_UPDATE;
394                         if (bcmp(&conf->copy_run, &conf->copy_pend,
395                                  sizeof(conf->copy_run)) == 0) {
396                                 fprintf(stderr, "VOLCONF: no changes\n");
397                                 continue;
398                         }
399                         /*
400                          * XXX TODO - auto reconnect on lookup failure or
401                          *              connect failure or stream failure.
402                          */
403
404                         pthread_mutex_unlock(&confmtx);
405                         hammer2_volconf_stop(conf);
406                         conf->copy_run = conf->copy_pend;
407                         if (conf->copy_run.copyid != 0 &&
408                             strncmp(conf->copy_run.path, "span:", 5) == 0) {
409                                 hammer2_volconf_start(conf,
410                                                       conf->copy_run.path + 5);
411                         }
412                         pthread_mutex_lock(&confmtx);
413                         fprintf(stderr, "VOLCONF UPDATE DONE state %d\n", conf->state);
414                 }
415                 if (conf->state == H2MC_CONNECT) {
416                         hammer2_volconf_start(conf, conf->copy_run.path + 5);
417                         pthread_mutex_unlock(&confmtx);
418                         sleep(5);
419                         pthread_mutex_lock(&confmtx);
420                 } else {
421                         pthread_cond_wait(&conf->cond, &confmtx);
422                 }
423         }
424         pthread_mutex_unlock(&confmtx);
425         hammer2_volconf_stop(conf);
426         return(NULL);
427 }
428
429 static
430 void
431 hammer2_volconf_start(hammer2_media_config_t *conf, const char *hostname)
432 {
433         dmsg_master_service_info_t *info;
434
435         switch(conf->state) {
436         case H2MC_STOPPED:
437         case H2MC_CONNECT:
438                 conf->fd = dmsg_connect(hostname);
439                 if (conf->fd < 0) {
440                         fprintf(stderr, "Unable to connect to %s\n", hostname);
441                         conf->state = H2MC_CONNECT;
442                 } else if (pipe(conf->pipefd) < 0) {
443                         close(conf->fd);
444                         fprintf(stderr, "pipe() failed during volconf\n");
445                         conf->state = H2MC_CONNECT;
446                 } else {
447                         fprintf(stderr, "VOLCONF CONNECT\n");
448                         info = malloc(sizeof(*info));
449                         bzero(info, sizeof(*info));
450                         info->fd = conf->fd;
451                         info->altfd = conf->pipefd[0];
452                         info->altmsg_callback = hammer2_volconf_signal;
453                         info->usrmsg_callback = hammer2_usrmsg_handler;
454                         info->detachme = 0;
455                         conf->state = H2MC_RUNNING;
456                         pthread_create(&conf->iocom_thread, NULL,
457                                        dmsg_master_service, info);
458                 }
459                 break;
460         case H2MC_RUNNING:
461                 break;
462         }
463 }
464
465 static
466 void
467 hammer2_volconf_stop(hammer2_media_config_t *conf)
468 {
469         switch(conf->state) {
470         case H2MC_STOPPED:
471                 break;
472         case H2MC_CONNECT:
473                 conf->state = H2MC_STOPPED;
474                 break;
475         case H2MC_RUNNING:
476                 close(conf->pipefd[1]);
477                 conf->pipefd[1] = -1;
478                 pthread_join(conf->iocom_thread, NULL);
479                 conf->iocom_thread = NULL;
480                 conf->state = H2MC_STOPPED;
481                 break;
482         }
483 }
484
485 static
486 void
487 hammer2_volconf_signal(dmsg_iocom_t *iocom)
488 {
489         atomic_set_int(&iocom->flags, DMSG_IOCOMF_EOF);
490 }
491
492 /*
493  * Monitor block devices.  Currently polls every ~10 seconds or so.
494  */
495 static
496 void *
497 udev_thread(void *data __unused)
498 {
499         int     fd;
500         int     seq = 0;
501
502         pthread_detach(pthread_self());
503
504         if ((fd = open(UDEV_DEVICE_PATH, O_RDWR)) < 0) {
505                 fprintf(stderr, "udev_thread: unable to open \"%s\"\n",
506                         UDEV_DEVICE_PATH);
507                 pthread_exit(NULL);
508         }
509         udev_check_disks();
510         while (ioctl(fd, UDEVWAIT, &seq) == 0) {
511                 udev_check_disks();
512                 sleep(1);
513         }
514         return (NULL);
515 }
516
517 static void *autoconn_connect_thread(void *data);
518 static void autoconn_disconnect_signal(dmsg_iocom_t *iocom);
519
520 static
521 void *
522 autoconn_thread(void *data __unused)
523 {
524         TAILQ_HEAD(, autoconn) autolist;
525         struct autoconn *ac;
526         struct autoconn *next;
527         pthread_t thread;
528         struct stat st;
529         time_t  t;
530         time_t  lmod;
531         int     found_last;
532         FILE    *fp;
533         char    buf[256];
534
535         TAILQ_INIT(&autolist);
536         found_last = 0;
537         lmod = 0;
538
539         pthread_detach(pthread_self());
540         for (;;) {
541                 /*
542                  * Polling interval
543                  */
544                 sleep(5);
545
546                 /*
547                  * Poll the file.  Loop up if the synchronized state (lmod)
548                  * has not changed.
549                  */
550                 if (stat(HAMMER2_DEFAULT_DIR "/autoconn", &st) == 0) {
551                         if (lmod == st.st_mtime)
552                                 continue;
553                         fp = fopen(HAMMER2_DEFAULT_DIR "/autoconn", "r");
554                         if (fp == NULL)
555                                 continue;
556                 } else {
557                         if (lmod == 0)
558                                 continue;
559                         fp = NULL;
560                 }
561
562                 /*
563                  * Wait at least 5 seconds after the file is created or
564                  * removed.
565                  *
566                  * Do not update the synchronized state.
567                  */
568                 if (fp == NULL && found_last) {
569                         found_last = 0;
570                         continue;
571                 } else if (fp && found_last == 0) {
572                         fclose(fp);
573                         found_last = 1;
574                         continue;
575                 }
576
577                 /*
578                  * Don't scan the file until the time progresses past the
579                  * file's mtime, so we can validate that the file was not
580                  * further modified during our scan.
581                  *
582                  * Do not update the synchronized state.
583                  */
584                 time(&t);
585                 if (fp) {
586                         if (t == st.st_mtime) {
587                                 fclose(fp);
588                                 continue;
589                         }
590                         t = st.st_mtime;
591                 } else {
592                         t = 0;
593                 }
594
595                 /*
596                  * Set staging to disconnect, then scan the file.
597                  */
598                 TAILQ_FOREACH(ac, &autolist, entry)
599                         ac->stage = 0;
600                 while (fp && fgets(buf, sizeof(buf), fp) != NULL) {
601                         char *host;
602
603                         if ((host = strtok(buf, " \t\r\n")) == NULL ||
604                             host[0] == '#') {
605                                 continue;
606                         }
607                         TAILQ_FOREACH(ac, &autolist, entry) {
608                                 if (strcmp(host, ac->host) == 0)
609                                         break;
610                         }
611                         if (ac == NULL) {
612                                 ac = malloc(sizeof(*ac));
613                                 bzero(ac, sizeof(*ac));
614                                 ac->host = strdup(host);
615                                 ac->state = AUTOCONN_INACTIVE;
616                                 TAILQ_INSERT_TAIL(&autolist, ac, entry);
617                         }
618                         ac->stage = 1;
619                 }
620
621                 /*
622                  * Ignore the scan (and retry again) if the file was
623                  * modified during the scan.
624                  *
625                  * Do not update the synchronized state.
626                  */
627                 if (fp) {
628                         if (fstat(fileno(fp), &st) < 0) {
629                                 fclose(fp);
630                                 continue;
631                         }
632                         fclose(fp);
633                         if (t != st.st_mtime)
634                                 continue;
635                 }
636
637                 /*
638                  * Update the synchronized state and reconfigure the
639                  * connect list as needed.
640                  */
641                 lmod = t;
642                 next = TAILQ_FIRST(&autolist);
643                 while ((ac = next) != NULL) {
644                         next = TAILQ_NEXT(ac, entry);
645
646                         /*
647                          * Staging, initiate
648                          */
649                         if (ac->stage && ac->state == AUTOCONN_INACTIVE) {
650                                 if (pipe(ac->pipefd) == 0) {
651                                         ac->stopme = 0;
652                                         ac->state = AUTOCONN_ACTIVE;
653                                         thread = NULL;
654                                         pthread_create(&thread, NULL,
655                                                        autoconn_connect_thread,
656                                                        ac);
657                                 }
658                         }
659
660                         /*
661                          * Unstaging, stop active connection.
662                          *
663                          * We write to the pipe which causes the iocom_core
664                          * to call autoconn_disconnect_signal().
665                          */
666                         if (ac->stage == 0 &&
667                             ac->state == AUTOCONN_ACTIVE) {
668                                 if (ac->stopme == 0) {
669                                         char dummy = 0;
670                                         ac->stopme = 1;
671                                         write(ac->pipefd[1], &dummy, 1);
672                                 }
673                         }
674
675                         /*
676                          * Unstaging, delete inactive connection.
677                          */
678                         if (ac->stage == 0 &&
679                             ac->state == AUTOCONN_INACTIVE) {
680                                 TAILQ_REMOVE(&autolist, ac, entry);
681                                 free(ac->host);
682                                 free(ac);
683                                 continue;
684                         }
685                 }
686                 sleep(5);
687         }
688         return(NULL);
689 }
690
691 static
692 void *
693 autoconn_connect_thread(void *data)
694 {
695         dmsg_master_service_info_t *info;
696         struct autoconn *ac;
697         void *res;
698         int fd;
699
700         ac = data;
701         pthread_detach(pthread_self());
702
703         while (ac->stopme == 0) {
704                 fd = dmsg_connect(ac->host);
705                 if (fd < 0) {
706                         fprintf(stderr, "autoconn: Connect failure: %s\n",
707                                 ac->host);
708                         sleep(5);
709                         continue;
710                 }
711                 fprintf(stderr, "autoconn: Connect %s\n", ac->host);
712
713                 info = malloc(sizeof(*info));
714                 bzero(info, sizeof(*info));
715                 info->fd = fd;
716                 info->altfd = ac->pipefd[0];
717                 info->altmsg_callback = autoconn_disconnect_signal;
718                 info->usrmsg_callback = hammer2_usrmsg_handler;
719                 info->detachme = 0;
720                 info->noclosealt = 1;
721                 pthread_create(&ac->thread, NULL, dmsg_master_service, info);
722                 pthread_join(ac->thread, &res);
723         }
724         close(ac->pipefd[0]);
725         ac->state = AUTOCONN_INACTIVE;
726         /* auto structure can be ripped out here */
727         return(NULL);
728 }
729
730 static
731 void
732 autoconn_disconnect_signal(dmsg_iocom_t *iocom)
733 {
734         fprintf(stderr, "autoconn: Shutting down socket\n");
735         atomic_set_int(&iocom->flags, DMSG_IOCOMF_EOF);
736 }
737
738 /*
739  * Retrieve the list of disk attachments and attempt to export
740  * them.
741  */
742 static
743 void
744 udev_check_disks(void)
745 {
746         char tmpbuf[1024];
747         char *buf = NULL;
748         char *disk;
749         int error;
750         size_t n;
751
752         for (;;) {
753                 n = 0;
754                 error = sysctlbyname("kern.disks", NULL, &n, NULL, 0);
755                 if (error < 0 || n == 0)
756                         break;
757                 if (n >= sizeof(tmpbuf))
758                         buf = malloc(n + 1);
759                 else
760                         buf = tmpbuf;
761                 error = sysctlbyname("kern.disks", buf, &n, NULL, 0);
762                 if (error == 0) {
763                         buf[n] = 0;
764                         break;
765                 }
766                 if (buf != tmpbuf) {
767                         free(buf);
768                         buf = NULL;
769                 }
770                 if (errno != ENOMEM)
771                         break;
772         }
773         if (buf) {
774                 fprintf(stderr, "DISKS: %s\n", buf);
775                 for (disk = strtok(buf, WS); disk; disk = strtok(NULL, WS)) {
776                         disk_reconnect(disk);
777                 }
778                 if (buf != tmpbuf)
779                         free(buf);
780         }
781 }
782
783 /*
784  * Normally the mount program supplies a cluster communications
785  * descriptor to the hammer2 vfs on mount, but if you kill the service
786  * daemon and restart it that link will be lost.
787  *
788  * This procedure attempts to [re]connect to existing mounts when
789  * the service daemon is started up before going into its accept
790  * loop.
791  *
792  * NOTE: A hammer2 mount point can only accomodate one connection at a time
793  *       so this will disconnect any existing connection during the
794  *       reconnect.
795  */
796 static
797 void
798 master_reconnect(const char *mntpt)
799 {
800         struct hammer2_ioc_recluster recls;
801         dmsg_master_service_info_t *info;
802         pthread_t thread;
803         int fd;
804         int pipefds[2];
805
806         fd = open(mntpt, O_RDONLY);
807         if (fd < 0) {
808                 fprintf(stderr, "reconnect %s: no access to mount\n", mntpt);
809                 return;
810         }
811         if (pipe(pipefds) < 0) {
812                 fprintf(stderr, "reconnect %s: pipe() failed\n", mntpt);
813                 close(fd);
814                 return;
815         }
816         bzero(&recls, sizeof(recls));
817         recls.fd = pipefds[0];
818         if (ioctl(fd, HAMMER2IOC_RECLUSTER, &recls) < 0) {
819                 fprintf(stderr, "reconnect %s: ioctl failed\n", mntpt);
820                 close(pipefds[0]);
821                 close(pipefds[1]);
822                 close(fd);
823                 return;
824         }
825         close(pipefds[0]);
826         close(fd);
827
828         info = malloc(sizeof(*info));
829         bzero(info, sizeof(*info));
830         info->fd = pipefds[1];
831         info->detachme = 1;
832         info->usrmsg_callback = hammer2_usrmsg_handler;
833         info->label = strdup("hammer2");
834         pthread_create(&thread, NULL, dmsg_master_service, info);
835 }
836
837 /*
838  * Reconnect a physical disk service to the mesh.
839  */
840 static
841 void
842 disk_reconnect(const char *disk)
843 {
844         struct disk_ioc_recluster recls;
845         struct diskcon *dc;
846         dmsg_master_service_info_t *info;
847         pthread_t thread;
848         int fd;
849         int pipefds[2];
850         char *path;
851
852         /*
853          * Urm, this will auto-create mdX+1, just ignore for now.
854          * This mechanic needs to be fixed.  It might actually be nice
855          * to be able to export md disks.
856          */
857         if (strncmp(disk, "md", 2) == 0)
858                 return;
859         if (strncmp(disk, "xa", 2) == 0)
860                 return;
861
862         /*
863          * Check if already connected
864          */
865         pthread_mutex_lock(&diskmtx);
866         TAILQ_FOREACH(dc, &diskconq, entry) {
867                 if (strcmp(dc->disk, disk) == 0)
868                         break;
869         }
870         pthread_mutex_unlock(&diskmtx);
871         if (dc)
872                 return;
873
874         /*
875          * Not already connected, create a connection to the kernel
876          * disk driver.
877          */
878         asprintf(&path, "/dev/%s", disk);
879         fd = open(path, O_RDONLY);
880         if (fd < 0) {
881                 fprintf(stderr, "reconnect %s: no access to disk\n", disk);
882                 free(path);
883                 return;
884         }
885         free(path);
886         if (pipe(pipefds) < 0) {
887                 fprintf(stderr, "reconnect %s: pipe() failed\n", disk);
888                 close(fd);
889                 return;
890         }
891         bzero(&recls, sizeof(recls));
892         recls.fd = pipefds[0];
893         if (ioctl(fd, DIOCRECLUSTER, &recls) < 0) {
894                 fprintf(stderr, "reconnect %s: ioctl failed\n", disk);
895                 close(pipefds[0]);
896                 close(pipefds[1]);
897                 close(fd);
898                 return;
899         }
900         close(pipefds[0]);
901         close(fd);
902
903         dc = malloc(sizeof(*dc));
904         dc->disk = strdup(disk);
905         pthread_mutex_lock(&diskmtx);
906         TAILQ_INSERT_TAIL(&diskconq, dc, entry);
907         pthread_mutex_unlock(&diskmtx);
908
909         info = malloc(sizeof(*info));
910         bzero(info, sizeof(*info));
911         info->fd = pipefds[1];
912         info->detachme = 1;
913         info->usrmsg_callback = hammer2_usrmsg_handler;
914         info->exit_callback = disk_disconnect;
915         info->handle = dc;
916         info->label = strdup(dc->disk);
917         pthread_create(&thread, NULL, dmsg_master_service, info);
918 }
919
920 static
921 void
922 disk_disconnect(void *handle)
923 {
924         struct diskcon *dc = handle;
925
926         fprintf(stderr, "DISK_DISCONNECT %s\n", dc->disk);
927
928         pthread_mutex_lock(&diskmtx);
929         TAILQ_REMOVE(&diskconq, dc, entry);
930         pthread_mutex_unlock(&diskmtx);
931         free(dc->disk);
932         free(dc);
933 }
934
935 /*
936  * Connect our cluster controller to /dev/xdisk.  xdisk will pick up
937  * SPAN messages that we route to it, makes remote block devices
938  * available to the host, and can issue dmsg transactions based on
939  * device requests.
940  */
941 static
942 void
943 xdisk_connect(void)
944 {
945         dmsg_master_service_info_t *info;
946         struct xdisk_attach_ioctl xaioc;
947         pthread_t thread;
948         int pipefds[2];
949         int xfd;
950         int error;
951
952         /*
953          * Is /dev/xdisk available?
954          */
955         xfd = open("/dev/xdisk", O_RDWR, 0600);
956         if (xfd < 0) {
957                 fprintf(stderr, "xdisk_connect: Unable to open /dev/xdisk\n");
958                 return;
959         }
960
961         if (pipe(pipefds) < 0) {
962                 fprintf(stderr, "xdisk_connect: pipe() failed\n");
963                 return;
964         }
965
966         /*
967          * Pipe between cluster controller (this user process).
968          */
969         info = malloc(sizeof(*info));
970         bzero(info, sizeof(*info));
971         info->fd = pipefds[1];
972         info->detachme = 1;
973         info->usrmsg_callback = hammer2_usrmsg_handler;
974         info->exit_callback = NULL;
975         pthread_create(&thread, NULL, dmsg_master_service, info);
976
977         /*
978          * And the xdisk device.
979          */
980         bzero(&xaioc, sizeof(xaioc));
981         xaioc.fd = pipefds[0];
982         error = ioctl(xfd, XDISKIOCATTACH, &xaioc);
983         close(pipefds[0]);
984         close(xfd);
985
986         if (error < 0) {
987                 fprintf(stderr,
988                         "xdisk_connect: cannot attach %s\n",
989                         strerror(errno));
990                 return;
991         }
992 }