3cc88a3de2830f208deaf6593dfcb78e381d406f
[dragonfly.git] / sbin / hammer / cmd_pseudofs.c
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:
10  * 
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.
20  * 
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.
33  * 
34  * $DragonFly: src/sbin/hammer/cmd_pseudofs.c,v 1.4 2008/07/07 00:27:22 dillon Exp $
35  */
36
37 #include "hammer.h"
38
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);
43
44 void
45 hammer_cmd_pseudofs_status(char **av, int ac)
46 {
47         struct hammer_ioc_pseudofs_rw pfs;
48         struct hammer_pseudofs_data pfsd;
49         int i;
50         int fd;
51
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");
65                 }
66         }
67 }
68
69 void
70 hammer_cmd_pseudofs_create(char **av, int ac)
71 {
72         if (ac == 0)
73                 pseudofs_usage(1);
74
75         if (mknod(av[0], S_IFDIR|0777, 0) < 0) {
76                 perror("mknod (create pseudofs):");
77                 exit(1);
78         }
79         hammer_cmd_pseudofs_update(av, ac, 1);
80 }
81
82 void
83 hammer_cmd_pseudofs_destroy(char **av, int ac)
84 {
85         fprintf(stderr, "pfs-destroy not implemented yet\n");
86 }
87
88 void
89 hammer_cmd_pseudofs_update(char **av, int ac, int doinit)
90 {
91         struct hammer_ioc_pseudofs_rw pfs;
92         struct hammer_pseudofs_data pfsd;
93         int fd;
94
95         if (ac == 0)
96                 pseudofs_usage(1);
97         bzero(&pfs, sizeof(pfs));
98         bzero(&pfsd, sizeof(pfsd));
99         pfs.ondisk = &pfsd;
100         pfs.bytes = sizeof(pfsd);
101         pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
102
103         printf("%s\t", av[0]);
104         fflush(stdout);
105         fd = open(av[0], O_RDONLY);
106
107         if (fd >= 0 && ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
108                 if (doinit) {
109                         printf("Pseudo-fs #0x%08x created\n", pfs.pseudoid);
110                         init_pfsd(&pfsd);
111                 } else {
112                         printf("\n");
113                 }
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) {
118                                 dump_pfsd(&pfsd);
119                         } else {
120                                 printf("Unable to retrieve pfs configuration after successful update: %s\n", strerror(errno));
121                                 exit(1);
122                         }
123                 } else {
124                         printf("Unable to adjust pfs configuration: %s\n", strerror(errno));
125                         exit(1);
126                 }
127         } else {
128                 printf("PFS Creation failed: %s\n", strerror(errno));
129                 exit(1);
130         }
131 }
132
133 static void
134 init_pfsd(hammer_pseudofs_data_t pfsd)
135 {
136         uint32_t status;
137
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);
144         pfsd->master_id = 0;
145 }
146
147 static
148 void
149 dump_pfsd(hammer_pseudofs_data_t pfsd)
150 {
151         u_int32_t status;
152         char *str = NULL;
153
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) {
161                 printf("    slave\n");
162         } else if (pfsd->master_id < 0) {
163                 printf("    no-mirror\n");
164         } else {
165                 printf("    master=%d\n", pfsd->master_id);
166         }
167         printf("    label=\"%s\"\n", pfsd->label);
168 }
169
170 static void
171 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
172 {
173         char *cmd;
174         char *ptr;
175         int len;
176         uint32_t status;
177
178         while (ac) {
179                 cmd = *av;
180                 if ((ptr = strchr(cmd, '=')) != NULL)
181                         *ptr++ = 0;
182
183                 /*
184                  * Basic assignment value test
185                  */
186                 if (strcmp(cmd, "no-mirror") == 0 ||
187                     strcmp(cmd, "slave") == 0) {
188                         if (ptr) {
189                                 fprintf(stderr,
190                                         "option %s should not have "
191                                         "an assignment\n",
192                                         cmd);
193                                 exit(1);
194                         }
195                 } else {
196                         if (ptr == NULL) {
197                                 fprintf(stderr,
198                                         "option %s requires an assignment\n",
199                                         cmd);
200                                 exit(1);
201                         }
202                 }
203
204                 status = uuid_s_ok;
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) {
223                         len = strlen(ptr);
224                         if (ptr[0] == '"' && ptr[len-1] == '"') {
225                                 ptr[len-1] = 0;
226                                 ++ptr;
227                         } else if (ptr[0] == '"') {
228                                 fprintf(stderr,
229                                         "option %s: malformed string\n",
230                                         cmd);
231                                 exit(1);
232                         }
233                         snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
234                 } else {
235                         fprintf(stderr, "invalid option: %s\n", cmd);
236                         exit(1);
237                 }
238                 if (status != uuid_s_ok) {
239                         fprintf(stderr, "option %s: error parsing uuid %s\n",
240                                 cmd, ptr);
241                         exit(1);
242                 }
243                 --ac;
244                 ++av;
245         }
246 }
247
248 static
249 void
250 pseudofs_usage(int code)
251 {
252         fprintf(stderr, 
253                 "hammer pfs-status <dirpath1>...<dirpathN>\n"
254                 "hammer pfs-create <dirpath> [options]\n"
255                 "hammer pfs-update <dirpath> [options]\n"
256                 "\n"
257                 "    sync-beg-tid=0x16llx\n"
258                 "    sync-end-tid=0x16llx\n"
259                 "    shared-uuid=0x16llx\n"
260                 "    unique-uuid=0x16llx\n"
261                 "    master=0-15\n"
262                 "    slave\n"
263                 "    no-mirror\n"
264                 "    label=\"string\"\n"
265         );
266         exit(code);
267 }
268