xref: /dragonfly/sbin/jscan/jscan.h (revision 4a9a2afb7b2d86ea674d59e2680245a8c92fbd6a)
1 /*
2  * Copyright (c) 2004,2005 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 
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <sys/journal.h>
38 #include <sys/stat.h>
39 #include <stdio.h>
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <stddef.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <limits.h>
46 #include <time.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <ctype.h>
50 #include <assert.h>
51 #include "jattr.h"
52 
53 struct jdata;
54 
55 enum jdirection { JD_FORWARDS, JD_BACKWARDS, JD_SEQFIRST, JD_SEQLAST };
56 
57 struct jfile {
58     off_t           jf_pos;             /* current seek position */
59     int                       jf_fd;              /* reading/scanning */
60     int                       jf_write_fd;        /* appending */
61     off_t           jf_write_pos;       /* append position */
62     int                       jf_error;
63     int                       jf_open_flags;
64     char            *jf_prefix;         /* prefix: name */
65     unsigned int    jf_seq_beg;         /* prefix: sequence space */
66     unsigned int    jf_seq_end;         /* prefix: sequence space */
67     unsigned int    jf_seq;             /* prefix: current sequence number */
68     int64_t                   jf_last_transid;/* prefix: last recorded transid */
69 };
70 
71 /*
72  * Output session (debug, record, output, etc)
73  */
74 struct jsession {
75     struct jfile    *ss_jfin;
76     struct jfile    *ss_jfout;
77     enum jdirection ss_direction;
78     const char                *ss_mirror_directory;
79     const char                *ss_transid_file;
80     int                       ss_transid_fd;
81     int64_t                   ss_transid;
82 };
83 
84 #define JF_FULL_DUPLEX        0x0001
85 
86 struct jdata {
87     int64_t                   jd_transid;         /* transaction id from header */
88     int                       jd_alloc; /* allocated bytes */
89     int                       jd_size;  /* data bytes */
90     int                       jd_refs;  /* ref count */
91     unsigned int    jd_seq;             /* location data */
92     off_t           jd_pos;             /* location data */
93     char            jd_data[4];         /* must be last field */
94 };
95 
96 struct jstream {
97     struct jstream  *js_next; /* linked list / same transaction */
98     struct jsession *js_session;
99     char            *js_alloc_buf;
100     int                       js_alloc_size;
101 
102     /*
103      * Normalized fields strip all rawrecbeg, rawrecend, and deadspace except
104      * for the initial rawrecbeg header.
105      */
106     char            *js_normalized_base;
107     int                       js_normalized_size;
108     off_t           js_normalized_off;
109     off_t           js_normalized_total;
110 
111     /*
112      * This is used by the first js record only to cache other records in the
113      * chain.
114      */
115     struct jstream  *js_cache;
116     struct jdata    *js_jdata;
117     struct journal_rawrecbeg *js_head;
118 };
119 
120 struct jhash {
121     struct jhash    *jh_hash;
122     struct jstream  *jh_first;
123     struct jstream  *jh_last;
124     struct jsession *jh_session;
125     int16_t                   jh_transid;
126 };
127 
128 #define JHASH_SIZE  1024
129 #define JHASH_MASK  (JHASH_SIZE - 1)
130 
131 #define JMODEF_DEBUG                              0x00000001
132 #define JMODEF_MIRROR                             0x00000002
133 #define JMODEF_UNUSED0004               0x00000004
134 #define JMODEF_RECORD                             0x00000008
135 #define JMODEF_RECORD_TMP               0x00000010
136 #define JMODEF_INPUT_FULL               0x00000020
137 #define JMODEF_INPUT_PIPE               0x00000040
138 #define JMODEF_INPUT_PREFIX             0x00000080
139 #define JMODEF_OUTPUT                             0x00000100
140 #define JMODEF_OUTPUT_FULL              0x00000200
141 #define JMODEF_MEMORY_TRACKING                    0x00000400
142 #define JMODEF_LOOP_FOREVER             0x00000800
143 
144 #define JMODEF_OUTPUT_TRANSID_GOOD      0x00010000
145 #define JMODEF_RECORD_TRANSID_GOOD      0x00020000
146 #define JMODEF_MIRROR_TRANSID_GOOD      0x00040000
147 #define JMODEF_TRANSID_GOOD_MASK        (JMODEF_OUTPUT_TRANSID_GOOD|\
148                                                    JMODEF_RECORD_TRANSID_GOOD|\
149                                                    JMODEF_MIRROR_TRANSID_GOOD)
150 #define JMODEF_COMMAND_MASK   (JMODEF_RECORD|JMODEF_MIRROR|JMODEF_DEBUG|\
151                                          JMODEF_OUTPUT)
152 
153 extern int jmodes;
154 extern int fsync_opt;
155 extern int verbose_opt;
156 extern off_t prefix_file_size;
157 extern off_t trans_count;
158 
159 const char *type_to_name(int16_t rectype);
160 void stringout(FILE *fp, char c, int exact);
161 void jattr_reset(struct jattr *jattr);
162 int64_t buf_to_int64(const void *buf, int bytes);
163 char *dupdatastr(const void *buf, int bytes);
164 char *dupdatapath(const void *buf, int bytes);
165 void get_transid_from_file(const char *path, int64_t *transid, int flags);
166 
167 void jsession_init(struct jsession *ss, struct jfile *jfin,
168                        enum jdirection direction,
169                        const char *transid_file, int64_t transid);
170 void jsession_update_transid(struct jsession *ss, int64_t transid);
171 int jsession_check(struct jsession *ss, struct jdata *jd);
172 void jsession_term(struct jsession *ss);
173 
174 struct jfile *jopen_fd(int fd);
175 struct jfile *jopen_prefix(const char *prefix, int rw);
176 void jclose(struct jfile *jf);
177 struct jdata *jread(struct jfile *jf, struct jdata *jd,
178                         enum jdirection direction);
179 struct jdata *jseek(struct jfile *jf, int64_t transid,
180                         enum jdirection direction);
181 void jwrite(struct jfile *jf, struct jdata *jd);
182 struct jdata *jref(struct jdata *jd);
183 void jfree(struct jfile *jf, struct jdata *jd);
184 void jf_warn(struct jfile *jf, const char *ctl, ...) __printflike(2, 3);
185 
186 struct jstream *jaddrecord(struct jsession *ss, struct jdata *jd);
187 void jscan_dispose(struct jstream *js);
188 
189 void dump_debug(struct jsession *ss, struct jdata *jd);
190 void dump_mirror(struct jsession *ss, struct jdata *jd);
191 void dump_record(struct jsession *ss, struct jdata *jd);
192 void dump_output(struct jsession *ss, struct jdata *jd);
193 
194 int jrecord_init(const char *record_prefix);
195 
196 int jsreadany(struct jstream *js, off_t off, const void **bufp);
197 int jsreadp(struct jstream *js, off_t off, const void **bufp, int bytes);
198 int jsread(struct jstream *js, off_t off, void *buf, int bytes);
199 int jsreadcallback(struct jstream *js, ssize_t (*func)(int, const void *, size_t), int fd, off_t off, int bytes);
200 
201