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.4 2008/07/07 00:27:22 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);
41 static void dump_pfsd(hammer_pseudofs_data_t pfsd);
42 static void pseudofs_usage(int code);
45 hammer_cmd_pseudofs_status(char **av, int ac)
47 struct hammer_ioc_pseudofs_rw pfs;
48 struct hammer_pseudofs_data pfsd;
52 for (i = 0; i < ac; ++i) {
53 bzero(&pfsd, sizeof(pfsd));
54 bzero(&pfs, sizeof(pfs));
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");
62 printf("Pseudo-fs #0x%08x {\n", pfs.pseudoid);
70 hammer_cmd_pseudofs_create(char **av, int ac)
75 if (mknod(av[0], S_IFDIR|0777, 0) < 0) {
76 perror("mknod (create pseudofs):");
79 hammer_cmd_pseudofs_update(av, ac, 1);
83 hammer_cmd_pseudofs_destroy(char **av, int ac)
85 fprintf(stderr, "pfs-destroy not implemented yet\n");
89 hammer_cmd_pseudofs_update(char **av, int ac, int doinit)
91 struct hammer_ioc_pseudofs_rw pfs;
92 struct hammer_pseudofs_data pfsd;
97 bzero(&pfs, sizeof(pfs));
98 bzero(&pfsd, sizeof(pfsd));
100 pfs.bytes = sizeof(pfsd);
101 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
103 printf("%s\t", av[0]);
105 fd = open(av[0], O_RDONLY);
107 if (fd >= 0 && ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
109 printf("Pseudo-fs #0x%08x created\n", pfs.pseudoid);
114 parse_pfsd_options(av + 1, ac - 1, &pfsd);
115 pfs.bytes = sizeof(pfsd);
116 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
117 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
120 printf("Unable to retrieve pfs configuration after successful update: %s\n", strerror(errno));
124 printf("Unable to adjust pfs configuration: %s\n", strerror(errno));
128 printf("PFS Creation failed: %s\n", strerror(errno));
134 init_pfsd(hammer_pseudofs_data_t pfsd)
138 pfsd->sync_beg_tid = 1;
139 pfsd->sync_end_tid = 1;
140 pfsd->sync_beg_ts = 0;
141 pfsd->sync_end_ts = 0;
142 uuid_create(&pfsd->shared_uuid, &status);
143 uuid_create(&pfsd->unique_uuid, &status);
149 dump_pfsd(hammer_pseudofs_data_t pfsd)
154 printf(" sync-beg-tid=0x%016llx\n", pfsd->sync_beg_tid);
155 printf(" sync-end-tid=0x%016llx\n", pfsd->sync_end_tid);
156 uuid_to_string(&pfsd->shared_uuid, &str, &status);
157 printf(" shared-uuid=%s\n", str);
158 uuid_to_string(&pfsd->unique_uuid, &str, &status);
159 printf(" unique-uuid=%s\n", str);
160 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
162 } else if (pfsd->master_id < 0) {
163 printf(" no-mirror\n");
165 printf(" master=%d\n", pfsd->master_id);
167 printf(" label=\"%s\"\n", pfsd->label);
171 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
180 if ((ptr = strchr(cmd, '=')) != NULL)
184 * Basic assignment value test
186 if (strcmp(cmd, "no-mirror") == 0 ||
187 strcmp(cmd, "slave") == 0) {
190 "option %s should not have "
198 "option %s requires an assignment\n",
205 if (strcmp(cmd, "sync-beg-tid") == 0) {
206 pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
207 } else if (strcmp(cmd, "sync-end-tid") == 0) {
208 pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
209 } else if (strcmp(cmd, "shared-uuid") == 0) {
210 uuid_from_string(ptr, &pfsd->shared_uuid, &status);
211 } else if (strcmp(cmd, "unique-uuid") == 0) {
212 uuid_from_string(ptr, &pfsd->unique_uuid, &status);
213 } else if (strcmp(cmd, "master") == 0) {
214 pfsd->master_id = strtol(ptr, NULL, 0);
215 pfsd->mirror_flags &= ~HAMMER_PFSD_SLAVE;
216 } else if (strcmp(cmd, "slave") == 0) {
217 pfsd->master_id = -1;
218 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
219 } else if (strcmp(cmd, "no-mirror") == 0) {
220 pfsd->master_id = -1;
221 pfsd->mirror_flags &= ~HAMMER_PFSD_SLAVE;
222 } else if (strcmp(cmd, "label") == 0) {
224 if (ptr[0] == '"' && ptr[len-1] == '"') {
227 } else if (ptr[0] == '"') {
229 "option %s: malformed string\n",
233 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
235 fprintf(stderr, "invalid option: %s\n", cmd);
238 if (status != uuid_s_ok) {
239 fprintf(stderr, "option %s: error parsing uuid %s\n",
250 pseudofs_usage(int code)
253 "hammer pfs-status <dirpath1>...<dirpathN>\n"
254 "hammer pfs-create <dirpath> [options]\n"
255 "hammer pfs-update <dirpath> [options]\n"
257 " sync-beg-tid=0x16llx\n"
258 " sync-end-tid=0x16llx\n"
259 " shared-uuid=0x16llx\n"
260 " unique-uuid=0x16llx\n"
264 " label=\"string\"\n"