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 * Return a directory that contains path.
66 * A caller need to free the returned pointer.
67 * This behaves differently from dirname(3) with trailing /.
70 getdir(const char *path
)
74 dirpath
= strdup(path
);
76 if (dirpath
[strlen(dirpath
) - 1] == '/') {
77 while (dirpath
[strlen(dirpath
) - 1] == '/')
78 dirpath
[strlen(dirpath
) - 1] = 0;
79 } else if (strrchr(dirpath
, '/')) {
80 *strrchr(dirpath
, '/') = 0;
83 dirpath
= strdup(".");
86 if (strlen(dirpath
) == 0) {
88 dirpath
= strdup("/");
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
, char *path
)
104 clrpfs(pfs
, NULL
, -1);
107 * Trailing '/' must be removed so that upon pfs-destroy
108 * the symlink can be deleted without problems.
109 * Root directory (/) must be excluded from this.
111 p
= path
+ (int)strlen(path
) - 1;
113 while (p
!= path
&& *p
== '/')
116 fd
= scanpfsid(pfs
, path
);
119 * Once it comes here the hammer command may fail even if
120 * this function returns valid file descriptor, however this
121 * open still needs to be here as some PFS commands can take
122 * a regular file or directory path (e.g. hammer pfs-status).
124 fd
= open(path
, O_RDONLY
);
126 fprintf(stderr
, "Failed to open %s\n", path
);
132 * If pfs.pfs_id has been set to non -1, the file descriptor fd
133 * could be any fd of HAMMER inodes since HAMMERIOC_GET_PSEUDOFS
134 * doesn't depend on inode attributes if it's set to a valid id.
136 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, pfs
) < 0) {
137 fprintf(stderr
, "Cannot access PFS %s: %s\n",
138 path
, strerror(errno
));
145 * Extract the PFS id from path. Return a file descriptor of
146 * the parent directory which is expected to be located under
147 * the root filesystem (not another PFS).
150 scanpfsid(struct hammer_ioc_pseudofs_rw
*pfs
, const char *path
)
159 if (stat(path
, &st
)) {
160 /* possibly slave PFS */
161 } else if (S_ISDIR(st
.st_mode
)) {
162 /* possibly master or slave PFS */
164 return(-1); /* neither */
168 * If the path is a link read the link.
170 if (lstat(path
, &st
) == 0 && S_ISLNK(st
.st_mode
)) {
171 bzero(buf
, sizeof(buf
));
172 if (readlink(path
, buf
, sizeof(buf
) - 1) < 0)
180 * The symlink created by pfs-master|slave is just a symlink.
181 * One could happen to remove a symlink and relink PFS as
182 * # ln -s ./@@-1:00001 ./link
183 * which results PFS having something extra before @@.
184 * One could also directly use the PFS and results the same.
185 * Get rid of it before we extract the PFS id.
187 if (strchr(p
, '/')) {
194 * Test and extract the PFS id from the link, and then open
195 * and return fd of the parent directory of that link. fd is
196 * not so important as long as the right PFS id is extracted.
198 * "@@%jx:%d" convers both "@@-1:%05d" format for master PFS
199 * and "@@0x%016jx:%05d" format for slave PFS.
201 dirpath
= getdir(path
);
202 if (sscanf(p
, "@@%jx:%d", &dummy_tid
, &pfs
->pfs_id
) == 2)
203 fd
= open(dirpath
, O_RDONLY
);
210 relpfs(int fd
, struct hammer_ioc_pseudofs_rw
*pfs
)
221 print_pfs_status(char *path
)
223 struct hammer_ioc_pseudofs_rw pfs
;
226 fd
= getpfs(&pfs
, path
);
227 printf("%s\t", path
);
228 if (fd
< 0 || ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
229 printf("Invalid PFS path %s\n", path
);
231 printf("PFS#%d {\n", pfs
.pfs_id
);
232 dump_pfsd(pfs
.ondisk
, fd
);
243 hammer_cmd_pseudofs_status(char **av
, int ac
)
248 char buf
[2] = "."; /* can't be readonly string */
249 print_pfs_status(buf
);
253 for (i
= 0; i
< ac
; ++i
)
254 print_pfs_status(av
[i
]);
258 hammer_cmd_pseudofs_create(char **av
, int ac
, int is_slave
)
260 struct hammer_ioc_pseudofs_rw pfs
;
261 struct hammer_pseudofs_data pfsd
;
273 if (lstat(path
, &st
) == 0) {
274 fprintf(stderr
, "Cannot create %s, file exists!\n", path
);
279 * Figure out the directory prefix, taking care of degenerate
282 dirpath
= getdir(path
);
283 fd
= open(dirpath
, O_RDONLY
);
285 fprintf(stderr
, "Cannot open directory %s\n", dirpath
);
290 * Avoid foot-shooting. Don't let the user create a PFS
291 * softlink via a PFS. PFS softlinks may only be accessed
292 * via the master filesystem. Checking it here ensures
293 * other PFS commands access PFS under the master filesystem.
295 clrpfs(&pfs
, &pfsd
, -1);
297 ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
);
298 if (pfs
.pfs_id
!= HAMMER_ROOT_PFSID
) {
300 "You are attempting to access a PFS softlink "
301 "from a PFS. It may not represent the PFS\n"
302 "on the main filesystem mount that you "
303 "expect! You may only access PFS softlinks\n"
304 "via the main filesystem mount!\n");
309 for (pfs_id
= 0; pfs_id
< HAMMER_MAX_PFS
; ++pfs_id
) {
310 clrpfs(&pfs
, &pfsd
, pfs_id
);
311 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
316 if (pfs_id
== HAMMER_MAX_PFS
) {
317 fprintf(stderr
, "Cannot create %s, all PFSs in use\n", path
);
319 } else if (pfs_id
== HAMMER_ROOT_PFSID
) {
320 fprintf(stderr
, "Fatal error: PFS#%d must exist\n",
325 if (error
!= ENOENT
) {
326 fprintf(stderr
, "Cannot create %s, got %s during scan\n",
327 path
, strerror(error
));
334 printf("Creating PFS#%d\t", pfs_id
);
335 clrpfs(&pfs
, &pfsd
, pfs_id
);
336 init_pfsd(&pfsd
, is_slave
);
338 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) < 0) {
339 printf("failed: %s\n", strerror(errno
));
341 /* special symlink, must be exactly 10 characters */
342 asprintf(&linkpath
, "@@PFS%05d", pfs_id
);
343 if (symlink(linkpath
, path
) < 0) {
344 printf("failed: cannot create symlink: %s\n",
347 printf("succeeded!\n");
348 hammer_cmd_pseudofs_update(av
, ac
);
356 hammer_cmd_pseudofs_destroy(char **av
, int ac
)
358 struct hammer_ioc_pseudofs_rw pfs
;
365 fd
= getpfs(&pfs
, av
[0]);
367 if (pfs
.pfs_id
== HAMMER_ROOT_PFSID
) {
368 fprintf(stderr
, "You cannot destroy PFS#0\n");
371 printf("You have requested that PFS#%d (%s) be destroyed\n",
372 pfs
.pfs_id
, pfs
.ondisk
->label
);
373 printf("This will irrevocably destroy all data on this PFS!!!!!\n");
374 printf("Do you really want to do this? ");
377 fprintf(stderr
, "No action taken on PFS#%d\n", pfs
.pfs_id
);
381 if (hammer_is_pfs_master(pfs
.ondisk
)) {
382 printf("This PFS is currently setup as a MASTER!\n");
383 printf("Are you absolutely sure you want to destroy it? ");
386 fprintf(stderr
, "No action taken on PFS#%d\n",
392 printf("Destroying PFS#%d (%s)", pfs
.pfs_id
, pfs
.ondisk
->label
);
397 for (i
= 5; i
; --i
) {
402 printf(".. starting destruction pass\n");
406 * Remove the softlink on success.
408 if (ioctl(fd
, HAMMERIOC_RMR_PSEUDOFS
, &pfs
) == 0) {
409 printf("pfs-destroy of PFS#%d succeeded!\n", pfs
.pfs_id
);
410 if (lstat(av
[0], &st
) == 0 && S_ISLNK(st
.st_mode
)) {
411 if (remove(av
[0]) < 0) {
412 fprintf(stderr
, "Unable to remove softlink: %s "
413 "(but the PFS has been destroyed)\n",
415 /* exit status 0 anyway */
419 printf("pfs-destroy of PFS#%d failed: %s\n",
420 pfs
.pfs_id
, strerror(errno
));
426 hammer_cmd_pseudofs_upgrade(char **av
, int ac
)
428 struct hammer_ioc_pseudofs_rw pfs
;
433 fd
= getpfs(&pfs
, av
[0]);
435 if (pfs
.pfs_id
== HAMMER_ROOT_PFSID
) {
436 fprintf(stderr
, "You cannot upgrade PFS#0"
437 " (It should already be a master)\n");
439 } else if (hammer_is_pfs_master(pfs
.ondisk
)) {
440 printf("It is already a master\n");
444 if (ioctl(fd
, HAMMERIOC_UPG_PSEUDOFS
, &pfs
) == 0) {
445 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
446 pfs
.pfs_id
, pfs
.ondisk
->label
);
448 fprintf(stderr
, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
449 pfs
.pfs_id
, pfs
.ondisk
->label
, strerror(errno
));
455 hammer_cmd_pseudofs_downgrade(char **av
, int ac
)
457 struct hammer_ioc_pseudofs_rw pfs
;
462 fd
= getpfs(&pfs
, av
[0]);
464 if (pfs
.pfs_id
== HAMMER_ROOT_PFSID
) {
465 fprintf(stderr
, "You cannot downgrade PFS#0\n");
467 } else if (hammer_is_pfs_slave(pfs
.ondisk
)) {
468 printf("It is already a slave\n");
472 if (ioctl(fd
, HAMMERIOC_DGD_PSEUDOFS
, &pfs
) == 0) {
473 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
474 pfs
.pfs_id
, pfs
.ondisk
->label
);
476 fprintf(stderr
, "pfs-downgrade of PFS#%d (%s) failed: %s\n",
477 pfs
.pfs_id
, pfs
.ondisk
->label
, strerror(errno
));
483 hammer_cmd_pseudofs_update(char **av
, int ac
)
485 struct hammer_ioc_pseudofs_rw pfs
;
490 fd
= getpfs(&pfs
, av
[0]);
492 printf("%s\n", av
[0]);
495 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) == 0) {
496 parse_pfsd_options(av
+ 1, ac
- 1, pfs
.ondisk
);
497 if (hammer_is_pfs_slave(pfs
.ondisk
) &&
498 pfs
.pfs_id
== HAMMER_ROOT_PFSID
) {
499 printf("The real mount point cannot be made a PFS "
500 "slave, only PFS sub-directories can be made "
504 pfs
.bytes
= sizeof(*pfs
.ondisk
);
505 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) == 0) {
506 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) == 0) {
507 dump_pfsd(pfs
.ondisk
, fd
);
509 printf("Unable to retrieve PFS configuration "
510 "after successful update: %s\n",
515 printf("Unable to adjust PFS configuration: %s\n",
524 init_pfsd(hammer_pseudofs_data_t pfsd
, int is_slave
)
528 bzero(pfsd
, sizeof(*pfsd
));
529 pfsd
->sync_beg_tid
= 1;
530 pfsd
->sync_end_tid
= 1;
531 pfsd
->sync_beg_ts
= 0;
532 pfsd
->sync_end_ts
= 0;
533 uuid_create(&pfsd
->shared_uuid
, &status
);
534 uuid_create(&pfsd
->unique_uuid
, &status
);
536 pfsd
->mirror_flags
|= HAMMER_PFSD_SLAVE
;
540 dump_pfsd(hammer_pseudofs_data_t pfsd
, int fd
)
542 struct hammer_ioc_version version
;
546 printf(" sync-beg-tid=0x%016jx\n", (uintmax_t)pfsd
->sync_beg_tid
);
547 printf(" sync-end-tid=0x%016jx\n", (uintmax_t)pfsd
->sync_end_tid
);
548 uuid_to_string(&pfsd
->shared_uuid
, &str
, &status
);
549 printf(" shared-uuid=%s\n", str
);
551 uuid_to_string(&pfsd
->unique_uuid
, &str
, &status
);
552 printf(" unique-uuid=%s\n", str
);
554 printf(" label=\"%s\"\n", pfsd
->label
);
555 if (pfsd
->snapshots
[0])
556 printf(" snapshots=\"%s\"\n", pfsd
->snapshots
);
557 if (pfsd
->prune_min
< (60 * 60 * 24)) {
558 printf(" prune-min=%02d:%02d:%02d\n",
559 pfsd
->prune_min
/ 60 / 60 % 24,
560 pfsd
->prune_min
/ 60 % 60,
561 pfsd
->prune_min
% 60);
562 } else if (pfsd
->prune_min
% (60 * 60 * 24)) {
563 printf(" prune-min=%dd/%02d:%02d:%02d\n",
564 pfsd
->prune_min
/ 60 / 60 / 24,
565 pfsd
->prune_min
/ 60 / 60 % 24,
566 pfsd
->prune_min
/ 60 % 60,
567 pfsd
->prune_min
% 60);
569 printf(" prune-min=%dd\n", pfsd
->prune_min
/ 60 / 60 / 24);
572 if (hammer_is_pfs_slave(pfsd
)) {
573 printf(" operating as a SLAVE\n");
575 printf(" operating as a MASTER\n");
579 * Snapshots directory cannot be shown when there is no fd since
580 * hammer version can't be retrieved. mirror-dump passes -1 because
581 * its input came from mirror-read output thus no path is available
584 if (fd
>= 0 && pfsd
->snapshots
[0] == 0) {
585 bzero(&version
, sizeof(version
));
586 if (ioctl(fd
, HAMMERIOC_GET_VERSION
, &version
) < 0)
588 if (version
.cur_version
< 3) {
589 if (hammer_is_pfs_slave(pfsd
)) {
590 printf(" snapshots directory not set for "
593 printf(" snapshots directory for master "
594 "defaults to <pfs>/snapshots\n");
597 printf(" snapshots directory defaults to "
598 "/var/hammer/<pfs>\n");
604 parse_pfsd_options(char **av
, int ac
, hammer_pseudofs_data_t pfsd
)
613 if ((ptr
= strchr(cmd
, '=')) != NULL
)
617 * Basic assignment value test
621 "option %s requires an assignment\n",
627 if (strcmp(cmd
, "sync-beg-tid") == 0) {
628 pfsd
->sync_beg_tid
= strtoull(ptr
, NULL
, 16);
629 } else if (strcmp(cmd
, "sync-end-tid") == 0) {
630 pfsd
->sync_end_tid
= strtoull(ptr
, NULL
, 16);
631 } else if (strcmp(cmd
, "shared-uuid") == 0) {
632 uuid_from_string(ptr
, &pfsd
->shared_uuid
, &status
);
633 } else if (strcmp(cmd
, "unique-uuid") == 0) {
634 uuid_from_string(ptr
, &pfsd
->unique_uuid
, &status
);
635 } else if (strcmp(cmd
, "label") == 0) {
637 if (ptr
[0] == '"' && ptr
[len
-1] == '"') {
640 } else if (ptr
[0] == '"') {
642 "option %s: malformed string\n",
646 snprintf(pfsd
->label
, sizeof(pfsd
->label
), "%s", ptr
);
647 } else if (strcmp(cmd
, "snapshots") == 0) {
651 "option %s: '%s' must be an "
652 "absolute path\n", cmd
, ptr
);
655 "use 'snapshots-clear' "
656 "to unset snapshots dir\n");
660 if (len
>= (int)sizeof(pfsd
->snapshots
)) {
662 "option %s: path too long, %d "
663 "character limit\n", cmd
, len
);
666 snprintf(pfsd
->snapshots
, sizeof(pfsd
->snapshots
),
668 } else if (strcmp(cmd
, "snapshots-clear") == 0) {
669 pfsd
->snapshots
[0] = 0;
670 } else if (strcmp(cmd
, "prune-min") == 0) {
671 pfsd
->prune_min
= timetosecs(ptr
);
672 if (pfsd
->prune_min
< 0) {
674 "option %s: illegal time spec, "
675 "use Nd or [Nd/]hh[:mm[:ss]]\n", ptr
);
679 fprintf(stderr
, "invalid option: %s\n", cmd
);
682 if (status
!= uuid_s_ok
) {
683 fprintf(stderr
, "option %s: error parsing uuid %s\n",
694 pseudofs_usage(int code
)
697 "hammer pfs-status <dirpath> ...\n"
698 "hammer pfs-master <dirpath> [options]\n"
699 "hammer pfs-slave <dirpath> [options]\n"
700 "hammer pfs-update <dirpath> [options]\n"
701 "hammer pfs-upgrade <dirpath>\n"
702 "hammer pfs-downgrade <dirpath>\n"
703 "hammer pfs-destroy <dirpath>\n"
706 " sync-beg-tid=0x16llx\n"
707 " sync-end-tid=0x16llx\n"
708 " shared-uuid=0x16llx\n"
709 " unique-uuid=0x16llx\n"
710 " label=\"string\"\n"
711 " snapshots=\"/path\"\n"
714 " prune-min=[Nd/]hh[:mm[:ss]]\n"
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
;