sbin/hammer - Remove trailing '/' from paths.
[dragonfly.git] / sbin / hammer / cmd_pseudofs.c
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sbin/hammer/cmd_pseudofs.c,v 1.12 2008/10/08 21:01:54 thomas Exp $
35  */
36
37 #include "hammer.h"
38
39 static void parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd);
40 static void init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave);
41 static void dump_pfsd(hammer_pseudofs_data_t pfsd, int fd);
42 static void pseudofs_usage(int code);
43 static int getyn(void);
44 static int timetosecs(char *str);
45
46 /*
47  * Calculate the pfs_id given a path to a directory or a @@PFS or @@%llx:%d
48  * softlink.
49  */
50 int
51 getpfs(struct hammer_ioc_pseudofs_rw *pfs, char *path)
52 {
53         uintmax_t dummy_tid;
54         struct stat st;
55         char *dirpath;
56         char buf[64];
57         size_t len;
58         int fd;
59         int n;
60
61         bzero(pfs, sizeof(*pfs));
62         pfs->ondisk = malloc(sizeof(*pfs->ondisk));
63         bzero(pfs->ondisk, sizeof(*pfs->ondisk));
64         pfs->bytes = sizeof(*pfs->ondisk);
65
66         /*
67          * Remove the trailing '/' if there is one so that
68          * in the case path is a symbolic link to the PFS,
69          * the symbolic link is used and not the root dir of
70          * the PFS
71          */
72         len = strnlen(path, MAXPATHLEN);
73         if (path[len-1] == '/' && path[len] == '\0')
74                 path[len-1] = '\0';
75
76         /*
77          * Calculate the directory containing the softlink
78          */
79         dirpath = strdup(path);
80         if (strrchr(dirpath, '/')) {
81                 *strrchr(dirpath, '/') = 0;
82                 if (strlen(dirpath) == 0) {
83                         free(dirpath);
84                         dirpath = strdup("/");
85                 }
86         } else {
87                 free(dirpath);
88                 dirpath = strdup(".");
89         }
90
91         if (lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) {
92                 /*
93                  * Avoid foot-shooting.  Don't let the user access a PFS
94                  * softlink via a PFS.  PFS softlinks may only be accessed
95                  * via the master filesystem.
96                  */
97                 fd = open(dirpath, O_RDONLY);
98                 if (fd < 0)
99                         goto done;
100                 pfs->pfs_id = -1;
101                 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
102                 if (pfs->pfs_id != 0) {
103                         fprintf(stderr,
104                                 "You are attempting to access a PFS softlink "
105                                 "from a PFS.  It may not represent the PFS\n"
106                                 "on the main filesystem mount that you "
107                                 "expect!  You may only access PFS softlinks\n"
108                                 "via the main filesystem mount!\n");
109                         exit(1);
110                 }
111                 close(fd);
112
113                 /*
114                  * Extract the PFS from the link.  HAMMER will automatically
115                  * convert @@PFS%05d links so if actually see one in that
116                  * form the target PFS may not exist or may be corrupt.  But
117                  * we can extract the PFS id anyway.
118                  */
119                 n = readlink(path, buf, sizeof(buf) - 1);
120                 if (n < 0)
121                         n = 0;
122                 buf[n] = 0;
123                 if (sscanf(buf, "@@PFS%d", &pfs->pfs_id) == 1) {
124                         fd = open(dirpath, O_RDONLY);
125                         goto done;
126                 }
127                 if (sscanf(buf, "@@%jx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
128                         fd = open(dirpath, O_RDONLY);
129                         goto done;
130                 }
131         }
132
133         /*
134          * Try to open the path and request the pfs_id that way.
135          */
136         fd = open(path, O_RDONLY);
137         if (fd >= 0) {
138                 pfs->pfs_id = -1;
139                 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
140                 if (pfs->pfs_id == -1) {
141                         close(fd);
142                         fd = -1;
143                 }
144         }
145
146         /*
147          * Cleanup
148          */
149 done:
150         if (fd < 0) {
151                 fprintf(stderr, "Cannot access PFS %s: %s\n",
152                         path, strerror(errno));
153                 exit(1);
154         }
155         if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
156                 fprintf(stderr, "Cannot access PFS %s: %s\n",
157                         path, strerror(errno));
158                 exit(1);
159         }
160         free(dirpath);
161         return(fd);
162 }
163
164 void
165 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs)
166 {
167         close(fd);
168         if (pfs->ondisk) {
169                 free(pfs->ondisk);
170                 pfs->ondisk = NULL;
171         }
172 }
173
174 void
175 hammer_cmd_pseudofs_status(char **av, int ac)
176 {
177         struct hammer_ioc_pseudofs_rw pfs;
178         int i;
179         int fd;
180
181         if (ac == 0)
182                 pseudofs_usage(1);
183
184         for (i = 0; i < ac; ++i) {
185                 printf("%s\t", av[i]);
186                 fd = getpfs(&pfs, av[i]);
187                 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
188                         printf("Not a HAMMER root\n");
189                 } else {
190                         printf("PFS #%d {\n", pfs.pfs_id);
191                         dump_pfsd(pfs.ondisk, fd);
192                         printf("}\n");
193                 }
194                 if (fd >= 0)
195                         close(fd);
196                 if (pfs.ondisk)
197                         free(pfs.ondisk);
198         }
199 }
200
201 void
202 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
203 {
204         struct hammer_ioc_pseudofs_rw pfs;
205         struct hammer_pseudofs_data pfsd;
206         struct stat st;
207         const char *path;
208         char *dirpath;
209         char *linkpath;
210         int pfs_id;
211         int fd;
212         int error;
213
214         if (ac == 0)
215                 pseudofs_usage(1);
216         path = av[0];
217         if (lstat(path, &st) == 0) {
218                 fprintf(stderr, "Cannot create %s, file exists!\n", path);
219                 exit(1);
220         }
221
222         /*
223          * Figure out the directory prefix, taking care of degenerate
224          * cases.
225          */
226         dirpath = strdup(path);
227         if (strrchr(dirpath, '/') != NULL) {
228                 *strrchr(dirpath, '/') = 0;
229                 if (dirpath[0] == 0) {
230                         free(dirpath);
231                         dirpath = strdup("/");
232                 }
233         } else {
234                 free(dirpath);
235                 dirpath = strdup(".");
236         }
237         fd = open(dirpath, O_RDONLY);
238         if (fd < 0) {
239                 fprintf(stderr, "Cannot open directory %s\n", dirpath);
240                 exit(1);
241         }
242
243         error = 0;
244         for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
245                 bzero(&pfs, sizeof(pfs));
246                 bzero(&pfsd, sizeof(pfsd));
247                 pfs.pfs_id = pfs_id;
248                 pfs.ondisk = &pfsd;
249                 pfs.bytes = sizeof(pfsd);
250                 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
251                 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
252                         error = errno;
253                         break;
254                 }
255         }
256         if (pfs_id == HAMMER_MAX_PFS) {
257                 fprintf(stderr, "Cannot create %s, all PFSs in use\n", path);
258                 exit(1);
259         }
260         if (error != ENOENT) {
261                 fprintf(stderr, "Cannot create %s, got %s during scan\n",
262                         path, strerror(error));
263                 exit(1);
264         }
265
266         /*
267          * Create the new PFS
268          */
269         printf("Creating PFS #%d\t", pfs_id);
270         bzero(&pfsd, sizeof(pfsd));
271         init_pfsd(&pfsd, is_slave);
272         pfs.pfs_id = pfs_id;
273         pfs.ondisk = &pfsd;
274         pfs.bytes = sizeof(pfsd);
275         pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
276
277         if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
278                 printf("failed: %s\n", strerror(errno));
279         } else {
280                 /* special symlink, must be exactly 10 characters */
281                 asprintf(&linkpath, "@@PFS%05d", pfs_id);
282                 if (symlink(linkpath, path) < 0) {
283                         printf("failed: cannot create symlink: %s\n",
284                                 strerror(errno));
285                 } else {
286                         printf("succeeded!\n");
287                         hammer_cmd_pseudofs_update(av, ac);
288                 }
289         }
290         close(fd);
291 }
292
293 void
294 hammer_cmd_pseudofs_destroy(char **av, int ac)
295 {
296         struct hammer_ioc_pseudofs_rw pfs;
297         struct stat st;
298         int fd;
299         int i;
300
301         if (ac == 0)
302                 pseudofs_usage(1);
303         bzero(&pfs, sizeof(pfs));
304         fd = getpfs(&pfs, av[0]);
305
306         if (pfs.pfs_id == 0) {
307                 fprintf(stderr, "You cannot destroy PFS#0\n");
308                 exit(1);
309         }
310         printf("You have requested that PFS#%d (%s) be destroyed\n",
311                 pfs.pfs_id, pfs.ondisk->label);
312         printf("This will irrevocably destroy all data on this PFS!!!!!\n");
313         printf("Do you really want to do this? ");
314         fflush(stdout);
315         if (getyn() == 0) {
316                 fprintf(stderr, "No action taken on PFS#%d\n", pfs.pfs_id);
317                 exit(1);
318         }
319
320         if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
321                 printf("This PFS is currently setup as a MASTER!\n");
322                 printf("Are you absolutely sure you want to destroy it? ");
323                 fflush(stdout);
324                 if (getyn() == 0) {
325                         fprintf(stderr, "No action taken on PFS#%d\n",
326                                 pfs.pfs_id);
327                         exit(1);
328                 }
329         }
330
331         printf("Destroying PFS #%d (%s) in ", pfs.pfs_id, pfs.ondisk->label);
332         for (i = 5; i; --i) {
333                 printf(" %d", i);
334                 fflush(stdout);
335                 sleep(1);
336         }
337         printf(".. starting destruction pass\n");
338         fflush(stdout);
339
340         /*
341          * Set the sync_beg_tid and sync_end_tid's to 1, once we start the
342          * RMR the PFS is basically destroyed even if someone ^C's it.
343          */
344         pfs.ondisk->mirror_flags |= HAMMER_PFSD_SLAVE;
345         pfs.ondisk->reserved01 = -1;
346         pfs.ondisk->sync_beg_tid = 1;
347         pfs.ondisk->sync_end_tid = 1;
348
349         if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
350                 fprintf(stderr, "Unable to update the PFS configuration: %s\n",
351                         strerror(errno));
352                 exit(1);
353         }
354
355         /*
356          * Ok, do it.  Remove the softlink on success.
357          */
358         if (ioctl(fd, HAMMERIOC_RMR_PSEUDOFS, &pfs) == 0) {
359                 printf("pfs-destroy of PFS#%d succeeded!\n", pfs.pfs_id);
360                 if (lstat(av[0], &st) == 0 && S_ISLNK(st.st_mode)) {
361                         if (remove(av[0]) < 0) {
362                                 fprintf(stderr, "Unable to remove softlink: %s "
363                                         "(but the PFS has been destroyed)\n",
364                                         av[0]);
365                                 /* exit status 0 anyway */
366                         }
367                 }
368         } else {
369                 printf("pfs-destroy of PFS#%d failed: %s\n",
370                         pfs.pfs_id, strerror(errno));
371         }
372 }
373
374 void
375 hammer_cmd_pseudofs_upgrade(char **av, int ac)
376 {
377         struct hammer_ioc_pseudofs_rw pfs;
378         int fd;
379
380         if (ac == 0)
381                 pseudofs_usage(1);
382         bzero(&pfs, sizeof(pfs));
383         fd = getpfs(&pfs, av[0]);
384
385         if (pfs.pfs_id == 0) {
386                 fprintf(stderr, "You cannot upgrade PFS#0"
387                                 " (It should already be a master)\n");
388                 exit(1);
389         }
390         if (ioctl(fd, HAMMERIOC_UPG_PSEUDOFS, &pfs) == 0) {
391                 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
392                         pfs.pfs_id, pfs.ondisk->label);
393         } else {
394                 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
395                         pfs.pfs_id, pfs.ondisk->label, strerror(errno));
396         }
397 }
398
399 void
400 hammer_cmd_pseudofs_downgrade(char **av, int ac)
401 {
402         struct hammer_ioc_pseudofs_rw pfs;
403         int fd;
404
405         if (ac == 0)
406                 pseudofs_usage(1);
407         bzero(&pfs, sizeof(pfs));
408         fd = getpfs(&pfs, av[0]);
409
410         if (pfs.pfs_id == 0) {
411                 fprintf(stderr, "You cannot downgrade PFS#0\n");
412                 exit(1);
413         }
414
415         if (ioctl(fd, HAMMERIOC_DGD_PSEUDOFS, &pfs) == 0) {
416                 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
417                         pfs.pfs_id, pfs.ondisk->label);
418         } else {
419                 fprintf(stderr, "pfs-downgrade of PFS#%d (%s) failed: %s\n",
420                         pfs.pfs_id, pfs.ondisk->label, strerror(errno));
421         }
422 }
423
424 void
425 hammer_cmd_pseudofs_update(char **av, int ac)
426 {
427         struct hammer_ioc_pseudofs_rw pfs;
428         int fd;
429
430         if (ac == 0)
431                 pseudofs_usage(1);
432         bzero(&pfs, sizeof(pfs));
433         fd = getpfs(&pfs, av[0]);
434
435         printf("%s\n", av[0]);
436         fflush(stdout);
437
438         if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
439                 parse_pfsd_options(av + 1, ac - 1, pfs.ondisk);
440                 if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) &&
441                     pfs.pfs_id == 0) {
442                         printf("The real mount point cannot be made a PFS "
443                                "slave, only PFS sub-directories can be made "
444                                "slaves\n");
445                         exit(1);
446                 }
447                 pfs.bytes = sizeof(*pfs.ondisk);
448                 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
449                         if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
450                                 dump_pfsd(pfs.ondisk, fd);
451                         } else {
452                                 printf("Unable to retrieve pfs configuration "
453                                         "after successful update: %s\n",
454                                         strerror(errno));
455                                 exit(1);
456                         }
457                 } else {
458                         printf("Unable to adjust pfs configuration: %s\n",
459                                 strerror(errno));
460                         exit(1);
461                 }
462         }
463 }
464
465 static void
466 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
467 {
468         uint32_t status;
469
470         pfsd->sync_beg_tid = 1;
471         pfsd->sync_end_tid = 1;
472         pfsd->sync_beg_ts = 0;
473         pfsd->sync_end_ts = 0;
474         uuid_create(&pfsd->shared_uuid, &status);
475         uuid_create(&pfsd->unique_uuid, &status);
476         if (is_slave)
477                 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
478 }
479
480 static
481 void
482 dump_pfsd(hammer_pseudofs_data_t pfsd, int fd)
483 {
484         struct hammer_ioc_version       version;
485         u_int32_t status;
486         char *str = NULL;
487
488         printf("    sync-beg-tid=0x%016jx\n", (uintmax_t)pfsd->sync_beg_tid);
489         printf("    sync-end-tid=0x%016jx\n", (uintmax_t)pfsd->sync_end_tid);
490         uuid_to_string(&pfsd->shared_uuid, &str, &status);
491         printf("    shared-uuid=%s\n", str);
492         uuid_to_string(&pfsd->unique_uuid, &str, &status);
493         printf("    unique-uuid=%s\n", str);
494         if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
495                 printf("    slave\n");
496         }
497         printf("    label=\"%s\"\n", pfsd->label);
498         if (pfsd->snapshots[0])
499                 printf("    snapshots=\"%s\"\n", pfsd->snapshots);
500         if (pfsd->prune_min < (60 * 60 * 24)) {
501                 printf("    prune-min=%02d:%02d:%02d\n",
502                         pfsd->prune_min / 60 / 60 % 24,
503                         pfsd->prune_min / 60 % 60,
504                         pfsd->prune_min % 60);
505         } else if (pfsd->prune_min % (60 * 60 * 24)) {
506                 printf("    prune-min=%dd/%02d:%02d:%02d\n",
507                         pfsd->prune_min / 60 / 60 / 24,
508                         pfsd->prune_min / 60 / 60 % 24,
509                         pfsd->prune_min / 60 % 60,
510                         pfsd->prune_min % 60);
511         } else {
512                 printf("    prune-min=%dd\n", pfsd->prune_min / 60 / 60 / 24);
513         }
514
515         if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
516                 printf("    operating as a SLAVE\n");
517         } else {
518                 printf("    operating as a MASTER\n");
519         }
520
521         if (pfsd->snapshots[0] == 0) {
522                 bzero(&version, sizeof(version));
523                 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
524                         return;
525                 if (version.cur_version < 3) {
526                         if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
527                                 printf("    snapshots directory not set for "
528                                        "slave\n");
529                         } else {
530                                 printf("    snapshots directory for master "
531                                        "defaults to <pfs>/snapshots\n");
532                         }
533                 } else {
534                         printf("    snapshots directory defaults to "
535                                "/var/hammer/<pfs>\n");
536                 }
537         }
538 }
539
540 static void
541 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
542 {
543         char *cmd;
544         char *ptr;
545         int len;
546         uint32_t status;
547
548         while (ac) {
549                 cmd = *av;
550                 if ((ptr = strchr(cmd, '=')) != NULL)
551                         *ptr++ = 0;
552
553                 /*
554                  * Basic assignment value test
555                  */
556                 if (ptr == NULL) {
557                         fprintf(stderr,
558                                 "option %s requires an assignment\n",
559                                 cmd);
560                         exit(1);
561                 }
562
563                 status = uuid_s_ok;
564                 if (strcmp(cmd, "sync-beg-tid") == 0) {
565                         pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
566                 } else if (strcmp(cmd, "sync-end-tid") == 0) {
567                         pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
568                 } else if (strcmp(cmd, "shared-uuid") == 0) {
569                         uuid_from_string(ptr, &pfsd->shared_uuid, &status);
570                 } else if (strcmp(cmd, "unique-uuid") == 0) {
571                         uuid_from_string(ptr, &pfsd->unique_uuid, &status);
572                 } else if (strcmp(cmd, "label") == 0) {
573                         len = strlen(ptr);
574                         if (ptr[0] == '"' && ptr[len-1] == '"') {
575                                 ptr[len-1] = 0;
576                                 ++ptr;
577                         } else if (ptr[0] == '"') {
578                                 fprintf(stderr,
579                                         "option %s: malformed string\n",
580                                         cmd);
581                                 exit(1);
582                         }
583                         snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
584                 } else if (strcmp(cmd, "snapshots") == 0) {
585                         len = strlen(ptr);
586                         if (ptr[0] != '/') {
587                                 fprintf(stderr,
588                                         "option %s: '%s' must be an "
589                                         "absolute path\n", cmd, ptr);
590                                 if (ptr[0] == 0) {
591                                         fprintf(stderr,
592                                                 "use 'snapshots-clear' "
593                                                 "to unset snapshots dir\n");
594                                 }
595                                 exit(1);
596                         }
597                         if (len >= (int)sizeof(pfsd->snapshots)) {
598                                 fprintf(stderr,
599                                         "option %s: path too long, %d "
600                                         "character limit\n", cmd, len);
601                                 exit(1);
602                         }
603                         snprintf(pfsd->snapshots, sizeof(pfsd->snapshots),
604                                  "%s", ptr);
605                 } else if (strcmp(cmd, "snapshots-clear") == 0) {
606                         pfsd->snapshots[0] = 0;
607                 } else if (strcmp(cmd, "prune-min") == 0) {
608                         pfsd->prune_min = timetosecs(ptr);
609                         if (pfsd->prune_min < 0) {
610                                 fprintf(stderr,
611                                         "option %s: illegal time spec, "
612                                         "use Nd or [Nd/]hh[:mm[:ss]]\n", ptr);
613                                 exit(1);
614                         }
615                 } else {
616                         fprintf(stderr, "invalid option: %s\n", cmd);
617                         exit(1);
618                 }
619                 if (status != uuid_s_ok) {
620                         fprintf(stderr, "option %s: error parsing uuid %s\n",
621                                 cmd, ptr);
622                         exit(1);
623                 }
624                 --ac;
625                 ++av;
626         }
627 }
628
629 static
630 void
631 pseudofs_usage(int code)
632 {
633         fprintf(stderr,
634                 "hammer pfs-status <dirpath> ...\n"
635                 "hammer pfs-master <dirpath> [options]\n"
636                 "hammer pfs-slave <dirpath> [options]\n"
637                 "hammer pfs-update <dirpath> [options]\n"
638                 "hammer pfs-upgrade <dirpath>\n"
639                 "hammer pfs-downgrade <dirpath>\n"
640                 "hammer pfs-destroy <dirpath>\n"
641                 "\n"
642                 "options:\n"
643                 "    sync-beg-tid=0x16llx\n"
644                 "    sync-end-tid=0x16llx\n"
645                 "    shared-uuid=0x16llx\n"
646                 "    unique-uuid=0x16llx\n"
647                 "    label=\"string\"\n"
648                 "    snapshots=\"/path\"\n"
649                 "    snapshots-clear\n"
650                 "    prune-min=Nd\n"
651                 "    prune-min=[Nd/]hh[:mm[:ss]]\n"
652         );
653         exit(code);
654 }
655
656 static
657 int
658 getyn(void)
659 {
660         char buf[256];
661         int len;
662
663         if (fgets(buf, sizeof(buf), stdin) == NULL)
664                 return(0);
665         len = strlen(buf);
666         while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
667                 --len;
668         buf[len] = 0;
669         if (strcmp(buf, "y") == 0 ||
670             strcmp(buf, "yes") == 0 ||
671             strcmp(buf, "Y") == 0 ||
672             strcmp(buf, "YES") == 0) {
673                 return(1);
674         }
675         return(0);
676 }
677
678 /*
679  * Convert time in the form [Nd/]hh[:mm[:ss]] to seconds.
680  *
681  * Return -1 if a parse error occurs.
682  * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
683  */
684 static
685 int
686 timetosecs(char *str)
687 {
688         int days = 0;
689         int hrs = 0;
690         int mins = 0;
691         int secs = 0;
692         int n;
693         long long v;
694         char *ptr;
695
696         n = strtol(str, &ptr, 10);
697         if (n < 0)
698                 return(-1);
699         if (*ptr == 'd') {
700                 days = n;
701                 ++ptr;
702                 if (*ptr == '/')
703                     n = strtol(ptr + 1, &ptr, 10);
704                 else
705                     n = 0;
706         }
707         if (n < 0)
708                 return(-1);
709         hrs = n;
710         if (*ptr == ':') {
711                 n = strtol(ptr + 1, &ptr, 10);
712                 if (n < 0)
713                         return(-1);
714                 mins = n;
715                 if (*ptr == ':') {
716                         n = strtol(ptr + 1, &ptr, 10);
717                         if (n < 0)
718                                 return(-1);
719                         secs = n;
720                 }
721         }
722         if (*ptr)
723                 return(-1);
724         v = days * 24 * 60 * 60 + hrs *  60 * 60 + mins * 60 + secs;
725         if (v > 0x7FFFFFFF)
726                 v = 0x7FFFFFFF;
727         return((int)v);
728 }