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.3 2008/07/02 22:05:59 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_update(char **av
, int ac
, int doinit
)
85 struct hammer_ioc_pseudofs_rw pfs
;
86 struct hammer_pseudofs_data pfsd
;
91 bzero(&pfs
, sizeof(pfs
));
92 bzero(&pfsd
, sizeof(pfsd
));
94 pfs
.bytes
= sizeof(pfsd
);
95 pfs
.version
= HAMMER_IOC_PSEUDOFS_VERSION
;
97 printf("%s\t", av
[0]);
99 fd
= open(av
[0], O_RDONLY
);
101 if (fd
>= 0 && ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) == 0) {
103 printf("Pseudo-fs #0x%08x created\n", pfs
.pseudoid
);
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) {
114 printf("Unable to retrieve pfs configuration after successful update: %s\n", strerror(errno
));
118 printf("Unable to adjust pfs configuration: %s\n", strerror(errno
));
122 printf("PFS Creation failed: %s\n", strerror(errno
));
128 init_pfsd(hammer_pseudofs_data_t pfsd
)
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
);
143 dump_pfsd(hammer_pseudofs_data_t pfsd
)
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
) {
156 } else if (pfsd
->master_id
< 0) {
157 printf(" no-mirror\n");
159 printf(" master=%d\n", pfsd
->master_id
);
161 printf(" label=\"%s\"\n", pfsd
->label
);
165 parse_pfsd_options(char **av
, int ac
, hammer_pseudofs_data_t pfsd
)
174 if ((ptr
= strchr(cmd
, '=')) != NULL
)
178 * Basic assignment value test
180 if (strcmp(cmd
, "no-mirror") == 0 ||
181 strcmp(cmd
, "slave") == 0) {
184 "option %s should not have "
192 "option %s requires an assignment\n",
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) {
218 if (ptr
[0] == '"' && ptr
[len
-1] == '"') {
221 } else if (ptr
[0] == '"') {
223 "option %s: malformed string\n",
227 snprintf(pfsd
->label
, sizeof(pfsd
->label
), "%s", ptr
);
229 fprintf(stderr
, "invalid option: %s\n", cmd
);
232 if (status
!= uuid_s_ok
) {
233 fprintf(stderr
, "option %s: error parsing uuid %s\n",
244 pseudofs_usage(int code
)
247 "hammer pfs-status <dirpath1>...<dirpathN>\n"
248 "hammer pfs-create <dirpath> [options]\n"
249 "hammer pfs-update <dirpath> [options]\n"
251 " sync-beg-tid=0x16llx\n"
252 " sync-end-tid=0x16llx\n"
253 " shared-uuid=0x16llx\n"
254 " unique-uuid=0x16llx\n"
258 " label=\"string\"\n"