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