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 $
41 static int scanpfsid(struct hammer_ioc_pseudofs_rw *pfs, const char *path);
42 static void parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd);
43 static void init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave);
44 static void pseudofs_usage(int code);
45 static int timetosecs(char *str);
48 clrpfs(struct hammer_ioc_pseudofs_rw *pfs, hammer_pseudofs_data_t pfsd,
51 bzero(pfs, sizeof(*pfs));
56 pfs->ondisk = malloc(sizeof(*pfs->ondisk));
57 bzero(pfs->ondisk, sizeof(*pfs->ondisk));
60 pfs->bytes = sizeof(*pfs->ondisk);
61 pfs->version = HAMMER_IOC_PSEUDOFS_VERSION;
65 * If path is a symlink, return strdup'd path.
66 * If it's a directory via symlink, strip trailing /
67 * from strdup'd path and return the symlink.
70 getlink(const char *path)
78 linkpath = strdup(path);
80 if (S_ISDIR(st.st_mode)) {
81 i = strlen(linkpath) - 1;
82 while (i > 0 && linkpath[i] == '/')
86 if (S_ISLNK(st.st_mode)) {
95 * Calculate the PFS id given a path to a file/directory or
96 * a @@%llx:%d softlink.
99 getpfs(struct hammer_ioc_pseudofs_rw *pfs, const char *path)
103 clrpfs(pfs, NULL, -1);
106 * Extract the PFS id.
107 * dirname(path) is supposed to be a directory in root PFS.
109 if (scanpfsid(pfs, path) == 0) {
110 path = dirname(path); /* strips trailing / first if any */
114 * Open the path regardless of scanpfsid() result, since some
115 * commands can take a regular file/directory (e.g. pfs-status).
117 fd = open(path, O_RDONLY);
119 err(1, "Failed to open %s", path);
122 * If pfs.pfs_id has been set to non -1, the file descriptor fd
123 * could be any fd of HAMMER inodes since HAMMERIOC_GET_PSEUDOFS
124 * doesn't depend on inode attributes if it's set to a valid id.
126 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0)
127 err(1, "Cannot access %s", path);
133 * Extract the PFS id from path.
136 scanpfsid(struct hammer_ioc_pseudofs_rw *pfs, const char *path)
143 if (stat(path, &st)) {
144 /* possibly slave PFS */
145 } else if (S_ISDIR(st.st_mode)) {
146 /* possibly master or slave PFS */
148 return(-1); /* neither */
151 linkpath = getlink(path);
154 * Read the symlink assuming it's a link to PFS.
156 bzero(buf, sizeof(buf));
157 if (readlink(linkpath, buf, sizeof(buf) - 1) < 0) {
166 * The symlink created by pfs-master|slave is just a symlink.
167 * One could happen to remove a symlink and relink PFS as
168 * # ln -s ./@@-1:00001 ./link
169 * which results PFS having something extra before @@.
170 * One could also directly use the PFS and results the same.
171 * Get rid of it before we extract the PFS id.
173 if (strchr(path, '/')) {
174 path = basename(path); /* strips trailing / first if any */
180 * Test and extract the PFS id from the link.
181 * "@@%jx:%d" covers both "@@-1:%05d" format for master PFS
182 * and "@@0x%016jx:%05d" format for slave PFS.
184 if (sscanf(path, "@@%jx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
185 assert(pfs->pfs_id > 0);
193 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs)
204 print_pfs_status(char *path)
206 struct hammer_ioc_pseudofs_rw pfs;
209 fd = getpfs(&pfs, path);
210 printf("%s\t", path);
211 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
212 printf("Invalid PFS path %s\n", path);
214 printf("PFS#%d {\n", pfs.pfs_id);
215 dump_pfsd(pfs.ondisk, fd);
226 hammer_cmd_pseudofs_status(char **av, int ac)
231 char buf[2] = "."; /* can't be readonly string */
232 print_pfs_status(buf);
236 for (i = 0; i < ac; ++i)
237 print_pfs_status(av[i]);
241 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
243 struct hammer_ioc_pseudofs_rw pfs;
244 struct hammer_pseudofs_data pfsd;
255 if (lstat(path, &st) == 0) {
256 errx(1, "Cannot create %s, file exists!", path);
257 } else if (path[strlen(path) - 1] == '/') {
258 errx(1, "Invalid PFS path %s with trailing /", path);
262 * Figure out the directory prefix, taking care of degenerate
265 dirpath = dirname(path);
266 fd = open(dirpath, O_RDONLY);
268 err(1, "Cannot open directory %s", dirpath);
271 * Avoid foot-shooting. Don't let the user create a PFS
272 * softlink via a PFS. PFS softlinks may only be accessed
273 * via the master filesystem. Checking it here ensures
274 * other PFS commands access PFS under the master filesystem.
276 clrpfs(&pfs, &pfsd, -1);
278 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs);
279 if (pfs.pfs_id != HAMMER_ROOT_PFSID) {
281 "You are attempting to access a PFS softlink "
282 "from a PFS. It may not represent the PFS\n"
283 "on the main filesystem mount that you "
284 "expect! You may only access PFS softlinks\n"
285 "via the main filesystem mount!\n");
289 for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
290 clrpfs(&pfs, &pfsd, pfs_id);
291 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
293 err(1, "Cannot create %s", path);
297 if (pfs_id == HAMMER_MAX_PFS) {
298 errx(1, "Cannot create %s, all PFSs in use", path);
299 } else if (pfs_id == HAMMER_ROOT_PFSID) {
300 errx(1, "Fatal error: PFS#%d must exist", HAMMER_ROOT_PFSID);
306 printf("Creating PFS#%d\t", pfs_id);
307 clrpfs(&pfs, &pfsd, pfs_id);
308 init_pfsd(&pfsd, is_slave);
310 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
311 printf("failed: %s\n", strerror(errno));
313 /* special symlink, must be exactly 10 characters */
314 asprintf(&linkpath, "@@PFS%05d", pfs_id);
315 if (symlink(linkpath, path) < 0) {
316 printf("failed: cannot create symlink: %s\n",
319 printf("succeeded!\n");
320 hammer_cmd_pseudofs_update(av, ac);
328 hammer_cmd_pseudofs_destroy(char **av, int ac)
330 struct hammer_ioc_pseudofs_rw pfs;
337 fd = getpfs(&pfs, av[0]);
339 if (pfs.pfs_id == HAMMER_ROOT_PFSID)
340 errx(1, "You cannot destroy PFS#%d", HAMMER_ROOT_PFSID);
342 printf("You have requested that PFS#%d (%s) be destroyed\n",
343 pfs.pfs_id, pfs.ondisk->label);
344 printf("This will irrevocably destroy all data on this PFS!!!!!\n");
345 printf("Do you really want to do this? [y/n] ");
348 errx(1, "No action taken on PFS#%d", pfs.pfs_id);
350 if (hammer_is_pfs_master(pfs.ondisk)) {
351 printf("This PFS is currently setup as a MASTER!\n");
352 printf("Are you absolutely sure you want to destroy it? [y/n] ");
355 errx(1, "No action taken on PFS#%d", pfs.pfs_id);
358 printf("Destroying PFS#%d (%s)", pfs.pfs_id, pfs.ondisk->label);
363 for (i = 5; i; --i) {
368 printf(".. starting destruction pass\n");
372 * Remove the softlink on success.
374 if (ioctl(fd, HAMMERIOC_RMR_PSEUDOFS, &pfs) == 0) {
375 printf("pfs-destroy of PFS#%d succeeded!\n", pfs.pfs_id);
376 linkpath = getlink(av[0]);
378 if (remove(linkpath) < 0) {
380 "Unable to remove softlink %s: %s\n",
381 linkpath, strerror(errno));
382 /* exit status 0 anyway */
387 printf("pfs-destroy of PFS#%d failed: %s\n",
388 pfs.pfs_id, strerror(errno));
394 hammer_cmd_pseudofs_upgrade(char **av, int ac)
396 struct hammer_ioc_pseudofs_rw pfs;
401 fd = getpfs(&pfs, av[0]);
403 if (pfs.pfs_id == HAMMER_ROOT_PFSID) {
404 errx(1, "You cannot upgrade PFS#%d"
405 " (It should already be a master)",
407 } else if (hammer_is_pfs_master(pfs.ondisk)) {
408 errx(1, "It is already a master");
411 if (ioctl(fd, HAMMERIOC_UPG_PSEUDOFS, &pfs) == 0) {
412 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
413 pfs.pfs_id, pfs.ondisk->label);
415 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
416 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
422 hammer_cmd_pseudofs_downgrade(char **av, int ac)
424 struct hammer_ioc_pseudofs_rw pfs;
429 fd = getpfs(&pfs, av[0]);
431 if (pfs.pfs_id == HAMMER_ROOT_PFSID) {
432 errx(1, "You cannot downgrade PFS#%d", HAMMER_ROOT_PFSID);
433 } else if (hammer_is_pfs_slave(pfs.ondisk)) {
434 errx(1, "It is already a slave");
437 if (ioctl(fd, HAMMERIOC_DGD_PSEUDOFS, &pfs) == 0) {
438 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
439 pfs.pfs_id, pfs.ondisk->label);
441 fprintf(stderr, "pfs-downgrade of PFS#%d (%s) failed: %s\n",
442 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
448 hammer_cmd_pseudofs_update(char **av, int ac)
450 struct hammer_ioc_pseudofs_rw pfs;
455 fd = getpfs(&pfs, av[0]);
457 printf("%s\n", av[0]);
460 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
461 parse_pfsd_options(av + 1, ac - 1, pfs.ondisk);
462 if (hammer_is_pfs_slave(pfs.ondisk) &&
463 pfs.pfs_id == HAMMER_ROOT_PFSID) {
464 errx(1, "The real mount point cannot be made a PFS "
465 "slave, only PFS sub-directories can be made "
468 pfs.bytes = sizeof(*pfs.ondisk);
469 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
470 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
471 dump_pfsd(pfs.ondisk, fd);
473 err(1, "Unable to retrieve PFS configuration "
474 "after successful update");
477 err(1, "Unable to adjust PFS configuration");
484 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
488 bzero(pfsd, sizeof(*pfsd));
489 pfsd->sync_beg_tid = 1;
490 pfsd->sync_end_tid = 1;
491 pfsd->sync_beg_ts = 0;
492 pfsd->sync_end_ts = 0;
493 uuid_create(&pfsd->shared_uuid, &status);
494 uuid_create(&pfsd->unique_uuid, &status);
496 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
500 dump_pfsd(hammer_pseudofs_data_t pfsd, int fd)
502 struct hammer_ioc_version version;
506 printf(" sync-beg-tid=0x%016jx\n", (uintmax_t)pfsd->sync_beg_tid);
507 printf(" sync-end-tid=0x%016jx\n", (uintmax_t)pfsd->sync_end_tid);
508 uuid_to_string(&pfsd->shared_uuid, &str, &status);
509 printf(" shared-uuid=%s\n", str);
511 uuid_to_string(&pfsd->unique_uuid, &str, &status);
512 printf(" unique-uuid=%s\n", str);
514 printf(" label=\"%s\"\n", pfsd->label);
515 if (pfsd->snapshots[0])
516 printf(" snapshots=\"%s\"\n", pfsd->snapshots);
517 if (pfsd->prune_min < (60 * 60 * 24)) {
518 printf(" prune-min=%02d:%02d:%02d\n",
519 pfsd->prune_min / 60 / 60 % 24,
520 pfsd->prune_min / 60 % 60,
521 pfsd->prune_min % 60);
522 } else if (pfsd->prune_min % (60 * 60 * 24)) {
523 printf(" prune-min=%dd/%02d:%02d:%02d\n",
524 pfsd->prune_min / 60 / 60 / 24,
525 pfsd->prune_min / 60 / 60 % 24,
526 pfsd->prune_min / 60 % 60,
527 pfsd->prune_min % 60);
529 printf(" prune-min=%dd\n", pfsd->prune_min / 60 / 60 / 24);
532 if (hammer_is_pfs_slave(pfsd)) {
533 printf(" operating as a SLAVE\n");
535 printf(" operating as a MASTER\n");
539 * Snapshots directory cannot be shown when there is no fd since
540 * hammer version can't be retrieved. mirror-dump passes -1 because
541 * its input came from mirror-read output thus no path is available
544 if (fd >= 0 && pfsd->snapshots[0] == 0) {
545 bzero(&version, sizeof(version));
546 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
548 if (version.cur_version < 3) {
549 if (hammer_is_pfs_slave(pfsd)) {
550 printf(" snapshots directory not set for "
553 printf(" snapshots directory for master "
554 "defaults to <pfs>/snapshots\n");
557 printf(" snapshots directory defaults to "
558 "/var/hammer/<pfs>\n");
564 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
573 if ((ptr = strchr(cmd, '=')) != NULL)
577 * Basic assignment value test
580 errx(1, "option %s requires an assignment", cmd);
583 if (strcmp(cmd, "sync-beg-tid") == 0) {
584 pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
585 } else if (strcmp(cmd, "sync-end-tid") == 0) {
586 pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
587 } else if (strcmp(cmd, "shared-uuid") == 0) {
588 uuid_from_string(ptr, &pfsd->shared_uuid, &status);
589 } else if (strcmp(cmd, "unique-uuid") == 0) {
590 uuid_from_string(ptr, &pfsd->unique_uuid, &status);
591 } else if (strcmp(cmd, "label") == 0) {
593 if (ptr[0] == '"' && ptr[len-1] == '"') {
596 } else if (ptr[0] == '"') {
597 errx(1, "option %s: malformed string", cmd);
599 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
600 } else if (strcmp(cmd, "snapshots") == 0) {
604 "option %s: '%s' must be an "
605 "absolute path\n", cmd, ptr);
608 "use 'snapshots-clear' "
609 "to unset snapshots dir\n");
613 if (len >= (int)sizeof(pfsd->snapshots)) {
614 errx(1, "option %s: path too long, %d "
615 "character limit", cmd, len);
617 snprintf(pfsd->snapshots, sizeof(pfsd->snapshots),
619 } else if (strcmp(cmd, "snapshots-clear") == 0) {
620 pfsd->snapshots[0] = 0;
621 } else if (strcmp(cmd, "prune-min") == 0) {
622 pfsd->prune_min = timetosecs(ptr);
623 if (pfsd->prune_min < 0) {
624 errx(1, "option %s: illegal time spec, "
625 "use Nd or [Nd/]hh[:mm[:ss]]", ptr);
628 errx(1, "invalid option: %s", cmd);
630 if (status != uuid_s_ok) {
631 errx(1, "option %s: error parsing uuid %s", cmd, ptr);
640 pseudofs_usage(int code)
643 "hammer pfs-status <dirpath> ...\n"
644 "hammer pfs-master <dirpath> [options]\n"
645 "hammer pfs-slave <dirpath> [options]\n"
646 "hammer pfs-update <dirpath> [options]\n"
647 "hammer pfs-upgrade <dirpath>\n"
648 "hammer pfs-downgrade <dirpath>\n"
649 "hammer pfs-destroy <dirpath>\n"
652 " sync-beg-tid=0x16llx\n"
653 " sync-end-tid=0x16llx\n"
654 " shared-uuid=0x16llx\n"
655 " unique-uuid=0x16llx\n"
656 " label=\"string\"\n"
657 " snapshots=\"/path\"\n"
660 " prune-min=[Nd/]hh[:mm[:ss]]\n"
666 * Convert time in the form [Nd/]hh[:mm[:ss]] to seconds.
668 * Return -1 if a parse error occurs.
669 * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
673 timetosecs(char *str)
683 n = strtol(str, &ptr, 10);
690 n = strtol(ptr + 1, &ptr, 10);
698 n = strtol(ptr + 1, &ptr, 10);
703 n = strtol(ptr + 1, &ptr, 10);
711 v = days * 24 * 60 * 60 + hrs * 60 * 60 + mins * 60 + secs;