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 int scanpfsid(struct hammer_ioc_pseudofs_rw
*pfs
, const char *path
);
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 int timetosecs(char *str
);
46 clrpfs(struct hammer_ioc_pseudofs_rw
*pfs
, hammer_pseudofs_data_t pfsd
,
49 bzero(pfs
, sizeof(*pfs
));
54 pfs
->ondisk
= malloc(sizeof(*pfs
->ondisk
));
55 bzero(pfs
->ondisk
, sizeof(*pfs
->ondisk
));
58 pfs
->bytes
= sizeof(*pfs
->ondisk
);
59 pfs
->version
= HAMMER_IOC_PSEUDOFS_VERSION
;
63 * If path is a symlink, return strdup'd path.
64 * If it's a directory via symlink, strip trailing /
65 * from strdup'd path and return the symlink.
69 getlink(const char *path
)
77 linkpath
= strdup(path
);
79 if (S_ISDIR(st
.st_mode
)) {
80 i
= strlen(linkpath
) - 1;
81 while (i
> 0 && linkpath
[i
] == '/')
85 if (S_ISLNK(st
.st_mode
))
93 * Calculate the PFS id given a path to a file/directory or
94 * a @@%llx:%d softlink.
97 getpfs(struct hammer_ioc_pseudofs_rw
*pfs
, const char *path
)
102 clrpfs(pfs
, NULL
, -1);
105 * Extract the PFS id.
106 * dirname(path) is supposed to be a directory in root PFS.
108 * strips trailing / first if any.
110 tmpbuf
= strdup(path
);
111 if (scanpfsid(pfs
, path
) == 0)
112 path
= dirname(tmpbuf
);
115 * Open the path regardless of scanpfsid() result, since some
116 * commands can take a regular file/directory (e.g. pfs-status).
118 fd
= open(path
, O_RDONLY
);
120 err(1, "Failed to open %s", path
);
125 * If pfs.pfs_id has been set to non -1, the file descriptor fd
126 * could be any fd of HAMMER inodes since HAMMERIOC_GET_PSEUDOFS
127 * doesn't depend on inode attributes if it's set to a valid id.
129 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, pfs
) < 0) {
130 err(1, "Cannot access %s", path
);
140 * Extract the PFS id from path.
144 scanpfsid(struct hammer_ioc_pseudofs_rw
*pfs
, const char *path
)
153 ; /* possibly slave PFS */
154 else if (S_ISDIR(st
.st_mode
))
155 ; /* possibly master or slave PFS */
157 return(-1); /* neither */
159 linkpath
= getlink(path
);
162 * Read the symlink assuming it's a link to PFS.
164 bzero(buf
, sizeof(buf
));
165 if (readlink(linkpath
, buf
, sizeof(buf
) - 1) < 0) {
174 * The symlink created by pfs-master|slave is just a symlink.
175 * One could happen to remove a symlink and relink PFS as
176 * # ln -s ./@@-1:00001 ./link
177 * which results PFS having something extra before @@.
178 * One could also directly use the PFS and results the same.
179 * Get rid of it before we extract the PFS id.
181 tmpbuf
= strdup(path
);
182 if (strchr(path
, '/')) {
183 path
= basename(tmpbuf
); /* strips trailing / first if any */
191 * Test and extract the PFS id from the link.
192 * "@@%jx:%d" covers both "@@-1:%05d" format for master PFS
193 * and "@@0x%016jx:%05d" format for slave PFS.
195 if (sscanf(path
, "@@%jx:%d", &dummy_tid
, &pfs
->pfs_id
) == 2) {
197 assert(pfs
->pfs_id
> 0);
206 relpfs(int fd
, struct hammer_ioc_pseudofs_rw
*pfs
)
218 print_pfs_status(char *path
)
220 struct hammer_ioc_pseudofs_rw pfs
;
223 fd
= getpfs(&pfs
, path
);
224 printf("%s\t", path
);
225 if (fd
< 0 || ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
226 printf("Invalid PFS path %s\n", path
);
228 printf("PFS#%d {\n", pfs
.pfs_id
);
229 dump_pfsd(pfs
.ondisk
, fd
);
240 hammer_cmd_pseudofs_status(char **av
, int ac
)
245 char buf
[2] = "."; /* can't be readonly string */
246 print_pfs_status(buf
);
250 for (i
= 0; i
< ac
; ++i
)
251 print_pfs_status(av
[i
]);
255 hammer_cmd_pseudofs_create(char **av
, int ac
, int is_slave
)
257 struct hammer_ioc_pseudofs_rw pfs
;
258 struct hammer_pseudofs_data pfsd
;
272 if (lstat(path
, &st
) == 0) {
273 errx(1, "Cannot create %s, file exists!", path
);
275 } else if (path
[strlen(path
) - 1] == '/') {
276 errx(1, "Invalid PFS path %s with trailing /", path
);
281 * Figure out the directory prefix, taking care of degenerate
284 tmpbuf
= strdup(path
);
285 dirpath
= dirname(tmpbuf
);
286 fd
= open(dirpath
, O_RDONLY
);
288 err(1, "Cannot open directory %s", dirpath
);
293 * Avoid foot-shooting. Don't let the user create a PFS
294 * softlink via a PFS. PFS softlinks may only be accessed
295 * via the master filesystem. Checking it here ensures
296 * other PFS commands access PFS under the master filesystem.
298 clrpfs(&pfs
, &pfsd
, -1);
300 ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
);
301 if (pfs
.pfs_id
!= HAMMER_ROOT_PFSID
) {
303 "You are attempting to access a PFS softlink "
304 "from a PFS. It may not represent the PFS\n"
305 "on the main filesystem mount that you "
306 "expect! You may only access PFS softlinks\n"
307 "via the main filesystem mount!\n");
311 for (pfs_id
= 0; pfs_id
< HAMMER_MAX_PFS
; ++pfs_id
) {
312 clrpfs(&pfs
, &pfsd
, pfs_id
);
313 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
314 if (errno
!= ENOENT
) {
315 err(1, "Cannot create %s", path
);
321 if (pfs_id
== HAMMER_MAX_PFS
) {
322 errx(1, "Cannot create %s, all PFSs in use", path
);
324 } else if (pfs_id
== HAMMER_ROOT_PFSID
) {
325 errx(1, "Fatal error: PFS#%d must exist", HAMMER_ROOT_PFSID
);
332 printf("Creating PFS#%d\t", pfs_id
);
333 clrpfs(&pfs
, &pfsd
, pfs_id
);
334 init_pfsd(&pfsd
, is_slave
);
336 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) < 0) {
337 printf("failed: %s\n", strerror(errno
));
339 /* special symlink, must be exactly 10 characters */
340 asprintf(&linkpath
, "@@PFS%05d", pfs_id
);
341 if (symlink(linkpath
, path
) < 0) {
342 printf("failed: cannot create symlink: %s\n",
345 printf("succeeded!\n");
346 hammer_cmd_pseudofs_update(av
, ac
);
355 hammer_cmd_pseudofs_destroy(char **av
, int ac
)
357 struct hammer_ioc_pseudofs_rw pfs
;
366 fd
= getpfs(&pfs
, av
[0]);
368 if (pfs
.pfs_id
== HAMMER_ROOT_PFSID
) {
369 errx(1, "You cannot destroy PFS#%d", HAMMER_ROOT_PFSID
);
373 printf("You have requested that PFS#%d (%s) be destroyed\n",
374 pfs
.pfs_id
, pfs
.ondisk
->label
);
375 printf("This will irrevocably destroy all data on this PFS!!!!!\n");
376 printf("Do you really want to do this? [y/n] ");
379 errx(1, "No action taken on PFS#%d", pfs
.pfs_id
);
383 if (hammer_is_pfs_master(pfs
.ondisk
)) {
384 printf("This PFS is currently setup as a MASTER!\n");
385 printf("Are you absolutely sure you want to destroy it? [y/n] ");
388 errx(1, "No action taken on PFS#%d", pfs
.pfs_id
);
393 printf("Destroying PFS#%d (%s)", pfs
.pfs_id
, pfs
.ondisk
->label
);
398 for (i
= 5; i
; --i
) {
403 printf(".. starting destruction pass\n");
407 * Remove the softlink on success.
409 if (ioctl(fd
, HAMMERIOC_RMR_PSEUDOFS
, &pfs
) == 0) {
410 printf("pfs-destroy of PFS#%d succeeded!\n", pfs
.pfs_id
);
411 linkpath
= getlink(av
[0]);
413 if (remove(linkpath
) < 0) {
414 err(1, "Unable to remove softlink %s", linkpath
);
420 printf("pfs-destroy of PFS#%d failed: %s\n",
421 pfs
.pfs_id
, strerror(errno
));
427 hammer_cmd_pseudofs_upgrade(char **av
, int ac
)
429 struct hammer_ioc_pseudofs_rw pfs
;
436 fd
= getpfs(&pfs
, av
[0]);
438 if (pfs
.pfs_id
== HAMMER_ROOT_PFSID
) {
439 errx(1, "You cannot upgrade PFS#%d"
440 " (It should already be a master)",
443 } else if (hammer_is_pfs_master(pfs
.ondisk
)) {
444 errx(1, "It is already a master");
448 if (ioctl(fd
, HAMMERIOC_UPG_PSEUDOFS
, &pfs
) == 0) {
449 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
450 pfs
.pfs_id
, pfs
.ondisk
->label
);
452 err(1, "pfs-upgrade of PFS#%d (%s) failed",
453 pfs
.pfs_id
, pfs
.ondisk
->label
);
460 hammer_cmd_pseudofs_downgrade(char **av
, int ac
)
462 struct hammer_ioc_pseudofs_rw pfs
;
469 fd
= getpfs(&pfs
, av
[0]);
471 if (pfs
.pfs_id
== HAMMER_ROOT_PFSID
) {
472 errx(1, "You cannot downgrade PFS#%d", HAMMER_ROOT_PFSID
);
474 } else if (hammer_is_pfs_slave(pfs
.ondisk
)) {
475 errx(1, "It is already a slave");
479 if (ioctl(fd
, HAMMERIOC_DGD_PSEUDOFS
, &pfs
) == 0) {
480 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
481 pfs
.pfs_id
, pfs
.ondisk
->label
);
483 err(1, "pfs-downgrade of PFS#%d (%s) failed",
484 pfs
.pfs_id
, pfs
.ondisk
->label
);
491 hammer_cmd_pseudofs_update(char **av
, int ac
)
493 struct hammer_ioc_pseudofs_rw pfs
;
500 fd
= getpfs(&pfs
, av
[0]);
502 printf("%s\n", av
[0]);
505 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) == 0) {
506 parse_pfsd_options(av
+ 1, ac
- 1, pfs
.ondisk
);
507 if (hammer_is_pfs_slave(pfs
.ondisk
) &&
508 pfs
.pfs_id
== HAMMER_ROOT_PFSID
) {
509 errx(1, "The real mount point cannot be made a PFS "
510 "slave, only PFS sub-directories can be made "
514 pfs
.bytes
= sizeof(*pfs
.ondisk
);
515 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) == 0) {
516 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) == 0) {
517 dump_pfsd(pfs
.ondisk
, fd
);
519 err(1, "Unable to retrieve PFS configuration "
520 "after successful update");
524 err(1, "Unable to adjust PFS configuration");
533 init_pfsd(hammer_pseudofs_data_t pfsd
, int is_slave
)
535 bzero(pfsd
, sizeof(*pfsd
));
536 pfsd
->sync_beg_tid
= 1;
537 pfsd
->sync_end_tid
= 1;
538 pfsd
->sync_beg_ts
= 0;
539 pfsd
->sync_end_ts
= 0;
540 hammer_uuid_create(&pfsd
->shared_uuid
);
541 hammer_uuid_create(&pfsd
->unique_uuid
);
543 pfsd
->mirror_flags
|= HAMMER_PFSD_SLAVE
;
547 dump_pfsd(hammer_pseudofs_data_t pfsd
, int fd
)
549 struct hammer_ioc_version version
;
552 printf(" sync-beg-tid=0x%016jx\n", (uintmax_t)pfsd
->sync_beg_tid
);
553 printf(" sync-end-tid=0x%016jx\n", (uintmax_t)pfsd
->sync_end_tid
);
554 hammer_uuid_to_string(&pfsd
->shared_uuid
, &str
);
555 printf(" shared-uuid=%s\n", str
);
557 hammer_uuid_to_string(&pfsd
->unique_uuid
, &str
);
558 printf(" unique-uuid=%s\n", str
);
560 printf(" label=\"%s\"\n", pfsd
->label
);
561 if (pfsd
->snapshots
[0])
562 printf(" snapshots=\"%s\"\n", pfsd
->snapshots
);
563 if (pfsd
->prune_min
< (60 * 60 * 24)) {
564 printf(" prune-min=%02d:%02d:%02d\n",
565 pfsd
->prune_min
/ 60 / 60 % 24,
566 pfsd
->prune_min
/ 60 % 60,
567 pfsd
->prune_min
% 60);
568 } else if (pfsd
->prune_min
% (60 * 60 * 24)) {
569 printf(" prune-min=%dd/%02d:%02d:%02d\n",
570 pfsd
->prune_min
/ 60 / 60 / 24,
571 pfsd
->prune_min
/ 60 / 60 % 24,
572 pfsd
->prune_min
/ 60 % 60,
573 pfsd
->prune_min
% 60);
575 printf(" prune-min=%dd\n", pfsd
->prune_min
/ 60 / 60 / 24);
578 if (hammer_is_pfs_slave(pfsd
))
579 printf(" operating as a SLAVE\n");
581 printf(" operating as a MASTER\n");
584 * Snapshots directory cannot be shown when there is no fd since
585 * hammer version can't be retrieved. mirror-dump passes -1 because
586 * its input came from mirror-read output thus no path is available
589 if (fd
>= 0 && pfsd
->snapshots
[0] == 0) {
590 bzero(&version
, sizeof(version
));
591 if (ioctl(fd
, HAMMERIOC_GET_VERSION
, &version
) < 0)
593 HammerVersion
= version
.cur_version
;
594 if (version
.cur_version
< 3) {
595 if (hammer_is_pfs_slave(pfsd
)) {
596 printf(" snapshots directory not set for "
599 printf(" snapshots directory for master "
600 "defaults to <pfs>/snapshots\n");
603 printf(" snapshots directory defaults to "
604 "/var/hammer/<pfs>\n");
611 parse_pfsd_options(char **av
, int ac
, hammer_pseudofs_data_t pfsd
)
619 if ((ptr
= strchr(cmd
, '=')) != NULL
)
623 * Basic assignment value test
626 errx(1, "option %s requires an assignment", cmd
);
630 if (strcmp(cmd
, "sync-beg-tid") == 0) {
631 pfsd
->sync_beg_tid
= strtoull(ptr
, NULL
, 16);
632 } else if (strcmp(cmd
, "sync-end-tid") == 0) {
633 pfsd
->sync_end_tid
= strtoull(ptr
, NULL
, 16);
634 } else if (strcmp(cmd
, "shared-uuid") == 0) {
635 if (hammer_uuid_from_string(ptr
, &pfsd
->shared_uuid
)) {
636 errx(1, "option %s: error parsing uuid %s",
640 } else if (strcmp(cmd
, "unique-uuid") == 0) {
641 if (hammer_uuid_from_string(ptr
, &pfsd
->unique_uuid
)) {
642 errx(1, "option %s: error parsing uuid %s",
646 } else if (strcmp(cmd
, "label") == 0) {
648 if (ptr
[0] == '"' && ptr
[len
-1] == '"') {
651 } else if (ptr
[0] == '"') {
652 errx(1, "option %s: malformed string", cmd
);
655 snprintf(pfsd
->label
, sizeof(pfsd
->label
), "%s", ptr
);
656 } else if (strcmp(cmd
, "snapshots") == 0) {
660 "option %s: '%s' must be an "
661 "absolute path\n", cmd
, ptr
);
664 "use 'snapshots-clear' "
665 "to unset snapshots dir\n");
669 if (len
>= (int)sizeof(pfsd
->snapshots
)) {
670 errx(1, "option %s: path too long, %d "
671 "character limit", cmd
, len
);
674 snprintf(pfsd
->snapshots
, sizeof(pfsd
->snapshots
),
676 } else if (strcmp(cmd
, "snapshots-clear") == 0) {
677 pfsd
->snapshots
[0] = 0;
678 } else if (strcmp(cmd
, "prune-min") == 0) {
679 pfsd
->prune_min
= timetosecs(ptr
);
680 if (pfsd
->prune_min
< 0) {
681 errx(1, "option %s: illegal time spec, "
682 "use Nd or [Nd/]hh[:mm[:ss]]", ptr
);
686 errx(1, "invalid option: %s", cmd
);
696 pseudofs_usage(int code
)
699 "hammer pfs-status <dirpath> ...\n"
700 "hammer pfs-master <dirpath> [options]\n"
701 "hammer pfs-slave <dirpath> [options]\n"
702 "hammer pfs-update <dirpath> [options]\n"
703 "hammer pfs-upgrade <dirpath>\n"
704 "hammer pfs-downgrade <dirpath>\n"
705 "hammer pfs-destroy <dirpath>\n"
708 " sync-beg-tid=0x16llx\n"
709 " sync-end-tid=0x16llx\n"
710 " shared-uuid=0x16llx\n"
711 " unique-uuid=0x16llx\n"
712 " label=\"string\"\n"
713 " snapshots=\"/path\"\n"
716 " prune-min=[Nd/]hh[:mm[:ss]]\n"
722 * Convert time in the form [Nd/]hh[:mm[:ss]] to seconds.
724 * Return -1 if a parse error occurs.
725 * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
729 timetosecs(char *str
)
739 n
= strtol(str
, &ptr
, 10);
746 n
= strtol(ptr
+ 1, &ptr
, 10);
754 n
= strtol(ptr
+ 1, &ptr
, 10);
759 n
= strtol(ptr
+ 1, &ptr
, 10);
767 v
= days
* 24 * 60 * 60 + hrs
* 60 * 60 + mins
* 60 + secs
;