HAMMER Utilities: Mirroring and pseudo-fs directives
[dragonfly.git] / sbin / hammer / cmd_pseudofs.c
blob01b0dbec1118d4346371c6b89c2f2571d21f4255
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.3 2008/07/02 22:05:59 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);
41 static void dump_pfsd(hammer_pseudofs_data_t pfsd);
42 static void pseudofs_usage(int code);
44 void
45 hammer_cmd_pseudofs_status(char **av, int ac)
47 struct hammer_ioc_pseudofs_rw pfs;
48 struct hammer_pseudofs_data pfsd;
49 int i;
50 int fd;
52 for (i = 0; i < ac; ++i) {
53 bzero(&pfsd, sizeof(pfsd));
54 bzero(&pfs, sizeof(pfs));
55 pfs.ondisk = &pfsd;
56 pfs.bytes = sizeof(pfsd);
57 printf("%s\t", av[i]);
58 fd = open(av[i], O_RDONLY);
59 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
60 printf("Not a HAMMER root\n");
61 } else {
62 printf("Pseudo-fs #0x%08x {\n", pfs.pseudoid);
63 dump_pfsd(&pfsd);
64 printf("}\n");
69 void
70 hammer_cmd_pseudofs_create(char **av, int ac)
72 if (ac == 0)
73 pseudofs_usage(1);
75 if (mknod(av[0], S_IFDIR|0777, 0) < 0) {
76 perror("mknod (create pseudofs):");
77 exit(1);
79 hammer_cmd_pseudofs_update(av, ac, 1);
82 void
83 hammer_cmd_pseudofs_update(char **av, int ac, int doinit)
85 struct hammer_ioc_pseudofs_rw pfs;
86 struct hammer_pseudofs_data pfsd;
87 int fd;
89 if (ac == 0)
90 pseudofs_usage(1);
91 bzero(&pfs, sizeof(pfs));
92 bzero(&pfsd, sizeof(pfsd));
93 pfs.ondisk = &pfsd;
94 pfs.bytes = sizeof(pfsd);
95 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
97 printf("%s\t", av[0]);
98 fflush(stdout);
99 fd = open(av[0], O_RDONLY);
101 if (fd >= 0 && ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
102 if (doinit) {
103 printf("Pseudo-fs #0x%08x created\n", pfs.pseudoid);
104 init_pfsd(&pfsd);
105 } else {
106 printf("\n");
108 parse_pfsd_options(av + 1, ac - 1, &pfsd);
109 pfs.bytes = sizeof(pfsd);
110 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
111 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
112 dump_pfsd(&pfsd);
113 } else {
114 printf("Unable to retrieve pfs configuration after successful update: %s\n", strerror(errno));
115 exit(1);
117 } else {
118 printf("Unable to adjust pfs configuration: %s\n", strerror(errno));
119 exit(1);
121 } else {
122 printf("PFS Creation failed: %s\n", strerror(errno));
123 exit(1);
127 static void
128 init_pfsd(hammer_pseudofs_data_t pfsd)
130 uint32_t status;
132 pfsd->sync_beg_tid = 1;
133 pfsd->sync_end_tid = 1;
134 pfsd->sync_beg_ts = 0;
135 pfsd->sync_end_ts = 0;
136 uuid_create(&pfsd->shared_uuid, &status);
137 uuid_create(&pfsd->unique_uuid, &status);
138 pfsd->master_id = 0;
141 static
142 void
143 dump_pfsd(hammer_pseudofs_data_t pfsd)
145 u_int32_t status;
146 char *str = NULL;
148 printf(" sync-beg-tid=0x%016llx\n", pfsd->sync_beg_tid);
149 printf(" sync-end-tid=0x%016llx\n", pfsd->sync_end_tid);
150 uuid_to_string(&pfsd->shared_uuid, &str, &status);
151 printf(" shared-uuid=%s\n", str);
152 uuid_to_string(&pfsd->unique_uuid, &str, &status);
153 printf(" unique-uuid=%s\n", str);
154 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
155 printf(" slave\n");
156 } else if (pfsd->master_id < 0) {
157 printf(" no-mirror\n");
158 } else {
159 printf(" master=%d\n", pfsd->master_id);
161 printf(" label=\"%s\"\n", pfsd->label);
164 static void
165 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
167 char *cmd;
168 char *ptr;
169 int len;
170 uint32_t status;
172 while (ac) {
173 cmd = *av;
174 if ((ptr = strchr(cmd, '=')) != NULL)
175 *ptr++ = 0;
178 * Basic assignment value test
180 if (strcmp(cmd, "no-mirror") == 0 ||
181 strcmp(cmd, "slave") == 0) {
182 if (ptr) {
183 fprintf(stderr,
184 "option %s should not have "
185 "an assignment\n",
186 cmd);
187 exit(1);
189 } else {
190 if (ptr == NULL) {
191 fprintf(stderr,
192 "option %s requires an assignment\n",
193 cmd);
194 exit(1);
198 status = uuid_s_ok;
199 if (strcmp(cmd, "sync-beg-tid") == 0) {
200 pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
201 } else if (strcmp(cmd, "sync-end-tid") == 0) {
202 pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
203 } else if (strcmp(cmd, "shared-uuid") == 0) {
204 uuid_from_string(ptr, &pfsd->shared_uuid, &status);
205 } else if (strcmp(cmd, "unique-uuid") == 0) {
206 uuid_from_string(ptr, &pfsd->unique_uuid, &status);
207 } else if (strcmp(cmd, "master") == 0) {
208 pfsd->master_id = strtol(ptr, NULL, 0);
209 pfsd->mirror_flags &= ~HAMMER_PFSD_SLAVE;
210 } else if (strcmp(cmd, "slave") == 0) {
211 pfsd->master_id = -1;
212 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
213 } else if (strcmp(cmd, "no-mirror") == 0) {
214 pfsd->master_id = -1;
215 pfsd->mirror_flags &= ~HAMMER_PFSD_SLAVE;
216 } else if (strcmp(cmd, "label") == 0) {
217 len = strlen(ptr);
218 if (ptr[0] == '"' && ptr[len-1] == '"') {
219 ptr[len-1] = 0;
220 ++ptr;
221 } else if (ptr[0] == '"') {
222 fprintf(stderr,
223 "option %s: malformed string\n",
224 cmd);
225 exit(1);
227 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
228 } else {
229 fprintf(stderr, "invalid option: %s\n", cmd);
230 exit(1);
232 if (status != uuid_s_ok) {
233 fprintf(stderr, "option %s: error parsing uuid %s\n",
234 cmd, ptr);
235 exit(1);
237 --ac;
238 ++av;
242 static
243 void
244 pseudofs_usage(int code)
246 fprintf(stderr,
247 "hammer pfs-status <dirpath1>...<dirpathN>\n"
248 "hammer pfs-create <dirpath> [options]\n"
249 "hammer pfs-update <dirpath> [options]\n"
250 "\n"
251 " sync-beg-tid=0x16llx\n"
252 " sync-end-tid=0x16llx\n"
253 " shared-uuid=0x16llx\n"
254 " unique-uuid=0x16llx\n"
255 " master=0-15\n"
256 " slave\n"
257 " no-mirror\n"
258 " label=\"string\"\n"
260 exit(code);