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
);
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
, const char *path
)
53 hammer_tid_t dummy_tid
;
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 * Calculate the directory containing the softlink
68 dirpath
= strdup(path
);
69 if (strrchr(dirpath
, '/')) {
70 *strrchr(dirpath
, '/') = 0;
71 if (strlen(dirpath
) == 0) {
73 dirpath
= strdup("/");
77 dirpath
= strdup(".");
80 if (lstat(path
, &st
) == 0 && S_ISLNK(st
.st_mode
)) {
82 * Avoid foot-shooting. Don't let the user access a PFS
83 * softlink via a PFS. PFS softlinks may only be accessed
84 * via the master filesystem.
86 fd
= open(dirpath
, O_RDONLY
);
90 ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, pfs
);
91 if (pfs
->pfs_id
!= 0) {
93 "You are attempting to access a PFS softlink "
94 "from a PFS. It may not represent the PFS\n"
95 "on the main filesystem mount that you "
96 "expect! You may only access PFS softlinks\n"
97 "via the main filesystem mount!\n");
103 * Extract the PFS from the link. HAMMER will automatically
104 * convert @@PFS%05d links so if actually see one in that
105 * form the target PFS may not exist or may be corrupt. But
106 * we can extract the PFS id anyway.
108 n
= readlink(path
, buf
, sizeof(buf
) - 1);
112 if (sscanf(buf
, "@@PFS%d", &pfs
->pfs_id
) == 1) {
113 fd
= open(dirpath
, O_RDONLY
);
116 if (sscanf(buf
, "@@%llx:%d", &dummy_tid
, &pfs
->pfs_id
) == 2) {
117 fd
= open(dirpath
, O_RDONLY
);
123 * Try to open the path and request the pfs_id that way.
125 fd
= open(path
, O_RDONLY
);
128 ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, pfs
);
129 if (pfs
->pfs_id
== -1) {
140 fprintf(stderr
, "Cannot access PFS %s: %s\n",
141 path
, strerror(errno
));
144 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, pfs
) < 0) {
145 fprintf(stderr
, "Cannot access PFS %s: %s\n",
146 path
, strerror(errno
));
154 relpfs(int fd
, struct hammer_ioc_pseudofs_rw
*pfs
)
164 hammer_cmd_pseudofs_status(char **av
, int ac
)
166 struct hammer_ioc_pseudofs_rw pfs
;
173 for (i
= 0; i
< ac
; ++i
) {
174 printf("%s\t", av
[i
]);
175 fd
= getpfs(&pfs
, av
[i
]);
176 if (fd
< 0 || ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
177 printf("Not a HAMMER root\n");
179 printf("PFS #%d {\n", pfs
.pfs_id
);
180 dump_pfsd(pfs
.ondisk
);
191 hammer_cmd_pseudofs_create(char **av
, int ac
, int is_slave
)
193 struct hammer_ioc_pseudofs_rw pfs
;
194 struct hammer_pseudofs_data pfsd
;
206 if (lstat(path
, &st
) == 0) {
207 fprintf(stderr
, "Cannot create %s, file exists!\n", path
);
212 * Figure out the directory prefix, taking care of degenerate
215 dirpath
= strdup(path
);
216 if (strrchr(dirpath
, '/') != NULL
) {
217 *strrchr(dirpath
, '/') = 0;
218 if (dirpath
[0] == 0) {
220 dirpath
= strdup("/");
224 dirpath
= strdup(".");
226 fd
= open(dirpath
, O_RDONLY
);
228 fprintf(stderr
, "Cannot open directory %s\n", dirpath
);
233 for (pfs_id
= 0; pfs_id
< HAMMER_MAX_PFS
; ++pfs_id
) {
234 bzero(&pfs
, sizeof(pfs
));
235 bzero(&pfsd
, sizeof(pfsd
));
238 pfs
.bytes
= sizeof(pfsd
);
239 pfs
.version
= HAMMER_IOC_PSEUDOFS_VERSION
;
240 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
245 if (pfs_id
== HAMMER_MAX_PFS
) {
246 fprintf(stderr
, "Cannot create %s, all PFSs in use\n", path
);
249 if (error
!= ENOENT
) {
250 fprintf(stderr
, "Cannot create %s, got %s during scan\n",
251 path
, strerror(error
));
258 printf("Creating PFS #%d\t", pfs_id
);
259 bzero(&pfsd
, sizeof(pfsd
));
260 init_pfsd(&pfsd
, is_slave
);
263 pfs
.bytes
= sizeof(pfsd
);
264 pfs
.version
= HAMMER_IOC_PSEUDOFS_VERSION
;
266 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) < 0) {
267 printf("failed: %s\n", strerror(errno
));
269 /* special symlink, must be exactly 10 characters */
270 asprintf(&linkpath
, "@@PFS%05d", pfs_id
);
271 if (symlink(linkpath
, path
) < 0) {
272 printf("failed: cannot create symlink: %s\n",
275 printf("succeeded!\n");
276 hammer_cmd_pseudofs_update(av
, ac
);
283 hammer_cmd_pseudofs_destroy(char **av
, int ac
)
285 struct hammer_ioc_pseudofs_rw pfs
;
292 bzero(&pfs
, sizeof(pfs
));
293 fd
= getpfs(&pfs
, av
[0]);
295 if (pfs
.pfs_id
== 0) {
296 fprintf(stderr
, "You cannot destroy PFS#0\n");
299 printf("You have requested that PFS#%d (%s) be destroyed\n",
300 pfs
.pfs_id
, pfs
.ondisk
->label
);
301 printf("This will irrevocably destroy all data on this PFS!!!!!\n");
302 printf("Do you really want to do this? ");
305 fprintf(stderr
, "No action taken on PFS#%d\n", pfs
.pfs_id
);
309 if ((pfs
.ondisk
->mirror_flags
& HAMMER_PFSD_SLAVE
) == 0) {
310 printf("This PFS is currently setup as a MASTER!\n");
311 printf("Are you absolutely sure you want to destroy it? ");
314 fprintf(stderr
, "No action taken on PFS#%d\n",
320 printf("Destroying PFS #%d (%s) in ", pfs
.pfs_id
, pfs
.ondisk
->label
);
321 for (i
= 5; i
; --i
) {
326 printf(".. starting destruction pass\n");
330 * Set the sync_beg_tid and sync_end_tid's to 1, once we start the
331 * RMR the PFS is basically destroyed even if someone ^C's it.
333 pfs
.ondisk
->mirror_flags
|= HAMMER_PFSD_SLAVE
;
334 pfs
.ondisk
->reserved01
= -1;
335 pfs
.ondisk
->sync_beg_tid
= 1;
336 pfs
.ondisk
->sync_end_tid
= 1;
338 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) < 0) {
339 fprintf(stderr
, "Unable to update the PFS configuration: %s\n",
345 * Ok, do it. Remove the softlink on success.
347 if (ioctl(fd
, HAMMERIOC_RMR_PSEUDOFS
, &pfs
) == 0) {
348 printf("pfs-destroy of PFS#%d succeeded!\n", pfs
.pfs_id
);
349 if (lstat(av
[0], &st
) == 0 && S_ISLNK(st
.st_mode
)) {
350 if (remove(av
[0]) < 0) {
351 fprintf(stderr
, "Unable to remove softlink: %s "
352 "(but the PFS has been destroyed)\n",
354 /* exit status 0 anyway */
358 printf("pfs-destroy of PFS#%d failed: %s\n",
359 pfs
.pfs_id
, strerror(errno
));
364 hammer_cmd_pseudofs_upgrade(char **av
, int ac
)
366 struct hammer_ioc_pseudofs_rw pfs
;
371 bzero(&pfs
, sizeof(pfs
));
372 fd
= getpfs(&pfs
, av
[0]);
374 if (pfs
.pfs_id
== 0) {
375 fprintf(stderr
, "You cannot upgrade PFS#0"
376 " (It should already be a master)\n");
379 if (ioctl(fd
, HAMMERIOC_UPG_PSEUDOFS
, &pfs
) == 0) {
380 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
381 pfs
.pfs_id
, pfs
.ondisk
->label
);
383 fprintf(stderr
, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
384 pfs
.pfs_id
, pfs
.ondisk
->label
, strerror(errno
));
389 hammer_cmd_pseudofs_downgrade(char **av
, int ac
)
391 struct hammer_ioc_pseudofs_rw pfs
;
396 bzero(&pfs
, sizeof(pfs
));
397 fd
= getpfs(&pfs
, av
[0]);
399 if (pfs
.pfs_id
== 0) {
400 fprintf(stderr
, "You cannot downgrade PFS#0\n");
404 if (ioctl(fd
, HAMMERIOC_DGD_PSEUDOFS
, &pfs
) == 0) {
405 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
406 pfs
.pfs_id
, pfs
.ondisk
->label
);
408 fprintf(stderr
, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
409 pfs
.pfs_id
, pfs
.ondisk
->label
, strerror(errno
));
414 hammer_cmd_pseudofs_update(char **av
, int ac
)
416 struct hammer_ioc_pseudofs_rw pfs
;
421 bzero(&pfs
, sizeof(pfs
));
422 fd
= getpfs(&pfs
, av
[0]);
424 printf("%s\n", av
[0]);
427 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) == 0) {
428 parse_pfsd_options(av
+ 1, ac
- 1, pfs
.ondisk
);
429 if ((pfs
.ondisk
->mirror_flags
& HAMMER_PFSD_SLAVE
) &&
431 printf("The real mount point cannot be made a PFS "
432 "slave, only PFS sub-directories can be made "
436 pfs
.bytes
= sizeof(*pfs
.ondisk
);
437 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) == 0) {
438 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) == 0) {
439 dump_pfsd(pfs
.ondisk
);
441 printf("Unable to retrieve pfs configuration "
442 "after successful update: %s\n",
447 printf("Unable to adjust pfs configuration: %s\n",
455 init_pfsd(hammer_pseudofs_data_t pfsd
, int is_slave
)
459 pfsd
->sync_beg_tid
= 1;
460 pfsd
->sync_end_tid
= 1;
461 pfsd
->sync_beg_ts
= 0;
462 pfsd
->sync_end_ts
= 0;
463 uuid_create(&pfsd
->shared_uuid
, &status
);
464 uuid_create(&pfsd
->unique_uuid
, &status
);
466 pfsd
->mirror_flags
|= HAMMER_PFSD_SLAVE
;
471 dump_pfsd(hammer_pseudofs_data_t pfsd
)
476 printf(" sync-beg-tid=0x%016llx\n", pfsd
->sync_beg_tid
);
477 printf(" sync-end-tid=0x%016llx\n", pfsd
->sync_end_tid
);
478 uuid_to_string(&pfsd
->shared_uuid
, &str
, &status
);
479 printf(" shared-uuid=%s\n", str
);
480 uuid_to_string(&pfsd
->unique_uuid
, &str
, &status
);
481 printf(" unique-uuid=%s\n", str
);
482 if (pfsd
->mirror_flags
& HAMMER_PFSD_SLAVE
) {
485 printf(" label=\"%s\"\n", pfsd
->label
);
486 if (pfsd
->snapshots
[0])
487 printf(" snapshots=\"%s\"\n", pfsd
->snapshots
);
488 if (pfsd
->prune_min
< (60 * 60 * 24)) {
489 printf(" prune-min=%02d:%02d:%02d\n",
490 pfsd
->prune_min
/ 60 / 60 % 24,
491 pfsd
->prune_min
/ 60 % 60,
492 pfsd
->prune_min
% 60);
493 } else if (pfsd
->prune_min
% (60 * 60 * 24)) {
494 printf(" prune-min=%dd/%02d:%02d:%02d\n",
495 pfsd
->prune_min
/ 60 / 60 / 24,
496 pfsd
->prune_min
/ 60 / 60 % 24,
497 pfsd
->prune_min
/ 60 % 60,
498 pfsd
->prune_min
% 60);
500 printf(" prune-min=%dd\n", pfsd
->prune_min
/ 60 / 60 / 24);
503 if (pfsd
->mirror_flags
& HAMMER_PFSD_SLAVE
) {
504 printf(" operating as a SLAVE\n");
505 if (pfsd
->snapshots
[0] == 0)
506 printf(" snapshots directory not set for slave\n");
508 printf(" operating as a MASTER\n");
509 if (pfsd
->snapshots
[0] == 0) {
510 printf(" snapshots dir for master "
511 "defaults to <fs>/snapshots\n");
517 parse_pfsd_options(char **av
, int ac
, hammer_pseudofs_data_t pfsd
)
526 if ((ptr
= strchr(cmd
, '=')) != NULL
)
530 * Basic assignment value test
534 "option %s requires an assignment\n",
540 if (strcmp(cmd
, "sync-beg-tid") == 0) {
541 pfsd
->sync_beg_tid
= strtoull(ptr
, NULL
, 16);
542 } else if (strcmp(cmd
, "sync-end-tid") == 0) {
543 pfsd
->sync_end_tid
= strtoull(ptr
, NULL
, 16);
544 } else if (strcmp(cmd
, "shared-uuid") == 0) {
545 uuid_from_string(ptr
, &pfsd
->shared_uuid
, &status
);
546 } else if (strcmp(cmd
, "unique-uuid") == 0) {
547 uuid_from_string(ptr
, &pfsd
->unique_uuid
, &status
);
548 } else if (strcmp(cmd
, "label") == 0) {
550 if (ptr
[0] == '"' && ptr
[len
-1] == '"') {
553 } else if (ptr
[0] == '"') {
555 "option %s: malformed string\n",
559 snprintf(pfsd
->label
, sizeof(pfsd
->label
), "%s", ptr
);
560 } else if (strcmp(cmd
, "snapshots") == 0) {
564 "option %s: '%s' must be an "
565 "absolute path\n", cmd
, ptr
);
568 "use 'snapshots-clear' "
569 "to unset snapshots dir\n");
573 if (len
>= (int)sizeof(pfsd
->snapshots
)) {
575 "option %s: path too long, %d "
576 "character limit\n", cmd
, len
);
579 snprintf(pfsd
->snapshots
, sizeof(pfsd
->snapshots
),
581 } else if (strcmp(cmd
, "snapshots-clear") == 0) {
582 pfsd
->snapshots
[0] = 0;
583 } else if (strcmp(cmd
, "prune-min") == 0) {
584 pfsd
->prune_min
= timetosecs(ptr
);
585 if (pfsd
->prune_min
< 0) {
587 "option %s: illegal time spec, "
588 "use Nd or [Nd/]hh[:mm[:ss]]\n", ptr
);
592 fprintf(stderr
, "invalid option: %s\n", cmd
);
595 if (status
!= uuid_s_ok
) {
596 fprintf(stderr
, "option %s: error parsing uuid %s\n",
607 pseudofs_usage(int code
)
610 "hammer pfs-status <dirpath> ...\n"
611 "hammer pfs-master <dirpath> [options]\n"
612 "hammer pfs-slave <dirpath> [options]\n"
613 "hammer pfs-update <dirpath> [options]\n"
614 "hammer pfs-upgrade <dirpath>\n"
615 "hammer pfs-downgrade <dirpath>\n"
616 "hammer pfs-destroy <dirpath>\n"
619 " sync-beg-tid=0x16llx\n"
620 " sync-end-tid=0x16llx\n"
621 " shared-uuid=0x16llx\n"
622 " unique-uuid=0x16llx\n"
623 " label=\"string\"\n"
624 " snapshots=\"/path\"\n"
626 " prune-min=[Nd/][hh[:mm[:ss]]]\n"
638 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
641 while (len
&& (buf
[len
-1] == '\n' || buf
[len
-1] == '\r'))
644 if (strcmp(buf
, "y") == 0 ||
645 strcmp(buf
, "yes") == 0 ||
646 strcmp(buf
, "Y") == 0 ||
647 strcmp(buf
, "YES") == 0) {
654 * Convert time in the form [Nd/][hh[:mm[:ss]]] to seconds.
656 * Return -1 if a parse error occurs.
657 * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
661 timetosecs(char *str
)
671 n
= strtol(str
, &ptr
, 10);
678 n
= strtol(ptr
+ 1, &ptr
, 10);
686 n
= strtol(ptr
+ 1, &ptr
, 10);
691 n
= strtol(ptr
+ 1, &ptr
, 10);
699 v
= days
* 24 * 60 * 60 + hrs
* 60 * 60 + mins
* 60 + secs
;