hammer(8): Update man page & sync usage()
[dragonfly.git] / sbin / hammer / cmd_pseudofs.c
blobb8d13e36bf52a3e5b03c17f4787c8fc0b8e112c3
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:
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.
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.
34 * $DragonFly: src/sbin/hammer/cmd_pseudofs.c,v 1.12 2008/10/08 21:01:54 thomas Exp $
37 #include "hammer.h"
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);
42 static void pseudofs_usage(int code);
43 static int getyn(void);
44 static int timetosecs(char *str);
47 * Calculate the pfs_id given a path to a directory or a @@PFS or @@%llx:%d
48 * softlink.
50 int
51 getpfs(struct hammer_ioc_pseudofs_rw *pfs, const char *path)
53 uintmax_t dummy_tid;
54 struct stat st;
55 char *dirpath;
56 char buf[64];
57 int fd;
58 int n;
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);
66 * Calculate the directory containing the softlink
68 dirpath = strdup(path);
69 if (strrchr(dirpath, '/')) {
70 *strrchr(dirpath, '/') = 0;
71 if (strlen(dirpath) == 0) {
72 free(dirpath);
73 dirpath = strdup("/");
75 } else {
76 free(dirpath);
77 dirpath = strdup(".");
80 if (lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) {
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.
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);
100 close(fd);
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.
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;
116 if (sscanf(buf, "@@%jx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
117 fd = open(dirpath, O_RDONLY);
118 goto done;
123 * Try to open the path and request the pfs_id that way.
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;
136 * Cleanup
138 done:
139 if (fd < 0) {
140 fprintf(stderr, "Cannot access PFS %s: %s\n",
141 path, strerror(errno));
142 exit(1);
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);
149 free(dirpath);
150 return(fd);
153 void
154 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs)
156 close(fd);
157 if (pfs->ondisk) {
158 free(pfs->ondisk);
159 pfs->ondisk = NULL;
163 void
164 hammer_cmd_pseudofs_status(char **av, int ac)
166 struct hammer_ioc_pseudofs_rw pfs;
167 int i;
168 int fd;
170 if (ac == 0)
171 pseudofs_usage(1);
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);
181 printf("}\n");
183 if (fd >= 0)
184 close(fd);
185 if (pfs.ondisk)
186 free(pfs.ondisk);
190 void
191 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
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;
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);
212 * Figure out the directory prefix, taking care of degenerate
213 * cases.
215 dirpath = strdup(path);
216 if (strrchr(dirpath, '/') != NULL) {
217 *strrchr(dirpath, '/') = 0;
218 if (dirpath[0] == 0) {
219 free(dirpath);
220 dirpath = strdup("/");
222 } else {
223 free(dirpath);
224 dirpath = strdup(".");
226 fd = open(dirpath, O_RDONLY);
227 if (fd < 0) {
228 fprintf(stderr, "Cannot open directory %s\n", dirpath);
229 exit(1);
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;
245 if (pfs_id == HAMMER_MAX_PFS) {
246 fprintf(stderr, "Cannot create %s, all PFSs in use\n", path);
247 exit(1);
249 if (error != ENOENT) {
250 fprintf(stderr, "Cannot create %s, got %s during scan\n",
251 path, strerror(error));
252 exit(1);
256 * Create the new PFS
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;
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);
279 close(fd);
282 void
283 hammer_cmd_pseudofs_destroy(char **av, int ac)
285 struct hammer_ioc_pseudofs_rw pfs;
286 struct stat st;
287 int fd;
288 int i;
290 if (ac == 0)
291 pseudofs_usage(1);
292 bzero(&pfs, sizeof(pfs));
293 fd = getpfs(&pfs, av[0]);
295 if (pfs.pfs_id == 0) {
296 fprintf(stderr, "You cannot destroy PFS#0\n");
297 exit(1);
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);
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);
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);
326 printf(".. starting destruction pass\n");
327 fflush(stdout);
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.
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;
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);
345 * Ok, do it. Remove the softlink on success.
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 */
357 } else {
358 printf("pfs-destroy of PFS#%d failed: %s\n",
359 pfs.pfs_id, strerror(errno));
363 void
364 hammer_cmd_pseudofs_upgrade(char **av, int ac)
366 struct hammer_ioc_pseudofs_rw pfs;
367 int fd;
369 if (ac == 0)
370 pseudofs_usage(1);
371 bzero(&pfs, sizeof(pfs));
372 fd = getpfs(&pfs, av[0]);
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);
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));
388 void
389 hammer_cmd_pseudofs_downgrade(char **av, int ac)
391 struct hammer_ioc_pseudofs_rw pfs;
392 int fd;
394 if (ac == 0)
395 pseudofs_usage(1);
396 bzero(&pfs, sizeof(pfs));
397 fd = getpfs(&pfs, av[0]);
399 if (pfs.pfs_id == 0) {
400 fprintf(stderr, "You cannot downgrade PFS#0\n");
401 exit(1);
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-upgrade of PFS#%d (%s) failed: %s\n",
409 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
413 void
414 hammer_cmd_pseudofs_update(char **av, int ac)
416 struct hammer_ioc_pseudofs_rw pfs;
417 int fd;
419 if (ac == 0)
420 pseudofs_usage(1);
421 bzero(&pfs, sizeof(pfs));
422 fd = getpfs(&pfs, av[0]);
424 printf("%s\n", av[0]);
425 fflush(stdout);
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);
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);
440 } else {
441 printf("Unable to retrieve pfs configuration "
442 "after successful update: %s\n",
443 strerror(errno));
444 exit(1);
446 } else {
447 printf("Unable to adjust pfs configuration: %s\n",
448 strerror(errno));
449 exit(1);
454 static void
455 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
457 uint32_t status;
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;
469 static
470 void
471 dump_pfsd(hammer_pseudofs_data_t pfsd)
473 u_int32_t status;
474 char *str = NULL;
476 printf(" sync-beg-tid=0x%016jx\n", (uintmax_t)pfsd->sync_beg_tid);
477 printf(" sync-end-tid=0x%016jx\n", (uintmax_t)pfsd->sync_end_tid);
478 uuid_to_string(&pfsd->shared_uuid, &str, &status);
479 printf(" shared-uuid=%s\n", str);
480 uuid_to_string(&pfsd->unique_uuid, &str, &status);
481 printf(" unique-uuid=%s\n", str);
482 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
483 printf(" slave\n");
485 printf(" label=\"%s\"\n", pfsd->label);
486 if (pfsd->snapshots[0])
487 printf(" snapshots=\"%s\"\n", pfsd->snapshots);
488 if (pfsd->prune_min < (60 * 60 * 24)) {
489 printf(" prune-min=%02d:%02d:%02d\n",
490 pfsd->prune_min / 60 / 60 % 24,
491 pfsd->prune_min / 60 % 60,
492 pfsd->prune_min % 60);
493 } else if (pfsd->prune_min % (60 * 60 * 24)) {
494 printf(" prune-min=%dd/%02d:%02d:%02d\n",
495 pfsd->prune_min / 60 / 60 / 24,
496 pfsd->prune_min / 60 / 60 % 24,
497 pfsd->prune_min / 60 % 60,
498 pfsd->prune_min % 60);
499 } else {
500 printf(" prune-min=%dd\n", pfsd->prune_min / 60 / 60 / 24);
503 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
504 printf(" operating as a SLAVE\n");
505 if (pfsd->snapshots[0] == 0)
506 printf(" snapshots directory not set for slave\n");
507 } else {
508 printf(" operating as a MASTER\n");
509 if (pfsd->snapshots[0] == 0) {
510 printf(" snapshots dir for master "
511 "defaults to <fs>/snapshots\n");
516 static void
517 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
519 char *cmd;
520 char *ptr;
521 int len;
522 uint32_t status;
524 while (ac) {
525 cmd = *av;
526 if ((ptr = strchr(cmd, '=')) != NULL)
527 *ptr++ = 0;
530 * Basic assignment value test
532 if (ptr == NULL) {
533 fprintf(stderr,
534 "option %s requires an assignment\n",
535 cmd);
536 exit(1);
539 status = uuid_s_ok;
540 if (strcmp(cmd, "sync-beg-tid") == 0) {
541 pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
542 } else if (strcmp(cmd, "sync-end-tid") == 0) {
543 pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
544 } else if (strcmp(cmd, "shared-uuid") == 0) {
545 uuid_from_string(ptr, &pfsd->shared_uuid, &status);
546 } else if (strcmp(cmd, "unique-uuid") == 0) {
547 uuid_from_string(ptr, &pfsd->unique_uuid, &status);
548 } else if (strcmp(cmd, "label") == 0) {
549 len = strlen(ptr);
550 if (ptr[0] == '"' && ptr[len-1] == '"') {
551 ptr[len-1] = 0;
552 ++ptr;
553 } else if (ptr[0] == '"') {
554 fprintf(stderr,
555 "option %s: malformed string\n",
556 cmd);
557 exit(1);
559 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
560 } else if (strcmp(cmd, "snapshots") == 0) {
561 len = strlen(ptr);
562 if (ptr[0] != '/') {
563 fprintf(stderr,
564 "option %s: '%s' must be an "
565 "absolute path\n", cmd, ptr);
566 if (ptr[0] == 0) {
567 fprintf(stderr,
568 "use 'snapshots-clear' "
569 "to unset snapshots dir\n");
571 exit(1);
573 if (len >= (int)sizeof(pfsd->snapshots)) {
574 fprintf(stderr,
575 "option %s: path too long, %d "
576 "character limit\n", cmd, len);
577 exit(1);
579 snprintf(pfsd->snapshots, sizeof(pfsd->snapshots),
580 "%s", ptr);
581 } else if (strcmp(cmd, "snapshots-clear") == 0) {
582 pfsd->snapshots[0] = 0;
583 } else if (strcmp(cmd, "prune-min") == 0) {
584 pfsd->prune_min = timetosecs(ptr);
585 if (pfsd->prune_min < 0) {
586 fprintf(stderr,
587 "option %s: illegal time spec, "
588 "use Nd or [Nd/]hh[:mm[:ss]]\n", ptr);
589 exit(1);
591 } else {
592 fprintf(stderr, "invalid option: %s\n", cmd);
593 exit(1);
595 if (status != uuid_s_ok) {
596 fprintf(stderr, "option %s: error parsing uuid %s\n",
597 cmd, ptr);
598 exit(1);
600 --ac;
601 ++av;
605 static
606 void
607 pseudofs_usage(int code)
609 fprintf(stderr,
610 "hammer pfs-status <dirpath> ...\n"
611 "hammer pfs-master <dirpath> [options]\n"
612 "hammer pfs-slave <dirpath> [options]\n"
613 "hammer pfs-update <dirpath> [options]\n"
614 "hammer pfs-upgrade <dirpath>\n"
615 "hammer pfs-downgrade <dirpath>\n"
616 "hammer pfs-destroy <dirpath>\n"
617 "\n"
618 "options:\n"
619 " sync-beg-tid=0x16llx\n"
620 " sync-end-tid=0x16llx\n"
621 " shared-uuid=0x16llx\n"
622 " unique-uuid=0x16llx\n"
623 " label=\"string\"\n"
624 " snapshots=\"/path\"\n"
625 " snapshots-clear\n"
626 " prune-min=Nd\n"
627 " prune-min=[Nd/]hh[:mm[:ss]]\n"
629 exit(code);
632 static
634 getyn(void)
636 char buf[256];
637 int len;
639 if (fgets(buf, sizeof(buf), stdin) == NULL)
640 return(0);
641 len = strlen(buf);
642 while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
643 --len;
644 buf[len] = 0;
645 if (strcmp(buf, "y") == 0 ||
646 strcmp(buf, "yes") == 0 ||
647 strcmp(buf, "Y") == 0 ||
648 strcmp(buf, "YES") == 0) {
649 return(1);
651 return(0);
655 * Convert time in the form [Nd/][hh[:mm[:ss]]] to seconds.
657 * Return -1 if a parse error occurs.
658 * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
660 static
662 timetosecs(char *str)
664 int days = 0;
665 int hrs = 0;
666 int mins = 0;
667 int secs = 0;
668 int n;
669 long long v;
670 char *ptr;
672 n = strtol(str, &ptr, 10);
673 if (n < 0)
674 return(-1);
675 if (*ptr == 'd') {
676 days = n;
677 ++ptr;
678 if (*ptr == '/')
679 n = strtol(ptr + 1, &ptr, 10);
680 else
681 n = 0;
683 if (n < 0)
684 return(-1);
685 hrs = n;
686 if (*ptr == ':') {
687 n = strtol(ptr + 1, &ptr, 10);
688 if (n < 0)
689 return(-1);
690 mins = n;
691 if (*ptr == ':') {
692 n = strtol(ptr + 1, &ptr, 10);
693 if (n < 0)
694 return(-1);
695 secs = n;
698 if (*ptr)
699 return(-1);
700 v = days * 24 * 60 * 60 + hrs * 60 * 60 + mins * 60 + secs;
701 if (v > 0x7FFFFFFF)
702 v = 0x7FFFFFFF;
703 return((int)v);