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