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.7 2008/07/19 18:48:14 dillon 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;
71 dirpath = strdup(".");
73 if (lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) {
75 * Avoid foot-shooting. Don't let the user access a PFS
76 * softlink via a PFS. PFS softlinks may only be accessed
77 * via the master filesystem.
79 fd = open(dirpath, O_RDONLY);
83 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
84 if (pfs->pfs_id != 0) {
86 "You are attempting to access a PFS softlink "
87 "from a PFS. It may not represent the PFS\n"
88 "on the main filesystem mount that you "
89 "expect! You may only access PFS softlinks\n"
90 "via the main filesystem mount!\n");
96 * Extract the PFS from the link. HAMMER will automatically
97 * convert @@PFS%05d links so if actually see one in that
98 * form the target PFS may not exist or may be corrupt. But
99 * we can extract the PFS id anyway.
101 n = readlink(path, buf, sizeof(buf) - 1);
105 if (sscanf(buf, "@@PFS%d", &pfs->pfs_id) == 1) {
106 fd = open(dirpath, O_RDONLY);
109 if (sscanf(buf, "@@%llx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
110 fd = open(dirpath, O_RDONLY);
116 * Try to open the path and request the pfs_id that way.
118 fd = open(path, O_RDONLY);
121 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
122 if (pfs->pfs_id == -1) {
133 fprintf(stderr, "Cannot access PFS %s: %s\n",
134 path, strerror(errno));
137 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
138 fprintf(stderr, "Cannot access PFS %s: %s\n",
139 path, strerror(errno));
147 hammer_cmd_pseudofs_status(char **av, int ac)
149 struct hammer_ioc_pseudofs_rw pfs;
153 for (i = 0; i < ac; ++i) {
154 printf("%s\t", av[i]);
155 fd = getpfs(&pfs, av[i]);
156 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
157 printf("Not a HAMMER root\n");
159 printf("PFS #%d {\n", pfs.pfs_id);
160 dump_pfsd(pfs.ondisk);
171 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
173 struct hammer_ioc_pseudofs_rw pfs;
174 struct hammer_pseudofs_data pfsd;
186 if (lstat(path, &st) == 0) {
187 fprintf(stderr, "cannot create %s, file exists!\n", path);
191 dirpath = strdup(path);
192 if (strrchr(dirpath, '/') != NULL)
193 *strrchr(dirpath, '/') = 0;
195 dirpath = strdup(".");
196 fd = open(dirpath, O_RDONLY);
198 fprintf(stderr, "Cannot open directory %s\n", dirpath);
203 for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
204 bzero(&pfs, sizeof(pfs));
205 bzero(&pfsd, sizeof(pfsd));
208 pfs.bytes = sizeof(pfsd);
209 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
210 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
215 if (pfs_id == HAMMER_MAX_PFS) {
216 fprintf(stderr, "Cannot create %s, all PFSs in use\n", path);
219 if (error != ENOENT) {
220 fprintf(stderr, "Cannot create %s, got %s during scan\n",
221 path, strerror(error));
228 printf("Creating PFS #%d\t", pfs_id);
229 bzero(&pfsd, sizeof(pfsd));
230 init_pfsd(&pfsd, is_slave);
233 pfs.bytes = sizeof(pfsd);
234 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
236 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
237 printf("failed: %s\n", strerror(errno));
239 /* special symlink, must be exactly 10 characters */
240 asprintf(&linkpath, "@@PFS%05d", pfs_id);
241 if (symlink(linkpath, path) < 0) {
242 printf("failed: cannot create symlink: %s\n",
245 printf("succeeded!\n");
246 hammer_cmd_pseudofs_update(av, ac);
253 hammer_cmd_pseudofs_destroy(char **av, int ac)
255 struct hammer_ioc_pseudofs_rw pfs;
262 bzero(&pfs, sizeof(pfs));
263 fd = getpfs(&pfs, av[0]);
265 if (pfs.pfs_id == 0) {
266 fprintf(stderr, "You cannot destroy PFS#0\n");
269 printf("You have requested that PFS#%d (%s) be destroyed\n",
270 pfs.pfs_id, pfs.ondisk->label);
271 printf("This will irrevocably destroy all data on this PFS!!!!!\n");
272 printf("Do you really want to do this? ");
275 fprintf(stderr, "No action taken on PFS#%d\n", pfs.pfs_id);
279 if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
280 printf("This PFS is currently setup as a MASTER!\n");
281 printf("Are you absolutely sure you want to destroy it? ");
284 fprintf(stderr, "No action taken on PFS#%d\n",
290 printf("Destroying PFS #%d (%s) in ", pfs.pfs_id, pfs.ondisk->label);
291 for (i = 5; i; --i) {
296 printf(".. starting destruction pass\n");
300 * Set the sync_beg_tid and sync_end_tid's to 1, once we start the
301 * RMR the PFS is basically destroyed even if someone ^C's it.
303 pfs.ondisk->mirror_flags |= HAMMER_PFSD_SLAVE;
304 pfs.ondisk->reserved01 = -1;
305 pfs.ondisk->sync_beg_tid = 1;
306 pfs.ondisk->sync_end_tid = 1;
308 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
309 fprintf(stderr, "Unable to update the PFS configuration: %s\n",
315 * Ok, do it. Remove the softlink on success.
317 if (ioctl(fd, HAMMERIOC_RMR_PSEUDOFS, &pfs) == 0) {
318 printf("pfs-destroy of PFS#%d succeeded!\n", pfs.pfs_id);
319 if (lstat(av[0], &st) == 0 && S_ISLNK(st.st_mode)) {
320 if (remove(av[0]) < 0) {
321 fprintf(stderr, "Unable to remove softlink: %s "
322 "(but the PFS has been destroyed)\n",
324 /* exit status 0 anyway */
328 printf("pfs-destroy of PFS#%d failed: %s\n",
329 pfs.pfs_id, strerror(errno));
334 hammer_cmd_pseudofs_upgrade(char **av, int ac)
336 struct hammer_ioc_pseudofs_rw pfs;
341 bzero(&pfs, sizeof(pfs));
342 fd = getpfs(&pfs, av[0]);
344 if (pfs.pfs_id == 0) {
345 fprintf(stderr, "You cannot upgrade PFS#0"
346 " (It should already be a master)\n");
349 if (ioctl(fd, HAMMERIOC_UPG_PSEUDOFS, &pfs) == 0) {
350 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
351 pfs.pfs_id, pfs.ondisk->label);
353 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
354 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
359 hammer_cmd_pseudofs_downgrade(char **av, int ac)
361 struct hammer_ioc_pseudofs_rw pfs;
366 bzero(&pfs, sizeof(pfs));
367 fd = getpfs(&pfs, av[0]);
369 if (pfs.pfs_id == 0) {
370 fprintf(stderr, "You cannot downgrade PFS#0\n");
374 if (ioctl(fd, HAMMERIOC_DGD_PSEUDOFS, &pfs) == 0) {
375 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
376 pfs.pfs_id, pfs.ondisk->label);
378 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
379 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
384 hammer_cmd_pseudofs_update(char **av, int ac)
386 struct hammer_ioc_pseudofs_rw pfs;
391 bzero(&pfs, sizeof(pfs));
392 fd = getpfs(&pfs, av[0]);
394 printf("%s\n", av[0]);
397 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
398 parse_pfsd_options(av + 1, ac - 1, pfs.ondisk);
399 if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) &&
401 printf("The real mount point cannot be made a PFS "
402 "slave, only PFS sub-directories can be made "
406 pfs.bytes = sizeof(*pfs.ondisk);
407 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
408 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
409 dump_pfsd(pfs.ondisk);
411 printf("Unable to retrieve pfs configuration after successful update: %s\n", strerror(errno));
415 printf("Unable to adjust pfs configuration: %s\n", strerror(errno));
422 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
426 pfsd->sync_beg_tid = 1;
427 pfsd->sync_end_tid = 1;
428 pfsd->sync_beg_ts = 0;
429 pfsd->sync_end_ts = 0;
430 uuid_create(&pfsd->shared_uuid, &status);
431 uuid_create(&pfsd->unique_uuid, &status);
433 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
438 dump_pfsd(hammer_pseudofs_data_t pfsd)
443 printf(" sync-beg-tid=0x%016llx\n", pfsd->sync_beg_tid);
444 printf(" sync-end-tid=0x%016llx\n", pfsd->sync_end_tid);
445 uuid_to_string(&pfsd->shared_uuid, &str, &status);
446 printf(" shared-uuid=%s\n", str);
447 uuid_to_string(&pfsd->unique_uuid, &str, &status);
448 printf(" unique-uuid=%s\n", str);
449 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
452 printf(" label=\"%s\"\n", pfsd->label);
453 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE)
454 printf(" operating as a SLAVE\n");
456 printf(" operating as a MASTER\n");
460 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
469 if ((ptr = strchr(cmd, '=')) != NULL)
473 * Basic assignment value test
477 "option %s requires an assignment\n",
483 if (strcmp(cmd, "sync-beg-tid") == 0) {
484 pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
485 } else if (strcmp(cmd, "sync-end-tid") == 0) {
486 pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
487 } else if (strcmp(cmd, "shared-uuid") == 0) {
488 uuid_from_string(ptr, &pfsd->shared_uuid, &status);
489 } else if (strcmp(cmd, "unique-uuid") == 0) {
490 uuid_from_string(ptr, &pfsd->unique_uuid, &status);
491 } else if (strcmp(cmd, "label") == 0) {
493 if (ptr[0] == '"' && ptr[len-1] == '"') {
496 } else if (ptr[0] == '"') {
498 "option %s: malformed string\n",
502 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
504 fprintf(stderr, "invalid option: %s\n", cmd);
507 if (status != uuid_s_ok) {
508 fprintf(stderr, "option %s: error parsing uuid %s\n",
519 pseudofs_usage(int code)
522 "hammer pfs-status <dirpath1>...<dirpathN>\n"
523 "hammer pfs-master <dirpath> [options]\n"
524 "hammer pfs-slave <dirpath> [options]\n"
525 "hammer pfs-update <dirpath> [options]\n"
526 "hammer pfs-upgrade <dirpath>\n"
527 "hammer pfs-downgrade <dirpath>\n"
528 "hammer pfs-destroy <dirpath>\n"
531 " sync-beg-tid=0x16llx\n"
532 " sync-end-tid=0x16llx\n"
533 " shared-uuid=0x16llx\n"
534 " unique-uuid=0x16llx\n"
535 " label=\"string\"\n"
547 if (fgets(buf, sizeof(buf), stdin) == NULL)
550 while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
553 if (strcmp(buf, "y") == 0 ||
554 strcmp(buf, "yes") == 0 ||
555 strcmp(buf, "Y") == 0 ||
556 strcmp(buf, "YES") == 0) {