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);
46 * Calculate the pfs_id given a path to a directory or a @@PFS or @@%llx:%d
50 getpfs(struct hammer_ioc_pseudofs_rw
*pfs
, const char *path
)
52 hammer_tid_t dummy_tid
;
59 bzero(pfs
, sizeof(*pfs
));
60 pfs
->ondisk
= malloc(sizeof(*pfs
->ondisk
));
61 bzero(pfs
->ondisk
, sizeof(*pfs
->ondisk
));
62 pfs
->bytes
= sizeof(*pfs
->ondisk
);
65 * Calculate the directory containing the softlink
67 dirpath
= strdup(path
);
68 if (strrchr(dirpath
, '/')) {
69 *strrchr(dirpath
, '/') = 0;
70 if (strlen(dirpath
) == 0) {
72 dirpath
= strdup("/");
76 dirpath
= strdup(".");
79 if (lstat(path
, &st
) == 0 && S_ISLNK(st
.st_mode
)) {
81 * Avoid foot-shooting. Don't let the user access a PFS
82 * softlink via a PFS. PFS softlinks may only be accessed
83 * via the master filesystem.
85 fd
= open(dirpath
, O_RDONLY
);
89 ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, pfs
);
90 if (pfs
->pfs_id
!= 0) {
92 "You are attempting to access a PFS softlink "
93 "from a PFS. It may not represent the PFS\n"
94 "on the main filesystem mount that you "
95 "expect! You may only access PFS softlinks\n"
96 "via the main filesystem mount!\n");
102 * Extract the PFS from the link. HAMMER will automatically
103 * convert @@PFS%05d links so if actually see one in that
104 * form the target PFS may not exist or may be corrupt. But
105 * we can extract the PFS id anyway.
107 n
= readlink(path
, buf
, sizeof(buf
) - 1);
111 if (sscanf(buf
, "@@PFS%d", &pfs
->pfs_id
) == 1) {
112 fd
= open(dirpath
, O_RDONLY
);
115 if (sscanf(buf
, "@@%llx:%d", &dummy_tid
, &pfs
->pfs_id
) == 2) {
116 fd
= open(dirpath
, O_RDONLY
);
122 * Try to open the path and request the pfs_id that way.
124 fd
= open(path
, O_RDONLY
);
127 ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, pfs
);
128 if (pfs
->pfs_id
== -1) {
139 fprintf(stderr
, "Cannot access PFS %s: %s\n",
140 path
, strerror(errno
));
143 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, pfs
) < 0) {
144 fprintf(stderr
, "Cannot access PFS %s: %s\n",
145 path
, strerror(errno
));
153 relpfs(int fd
, struct hammer_ioc_pseudofs_rw
*pfs
)
163 hammer_cmd_pseudofs_status(char **av
, int ac
)
165 struct hammer_ioc_pseudofs_rw pfs
;
172 for (i
= 0; i
< ac
; ++i
) {
173 printf("%s\t", av
[i
]);
174 fd
= getpfs(&pfs
, av
[i
]);
175 if (fd
< 0 || ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
176 printf("Not a HAMMER root\n");
178 printf("PFS #%d {\n", pfs
.pfs_id
);
179 dump_pfsd(pfs
.ondisk
);
190 hammer_cmd_pseudofs_create(char **av
, int ac
, int is_slave
)
192 struct hammer_ioc_pseudofs_rw pfs
;
193 struct hammer_pseudofs_data pfsd
;
205 if (lstat(path
, &st
) == 0) {
206 fprintf(stderr
, "Cannot create %s, file exists!\n", path
);
210 dirpath
= strdup(path
);
211 if (strrchr(dirpath
, '/') != NULL
) {
212 *strrchr(dirpath
, '/') = 0;
215 dirpath
= strdup(".");
217 fd
= open(dirpath
, O_RDONLY
);
219 fprintf(stderr
, "Cannot open directory %s\n", dirpath
);
224 for (pfs_id
= 0; pfs_id
< HAMMER_MAX_PFS
; ++pfs_id
) {
225 bzero(&pfs
, sizeof(pfs
));
226 bzero(&pfsd
, sizeof(pfsd
));
229 pfs
.bytes
= sizeof(pfsd
);
230 pfs
.version
= HAMMER_IOC_PSEUDOFS_VERSION
;
231 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
236 if (pfs_id
== HAMMER_MAX_PFS
) {
237 fprintf(stderr
, "Cannot create %s, all PFSs in use\n", path
);
240 if (error
!= ENOENT
) {
241 fprintf(stderr
, "Cannot create %s, got %s during scan\n",
242 path
, strerror(error
));
249 printf("Creating PFS #%d\t", pfs_id
);
250 bzero(&pfsd
, sizeof(pfsd
));
251 init_pfsd(&pfsd
, is_slave
);
254 pfs
.bytes
= sizeof(pfsd
);
255 pfs
.version
= HAMMER_IOC_PSEUDOFS_VERSION
;
257 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) < 0) {
258 printf("failed: %s\n", strerror(errno
));
260 /* special symlink, must be exactly 10 characters */
261 asprintf(&linkpath
, "@@PFS%05d", pfs_id
);
262 if (symlink(linkpath
, path
) < 0) {
263 printf("failed: cannot create symlink: %s\n",
266 printf("succeeded!\n");
267 hammer_cmd_pseudofs_update(av
, ac
);
274 hammer_cmd_pseudofs_destroy(char **av
, int ac
)
276 struct hammer_ioc_pseudofs_rw pfs
;
283 bzero(&pfs
, sizeof(pfs
));
284 fd
= getpfs(&pfs
, av
[0]);
286 if (pfs
.pfs_id
== 0) {
287 fprintf(stderr
, "You cannot destroy PFS#0\n");
290 printf("You have requested that PFS#%d (%s) be destroyed\n",
291 pfs
.pfs_id
, pfs
.ondisk
->label
);
292 printf("This will irrevocably destroy all data on this PFS!!!!!\n");
293 printf("Do you really want to do this? ");
296 fprintf(stderr
, "No action taken on PFS#%d\n", pfs
.pfs_id
);
300 if ((pfs
.ondisk
->mirror_flags
& HAMMER_PFSD_SLAVE
) == 0) {
301 printf("This PFS is currently setup as a MASTER!\n");
302 printf("Are you absolutely sure you want to destroy it? ");
305 fprintf(stderr
, "No action taken on PFS#%d\n",
311 printf("Destroying PFS #%d (%s) in ", pfs
.pfs_id
, pfs
.ondisk
->label
);
312 for (i
= 5; i
; --i
) {
317 printf(".. starting destruction pass\n");
321 * Set the sync_beg_tid and sync_end_tid's to 1, once we start the
322 * RMR the PFS is basically destroyed even if someone ^C's it.
324 pfs
.ondisk
->mirror_flags
|= HAMMER_PFSD_SLAVE
;
325 pfs
.ondisk
->reserved01
= -1;
326 pfs
.ondisk
->sync_beg_tid
= 1;
327 pfs
.ondisk
->sync_end_tid
= 1;
329 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) < 0) {
330 fprintf(stderr
, "Unable to update the PFS configuration: %s\n",
336 * Ok, do it. Remove the softlink on success.
338 if (ioctl(fd
, HAMMERIOC_RMR_PSEUDOFS
, &pfs
) == 0) {
339 printf("pfs-destroy of PFS#%d succeeded!\n", pfs
.pfs_id
);
340 if (lstat(av
[0], &st
) == 0 && S_ISLNK(st
.st_mode
)) {
341 if (remove(av
[0]) < 0) {
342 fprintf(stderr
, "Unable to remove softlink: %s "
343 "(but the PFS has been destroyed)\n",
345 /* exit status 0 anyway */
349 printf("pfs-destroy of PFS#%d failed: %s\n",
350 pfs
.pfs_id
, strerror(errno
));
355 hammer_cmd_pseudofs_upgrade(char **av
, int ac
)
357 struct hammer_ioc_pseudofs_rw pfs
;
362 bzero(&pfs
, sizeof(pfs
));
363 fd
= getpfs(&pfs
, av
[0]);
365 if (pfs
.pfs_id
== 0) {
366 fprintf(stderr
, "You cannot upgrade PFS#0"
367 " (It should already be a master)\n");
370 if (ioctl(fd
, HAMMERIOC_UPG_PSEUDOFS
, &pfs
) == 0) {
371 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
372 pfs
.pfs_id
, pfs
.ondisk
->label
);
374 fprintf(stderr
, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
375 pfs
.pfs_id
, pfs
.ondisk
->label
, strerror(errno
));
380 hammer_cmd_pseudofs_downgrade(char **av
, int ac
)
382 struct hammer_ioc_pseudofs_rw pfs
;
387 bzero(&pfs
, sizeof(pfs
));
388 fd
= getpfs(&pfs
, av
[0]);
390 if (pfs
.pfs_id
== 0) {
391 fprintf(stderr
, "You cannot downgrade PFS#0\n");
395 if (ioctl(fd
, HAMMERIOC_DGD_PSEUDOFS
, &pfs
) == 0) {
396 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
397 pfs
.pfs_id
, pfs
.ondisk
->label
);
399 fprintf(stderr
, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
400 pfs
.pfs_id
, pfs
.ondisk
->label
, strerror(errno
));
405 hammer_cmd_pseudofs_update(char **av
, int ac
)
407 struct hammer_ioc_pseudofs_rw pfs
;
412 bzero(&pfs
, sizeof(pfs
));
413 fd
= getpfs(&pfs
, av
[0]);
415 printf("%s\n", av
[0]);
418 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) == 0) {
419 parse_pfsd_options(av
+ 1, ac
- 1, pfs
.ondisk
);
420 if ((pfs
.ondisk
->mirror_flags
& HAMMER_PFSD_SLAVE
) &&
422 printf("The real mount point cannot be made a PFS "
423 "slave, only PFS sub-directories can be made "
427 pfs
.bytes
= sizeof(*pfs
.ondisk
);
428 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) == 0) {
429 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) == 0) {
430 dump_pfsd(pfs
.ondisk
);
432 printf("Unable to retrieve pfs configuration "
433 "after successful update: %s\n",
438 printf("Unable to adjust pfs configuration: %s\n",
446 init_pfsd(hammer_pseudofs_data_t pfsd
, int is_slave
)
450 pfsd
->sync_beg_tid
= 1;
451 pfsd
->sync_end_tid
= 1;
452 pfsd
->sync_beg_ts
= 0;
453 pfsd
->sync_end_ts
= 0;
454 uuid_create(&pfsd
->shared_uuid
, &status
);
455 uuid_create(&pfsd
->unique_uuid
, &status
);
457 pfsd
->mirror_flags
|= HAMMER_PFSD_SLAVE
;
462 dump_pfsd(hammer_pseudofs_data_t pfsd
)
467 printf(" sync-beg-tid=0x%016llx\n", pfsd
->sync_beg_tid
);
468 printf(" sync-end-tid=0x%016llx\n", pfsd
->sync_end_tid
);
469 uuid_to_string(&pfsd
->shared_uuid
, &str
, &status
);
470 printf(" shared-uuid=%s\n", str
);
471 uuid_to_string(&pfsd
->unique_uuid
, &str
, &status
);
472 printf(" unique-uuid=%s\n", str
);
473 if (pfsd
->mirror_flags
& HAMMER_PFSD_SLAVE
) {
476 printf(" label=\"%s\"\n", pfsd
->label
);
477 if (pfsd
->snapshots
[0])
478 printf(" snapshots=\"%s\"\n", pfsd
->snapshots
);
479 if (pfsd
->mirror_flags
& HAMMER_PFSD_SLAVE
) {
480 printf(" operating as a SLAVE\n");
481 if (pfsd
->snapshots
[0] == 0)
482 printf(" snapshots directory not set for slave\n");
484 printf(" operating as a MASTER\n");
485 if (pfsd
->snapshots
[0] == 0) {
486 printf(" snapshots dir for master "
487 "defaults to <fs>/snapshots\n");
493 parse_pfsd_options(char **av
, int ac
, hammer_pseudofs_data_t pfsd
)
502 if ((ptr
= strchr(cmd
, '=')) != NULL
)
506 * Basic assignment value test
510 "option %s requires an assignment\n",
516 if (strcmp(cmd
, "sync-beg-tid") == 0) {
517 pfsd
->sync_beg_tid
= strtoull(ptr
, NULL
, 16);
518 } else if (strcmp(cmd
, "sync-end-tid") == 0) {
519 pfsd
->sync_end_tid
= strtoull(ptr
, NULL
, 16);
520 } else if (strcmp(cmd
, "shared-uuid") == 0) {
521 uuid_from_string(ptr
, &pfsd
->shared_uuid
, &status
);
522 } else if (strcmp(cmd
, "unique-uuid") == 0) {
523 uuid_from_string(ptr
, &pfsd
->unique_uuid
, &status
);
524 } else if (strcmp(cmd
, "label") == 0) {
526 if (ptr
[0] == '"' && ptr
[len
-1] == '"') {
529 } else if (ptr
[0] == '"') {
531 "option %s: malformed string\n",
535 snprintf(pfsd
->label
, sizeof(pfsd
->label
), "%s", ptr
);
536 } else if (strcmp(cmd
, "snapshots") == 0) {
540 "option %s: '%s' must be an "
541 "absolute path\n", cmd
, ptr
);
544 "use 'snapshots-clear' "
545 "to unset snapshots dir\n");
549 if (len
>= (int)sizeof(pfsd
->snapshots
)) {
551 "option %s: path too long, %d "
552 "character limit\n", cmd
, len
);
555 snprintf(pfsd
->snapshots
, sizeof(pfsd
->snapshots
),
557 } else if (strcmp(cmd
, "snapshots-clear") == 0) {
558 pfsd
->snapshots
[0] = 0;
560 fprintf(stderr
, "invalid option: %s\n", cmd
);
563 if (status
!= uuid_s_ok
) {
564 fprintf(stderr
, "option %s: error parsing uuid %s\n",
575 pseudofs_usage(int code
)
578 "hammer pfs-status <dirpath> ...\n"
579 "hammer pfs-master <dirpath> [options]\n"
580 "hammer pfs-slave <dirpath> [options]\n"
581 "hammer pfs-update <dirpath> [options]\n"
582 "hammer pfs-upgrade <dirpath>\n"
583 "hammer pfs-downgrade <dirpath>\n"
584 "hammer pfs-destroy <dirpath>\n"
587 " sync-beg-tid=0x16llx\n"
588 " sync-end-tid=0x16llx\n"
589 " shared-uuid=0x16llx\n"
590 " unique-uuid=0x16llx\n"
591 " label=\"string\"\n"
592 " snapshots=\"/path\"\n"
605 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
608 while (len
&& (buf
[len
-1] == '\n' || buf
[len
-1] == '\r'))
611 if (strcmp(buf
, "y") == 0 ||
612 strcmp(buf
, "yes") == 0 ||
613 strcmp(buf
, "Y") == 0 ||
614 strcmp(buf
, "YES") == 0) {