MFC bandwith, delay, mirroring, and pfs work from HEAD.
[dragonfly.git] / sbin / hammer / cmd_pseudofs.c
blob95aeb418dfb8adb4736264a8d8b8416d0b8653dc
1 /*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 * $DragonFly: src/sbin/hammer/cmd_pseudofs.c,v 1.6.2.2 2008/08/04 19:41:25 dillon Exp $
37 #include "hammer.h"
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
47 * softlink.
49 int
50 getpfs(struct hammer_ioc_pseudofs_rw *pfs, const char *path)
52 hammer_tid_t dummy_tid;
53 struct stat st;
54 char *dirpath;
55 char buf[64];
56 int fd;
57 int n;
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 } else {
71 free(dirpath);
72 dirpath = strdup(".");
75 if (lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) {
77 * Avoid foot-shooting. Don't let the user access a PFS
78 * softlink via a PFS. PFS softlinks may only be accessed
79 * via the master filesystem.
81 fd = open(dirpath, O_RDONLY);
82 if (fd < 0)
83 goto done;
84 pfs->pfs_id = -1;
85 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
86 if (pfs->pfs_id != 0) {
87 fprintf(stderr,
88 "You are attempting to access a PFS softlink "
89 "from a PFS. It may not represent the PFS\n"
90 "on the main filesystem mount that you "
91 "expect! You may only access PFS softlinks\n"
92 "via the main filesystem mount!\n");
93 exit(1);
95 close(fd);
98 * Extract the PFS from the link. HAMMER will automatically
99 * convert @@PFS%05d links so if actually see one in that
100 * form the target PFS may not exist or may be corrupt. But
101 * we can extract the PFS id anyway.
103 n = readlink(path, buf, sizeof(buf) - 1);
104 if (n < 0)
105 n = 0;
106 buf[n] = 0;
107 if (sscanf(buf, "@@PFS%d", &pfs->pfs_id) == 1) {
108 fd = open(dirpath, O_RDONLY);
109 goto done;
111 if (sscanf(buf, "@@%llx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
112 fd = open(dirpath, O_RDONLY);
113 goto done;
118 * Try to open the path and request the pfs_id that way.
120 fd = open(path, O_RDONLY);
121 if (fd >= 0) {
122 pfs->pfs_id = -1;
123 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
124 if (pfs->pfs_id == -1) {
125 close(fd);
126 fd = -1;
131 * Cleanup
133 done:
134 if (fd < 0) {
135 fprintf(stderr, "Cannot access PFS %s: %s\n",
136 path, strerror(errno));
137 exit(1);
139 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
140 fprintf(stderr, "Cannot access PFS %s: %s\n",
141 path, strerror(errno));
142 exit(1);
144 free(dirpath);
145 return(fd);
148 void
149 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs)
151 close(fd);
152 if (pfs->ondisk) {
153 free(pfs->ondisk);
154 pfs->ondisk = NULL;
158 void
159 hammer_cmd_pseudofs_status(char **av, int ac)
161 struct hammer_ioc_pseudofs_rw pfs;
162 int i;
163 int fd;
165 for (i = 0; i < ac; ++i) {
166 printf("%s\t", av[i]);
167 fd = getpfs(&pfs, av[i]);
168 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
169 printf("Not a HAMMER root\n");
170 } else {
171 printf("PFS #%d {\n", pfs.pfs_id);
172 dump_pfsd(pfs.ondisk);
173 printf("}\n");
175 if (fd >= 0)
176 close(fd);
177 if (pfs.ondisk)
178 free(pfs.ondisk);
182 void
183 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
185 struct hammer_ioc_pseudofs_rw pfs;
186 struct hammer_pseudofs_data pfsd;
187 struct stat st;
188 const char *path;
189 char *dirpath;
190 char *linkpath;
191 int pfs_id;
192 int fd;
193 int error;
195 if (ac == 0)
196 pseudofs_usage(1);
197 path = av[0];
198 if (lstat(path, &st) == 0) {
199 fprintf(stderr, "cannot create %s, file exists!\n", path);
200 exit(1);
203 dirpath = strdup(path);
204 if (strrchr(dirpath, '/') != NULL) {
205 *strrchr(dirpath, '/') = 0;
206 } else {
207 free(dirpath);
208 dirpath = strdup(".");
210 fd = open(dirpath, O_RDONLY);
211 if (fd < 0) {
212 fprintf(stderr, "Cannot open directory %s\n", dirpath);
213 exit(1);
216 error = 0;
217 for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
218 bzero(&pfs, sizeof(pfs));
219 bzero(&pfsd, sizeof(pfsd));
220 pfs.pfs_id = pfs_id;
221 pfs.ondisk = &pfsd;
222 pfs.bytes = sizeof(pfsd);
223 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
224 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
225 error = errno;
226 break;
229 if (pfs_id == HAMMER_MAX_PFS) {
230 fprintf(stderr, "Cannot create %s, all PFSs in use\n", path);
231 exit(1);
233 if (error != ENOENT) {
234 fprintf(stderr, "Cannot create %s, got %s during scan\n",
235 path, strerror(error));
236 exit(1);
240 * Create the new PFS
242 printf("Creating PFS #%d\t", pfs_id);
243 bzero(&pfsd, sizeof(pfsd));
244 init_pfsd(&pfsd, is_slave);
245 pfs.pfs_id = pfs_id;
246 pfs.ondisk = &pfsd;
247 pfs.bytes = sizeof(pfsd);
248 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
250 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
251 printf("failed: %s\n", strerror(errno));
252 } else {
253 /* special symlink, must be exactly 10 characters */
254 asprintf(&linkpath, "@@PFS%05d", pfs_id);
255 if (symlink(linkpath, path) < 0) {
256 printf("failed: cannot create symlink: %s\n",
257 strerror(errno));
258 } else {
259 printf("succeeded!\n");
260 hammer_cmd_pseudofs_update(av, ac);
263 close(fd);
266 void
267 hammer_cmd_pseudofs_destroy(char **av, int ac)
269 struct hammer_ioc_pseudofs_rw pfs;
270 struct stat st;
271 int fd;
272 int i;
274 if (ac == 0)
275 pseudofs_usage(1);
276 bzero(&pfs, sizeof(pfs));
277 fd = getpfs(&pfs, av[0]);
279 if (pfs.pfs_id == 0) {
280 fprintf(stderr, "You cannot destroy PFS#0\n");
281 exit(1);
283 printf("You have requested that PFS#%d (%s) be destroyed\n",
284 pfs.pfs_id, pfs.ondisk->label);
285 printf("This will irrevocably destroy all data on this PFS!!!!!\n");
286 printf("Do you really want to do this? ");
287 fflush(stdout);
288 if (getyn() == 0) {
289 fprintf(stderr, "No action taken on PFS#%d\n", pfs.pfs_id);
290 exit(1);
293 if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
294 printf("This PFS is currently setup as a MASTER!\n");
295 printf("Are you absolutely sure you want to destroy it? ");
296 fflush(stdout);
297 if (getyn() == 0) {
298 fprintf(stderr, "No action taken on PFS#%d\n",
299 pfs.pfs_id);
300 exit(1);
304 printf("Destroying PFS #%d (%s) in ", pfs.pfs_id, pfs.ondisk->label);
305 for (i = 5; i; --i) {
306 printf(" %d", i);
307 fflush(stdout);
308 sleep(1);
310 printf(".. starting destruction pass\n");
311 fflush(stdout);
314 * Set the sync_beg_tid and sync_end_tid's to 1, once we start the
315 * RMR the PFS is basically destroyed even if someone ^C's it.
317 pfs.ondisk->mirror_flags |= HAMMER_PFSD_SLAVE;
318 pfs.ondisk->reserved01 = -1;
319 pfs.ondisk->sync_beg_tid = 1;
320 pfs.ondisk->sync_end_tid = 1;
322 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
323 fprintf(stderr, "Unable to update the PFS configuration: %s\n",
324 strerror(errno));
325 exit(1);
329 * Ok, do it. Remove the softlink on success.
331 if (ioctl(fd, HAMMERIOC_RMR_PSEUDOFS, &pfs) == 0) {
332 printf("pfs-destroy of PFS#%d succeeded!\n", pfs.pfs_id);
333 if (lstat(av[0], &st) == 0 && S_ISLNK(st.st_mode)) {
334 if (remove(av[0]) < 0) {
335 fprintf(stderr, "Unable to remove softlink: %s "
336 "(but the PFS has been destroyed)\n",
337 av[0]);
338 /* exit status 0 anyway */
341 } else {
342 printf("pfs-destroy of PFS#%d failed: %s\n",
343 pfs.pfs_id, strerror(errno));
347 void
348 hammer_cmd_pseudofs_upgrade(char **av, int ac)
350 struct hammer_ioc_pseudofs_rw pfs;
351 int fd;
353 if (ac == 0)
354 pseudofs_usage(1);
355 bzero(&pfs, sizeof(pfs));
356 fd = getpfs(&pfs, av[0]);
358 if (pfs.pfs_id == 0) {
359 fprintf(stderr, "You cannot upgrade PFS#0"
360 " (It should already be a master)\n");
361 exit(1);
363 if (ioctl(fd, HAMMERIOC_UPG_PSEUDOFS, &pfs) == 0) {
364 printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
365 pfs.pfs_id, pfs.ondisk->label);
366 } else {
367 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
368 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
372 void
373 hammer_cmd_pseudofs_downgrade(char **av, int ac)
375 struct hammer_ioc_pseudofs_rw pfs;
376 int fd;
378 if (ac == 0)
379 pseudofs_usage(1);
380 bzero(&pfs, sizeof(pfs));
381 fd = getpfs(&pfs, av[0]);
383 if (pfs.pfs_id == 0) {
384 fprintf(stderr, "You cannot downgrade PFS#0\n");
385 exit(1);
388 if (ioctl(fd, HAMMERIOC_DGD_PSEUDOFS, &pfs) == 0) {
389 printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
390 pfs.pfs_id, pfs.ondisk->label);
391 } else {
392 fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
393 pfs.pfs_id, pfs.ondisk->label, strerror(errno));
397 void
398 hammer_cmd_pseudofs_update(char **av, int ac)
400 struct hammer_ioc_pseudofs_rw pfs;
401 int fd;
403 if (ac == 0)
404 pseudofs_usage(1);
405 bzero(&pfs, sizeof(pfs));
406 fd = getpfs(&pfs, av[0]);
408 printf("%s\n", av[0]);
409 fflush(stdout);
411 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
412 parse_pfsd_options(av + 1, ac - 1, pfs.ondisk);
413 if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) &&
414 pfs.pfs_id == 0) {
415 printf("The real mount point cannot be made a PFS "
416 "slave, only PFS sub-directories can be made "
417 "slaves\n");
418 exit(1);
420 pfs.bytes = sizeof(*pfs.ondisk);
421 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
422 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
423 dump_pfsd(pfs.ondisk);
424 } else {
425 printf("Unable to retrieve pfs configuration after successful update: %s\n", strerror(errno));
426 exit(1);
428 } else {
429 printf("Unable to adjust pfs configuration: %s\n", strerror(errno));
430 exit(1);
435 static void
436 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
438 uint32_t status;
440 pfsd->sync_beg_tid = 1;
441 pfsd->sync_end_tid = 1;
442 pfsd->sync_beg_ts = 0;
443 pfsd->sync_end_ts = 0;
444 uuid_create(&pfsd->shared_uuid, &status);
445 uuid_create(&pfsd->unique_uuid, &status);
446 if (is_slave)
447 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
450 static
451 void
452 dump_pfsd(hammer_pseudofs_data_t pfsd)
454 u_int32_t status;
455 char *str = NULL;
457 printf(" sync-beg-tid=0x%016llx\n", pfsd->sync_beg_tid);
458 printf(" sync-end-tid=0x%016llx\n", pfsd->sync_end_tid);
459 uuid_to_string(&pfsd->shared_uuid, &str, &status);
460 printf(" shared-uuid=%s\n", str);
461 uuid_to_string(&pfsd->unique_uuid, &str, &status);
462 printf(" unique-uuid=%s\n", str);
463 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
464 printf(" slave\n");
466 printf(" label=\"%s\"\n", pfsd->label);
467 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE)
468 printf(" operating as a SLAVE\n");
469 else
470 printf(" operating as a MASTER\n");
473 static void
474 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
476 char *cmd;
477 char *ptr;
478 int len;
479 uint32_t status;
481 while (ac) {
482 cmd = *av;
483 if ((ptr = strchr(cmd, '=')) != NULL)
484 *ptr++ = 0;
487 * Basic assignment value test
489 if (ptr == NULL) {
490 fprintf(stderr,
491 "option %s requires an assignment\n",
492 cmd);
493 exit(1);
496 status = uuid_s_ok;
497 if (strcmp(cmd, "sync-beg-tid") == 0) {
498 pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
499 } else if (strcmp(cmd, "sync-end-tid") == 0) {
500 pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
501 } else if (strcmp(cmd, "shared-uuid") == 0) {
502 uuid_from_string(ptr, &pfsd->shared_uuid, &status);
503 } else if (strcmp(cmd, "unique-uuid") == 0) {
504 uuid_from_string(ptr, &pfsd->unique_uuid, &status);
505 } else if (strcmp(cmd, "label") == 0) {
506 len = strlen(ptr);
507 if (ptr[0] == '"' && ptr[len-1] == '"') {
508 ptr[len-1] = 0;
509 ++ptr;
510 } else if (ptr[0] == '"') {
511 fprintf(stderr,
512 "option %s: malformed string\n",
513 cmd);
514 exit(1);
516 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
517 } else {
518 fprintf(stderr, "invalid option: %s\n", cmd);
519 exit(1);
521 if (status != uuid_s_ok) {
522 fprintf(stderr, "option %s: error parsing uuid %s\n",
523 cmd, ptr);
524 exit(1);
526 --ac;
527 ++av;
531 static
532 void
533 pseudofs_usage(int code)
535 fprintf(stderr,
536 "hammer pfs-status <dirpath1>...<dirpathN>\n"
537 "hammer pfs-master <dirpath> [options]\n"
538 "hammer pfs-slave <dirpath> [options]\n"
539 "hammer pfs-update <dirpath> [options]\n"
540 "hammer pfs-upgrade <dirpath>\n"
541 "hammer pfs-downgrade <dirpath>\n"
542 "hammer pfs-destroy <dirpath>\n"
543 "\n"
544 "options:\n"
545 " sync-beg-tid=0x16llx\n"
546 " sync-end-tid=0x16llx\n"
547 " shared-uuid=0x16llx\n"
548 " unique-uuid=0x16llx\n"
549 " label=\"string\"\n"
551 exit(code);
554 static
556 getyn(void)
558 char buf[256];
559 int len;
561 if (fgets(buf, sizeof(buf), stdin) == NULL)
562 return(0);
563 len = strlen(buf);
564 while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
565 --len;
566 buf[len] = 0;
567 if (strcmp(buf, "y") == 0 ||
568 strcmp(buf, "yes") == 0 ||
569 strcmp(buf, "Y") == 0 ||
570 strcmp(buf, "YES") == 0) {
571 return(1);
573 return(0);