2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
34 * $DragonFly: src/sbin/hammer/cmd_pseudofs.c,v 1.12 2008/10/08 21:01:54 thomas Exp $
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);
47 * Calculate the pfs_id given a path to a directory or a @@PFS or @@%llx:%d
51 getpfs(struct hammer_ioc_pseudofs_rw *pfs, char *path)
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);
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.
71 len = strnlen(path, MAXPATHLEN);
73 if (strtrl(&path, len) == NULL)
74 errx(1, "Unexpected NULL path");
78 * Calculate the directory containing the softlink
80 dirpath = strdup(path);
81 if (strrchr(dirpath, '/')) {
82 *strrchr(dirpath, '/') = 0;
83 if (strlen(dirpath) == 0) {
85 dirpath = strdup("/");
89 dirpath = strdup(".");
92 if (lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) {
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.
98 fd = open(dirpath, O_RDONLY);
102 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
103 if (pfs->pfs_id != 0) {
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");
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.
120 n = readlink(path, buf, sizeof(buf) - 1);
124 if (sscanf(buf, "@@PFS%d", &pfs->pfs_id) == 1) {
125 fd = open(dirpath, O_RDONLY);
128 if (sscanf(buf, "@@%jx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
129 fd = open(dirpath, O_RDONLY);
135 * Try to open the path and request the pfs_id that way.
137 fd = open(path, O_RDONLY);
140 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
141 if (pfs->pfs_id == -1) {
152 fprintf(stderr, "Cannot access PFS %s: %s\n",
153 path, strerror(errno));
156 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
157 fprintf(stderr, "Cannot access PFS %s: %s\n",
158 path, strerror(errno));
166 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs)
176 hammer_cmd_pseudofs_status(char **av, int ac)
178 struct hammer_ioc_pseudofs_rw pfs;
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");
191 printf("PFS #%d {\n", pfs.pfs_id);
192 dump_pfsd(pfs.ondisk, fd);
203 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
205 struct hammer_ioc_pseudofs_rw pfs;
206 struct hammer_pseudofs_data pfsd;
218 if (lstat(path, &st) == 0) {
219 fprintf(stderr, "Cannot create %s, file exists!\n", path);
224 * Figure out the directory prefix, taking care of degenerate
227 dirpath = strdup(path);
228 if (strrchr(dirpath, '/') != NULL) {
229 *strrchr(dirpath, '/') = 0;
230 if (dirpath[0] == 0) {
232 dirpath = strdup("/");
236 dirpath = strdup(".");
238 fd = open(dirpath, O_RDONLY);
240 fprintf(stderr, "Cannot open directory %s\n", dirpath);
245 for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
246 bzero(&pfs, sizeof(pfs));
247 bzero(&pfsd, sizeof(pfsd));
250 pfs.bytes = sizeof(pfsd);
251 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
252 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
257 if (pfs_id == HAMMER_MAX_PFS) {
258 fprintf(stderr, "Cannot create %s, all PFSs in use\n", path);
261 if (error != ENOENT) {
262 fprintf(stderr, "Cannot create %s, got %s during scan\n",
263 path, strerror(error));
270 printf("Creating PFS #%d\t", pfs_id);
271 bzero(&pfsd, sizeof(pfsd));
272 init_pfsd(&pfsd, is_slave);
275 pfs.bytes = sizeof(pfsd);
276 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
278 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
279 printf("failed: %s\n", strerror(errno));
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",
287 printf("succeeded!\n");
288 hammer_cmd_pseudofs_update(av, ac);
295 hammer_cmd_pseudofs_destroy(char **av, int ac)
297 struct hammer_ioc_pseudofs_rw pfs;
304 bzero(&pfs, sizeof(pfs));
305 fd = getpfs(&pfs, av[0]);
307 if (pfs.pfs_id == 0) {
308 fprintf(stderr, "You cannot destroy PFS#0\n");
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? ");
317 fprintf(stderr, "No action taken on PFS#%d\n", pfs.pfs_id);
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? ");
326 fprintf(stderr, "No action taken on PFS#%d\n",
332 printf("Destroying PFS #%d (%s) in ", pfs.pfs_id, pfs.ondisk->label);
333 for (i = 5; i; --i) {
338 printf(".. starting destruction pass\n");
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.
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;
350 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
351 fprintf(stderr, "Unable to update the PFS configuration: %s\n",
357 * Ok, do it. Remove the softlink on success.
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",
366 /* exit status 0 anyway */
370 printf("pfs-destroy of PFS#%d failed: %s\n",
371 pfs.pfs_id, strerror(errno));
376 hammer_cmd_pseudofs_upgrade(char **av, int ac)
378 struct hammer_ioc_pseudofs_rw pfs;
383 bzero(&pfs, sizeof(pfs));
384 fd = getpfs(&pfs, av[0]);
386 if (pfs.pfs_id == 0) {
387 fprintf(stderr, "You cannot upgrade PFS#0"
388 " (It should already be a master)\n");
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);
395 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
396 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
401 hammer_cmd_pseudofs_downgrade(char **av, int ac)
403 struct hammer_ioc_pseudofs_rw pfs;
408 bzero(&pfs, sizeof(pfs));
409 fd = getpfs(&pfs, av[0]);
411 if (pfs.pfs_id == 0) {
412 fprintf(stderr, "You cannot downgrade PFS#0\n");
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);
420 fprintf(stderr, "pfs-downgrade of PFS#%d (%s) failed: %s\n",
421 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
426 hammer_cmd_pseudofs_update(char **av, int ac)
428 struct hammer_ioc_pseudofs_rw pfs;
433 bzero(&pfs, sizeof(pfs));
434 fd = getpfs(&pfs, av[0]);
436 printf("%s\n", av[0]);
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) &&
443 printf("The real mount point cannot be made a PFS "
444 "slave, only PFS sub-directories can be made "
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);
453 printf("Unable to retrieve pfs configuration "
454 "after successful update: %s\n",
459 printf("Unable to adjust pfs configuration: %s\n",
467 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
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);
478 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
482 dump_pfsd(hammer_pseudofs_data_t pfsd, int fd)
484 struct hammer_ioc_version version;
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) {
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);
512 printf(" prune-min=%dd\n", pfsd->prune_min / 60 / 60 / 24);
515 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
516 printf(" operating as a SLAVE\n");
518 printf(" operating as a MASTER\n");
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
527 if (fd >= 0 && pfsd->snapshots[0] == 0) {
528 bzero(&version, sizeof(version));
529 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
531 if (version.cur_version < 3) {
532 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
533 printf(" snapshots directory not set for "
536 printf(" snapshots directory for master "
537 "defaults to <pfs>/snapshots\n");
540 printf(" snapshots directory defaults to "
541 "/var/hammer/<pfs>\n");
547 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
556 if ((ptr = strchr(cmd, '=')) != NULL)
560 * Basic assignment value test
564 "option %s requires an assignment\n",
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) {
580 if (ptr[0] == '"' && ptr[len-1] == '"') {
583 } else if (ptr[0] == '"') {
585 "option %s: malformed string\n",
589 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
590 } else if (strcmp(cmd, "snapshots") == 0) {
594 "option %s: '%s' must be an "
595 "absolute path\n", cmd, ptr);
598 "use 'snapshots-clear' "
599 "to unset snapshots dir\n");
603 if (len >= (int)sizeof(pfsd->snapshots)) {
605 "option %s: path too long, %d "
606 "character limit\n", cmd, len);
609 snprintf(pfsd->snapshots, sizeof(pfsd->snapshots),
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) {
617 "option %s: illegal time spec, "
618 "use Nd or [Nd/]hh[:mm[:ss]]\n", ptr);
622 fprintf(stderr, "invalid option: %s\n", cmd);
625 if (status != uuid_s_ok) {
626 fprintf(stderr, "option %s: error parsing uuid %s\n",
637 pseudofs_usage(int code)
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"
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"
657 " prune-min=[Nd/]hh[:mm[:ss]]\n"
669 if (fgets(buf, sizeof(buf), stdin) == NULL)
672 while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
675 if (strcmp(buf, "y") == 0 ||
676 strcmp(buf, "yes") == 0 ||
677 strcmp(buf, "Y") == 0 ||
678 strcmp(buf, "YES") == 0) {
685 * Convert time in the form [Nd/]hh[:mm[:ss]] to seconds.
687 * Return -1 if a parse error occurs.
688 * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
692 timetosecs(char *str)
702 n = strtol(str, &ptr, 10);
709 n = strtol(ptr + 1, &ptr, 10);
717 n = strtol(ptr + 1, &ptr, 10);
722 n = strtol(ptr + 1, &ptr, 10);
730 v = days * 24 * 60 * 60 + hrs * 60 * 60 + mins * 60 + secs;
738 strtrl(char **path, int len)
747 /* Attempt to remove all trailing slashes */
748 while (p-- > s && *p == '/')