Revert "Add back la-count based migration"
[ctdb.git] / tools / ltdbtool.c
1 /*
2  * ctdb local tdb tool
3  *
4  * Copyright (C) Gregor Beck 2011
5  * Copyright (C) Michael Adam 2011
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <stdio.h>
22 #include <stdbool.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <ctype.h> /* isprint */
26 #include <string.h> /* strstr */
27 #include <fcntl.h> /* mode_t */
28 #include <sys/stat.h> /* S_IRUSR */
29 #include <stdint.h> /* uint32_t */
30 #include <netinet/in.h> /* struct sockaddr_in */
31 #include <sys/socket.h> /* struct sockaddr */
32 #include <sys/param.h>  /* MIN */
33 #include <tdb.h>
34 #include <unistd.h> /* getopt */
35 #include <errno.h>
36
37 #include "ctdb_protocol.h"
38
39 enum {
40         MAX_HEADER_SIZE=24,
41         OUT_MODE = S_IRUSR | S_IWUSR,
42         OUT_FLAGS = O_EXCL|O_CREAT|O_RDWR,
43 };
44
45 union  ltdb_header {
46         struct ctdb_ltdb_header hdr;
47         uint32_t uints[MAX_HEADER_SIZE/4];
48 };
49
50 static const union ltdb_header DEFAULT_HDR = {
51         .hdr.dmaster = -1,
52 };
53
54 static int help(const char* cmd)
55 {
56         fprintf(stdout, ""
57 "Usage: %s [options] <command>\n"
58 "\n"
59 "Options:\n"
60 "   -s {0|32|64}    specify how to determine the ctdb record header size\n"
61 "                   for the input database:\n"
62 "                   0: no ctdb header\n"
63 "                   32: ctdb header size of a 32 bit system (20 bytes)\n"
64 "                   64: ctdb header size of a 64 bit system (24 bytes)\n"
65 "                   default: 32 or 64 depending on the system architecture\n"
66 "\n"
67 "   -S <num>        the number of bytes to interpret as ctdb record header\n"
68 "                   for the input database (beware!)\n"
69 "\n"
70 "   -o {0|32|64}    specify how to determine the ctdb record header size\n"
71 "                   for the output database\n"
72 "                   0: no ctdb header\n"
73 "                   32: ctdb header size of a 32 bit system (20 bytes)\n"
74 "                   64: ctdb header size of a 64 bit system (24 bytes)\n"
75 "                   default: 32 or 64 depending on the system architecture\n"
76 "\n"
77 "   -O <num>        the number of bytes to interpret as ctdb record header\n"
78 "                   for the output database (beware!)\n"
79 "\n"
80 "   -p              print header (for the dump command), defaults ot off\n"
81 "\n"
82 "   -h              print this help\n"
83 "\n"
84 "Commands:\n"
85 "  help                         print this help\n"
86 "  dump <db>                    dump the db to stdout\n"
87 "  convert <in_db> <out_db>     convert the db\n\n", cmd);
88         return 0;
89 }
90
91 static int usage(const char* cmd)
92 {
93         fprintf(stderr,
94                 "Usage: %s dump [-p] [-s{0|32|64}] <idb>\n"
95                 "       %s convert [-s{0|32|64}] [-o{0|32|64}] <idb> <odb>\n"
96                 "       %s {help|-h}\n"
97                 , cmd, cmd, cmd);
98         return -1;
99 }
100
101 static int
102 ltdb_traverse(TDB_CONTEXT *tdb, int (*fn)(TDB_CONTEXT*, TDB_DATA, TDB_DATA,
103                                           struct ctdb_ltdb_header*, void *),
104               void *state, int hsize);
105
106 struct write_record_ctx {
107         TDB_CONTEXT* tdb;
108         size_t hsize;
109         int tdb_store_flags;
110 };
111
112 static int
113 write_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
114              struct ctdb_ltdb_header* hdr,
115              void* write_record_ctx);
116
117
118 struct dump_record_ctx {
119         FILE* file;
120         void (*print_data)(FILE*, TDB_DATA);
121         void (*dump_header)(struct dump_record_ctx*, struct ctdb_ltdb_header*);
122 };
123
124 static int dump_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
125                        struct ctdb_ltdb_header* hdr,
126                        void* dump_record_ctx);
127 static void print_data_tdbdump(FILE* file, TDB_DATA data);
128 static void dump_header_full(struct dump_record_ctx*, struct ctdb_ltdb_header*);
129 static void dump_header_nop(struct dump_record_ctx* c,
130                             struct ctdb_ltdb_header* h)
131 {}
132
133 static int dump_db(const char* iname, FILE* ofile, int hsize, bool dump_header)
134 {
135         int ret = -1;
136         TDB_CONTEXT* idb = tdb_open(iname, 0, TDB_DEFAULT, O_RDONLY, 0);
137         if (!idb) {
138                 perror("tdbopen in");
139         } else {
140                 struct dump_record_ctx dump_ctx = {
141                         .file = ofile,
142                         .print_data =  &print_data_tdbdump,
143                         .dump_header = dump_header ? &dump_header_full
144                                                    : &dump_header_nop,
145                 };
146                 ret = ltdb_traverse(idb, &dump_record, &dump_ctx, hsize);
147                 tdb_close(idb);
148         }
149         return ret;
150 }
151
152 static int conv_db(const char* iname, const char* oname, size_t isize, size_t osize)
153 {
154         int ret = -1;
155         TDB_CONTEXT* idb = tdb_open(iname, 0, TDB_DEFAULT, O_RDONLY, 0);
156         if (!idb) {
157                 perror("tdbopen in");
158         } else {
159                 TDB_CONTEXT* odb = tdb_open(oname, 0, TDB_DEFAULT, OUT_FLAGS, OUT_MODE);
160                 if (!odb) {
161                         perror("tdbopen out");
162                 } else {
163                         struct write_record_ctx ctx = {
164                                 .tdb = odb,
165                                 .hsize = osize,
166                                 .tdb_store_flags = TDB_REPLACE,
167                         };
168                         ret = ltdb_traverse(idb, &write_record, &ctx, isize);
169                         tdb_close(odb);
170                 }
171                 tdb_close(idb);
172         }
173         return ret;
174 }
175
176 static bool parse_size(size_t* size, const char* arg, bool raw) {
177         long val;
178         errno = 0;
179         val = strtol(arg, (char **) NULL, 10);
180         if (errno != 0) {
181                 return false;
182         }
183         if (!raw) {
184                 switch(val) {
185                 case 0:
186                         break;
187                 case 32:
188                         val = 20;
189                         break;
190                 case 64:
191                         val = 24;
192                         break;
193                 default:
194                         return false;
195                 }
196         }
197         *size = MIN(val, MAX_HEADER_SIZE);
198         return true;
199 }
200
201
202 int main(int argc, char* argv[])
203 {
204         size_t isize = sizeof(struct ctdb_ltdb_header);
205         size_t osize = sizeof(struct ctdb_ltdb_header);
206         bool print_header = false;
207         int opt;
208         const char *cmd, *idb, *odb;
209
210         while ((opt = getopt(argc, argv, "s:o:S:O:ph")) != -1) {
211                 switch (opt) {
212                 case 's':
213                 case 'S':
214                         if (!parse_size(&isize, optarg, isupper(opt))) {
215                                 return usage(argv[0]);
216                         }
217                         break;
218                 case 'o':
219                 case 'O':
220                         if (!parse_size(&osize, optarg, isupper(opt))) {
221                                 return usage(argv[0]);
222                         }
223                         break;
224                 case 'p':
225                         print_header = true;
226                         break;
227                 case 'h':
228                         return help(argv[0]);
229                 default:
230                         return usage(argv[0]);
231                 }
232         }
233
234         if (argc - optind < 1) {
235                 return usage(argv[0]);
236         }
237
238         cmd = argv[optind];
239
240         if (strcmp(cmd, "help") == 0) {
241                 return help(argv[0]);
242         }
243         else if (strcmp(cmd, "dump") == 0) {
244                 int ret;
245                 if (argc - optind != 2) {
246                         return usage(argv[0]);
247                 }
248                 idb = argv[optind+1];
249                 ret = dump_db(idb, stdout, isize, print_header);
250                 return (ret >= 0) ? 0 : ret;
251         }
252         else if (strcmp(cmd, "convert") == 0) {
253                 int ret;
254                 if (argc - optind != 3) {
255                         return usage(argv[0]);
256                 }
257                 idb = argv[optind+1];
258                 odb = argv[optind+2];
259                 ret = conv_db(idb, odb, isize, osize);
260                 return (ret >= 0) ? 0 : ret;
261         }
262
263         return usage(argv[0]);
264 }
265
266 struct ltdb_traverse_ctx {
267         int (*fn)(TDB_CONTEXT*,TDB_DATA,TDB_DATA,struct ctdb_ltdb_header*,void *);
268         void* state;
269         size_t hsize;
270 };
271
272 static int
273 ltdb_traverse_fn(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
274                  void* ltdb_traverse_ctx)
275 {
276         struct ltdb_traverse_ctx* ctx =
277                 (struct ltdb_traverse_ctx*)ltdb_traverse_ctx;
278         union ltdb_header hdr = DEFAULT_HDR;
279
280         const size_t hsize = MIN(sizeof(hdr), ctx->hsize);
281         if (val.dsize < hsize) {
282                 fprintf(stderr, "Value too short to contain a ctdb header: ");
283                 print_data_tdbdump(stderr, key);
284                 fprintf(stderr, " = ");
285                 print_data_tdbdump(stderr, val);
286                 fputc('\n', stderr);
287                 return -1;
288         }
289
290         memcpy(&hdr, val.dptr, hsize);
291
292         if (hdr.uints[5] != 0) {
293                 fprintf(stderr, "Warning: header padding isn't zero! Wrong header size?\n");
294         }
295         val.dptr += ctx->hsize;
296         val.dsize -= ctx->hsize;
297         return ctx->fn(tdb, key, val, &hdr.hdr, ctx->state);
298 }
299
300 int ltdb_traverse(TDB_CONTEXT *tdb,
301                   int (*fn)(TDB_CONTEXT *,TDB_DATA,TDB_DATA,struct ctdb_ltdb_header*,void *),
302                   void *state, int hsize)
303 {
304         struct ltdb_traverse_ctx ctx = {
305                 .fn = fn,
306                 .state = state,
307                 .hsize = hsize < 0 ? sizeof(struct ctdb_ltdb_header) : hsize,
308         };
309         return tdb_traverse(tdb, &ltdb_traverse_fn, &ctx);
310 }
311
312 int write_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
313                  struct ctdb_ltdb_header* hdr,
314                  void* write_record_ctx)
315 {
316         struct write_record_ctx* ctx
317                 = (struct write_record_ctx*)write_record_ctx;
318
319         if (ctx->hsize == 0) {
320                 if (tdb_store(ctx->tdb, key, val, ctx->tdb_store_flags) == -1) {
321                         fprintf(stderr, "tdb_store: %s\n", tdb_errorstr(ctx->tdb));
322                         return -1;
323                 }
324         } else {
325                 TDB_DATA h = {
326                         .dptr = (void*)hdr,
327                         .dsize = ctx->hsize,
328                 };
329                 if(tdb_store(ctx->tdb, key, h, ctx->tdb_store_flags) == -1) {
330                         fprintf(stderr, "tdb_store: %s\n", tdb_errorstr(ctx->tdb));
331                         return -1;
332                 }
333                 if(tdb_append(ctx->tdb, key, val) == -1) {
334                         fprintf(stderr, "tdb_append: %s\n", tdb_errorstr(ctx->tdb));
335                         return -1;
336                 }
337         }
338         return 0;
339 }
340
341 int dump_record(TDB_CONTEXT* tdb, TDB_DATA key, TDB_DATA val,
342                 struct ctdb_ltdb_header* hdr,
343                 void* dump_record_ctx)
344 {
345         struct dump_record_ctx* ctx = (struct dump_record_ctx*)dump_record_ctx;
346
347         fprintf(ctx->file, "{\nkey(%d) = ", (int)key.dsize);
348         ctx->print_data(ctx->file, key);
349         fputc('\n', ctx->file);
350         ctx->dump_header(ctx, hdr);
351         fprintf(ctx->file, "data(%d) = ", (int)val.dsize);
352         ctx->print_data(ctx->file, val);
353         fprintf(ctx->file, "\n}\n");
354         return 0;
355 }
356
357 void dump_header_full(struct dump_record_ctx* c, struct ctdb_ltdb_header* h)
358 {
359         fprintf(c->file, "dmaster: %d\nrsn: %llu\nflags: 0x%X\n",
360                 (int)h->dmaster,
361                 (unsigned long long)h->rsn, h->flags);
362 }
363
364 void print_data_tdbdump(FILE* file, TDB_DATA data) {
365         unsigned char *ptr = data.dptr;
366         fputc('"', file);
367         while (data.dsize--) {
368                 if (isprint(*ptr) && !strchr("\"\\", *ptr)) {
369                         fputc(*ptr, file);
370                 } else {
371                         fprintf(file, "\\%02X", *ptr);
372                 }
373                 ptr++;
374         }
375         fputc('"',file);
376 }
377