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 int getyn(void);
43 static int timetosecs(char *str);
46 * Calculate the pfs_id given a path to a directory or a @@PFS or @@%llx:%d
50 getpfs(struct hammer_ioc_pseudofs_rw *pfs, char *path)
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 * Remove the trailing '/' if there is one so that
67 * in the case path is a symbolic link to the PFS,
68 * the symbolic link is used and not the root dir of
71 len = strnlen(path, MAXPATHLEN);
72 if (path[len-1] == '/' && path[len] == '\0')
76 * Calculate the directory containing the softlink
78 dirpath = strdup(path);
79 if (strrchr(dirpath, '/')) {
80 *strrchr(dirpath, '/') = 0;
81 if (strlen(dirpath) == 0) {
83 dirpath = strdup("/");
87 dirpath = strdup(".");
90 if (lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) {
92 * Avoid foot-shooting. Don't let the user access a PFS
93 * softlink via a PFS. PFS softlinks may only be accessed
94 * via the master filesystem.
96 fd = open(dirpath, O_RDONLY);
100 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
101 if (pfs->pfs_id != 0) {
103 "You are attempting to access a PFS softlink "
104 "from a PFS. It may not represent the PFS\n"
105 "on the main filesystem mount that you "
106 "expect! You may only access PFS softlinks\n"
107 "via the main filesystem mount!\n");
113 * Extract the PFS from the link. HAMMER will automatically
114 * convert @@PFS%05d links so if actually see one in that
115 * form the target PFS may not exist or may be corrupt. But
116 * we can extract the PFS id anyway.
118 n = readlink(path, buf, sizeof(buf) - 1);
122 if (sscanf(buf, "@@PFS%d", &pfs->pfs_id) == 1) {
123 fd = open(dirpath, O_RDONLY);
126 if (sscanf(buf, "@@%jx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
127 fd = open(dirpath, O_RDONLY);
133 * Try to open the path and request the pfs_id that way.
135 fd = open(path, O_RDONLY);
138 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
139 if (pfs->pfs_id == -1) {
150 fprintf(stderr, "Cannot access PFS %s: %s\n",
151 path, strerror(errno));
154 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
155 fprintf(stderr, "Cannot access PFS %s: %s\n",
156 path, strerror(errno));
164 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs)
174 hammer_cmd_pseudofs_status(char **av, int ac)
176 struct hammer_ioc_pseudofs_rw pfs;
183 for (i = 0; i < ac; ++i) {
184 printf("%s\t", av[i]);
185 fd = getpfs(&pfs, av[i]);
186 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
187 printf("Not a HAMMER root\n");
189 printf("PFS #%d {\n", pfs.pfs_id);
190 dump_pfsd(pfs.ondisk, fd);
201 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
203 struct hammer_ioc_pseudofs_rw pfs;
204 struct hammer_pseudofs_data pfsd;
216 if (lstat(path, &st) == 0) {
217 fprintf(stderr, "Cannot create %s, file exists!\n", path);
222 * Figure out the directory prefix, taking care of degenerate
225 dirpath = strdup(path);
226 if (strrchr(dirpath, '/') != NULL) {
227 *strrchr(dirpath, '/') = 0;
228 if (dirpath[0] == 0) {
230 dirpath = strdup("/");
234 dirpath = strdup(".");
236 fd = open(dirpath, O_RDONLY);
238 fprintf(stderr, "Cannot open directory %s\n", dirpath);
243 for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
244 bzero(&pfs, sizeof(pfs));
245 bzero(&pfsd, sizeof(pfsd));
248 pfs.bytes = sizeof(pfsd);
249 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
250 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
255 if (pfs_id == HAMMER_MAX_PFS) {
256 fprintf(stderr, "Cannot create %s, all PFSs in use\n", path);
259 if (error != ENOENT) {
260 fprintf(stderr, "Cannot create %s, got %s during scan\n",
261 path, strerror(error));
268 printf("Creating PFS #%d\t", pfs_id);
269 bzero(&pfsd, sizeof(pfsd));
270 init_pfsd(&pfsd, is_slave);
273 pfs.bytes = sizeof(pfsd);
274 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
276 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
277 printf("failed: %s\n", strerror(errno));
279 /* special symlink, must be exactly 10 characters */
280 asprintf(&linkpath, "@@PFS%05d", pfs_id);
281 if (symlink(linkpath, path) < 0) {
282 printf("failed: cannot create symlink: %s\n",
285 printf("succeeded!\n");
286 hammer_cmd_pseudofs_update(av, ac);
293 hammer_cmd_pseudofs_destroy(char **av, int ac)
295 struct hammer_ioc_pseudofs_rw pfs;
302 bzero(&pfs, sizeof(pfs));
303 fd = getpfs(&pfs, av[0]);
305 if (pfs.pfs_id == 0) {
306 fprintf(stderr, "You cannot destroy PFS#0\n");
309 printf("You have requested that PFS#%d (%s) be destroyed\n",
310 pfs.pfs_id, pfs.ondisk->label);
311 printf("This will irrevocably destroy all data on this PFS!!!!!\n");
312 printf("Do you really want to do this? ");
315 fprintf(stderr, "No action taken on PFS#%d\n", pfs.pfs_id);
319 if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
320 printf("This PFS is currently setup as a MASTER!\n");
321 printf("Are you absolutely sure you want to destroy it? ");
324 fprintf(stderr, "No action taken on PFS#%d\n",
330 printf("Destroying PFS #%d (%s) in ", pfs.pfs_id, pfs.ondisk->label);
331 for (i = 5; i; --i) {
336 printf(".. starting destruction pass\n");
340 * Set the sync_beg_tid and sync_end_tid's to 1, once we start the
341 * RMR the PFS is basically destroyed even if someone ^C's it.
343 pfs.ondisk->mirror_flags |= HAMMER_PFSD_SLAVE;
344 pfs.ondisk->reserved01 = -1;
345 pfs.ondisk->sync_beg_tid = 1;
346 pfs.ondisk->sync_end_tid = 1;
348 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
349 fprintf(stderr, "Unable to update the PFS configuration: %s\n",
355 * Ok, do it. Remove the softlink on success.
357 if (ioctl(fd, HAMMERIOC_RMR_PSEUDOFS, &pfs) == 0) {
358 printf("pfs-destroy of PFS#%d succeeded!\n", pfs.pfs_id);
359 if (lstat(av[0], &st) == 0 && S_ISLNK(st.st_mode)) {
360 if (remove(av[0]) < 0) {
361 fprintf(stderr, "Unable to remove softlink: %s "
362 "(but the PFS has been destroyed)\n",
364 /* exit status 0 anyway */
368 printf("pfs-destroy of PFS#%d failed: %s\n",
369 pfs.pfs_id, strerror(errno));
374 hammer_cmd_pseudofs_upgrade(char **av, int ac)
376 struct hammer_ioc_pseudofs_rw pfs;
381 bzero(&pfs, sizeof(pfs));
382 fd = getpfs(&pfs, av[0]);
384 if (pfs.pfs_id == 0) {
385 fprintf(stderr, "You cannot upgrade PFS#0"
386 " (It should already be a master)\n");
389 if (ioctl(fd, HAMMERIOC_UPG_PSEUDOFS, &pfs) == 0) {
390 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
391 pfs.pfs_id, pfs.ondisk->label);
393 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
394 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
399 hammer_cmd_pseudofs_downgrade(char **av, int ac)
401 struct hammer_ioc_pseudofs_rw pfs;
406 bzero(&pfs, sizeof(pfs));
407 fd = getpfs(&pfs, av[0]);
409 if (pfs.pfs_id == 0) {
410 fprintf(stderr, "You cannot downgrade PFS#0\n");
414 if (ioctl(fd, HAMMERIOC_DGD_PSEUDOFS, &pfs) == 0) {
415 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
416 pfs.pfs_id, pfs.ondisk->label);
418 fprintf(stderr, "pfs-downgrade of PFS#%d (%s) failed: %s\n",
419 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
424 hammer_cmd_pseudofs_update(char **av, int ac)
426 struct hammer_ioc_pseudofs_rw pfs;
431 bzero(&pfs, sizeof(pfs));
432 fd = getpfs(&pfs, av[0]);
434 printf("%s\n", av[0]);
437 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
438 parse_pfsd_options(av + 1, ac - 1, pfs.ondisk);
439 if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) &&
441 printf("The real mount point cannot be made a PFS "
442 "slave, only PFS sub-directories can be made "
446 pfs.bytes = sizeof(*pfs.ondisk);
447 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
448 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
449 dump_pfsd(pfs.ondisk, fd);
451 printf("Unable to retrieve pfs configuration "
452 "after successful update: %s\n",
457 printf("Unable to adjust pfs configuration: %s\n",
465 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
469 pfsd->sync_beg_tid = 1;
470 pfsd->sync_end_tid = 1;
471 pfsd->sync_beg_ts = 0;
472 pfsd->sync_end_ts = 0;
473 uuid_create(&pfsd->shared_uuid, &status);
474 uuid_create(&pfsd->unique_uuid, &status);
476 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
480 dump_pfsd(hammer_pseudofs_data_t pfsd, int fd)
482 struct hammer_ioc_version version;
486 printf(" sync-beg-tid=0x%016jx\n", (uintmax_t)pfsd->sync_beg_tid);
487 printf(" sync-end-tid=0x%016jx\n", (uintmax_t)pfsd->sync_end_tid);
488 uuid_to_string(&pfsd->shared_uuid, &str, &status);
489 printf(" shared-uuid=%s\n", str);
490 uuid_to_string(&pfsd->unique_uuid, &str, &status);
491 printf(" unique-uuid=%s\n", str);
492 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
495 printf(" label=\"%s\"\n", pfsd->label);
496 if (pfsd->snapshots[0])
497 printf(" snapshots=\"%s\"\n", pfsd->snapshots);
498 if (pfsd->prune_min < (60 * 60 * 24)) {
499 printf(" prune-min=%02d:%02d:%02d\n",
500 pfsd->prune_min / 60 / 60 % 24,
501 pfsd->prune_min / 60 % 60,
502 pfsd->prune_min % 60);
503 } else if (pfsd->prune_min % (60 * 60 * 24)) {
504 printf(" prune-min=%dd/%02d:%02d:%02d\n",
505 pfsd->prune_min / 60 / 60 / 24,
506 pfsd->prune_min / 60 / 60 % 24,
507 pfsd->prune_min / 60 % 60,
508 pfsd->prune_min % 60);
510 printf(" prune-min=%dd\n", pfsd->prune_min / 60 / 60 / 24);
513 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
514 printf(" operating as a SLAVE\n");
516 printf(" operating as a MASTER\n");
520 * Snapshots directory cannot be shown when there is no fd since
521 * hammer version can't be retrieved. mirror-dump passes -1 because
522 * its input came from mirror-read output thus no path is available
525 if (fd >= 0 && pfsd->snapshots[0] == 0) {
526 bzero(&version, sizeof(version));
527 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
529 if (version.cur_version < 3) {
530 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
531 printf(" snapshots directory not set for "
534 printf(" snapshots directory for master "
535 "defaults to <pfs>/snapshots\n");
538 printf(" snapshots directory defaults to "
539 "/var/hammer/<pfs>\n");
545 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
554 if ((ptr = strchr(cmd, '=')) != NULL)
558 * Basic assignment value test
562 "option %s requires an assignment\n",
568 if (strcmp(cmd, "sync-beg-tid") == 0) {
569 pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
570 } else if (strcmp(cmd, "sync-end-tid") == 0) {
571 pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
572 } else if (strcmp(cmd, "shared-uuid") == 0) {
573 uuid_from_string(ptr, &pfsd->shared_uuid, &status);
574 } else if (strcmp(cmd, "unique-uuid") == 0) {
575 uuid_from_string(ptr, &pfsd->unique_uuid, &status);
576 } else if (strcmp(cmd, "label") == 0) {
578 if (ptr[0] == '"' && ptr[len-1] == '"') {
581 } else if (ptr[0] == '"') {
583 "option %s: malformed string\n",
587 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
588 } else if (strcmp(cmd, "snapshots") == 0) {
592 "option %s: '%s' must be an "
593 "absolute path\n", cmd, ptr);
596 "use 'snapshots-clear' "
597 "to unset snapshots dir\n");
601 if (len >= (int)sizeof(pfsd->snapshots)) {
603 "option %s: path too long, %d "
604 "character limit\n", cmd, len);
607 snprintf(pfsd->snapshots, sizeof(pfsd->snapshots),
609 } else if (strcmp(cmd, "snapshots-clear") == 0) {
610 pfsd->snapshots[0] = 0;
611 } else if (strcmp(cmd, "prune-min") == 0) {
612 pfsd->prune_min = timetosecs(ptr);
613 if (pfsd->prune_min < 0) {
615 "option %s: illegal time spec, "
616 "use Nd or [Nd/]hh[:mm[:ss]]\n", ptr);
620 fprintf(stderr, "invalid option: %s\n", cmd);
623 if (status != uuid_s_ok) {
624 fprintf(stderr, "option %s: error parsing uuid %s\n",
635 pseudofs_usage(int code)
638 "hammer pfs-status <dirpath> ...\n"
639 "hammer pfs-master <dirpath> [options]\n"
640 "hammer pfs-slave <dirpath> [options]\n"
641 "hammer pfs-update <dirpath> [options]\n"
642 "hammer pfs-upgrade <dirpath>\n"
643 "hammer pfs-downgrade <dirpath>\n"
644 "hammer pfs-destroy <dirpath>\n"
647 " sync-beg-tid=0x16llx\n"
648 " sync-end-tid=0x16llx\n"
649 " shared-uuid=0x16llx\n"
650 " unique-uuid=0x16llx\n"
651 " label=\"string\"\n"
652 " snapshots=\"/path\"\n"
655 " prune-min=[Nd/]hh[:mm[:ss]]\n"
667 if (fgets(buf, sizeof(buf), stdin) == NULL)
670 while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
673 if (strcmp(buf, "y") == 0 ||
674 strcmp(buf, "yes") == 0 ||
675 strcmp(buf, "Y") == 0 ||
676 strcmp(buf, "YES") == 0) {
683 * Convert time in the form [Nd/]hh[:mm[:ss]] to seconds.
685 * Return -1 if a parse error occurs.
686 * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
690 timetosecs(char *str)
700 n = strtol(str, &ptr, 10);
707 n = strtol(ptr + 1, &ptr, 10);
715 n = strtol(ptr + 1, &ptr, 10);
720 n = strtol(ptr + 1, &ptr, 10);
728 v = days * 24 * 60 * 60 + hrs * 60 * 60 + mins * 60 + secs;