2 * Copyright (c) 2003,2004 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/jscan/jscan.c,v 1.7 2005/09/06 22:33:00 dillon Exp $
39 static void usage(const char *av0);
44 off_t prefix_file_size = 100 * 1024 * 1024;
46 static enum jdirection jdirection = JD_FORWARDS;
48 static void jscan_do_output(struct jfile *, const char *,
49 const char *, int64_t);
50 static void jscan_do_mirror(struct jfile *, const char *,
51 const char *, int64_t);
52 static void jscan_do_record(struct jfile *, const char *,
53 const char *, int64_t);
54 static void jscan_do_debug(struct jfile *, const char *,
55 const char *, int64_t);
56 static void fork_subprocess(struct jfile *,
57 void (*)(struct jfile *, const char *,
58 const char *, int64_t),
60 const char *, const char *, int64_t);
63 main(int ac, char **av)
65 const char *input_prefix = NULL;
66 char *output_transid_file = NULL;
67 char *mirror_transid_file = NULL;
68 const char *mirror_directory = ".";
69 char *record_prefix = NULL;
70 char *record_transid_file = NULL;
71 struct jsession jsdebug;
72 struct jsession jsoutput;
73 struct jsession jsmirror;
75 int64_t mirror_transid;
76 int64_t output_transid;
77 int64_t record_transid;
86 while ((ch = getopt(ac, av, "2dfm:o:s:uvw:D:O:W:F")) != -1) {
89 jmodes |= JMODEF_INPUT_FULL;
92 trans_count = strtoll(optarg, &ptr, 0);
109 fprintf(stderr, "Bad suffix for value specified with -c, use 'k', 'm', 'g', 't', or nothing\n");
114 jmodes |= JMODEF_DEBUG;
117 jmodes |= JMODEF_LOOP_FOREVER;
123 jmodes |= JMODEF_MIRROR;
124 if (strcmp(optarg, "none") != 0)
125 mirror_transid_file = optarg;
128 jmodes |= JMODEF_OUTPUT_FULL;
131 jmodes |= JMODEF_OUTPUT;
132 if (strcmp(optarg, "none") != 0)
133 output_transid_file = optarg;
136 prefix_file_size = strtoll(optarg, &ptr, 0);
139 prefix_file_size *= 1024;
142 prefix_file_size *= 1024;
145 prefix_file_size *= 1024;
148 prefix_file_size *= 1024;
153 fprintf(stderr, "Bad suffix for value specified with -s, use 'k', 'm', 'g', 't', or nothing\n");
158 jdirection = JD_BACKWARDS;
161 jmodes |= JMODEF_RECORD_TMP;
164 jmodes |= JMODEF_RECORD;
165 record_prefix = optarg;
166 asprintf(&record_transid_file, "%s.transid", record_prefix);
169 mirror_directory = optarg;
175 fprintf(stderr, "unknown option: -%c\n", optopt);
183 if ((jmodes & JMODEF_COMMAND_MASK) == 0)
185 if (optind > ac + 1) {
186 fprintf(stderr, "Only one input file or prefix may be specified,\n"
187 "or zero if stdin is to be the input.\n");
190 if (jdirection == JD_BACKWARDS && (jmodes & (JMODEF_RECORD|JMODEF_OUTPUT))) {
191 fprintf(stderr, "Undo mode is only good in mirroring mode and "
192 "cannot be mixed with other modes.\n");
199 * The input will either be a pipe, a regular file, or a journaling
204 input_prefix = "<stdin>";
206 if (fstat(0, &st) < 0 || !S_ISREG(st.st_mode)) {
207 jmodes |= JMODEF_INPUT_PIPE;
208 if (jdirection == JD_BACKWARDS) {
209 fprintf(stderr, "Cannot scan journals on pipes backwards\n");
213 jf = jopen_fd(input_fd, jdirection);
214 } else if (stat(av[optind], &st) == 0 && S_ISREG(st.st_mode)) {
215 input_prefix = av[optind];
216 if ((input_fd = open(av[optind], O_RDONLY)) != NULL) {
217 jf = jopen_fd(input_fd, jdirection);
222 input_prefix = av[optind];
223 jf = jopen_prefix(input_prefix, jdirection, 0);
224 jmodes |= JMODEF_INPUT_PREFIX;
227 fprintf(stderr, "Unable to open input %s: %s\n",
228 input_prefix, strerror(errno));
233 * STEP 1 - SYNCHRONIZING THE INPUT STREAM
235 * Figure out the starting point for our various output modes. Figure
236 * out the earliest transaction id and try to seek to that point,
237 * otherwise we might have to scan through terrabytes of data.
239 * Invalid transid's will be set to 0, but it should also be noted
240 * that 0 is also a valid transid.
242 get_transid_from_file(output_transid_file, &output_transid,
243 JMODEF_OUTPUT_TRANSID_GOOD);
244 get_transid_from_file(mirror_transid_file, &mirror_transid,
245 JMODEF_MIRROR_TRANSID_GOOD);
246 get_transid_from_file(record_transid_file, &record_transid,
247 JMODEF_RECORD_TRANSID_GOOD);
249 if ((jmodes & JMODEF_OUTPUT_TRANSID_GOOD) && output_transid < transid)
250 transid = output_transid;
251 if ((jmodes & JMODEF_MIRROR_TRANSID_GOOD) && mirror_transid < transid)
252 transid = mirror_transid;
253 if ((jmodes & JMODEF_RECORD_TRANSID_GOOD) && record_transid < transid)
254 transid = record_transid;
255 if ((jmodes & JMODEF_TRANSID_GOOD_MASK) == 0)
258 if (jmodes & JMODEF_OUTPUT) {
259 fprintf(stderr, "Starting transid for OUTPUT: %016llx\n",
262 if (jmodes & JMODEF_MIRROR) {
263 fprintf(stderr, "Starting transid for MIRROR: %016llx\n",
266 if (jmodes & JMODEF_RECORD) {
267 fprintf(stderr, "Starting transid for RECORD: %016llx\n",
273 * Now it gets more difficult. If we are recording then the input
274 * could be representative of continuing data and not have any
275 * prior, older data that the output or mirror modes might need. Those
276 * modes must work off the recording data even as we write to it.
277 * In that case we fork and have the sub-processes work off the
280 * Then we take the input and start recording.
282 if (jmodes & JMODEF_RECORD) {
283 if (jrecord_init(record_prefix) < 0) {
284 fprintf(stderr, "Unable to initialize file set for: %s\n",
288 if (jmodes & JMODEF_MIRROR) {
289 fork_subprocess(jf, jscan_do_mirror, record_prefix,
291 mirror_directory, mirror_transid);
292 /* XXX ack stream for temporary record file removal */
294 if (jmodes & JMODEF_OUTPUT) {
295 fork_subprocess(jf, jscan_do_output, record_prefix,
297 NULL, output_transid);
298 /* XXX ack stream for temporary record file removal */
300 jscan_do_record(jf, record_transid_file, record_prefix, record_transid);
305 * If the input is a prefix set we can just pass it to the appropriate
306 * jscan_do_*() function. If we are doing both output and mirroring
307 * we fork the mirror and do the output in the foreground since that
308 * is going to stdout.
310 if (jmodes & JMODEF_INPUT_PREFIX) {
311 if ((jmodes & JMODEF_OUTPUT) && (jmodes & JMODEF_MIRROR)) {
312 fork_subprocess(jf, jscan_do_mirror, input_prefix,
314 mirror_directory, mirror_transid);
315 jscan_do_output(jf, output_transid_file, NULL, output_transid);
316 } else if (jmodes & JMODEF_OUTPUT) {
317 jscan_do_output(jf, output_transid_file, NULL, output_transid);
318 } else if (jmodes & JMODEF_MIRROR) {
319 jscan_do_mirror(jf, mirror_transid_file, mirror_directory,
321 } else if (jmodes & JMODEF_DEBUG) {
322 jscan_do_debug(jf, NULL, NULL, 0);
328 * The input is not a prefix set and we are not recording, which means
329 * we have to transfer the data on the input pipe to the output and
330 * mirroring code on the fly. This also means that we must keep track
331 * of meta-data records in-memory. However, if the input is a regular
332 * file we *CAN* try to optimize where we start reading.
334 * NOTE: If the mirroring code encounters a transaction record that is
335 * not marked begin, and it does not have the begin record, it will
336 * attempt to locate the begin record if the input is not a pipe, then
339 if ((jmodes & JMODEF_TRANSID_GOOD_MASK) && !(jmodes & JMODEF_INPUT_PIPE))
340 jseek(jf, transid, jdirection);
341 jmodes |= JMODEF_MEMORY_TRACKING;
343 jsession_init(&jsdebug, jf, NULL, 0);
344 jsession_init(&jsoutput, jf, output_transid_file, output_transid);
345 jsession_init(&jsmirror, jf, mirror_transid_file, mirror_transid);
346 jsmirror.ss_mirror_directory = mirror_directory;
348 while ((error = jread(jf, &jd, jdirection)) == 0) {
349 if (jmodes & JMODEF_DEBUG)
350 dump_debug(&jsdebug, jd);
351 if (jmodes & JMODEF_OUTPUT)
352 dump_output(&jsoutput, jd);
353 if (jmodes & JMODEF_MIRROR)
354 dump_mirror(&jsmirror, jd);
358 jsession_term(&jsdebug);
359 jsession_term(&jsoutput);
360 jsession_term(&jsmirror);
365 * When we have multiple commands and are writing to a prefix set, we can
366 * 'background' the output and/or mirroring command and have the background
367 * processes feed off the prefix set the foreground process is writing to.
371 fork_subprocess(struct jfile *jftoclose,
372 void (*func)(struct jfile *, const char *, const char *, int64_t),
373 const char *input_prefix, const char *transid_file, const char *info,
379 if ((pid = fork()) == 0) {
380 jmodes &= ~(JMODEF_DEBUG | JMODEF_INPUT_PIPE);
381 jmodes |= JMODEF_LOOP_FOREVER; /* keep checking for new input */
383 jf = jopen_prefix(input_prefix, jdirection, 0);
384 jmodes |= JMODEF_INPUT_PREFIX;
385 func(jf, transid_file, info, transid);
388 } else if (pid < 0) {
389 fprintf(stderr, "fork(): %s\n", strerror(errno));
396 jscan_do_output(struct jfile *jf, const char *output_transid_file, const char *dummy __unused, int64_t transid)
399 struct jsession jsdebug;
400 struct jsession jsoutput;
402 jsession_init(&jsdebug, jf, NULL, 0);
403 jsession_init(&jsoutput, jf, output_transid_file, transid);
405 if ((jmodes & JMODEF_OUTPUT_TRANSID_GOOD) && !(jmodes & JMODEF_INPUT_PIPE))
406 jseek(jf, transid, jdirection);
407 while (jread(jf, &jd, jdirection) == 0) {
408 if (jmodes & JMODEF_DEBUG)
409 dump_debug(&jsdebug, jd);
410 dump_output(&jsoutput, jd);
417 jscan_do_mirror(struct jfile *jf, const char *mirror_transid_file, const char *mirror_directory, int64_t transid)
419 struct jsession jsdebug;
420 struct jsession jsmirror;
423 jsession_init(&jsdebug, jf, NULL, 0);
424 jsession_init(&jsmirror, jf, mirror_transid_file, transid);
425 jsmirror.ss_mirror_directory = mirror_directory;
427 if ((jmodes & JMODEF_MIRROR_TRANSID_GOOD) && !(jmodes & JMODEF_INPUT_PIPE))
428 jseek(jf, transid, jdirection);
429 while (jread(jf, &jd, jdirection) == 0) {
430 if (jmodes & JMODEF_DEBUG)
431 dump_debug(&jsdebug, jd);
432 dump_mirror(&jsmirror, jd);
439 jscan_do_record(struct jfile *jfin, const char *record_transid_file, const char *prefix, int64_t transid)
441 struct jsession jsdebug;
442 struct jsession jsrecord;
445 jsession_init(&jsdebug, jfin, NULL, 0);
446 jsession_init(&jsrecord, jfin, record_transid_file, transid);
448 assert(jdirection == JD_FORWARDS);
449 jsrecord.ss_jfout = jopen_prefix(prefix, JD_FORWARDS, 1);
450 if (jsrecord.ss_jfout == NULL) {
451 fprintf(stderr, "Unable to open prefix set for writing: %s\n", prefix);
454 if ((jmodes & JMODEF_RECORD_TRANSID_GOOD) && !(jmodes & JMODEF_INPUT_PIPE))
455 jseek(jfin, transid, jdirection);
456 while (jread(jfin, &jd, jdirection) == 0) {
457 if (jmodes & JMODEF_DEBUG)
458 dump_debug(&jsdebug, jd);
459 dump_record(&jsrecord, jd);
462 jclose(jsrecord.ss_jfout);
467 jscan_do_debug(struct jfile *jfin, const char *dummy1 __unused,
468 const char *dummy __unused, int64_t transid __unused)
470 struct jsession jsdebug;
473 jsession_init(&jsdebug, jfin, NULL, 0);
474 while (jread(jfin, &jd, jdirection) == 0) {
475 dump_debug(&jsdebug, jd);
480 usage(const char *av0)
483 "%s [-2duF] [-D dir] [-m mirror_transid_file/none]\n"
484 "\t[-o/O output_trnasid_file/none]\n"
485 "\t[-s size[kmgt]] -w/W record_prefix] [input_file/input_prefix]\n",