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 dump_pfsd(hammer_pseudofs_data_t pfsd, int fd);
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
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 * Remove the trailing '/' if there is one so that
68 * in the case path is a symbolic link to the PFS,
69 * the symbolic link is used and not the root dir of
72 len = strnlen(path, MAXPATHLEN);
73 if (path[len-1] == '/' && path[len] == '\0')
77 * Calculate the directory containing the softlink
79 dirpath = strdup(path);
80 if (strrchr(dirpath, '/')) {
81 *strrchr(dirpath, '/') = 0;
82 if (strlen(dirpath) == 0) {
84 dirpath = strdup("/");
88 dirpath = strdup(".");
91 if (lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) {
93 * Avoid foot-shooting. Don't let the user access a PFS
94 * softlink via a PFS. PFS softlinks may only be accessed
95 * via the master filesystem.
97 fd = open(dirpath, O_RDONLY);
101 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
102 if (pfs->pfs_id != 0) {
104 "You are attempting to access a PFS softlink "
105 "from a PFS. It may not represent the PFS\n"
106 "on the main filesystem mount that you "
107 "expect! You may only access PFS softlinks\n"
108 "via the main filesystem mount!\n");
114 * Extract the PFS from the link. HAMMER will automatically
115 * convert @@PFS%05d links so if actually see one in that
116 * form the target PFS may not exist or may be corrupt. But
117 * we can extract the PFS id anyway.
119 n = readlink(path, buf, sizeof(buf) - 1);
123 if (sscanf(buf, "@@PFS%d", &pfs->pfs_id) == 1) {
124 fd = open(dirpath, O_RDONLY);
127 if (sscanf(buf, "@@%jx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
128 fd = open(dirpath, O_RDONLY);
134 * Try to open the path and request the pfs_id that way.
136 fd = open(path, O_RDONLY);
139 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
140 if (pfs->pfs_id == -1) {
151 fprintf(stderr, "Cannot access PFS %s: %s\n",
152 path, strerror(errno));
155 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
156 fprintf(stderr, "Cannot access PFS %s: %s\n",
157 path, strerror(errno));
165 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs)
175 hammer_cmd_pseudofs_status(char **av, int ac)
177 struct hammer_ioc_pseudofs_rw pfs;
184 for (i = 0; i < ac; ++i) {
185 printf("%s\t", av[i]);
186 fd = getpfs(&pfs, av[i]);
187 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
188 printf("Not a HAMMER root\n");
190 printf("PFS #%d {\n", pfs.pfs_id);
191 dump_pfsd(pfs.ondisk, fd);
202 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
204 struct hammer_ioc_pseudofs_rw pfs;
205 struct hammer_pseudofs_data pfsd;
217 if (lstat(path, &st) == 0) {
218 fprintf(stderr, "Cannot create %s, file exists!\n", path);
223 * Figure out the directory prefix, taking care of degenerate
226 dirpath = strdup(path);
227 if (strrchr(dirpath, '/') != NULL) {
228 *strrchr(dirpath, '/') = 0;
229 if (dirpath[0] == 0) {
231 dirpath = strdup("/");
235 dirpath = strdup(".");
237 fd = open(dirpath, O_RDONLY);
239 fprintf(stderr, "Cannot open directory %s\n", dirpath);
244 for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
245 bzero(&pfs, sizeof(pfs));
246 bzero(&pfsd, sizeof(pfsd));
249 pfs.bytes = sizeof(pfsd);
250 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
251 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
256 if (pfs_id == HAMMER_MAX_PFS) {
257 fprintf(stderr, "Cannot create %s, all PFSs in use\n", path);
260 if (error != ENOENT) {
261 fprintf(stderr, "Cannot create %s, got %s during scan\n",
262 path, strerror(error));
269 printf("Creating PFS #%d\t", pfs_id);
270 bzero(&pfsd, sizeof(pfsd));
271 init_pfsd(&pfsd, is_slave);
274 pfs.bytes = sizeof(pfsd);
275 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
277 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
278 printf("failed: %s\n", strerror(errno));
280 /* special symlink, must be exactly 10 characters */
281 asprintf(&linkpath, "@@PFS%05d", pfs_id);
282 if (symlink(linkpath, path) < 0) {
283 printf("failed: cannot create symlink: %s\n",
286 printf("succeeded!\n");
287 hammer_cmd_pseudofs_update(av, ac);
294 hammer_cmd_pseudofs_destroy(char **av, int ac)
296 struct hammer_ioc_pseudofs_rw pfs;
303 bzero(&pfs, sizeof(pfs));
304 fd = getpfs(&pfs, av[0]);
306 if (pfs.pfs_id == 0) {
307 fprintf(stderr, "You cannot destroy PFS#0\n");
310 printf("You have requested that PFS#%d (%s) be destroyed\n",
311 pfs.pfs_id, pfs.ondisk->label);
312 printf("This will irrevocably destroy all data on this PFS!!!!!\n");
313 printf("Do you really want to do this? ");
316 fprintf(stderr, "No action taken on PFS#%d\n", pfs.pfs_id);
320 if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
321 printf("This PFS is currently setup as a MASTER!\n");
322 printf("Are you absolutely sure you want to destroy it? ");
325 fprintf(stderr, "No action taken on PFS#%d\n",
331 printf("Destroying PFS #%d (%s) in ", pfs.pfs_id, pfs.ondisk->label);
332 for (i = 5; i; --i) {
337 printf(".. starting destruction pass\n");
341 * Set the sync_beg_tid and sync_end_tid's to 1, once we start the
342 * RMR the PFS is basically destroyed even if someone ^C's it.
344 pfs.ondisk->mirror_flags |= HAMMER_PFSD_SLAVE;
345 pfs.ondisk->reserved01 = -1;
346 pfs.ondisk->sync_beg_tid = 1;
347 pfs.ondisk->sync_end_tid = 1;
349 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
350 fprintf(stderr, "Unable to update the PFS configuration: %s\n",
356 * Ok, do it. Remove the softlink on success.
358 if (ioctl(fd, HAMMERIOC_RMR_PSEUDOFS, &pfs) == 0) {
359 printf("pfs-destroy of PFS#%d succeeded!\n", pfs.pfs_id);
360 if (lstat(av[0], &st) == 0 && S_ISLNK(st.st_mode)) {
361 if (remove(av[0]) < 0) {
362 fprintf(stderr, "Unable to remove softlink: %s "
363 "(but the PFS has been destroyed)\n",
365 /* exit status 0 anyway */
369 printf("pfs-destroy of PFS#%d failed: %s\n",
370 pfs.pfs_id, strerror(errno));
375 hammer_cmd_pseudofs_upgrade(char **av, int ac)
377 struct hammer_ioc_pseudofs_rw pfs;
382 bzero(&pfs, sizeof(pfs));
383 fd = getpfs(&pfs, av[0]);
385 if (pfs.pfs_id == 0) {
386 fprintf(stderr, "You cannot upgrade PFS#0"
387 " (It should already be a master)\n");
390 if (ioctl(fd, HAMMERIOC_UPG_PSEUDOFS, &pfs) == 0) {
391 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
392 pfs.pfs_id, pfs.ondisk->label);
394 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
395 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
400 hammer_cmd_pseudofs_downgrade(char **av, int ac)
402 struct hammer_ioc_pseudofs_rw pfs;
407 bzero(&pfs, sizeof(pfs));
408 fd = getpfs(&pfs, av[0]);
410 if (pfs.pfs_id == 0) {
411 fprintf(stderr, "You cannot downgrade PFS#0\n");
415 if (ioctl(fd, HAMMERIOC_DGD_PSEUDOFS, &pfs) == 0) {
416 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
417 pfs.pfs_id, pfs.ondisk->label);
419 fprintf(stderr, "pfs-downgrade of PFS#%d (%s) failed: %s\n",
420 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
425 hammer_cmd_pseudofs_update(char **av, int ac)
427 struct hammer_ioc_pseudofs_rw pfs;
432 bzero(&pfs, sizeof(pfs));
433 fd = getpfs(&pfs, av[0]);
435 printf("%s\n", av[0]);
438 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
439 parse_pfsd_options(av + 1, ac - 1, pfs.ondisk);
440 if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) &&
442 printf("The real mount point cannot be made a PFS "
443 "slave, only PFS sub-directories can be made "
447 pfs.bytes = sizeof(*pfs.ondisk);
448 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
449 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
450 dump_pfsd(pfs.ondisk, fd);
452 printf("Unable to retrieve pfs configuration "
453 "after successful update: %s\n",
458 printf("Unable to adjust pfs configuration: %s\n",
466 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
470 pfsd->sync_beg_tid = 1;
471 pfsd->sync_end_tid = 1;
472 pfsd->sync_beg_ts = 0;
473 pfsd->sync_end_ts = 0;
474 uuid_create(&pfsd->shared_uuid, &status);
475 uuid_create(&pfsd->unique_uuid, &status);
477 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");
521 if (pfsd->snapshots[0] == 0) {
522 bzero(&version, sizeof(version));
523 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
525 if (version.cur_version < 3) {
526 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
527 printf(" snapshots directory not set for "
530 printf(" snapshots directory for master "
531 "defaults to <pfs>/snapshots\n");
534 printf(" snapshots directory defaults to "
535 "/var/hammer/<pfs>\n");
541 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
550 if ((ptr = strchr(cmd, '=')) != NULL)
554 * Basic assignment value test
558 "option %s requires an assignment\n",
564 if (strcmp(cmd, "sync-beg-tid") == 0) {
565 pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
566 } else if (strcmp(cmd, "sync-end-tid") == 0) {
567 pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
568 } else if (strcmp(cmd, "shared-uuid") == 0) {
569 uuid_from_string(ptr, &pfsd->shared_uuid, &status);
570 } else if (strcmp(cmd, "unique-uuid") == 0) {
571 uuid_from_string(ptr, &pfsd->unique_uuid, &status);
572 } else if (strcmp(cmd, "label") == 0) {
574 if (ptr[0] == '"' && ptr[len-1] == '"') {
577 } else if (ptr[0] == '"') {
579 "option %s: malformed string\n",
583 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
584 } else if (strcmp(cmd, "snapshots") == 0) {
588 "option %s: '%s' must be an "
589 "absolute path\n", cmd, ptr);
592 "use 'snapshots-clear' "
593 "to unset snapshots dir\n");
597 if (len >= (int)sizeof(pfsd->snapshots)) {
599 "option %s: path too long, %d "
600 "character limit\n", cmd, len);
603 snprintf(pfsd->snapshots, sizeof(pfsd->snapshots),
605 } else if (strcmp(cmd, "snapshots-clear") == 0) {
606 pfsd->snapshots[0] = 0;
607 } else if (strcmp(cmd, "prune-min") == 0) {
608 pfsd->prune_min = timetosecs(ptr);
609 if (pfsd->prune_min < 0) {
611 "option %s: illegal time spec, "
612 "use Nd or [Nd/]hh[:mm[:ss]]\n", ptr);
616 fprintf(stderr, "invalid option: %s\n", cmd);
619 if (status != uuid_s_ok) {
620 fprintf(stderr, "option %s: error parsing uuid %s\n",
631 pseudofs_usage(int code)
634 "hammer pfs-status <dirpath> ...\n"
635 "hammer pfs-master <dirpath> [options]\n"
636 "hammer pfs-slave <dirpath> [options]\n"
637 "hammer pfs-update <dirpath> [options]\n"
638 "hammer pfs-upgrade <dirpath>\n"
639 "hammer pfs-downgrade <dirpath>\n"
640 "hammer pfs-destroy <dirpath>\n"
643 " sync-beg-tid=0x16llx\n"
644 " sync-end-tid=0x16llx\n"
645 " shared-uuid=0x16llx\n"
646 " unique-uuid=0x16llx\n"
647 " label=\"string\"\n"
648 " snapshots=\"/path\"\n"
651 " prune-min=[Nd/]hh[:mm[:ss]]\n"
663 if (fgets(buf, sizeof(buf), stdin) == NULL)
666 while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
669 if (strcmp(buf, "y") == 0 ||
670 strcmp(buf, "yes") == 0 ||
671 strcmp(buf, "Y") == 0 ||
672 strcmp(buf, "YES") == 0) {
679 * Convert time in the form [Nd/]hh[:mm[:ss]] to seconds.
681 * Return -1 if a parse error occurs.
682 * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
686 timetosecs(char *str)
696 n = strtol(str, &ptr, 10);
703 n = strtol(ptr + 1, &ptr, 10);
711 n = strtol(ptr + 1, &ptr, 10);
716 n = strtol(ptr + 1, &ptr, 10);
724 v = days * 24 * 60 * 60 + hrs * 60 * 60 + mins * 60 + secs;