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