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 $
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);
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.
53 getdir(const char *path)
57 dirpath = strdup(path);
58 if (strrchr(dirpath, '/')) {
59 *strrchr(dirpath, '/') = 0;
60 if (strlen(dirpath) == 0) {
62 dirpath = strdup("/");
66 dirpath = strdup(".");
73 * Calculate the pfs_id given a path to a directory or a @@PFS or @@%llx:%d
77 getpfs(struct hammer_ioc_pseudofs_rw *pfs, char *path)
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);
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.
98 len = strnlen(path, MAXPATHLEN);
100 if (strtrl(&path, len) == NULL)
101 errx(1, "Unexpected NULL path");
104 if (lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) {
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.
111 dirpath = getdir(path);
112 n = readlink(path, buf, sizeof(buf) - 1);
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 @@
123 if (strchr(buf, '/') == NULL) {
124 p = buf; /* likely */
130 if (sscanf(p, "@@PFS%d", &pfs->pfs_id) == 1) {
131 fd = open(dirpath, O_RDONLY);
134 if (sscanf(p, "@@%jx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
135 fd = open(dirpath, O_RDONLY);
139 * Once it comes here the hammer command may fail even if
140 * this function returns valid file descriptor.
145 * Try to open the path and request the pfs_id that way.
147 fd = open(path, O_RDONLY);
150 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
151 if (pfs->pfs_id == -1) {
162 fprintf(stderr, "Cannot access PFS %s: %s\n",
163 path, strerror(errno));
166 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
167 fprintf(stderr, "Cannot access PFS %s: %s\n",
168 path, strerror(errno));
176 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs)
187 hammer_cmd_pseudofs_status(char **av, int ac)
189 struct hammer_ioc_pseudofs_rw pfs;
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]);
202 printf("PFS #%d {\n", pfs.pfs_id);
203 dump_pfsd(pfs.ondisk, fd);
215 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
217 struct hammer_ioc_pseudofs_rw pfs;
218 struct hammer_pseudofs_data pfsd;
230 if (lstat(path, &st) == 0) {
231 fprintf(stderr, "Cannot create %s, file exists!\n", path);
236 * Figure out the directory prefix, taking care of degenerate
239 dirpath = getdir(path);
240 fd = open(dirpath, O_RDONLY);
242 fprintf(stderr, "Cannot open directory %s\n", dirpath);
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.
252 bzero(&pfs, sizeof(pfs));
253 bzero(&pfsd, sizeof(pfsd));
256 pfs.bytes = sizeof(pfsd);
258 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs);
259 if (pfs.pfs_id != 0) {
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");
270 for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
271 bzero(&pfs, sizeof(pfs));
272 bzero(&pfsd, sizeof(pfsd));
275 pfs.bytes = sizeof(pfsd);
276 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
277 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
282 if (pfs_id == HAMMER_MAX_PFS) {
283 fprintf(stderr, "Cannot create %s, all PFSs in use\n", path);
286 if (error != ENOENT) {
287 fprintf(stderr, "Cannot create %s, got %s during scan\n",
288 path, strerror(error));
295 printf("Creating PFS #%d\t", pfs_id);
296 init_pfsd(&pfsd, is_slave);
299 pfs.bytes = sizeof(pfsd);
300 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
302 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
303 printf("failed: %s\n", strerror(errno));
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",
311 printf("succeeded!\n");
312 hammer_cmd_pseudofs_update(av, ac);
320 hammer_cmd_pseudofs_destroy(char **av, int ac)
322 struct hammer_ioc_pseudofs_rw pfs;
329 fd = getpfs(&pfs, av[0]);
331 if (pfs.pfs_id == 0) {
332 fprintf(stderr, "You cannot destroy PFS#0\n");
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? ");
341 fprintf(stderr, "No action taken on PFS#%d\n", pfs.pfs_id);
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? ");
350 fprintf(stderr, "No action taken on PFS#%d\n",
356 printf("Destroying PFS #%d (%s) in ", pfs.pfs_id, pfs.ondisk->label);
357 for (i = 5; i; --i) {
362 printf(".. starting destruction pass\n");
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.
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;
374 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
375 fprintf(stderr, "Unable to update the PFS configuration: %s\n",
381 * Ok, do it. Remove the softlink on success.
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",
390 /* exit status 0 anyway */
394 printf("pfs-destroy of PFS#%d failed: %s\n",
395 pfs.pfs_id, strerror(errno));
401 hammer_cmd_pseudofs_upgrade(char **av, int ac)
403 struct hammer_ioc_pseudofs_rw pfs;
408 fd = getpfs(&pfs, av[0]);
410 if (pfs.pfs_id == 0) {
411 fprintf(stderr, "You cannot upgrade PFS#0"
412 " (It should already be a master)\n");
414 } else if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
415 printf("It is already a master\n");
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);
423 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
424 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
430 hammer_cmd_pseudofs_downgrade(char **av, int ac)
432 struct hammer_ioc_pseudofs_rw pfs;
437 fd = getpfs(&pfs, av[0]);
439 if (pfs.pfs_id == 0) {
440 fprintf(stderr, "You cannot downgrade PFS#0\n");
442 } else if (pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) {
443 printf("It is already a slave\n");
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);
451 fprintf(stderr, "pfs-downgrade of PFS#%d (%s) failed: %s\n",
452 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
458 hammer_cmd_pseudofs_update(char **av, int ac)
460 struct hammer_ioc_pseudofs_rw pfs;
465 fd = getpfs(&pfs, av[0]);
467 printf("%s\n", av[0]);
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) &&
474 printf("The real mount point cannot be made a PFS "
475 "slave, only PFS sub-directories can be made "
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);
484 printf("Unable to retrieve pfs configuration "
485 "after successful update: %s\n",
490 printf("Unable to adjust pfs configuration: %s\n",
499 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
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);
511 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
515 dump_pfsd(hammer_pseudofs_data_t pfsd, int fd)
517 struct hammer_ioc_version version;
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);
526 uuid_to_string(&pfsd->unique_uuid, &str, &status);
527 printf(" unique-uuid=%s\n", str);
529 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
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);
547 printf(" prune-min=%dd\n", pfsd->prune_min / 60 / 60 / 24);
550 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
551 printf(" operating as a SLAVE\n");
553 printf(" operating as a MASTER\n");
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
562 if (fd >= 0 && pfsd->snapshots[0] == 0) {
563 bzero(&version, sizeof(version));
564 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
566 if (version.cur_version < 3) {
567 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
568 printf(" snapshots directory not set for "
571 printf(" snapshots directory for master "
572 "defaults to <pfs>/snapshots\n");
575 printf(" snapshots directory defaults to "
576 "/var/hammer/<pfs>\n");
582 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
591 if ((ptr = strchr(cmd, '=')) != NULL)
595 * Basic assignment value test
599 "option %s requires an assignment\n",
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) {
615 if (ptr[0] == '"' && ptr[len-1] == '"') {
618 } else if (ptr[0] == '"') {
620 "option %s: malformed string\n",
624 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
625 } else if (strcmp(cmd, "snapshots") == 0) {
629 "option %s: '%s' must be an "
630 "absolute path\n", cmd, ptr);
633 "use 'snapshots-clear' "
634 "to unset snapshots dir\n");
638 if (len >= (int)sizeof(pfsd->snapshots)) {
640 "option %s: path too long, %d "
641 "character limit\n", cmd, len);
644 snprintf(pfsd->snapshots, sizeof(pfsd->snapshots),
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) {
652 "option %s: illegal time spec, "
653 "use Nd or [Nd/]hh[:mm[:ss]]\n", ptr);
657 fprintf(stderr, "invalid option: %s\n", cmd);
660 if (status != uuid_s_ok) {
661 fprintf(stderr, "option %s: error parsing uuid %s\n",
672 pseudofs_usage(int code)
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"
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"
692 " prune-min=[Nd/]hh[:mm[:ss]]\n"
704 if (fgets(buf, sizeof(buf), stdin) == NULL)
707 while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
710 if (strcmp(buf, "y") == 0 ||
711 strcmp(buf, "yes") == 0 ||
712 strcmp(buf, "Y") == 0 ||
713 strcmp(buf, "YES") == 0) {
720 * Convert time in the form [Nd/]hh[:mm[:ss]] to seconds.
722 * Return -1 if a parse error occurs.
723 * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
727 timetosecs(char *str)
737 n = strtol(str, &ptr, 10);
744 n = strtol(ptr + 1, &ptr, 10);
752 n = strtol(ptr + 1, &ptr, 10);
757 n = strtol(ptr + 1, &ptr, 10);
765 v = days * 24 * 60 * 60 + hrs * 60 * 60 + mins * 60 + secs;
773 strtrl(char **path, int len)
782 /* Attempt to remove all trailing slashes */
783 while (p-- > s && *p == '/')