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
, const 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 * 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
, "@@%jx:%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
, fd
);
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
, fd
);
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
, int fd
)
473 struct hammer_ioc_version version
;
477 printf(" sync-beg-tid=0x%016jx\n", (uintmax_t)pfsd
->sync_beg_tid
);
478 printf(" sync-end-tid=0x%016jx\n", (uintmax_t)pfsd
->sync_end_tid
);
479 uuid_to_string(&pfsd
->shared_uuid
, &str
, &status
);
480 printf(" shared-uuid=%s\n", str
);
481 uuid_to_string(&pfsd
->unique_uuid
, &str
, &status
);
482 printf(" unique-uuid=%s\n", str
);
483 if (pfsd
->mirror_flags
& HAMMER_PFSD_SLAVE
) {
486 printf(" label=\"%s\"\n", pfsd
->label
);
487 if (pfsd
->snapshots
[0])
488 printf(" snapshots=\"%s\"\n", pfsd
->snapshots
);
489 if (pfsd
->prune_min
< (60 * 60 * 24)) {
490 printf(" prune-min=%02d:%02d:%02d\n",
491 pfsd
->prune_min
/ 60 / 60 % 24,
492 pfsd
->prune_min
/ 60 % 60,
493 pfsd
->prune_min
% 60);
494 } else if (pfsd
->prune_min
% (60 * 60 * 24)) {
495 printf(" prune-min=%dd/%02d:%02d:%02d\n",
496 pfsd
->prune_min
/ 60 / 60 / 24,
497 pfsd
->prune_min
/ 60 / 60 % 24,
498 pfsd
->prune_min
/ 60 % 60,
499 pfsd
->prune_min
% 60);
501 printf(" prune-min=%dd\n", pfsd
->prune_min
/ 60 / 60 / 24);
504 if (pfsd
->mirror_flags
& HAMMER_PFSD_SLAVE
) {
505 printf(" operating as a SLAVE\n");
507 printf(" operating as a MASTER\n");
510 if (pfsd
->snapshots
[0] == 0) {
511 bzero(&version
, sizeof(version
));
512 if (ioctl(fd
, HAMMERIOC_GET_VERSION
, &version
) < 0)
514 if (version
.cur_version
< 3) {
515 if (pfsd
->mirror_flags
& HAMMER_PFSD_SLAVE
) {
516 printf(" snapshots directory not set for "
519 printf(" snapshots directory for master "
520 "defaults to <pfs>/snapshots\n");
523 printf(" snapshots directory defaults to "
524 "/var/hammer/<pfs>\n");
530 parse_pfsd_options(char **av
, int ac
, hammer_pseudofs_data_t pfsd
)
539 if ((ptr
= strchr(cmd
, '=')) != NULL
)
543 * Basic assignment value test
547 "option %s requires an assignment\n",
553 if (strcmp(cmd
, "sync-beg-tid") == 0) {
554 pfsd
->sync_beg_tid
= strtoull(ptr
, NULL
, 16);
555 } else if (strcmp(cmd
, "sync-end-tid") == 0) {
556 pfsd
->sync_end_tid
= strtoull(ptr
, NULL
, 16);
557 } else if (strcmp(cmd
, "shared-uuid") == 0) {
558 uuid_from_string(ptr
, &pfsd
->shared_uuid
, &status
);
559 } else if (strcmp(cmd
, "unique-uuid") == 0) {
560 uuid_from_string(ptr
, &pfsd
->unique_uuid
, &status
);
561 } else if (strcmp(cmd
, "label") == 0) {
563 if (ptr
[0] == '"' && ptr
[len
-1] == '"') {
566 } else if (ptr
[0] == '"') {
568 "option %s: malformed string\n",
572 snprintf(pfsd
->label
, sizeof(pfsd
->label
), "%s", ptr
);
573 } else if (strcmp(cmd
, "snapshots") == 0) {
577 "option %s: '%s' must be an "
578 "absolute path\n", cmd
, ptr
);
581 "use 'snapshots-clear' "
582 "to unset snapshots dir\n");
586 if (len
>= (int)sizeof(pfsd
->snapshots
)) {
588 "option %s: path too long, %d "
589 "character limit\n", cmd
, len
);
592 snprintf(pfsd
->snapshots
, sizeof(pfsd
->snapshots
),
594 } else if (strcmp(cmd
, "snapshots-clear") == 0) {
595 pfsd
->snapshots
[0] = 0;
596 } else if (strcmp(cmd
, "prune-min") == 0) {
597 pfsd
->prune_min
= timetosecs(ptr
);
598 if (pfsd
->prune_min
< 0) {
600 "option %s: illegal time spec, "
601 "use Nd or [Nd/]hh[:mm[:ss]]\n", ptr
);
605 fprintf(stderr
, "invalid option: %s\n", cmd
);
608 if (status
!= uuid_s_ok
) {
609 fprintf(stderr
, "option %s: error parsing uuid %s\n",
620 pseudofs_usage(int code
)
623 "hammer pfs-status <dirpath> ...\n"
624 "hammer pfs-master <dirpath> [options]\n"
625 "hammer pfs-slave <dirpath> [options]\n"
626 "hammer pfs-update <dirpath> [options]\n"
627 "hammer pfs-upgrade <dirpath>\n"
628 "hammer pfs-downgrade <dirpath>\n"
629 "hammer pfs-destroy <dirpath>\n"
632 " sync-beg-tid=0x16llx\n"
633 " sync-end-tid=0x16llx\n"
634 " shared-uuid=0x16llx\n"
635 " unique-uuid=0x16llx\n"
636 " label=\"string\"\n"
637 " snapshots=\"/path\"\n"
640 " prune-min=[Nd/]hh[:mm[:ss]]\n"
652 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
655 while (len
&& (buf
[len
-1] == '\n' || buf
[len
-1] == '\r'))
658 if (strcmp(buf
, "y") == 0 ||
659 strcmp(buf
, "yes") == 0 ||
660 strcmp(buf
, "Y") == 0 ||
661 strcmp(buf
, "YES") == 0) {
668 * Convert time in the form [Nd/]hh[:mm[:ss]] to seconds.
670 * Return -1 if a parse error occurs.
671 * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
675 timetosecs(char *str
)
685 n
= strtol(str
, &ptr
, 10);
692 n
= strtol(ptr
+ 1, &ptr
, 10);
700 n
= strtol(ptr
+ 1, &ptr
, 10);
705 n
= strtol(ptr
+ 1, &ptr
, 10);
713 v
= days
* 24 * 60 * 60 + hrs
* 60 * 60 + mins
* 60 + secs
;