1ce5e5ac4SMatthew Dillon /* 2ce5e5ac4SMatthew Dillon * Copyright (c) 2004,2005 The DragonFly Project. All rights reserved. 3ce5e5ac4SMatthew Dillon * 4ce5e5ac4SMatthew Dillon * This code is derived from software contributed to The DragonFly Project 5ce5e5ac4SMatthew Dillon * by Matthew Dillon <dillon@backplane.com> 6ce5e5ac4SMatthew Dillon * 7ce5e5ac4SMatthew Dillon * Redistribution and use in source and binary forms, with or without 8ce5e5ac4SMatthew Dillon * modification, are permitted provided that the following conditions 9ce5e5ac4SMatthew Dillon * are met: 10ce5e5ac4SMatthew Dillon * 11ce5e5ac4SMatthew Dillon * 1. Redistributions of source code must retain the above copyright 12ce5e5ac4SMatthew Dillon * notice, this list of conditions and the following disclaimer. 13ce5e5ac4SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright 14ce5e5ac4SMatthew Dillon * notice, this list of conditions and the following disclaimer in 15ce5e5ac4SMatthew Dillon * the documentation and/or other materials provided with the 16ce5e5ac4SMatthew Dillon * distribution. 17ce5e5ac4SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its 18ce5e5ac4SMatthew Dillon * contributors may be used to endorse or promote products derived 19ce5e5ac4SMatthew Dillon * from this software without specific, prior written permission. 20ce5e5ac4SMatthew Dillon * 21ce5e5ac4SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22ce5e5ac4SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23ce5e5ac4SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24ce5e5ac4SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25ce5e5ac4SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26ce5e5ac4SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27ce5e5ac4SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28ce5e5ac4SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29ce5e5ac4SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30ce5e5ac4SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31ce5e5ac4SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32ce5e5ac4SMatthew Dillon * SUCH DAMAGE. 33ce5e5ac4SMatthew Dillon */ 34ce5e5ac4SMatthew Dillon 35ce5e5ac4SMatthew Dillon #include <sys/types.h> 36ce5e5ac4SMatthew Dillon #include <sys/time.h> 37ce5e5ac4SMatthew Dillon #include <sys/journal.h> 38712e03b0SMatthew Dillon #include <sys/stat.h> 39ce5e5ac4SMatthew Dillon #include <stdio.h> 40ce5e5ac4SMatthew Dillon #include <stdarg.h> 41ce5e5ac4SMatthew Dillon #include <stdlib.h> 42ce5e5ac4SMatthew Dillon #include <stddef.h> 43ce5e5ac4SMatthew Dillon #include <unistd.h> 44ce5e5ac4SMatthew Dillon #include <string.h> 45c2b2044aSMatthew Dillon #include <limits.h> 46ce5e5ac4SMatthew Dillon #include <time.h> 47ce5e5ac4SMatthew Dillon #include <fcntl.h> 48ce5e5ac4SMatthew Dillon #include <errno.h> 49712e03b0SMatthew Dillon #include <ctype.h> 50712e03b0SMatthew Dillon #include <assert.h> 51712e03b0SMatthew Dillon #include "jattr.h" 52ce5e5ac4SMatthew Dillon 53c2b2044aSMatthew Dillon struct jdata; 54c2b2044aSMatthew Dillon 55a9cb3889SMatthew Dillon enum jdirection { JD_FORWARDS, JD_BACKWARDS, JD_SEQFIRST, JD_SEQLAST }; 56ce5e5ac4SMatthew Dillon 57ce5e5ac4SMatthew Dillon struct jfile { 58ce5e5ac4SMatthew Dillon off_t jf_pos; /* current seek position */ 59bb406b71SMatthew Dillon int jf_fd; /* reading/scanning */ 60bb406b71SMatthew Dillon int jf_write_fd; /* appending */ 61bb406b71SMatthew Dillon off_t jf_write_pos; /* append position */ 62ce5e5ac4SMatthew Dillon int jf_error; 63c2b2044aSMatthew Dillon int jf_open_flags; 64c2b2044aSMatthew Dillon char *jf_prefix; /* prefix: name */ 65c2b2044aSMatthew Dillon unsigned int jf_seq_beg; /* prefix: sequence space */ 66c2b2044aSMatthew Dillon unsigned int jf_seq_end; /* prefix: sequence space */ 67c2b2044aSMatthew Dillon unsigned int jf_seq; /* prefix: current sequence number */ 68bb406b71SMatthew Dillon int64_t jf_last_transid;/* prefix: last recorded transid */ 69bb406b71SMatthew Dillon }; 70bb406b71SMatthew Dillon 71bb406b71SMatthew Dillon /* 72bb406b71SMatthew Dillon * Output session (debug, record, output, etc) 73bb406b71SMatthew Dillon */ 74bb406b71SMatthew Dillon struct jsession { 75bb406b71SMatthew Dillon struct jfile *ss_jfin; 76bb406b71SMatthew Dillon struct jfile *ss_jfout; 77a9cb3889SMatthew Dillon enum jdirection ss_direction; 78bb406b71SMatthew Dillon const char *ss_mirror_directory; 79bb406b71SMatthew Dillon const char *ss_transid_file; 80bb406b71SMatthew Dillon int ss_transid_fd; 81bb406b71SMatthew Dillon int64_t ss_transid; 82ce5e5ac4SMatthew Dillon }; 83ce5e5ac4SMatthew Dillon 845adba82eSMatthew Dillon #define JF_FULL_DUPLEX 0x0001 855adba82eSMatthew Dillon 86ce5e5ac4SMatthew Dillon struct jdata { 87c2b2044aSMatthew Dillon int64_t jd_transid; /* transaction id from header */ 88c2b2044aSMatthew Dillon int jd_alloc; /* allocated bytes */ 89c2b2044aSMatthew Dillon int jd_size; /* data bytes */ 90c2b2044aSMatthew Dillon int jd_refs; /* ref count */ 91a9cb3889SMatthew Dillon unsigned int jd_seq; /* location data */ 92a9cb3889SMatthew Dillon off_t jd_pos; /* location data */ 93ce5e5ac4SMatthew Dillon char jd_data[4]; /* must be last field */ 94ce5e5ac4SMatthew Dillon }; 95ce5e5ac4SMatthew Dillon 96ce5e5ac4SMatthew Dillon struct jstream { 97ce5e5ac4SMatthew Dillon struct jstream *js_next; /* linked list / same transaction */ 98bb406b71SMatthew Dillon struct jsession *js_session; 99ce5e5ac4SMatthew Dillon char *js_alloc_buf; 100ce5e5ac4SMatthew Dillon int js_alloc_size; 101712e03b0SMatthew Dillon 102712e03b0SMatthew Dillon /* 103712e03b0SMatthew Dillon * Normalized fields strip all rawrecbeg, rawrecend, and deadspace except 104712e03b0SMatthew Dillon * for the initial rawrecbeg header. 105712e03b0SMatthew Dillon */ 106712e03b0SMatthew Dillon char *js_normalized_base; 107712e03b0SMatthew Dillon int js_normalized_size; 108712e03b0SMatthew Dillon off_t js_normalized_off; 109712e03b0SMatthew Dillon off_t js_normalized_total; 110712e03b0SMatthew Dillon 111712e03b0SMatthew Dillon /* 112712e03b0SMatthew Dillon * This is used by the first js record only to cache other records in the 113712e03b0SMatthew Dillon * chain. 114712e03b0SMatthew Dillon */ 115ce5e5ac4SMatthew Dillon struct jstream *js_cache; 116c2b2044aSMatthew Dillon struct jdata *js_jdata; 117c2b2044aSMatthew Dillon struct journal_rawrecbeg *js_head; 118ce5e5ac4SMatthew Dillon }; 119ce5e5ac4SMatthew Dillon 120ce5e5ac4SMatthew Dillon struct jhash { 121ce5e5ac4SMatthew Dillon struct jhash *jh_hash; 122ce5e5ac4SMatthew Dillon struct jstream *jh_first; 123ce5e5ac4SMatthew Dillon struct jstream *jh_last; 124bb406b71SMatthew Dillon struct jsession *jh_session; 125ce5e5ac4SMatthew Dillon int16_t jh_transid; 126ce5e5ac4SMatthew Dillon }; 127ce5e5ac4SMatthew Dillon 128ce5e5ac4SMatthew Dillon #define JHASH_SIZE 1024 129ce5e5ac4SMatthew Dillon #define JHASH_MASK (JHASH_SIZE - 1) 130ce5e5ac4SMatthew Dillon 131c2b2044aSMatthew Dillon #define JMODEF_DEBUG 0x00000001 132c2b2044aSMatthew Dillon #define JMODEF_MIRROR 0x00000002 133c2b2044aSMatthew Dillon #define JMODEF_UNUSED0004 0x00000004 134c2b2044aSMatthew Dillon #define JMODEF_RECORD 0x00000008 135c2b2044aSMatthew Dillon #define JMODEF_RECORD_TMP 0x00000010 136c2b2044aSMatthew Dillon #define JMODEF_INPUT_FULL 0x00000020 137c2b2044aSMatthew Dillon #define JMODEF_INPUT_PIPE 0x00000040 138c2b2044aSMatthew Dillon #define JMODEF_INPUT_PREFIX 0x00000080 139c2b2044aSMatthew Dillon #define JMODEF_OUTPUT 0x00000100 140c2b2044aSMatthew Dillon #define JMODEF_OUTPUT_FULL 0x00000200 141c2b2044aSMatthew Dillon #define JMODEF_MEMORY_TRACKING 0x00000400 142cbeb73b9SMatthew Dillon #define JMODEF_LOOP_FOREVER 0x00000800 143c2b2044aSMatthew Dillon 144c2b2044aSMatthew Dillon #define JMODEF_OUTPUT_TRANSID_GOOD 0x00010000 145c2b2044aSMatthew Dillon #define JMODEF_RECORD_TRANSID_GOOD 0x00020000 146c2b2044aSMatthew Dillon #define JMODEF_MIRROR_TRANSID_GOOD 0x00040000 147c2b2044aSMatthew Dillon #define JMODEF_TRANSID_GOOD_MASK (JMODEF_OUTPUT_TRANSID_GOOD|\ 148c2b2044aSMatthew Dillon JMODEF_RECORD_TRANSID_GOOD|\ 149c2b2044aSMatthew Dillon JMODEF_MIRROR_TRANSID_GOOD) 150c2b2044aSMatthew Dillon #define JMODEF_COMMAND_MASK (JMODEF_RECORD|JMODEF_MIRROR|JMODEF_DEBUG|\ 151c2b2044aSMatthew Dillon JMODEF_OUTPUT) 152c2b2044aSMatthew Dillon 153c2b2044aSMatthew Dillon extern int jmodes; 154c2b2044aSMatthew Dillon extern int fsync_opt; 155bb406b71SMatthew Dillon extern int verbose_opt; 156bb406b71SMatthew Dillon extern off_t prefix_file_size; 157c2b2044aSMatthew Dillon extern off_t trans_count; 158712e03b0SMatthew Dillon 159ce5e5ac4SMatthew Dillon const char *type_to_name(int16_t rectype); 1609c118cb2SMatthew Dillon void stringout(FILE *fp, char c, int exact); 161712e03b0SMatthew Dillon void jattr_reset(struct jattr *jattr); 162712e03b0SMatthew Dillon int64_t buf_to_int64(const void *buf, int bytes); 163712e03b0SMatthew Dillon char *dupdatastr(const void *buf, int bytes); 164712e03b0SMatthew Dillon char *dupdatapath(const void *buf, int bytes); 165c2b2044aSMatthew Dillon void get_transid_from_file(const char *path, int64_t *transid, int flags); 166712e03b0SMatthew Dillon 167bb406b71SMatthew Dillon void jsession_init(struct jsession *ss, struct jfile *jfin, 168a9cb3889SMatthew Dillon enum jdirection direction, 169bb406b71SMatthew Dillon const char *transid_file, int64_t transid); 170bb406b71SMatthew Dillon void jsession_update_transid(struct jsession *ss, int64_t transid); 171*36456e49SMatthew Dillon int jsession_check(struct jsession *ss, struct jdata *jd); 172bb406b71SMatthew Dillon void jsession_term(struct jsession *ss); 173bb406b71SMatthew Dillon 174a9cb3889SMatthew Dillon struct jfile *jopen_fd(int fd); 175a9cb3889SMatthew Dillon struct jfile *jopen_prefix(const char *prefix, int rw); 176c2b2044aSMatthew Dillon void jclose(struct jfile *jf); 177a9cb3889SMatthew Dillon struct jdata *jread(struct jfile *jf, struct jdata *jd, 178a9cb3889SMatthew Dillon enum jdirection direction); 179a9cb3889SMatthew Dillon struct jdata *jseek(struct jfile *jf, int64_t transid, 180a9cb3889SMatthew Dillon enum jdirection direction); 181bb406b71SMatthew Dillon void jwrite(struct jfile *jf, struct jdata *jd); 182c2b2044aSMatthew Dillon struct jdata *jref(struct jdata *jd); 183c2b2044aSMatthew Dillon void jfree(struct jfile *jf, struct jdata *jd); 184b58f1e66SSascha Wildner void jf_warn(struct jfile *jf, const char *ctl, ...) __printflike(2, 3); 185ce5e5ac4SMatthew Dillon 186bb406b71SMatthew Dillon struct jstream *jaddrecord(struct jsession *ss, struct jdata *jd); 187c2b2044aSMatthew Dillon void jscan_dispose(struct jstream *js); 188c2b2044aSMatthew Dillon 189bb406b71SMatthew Dillon void dump_debug(struct jsession *ss, struct jdata *jd); 190bb406b71SMatthew Dillon void dump_mirror(struct jsession *ss, struct jdata *jd); 191bb406b71SMatthew Dillon void dump_record(struct jsession *ss, struct jdata *jd); 192bb406b71SMatthew Dillon void dump_output(struct jsession *ss, struct jdata *jd); 193c2b2044aSMatthew Dillon 194c2b2044aSMatthew Dillon int jrecord_init(const char *record_prefix); 195ce5e5ac4SMatthew Dillon 196ce5e5ac4SMatthew Dillon int jsreadany(struct jstream *js, off_t off, const void **bufp); 197ce5e5ac4SMatthew Dillon int jsreadp(struct jstream *js, off_t off, const void **bufp, int bytes); 198ce5e5ac4SMatthew Dillon int jsread(struct jstream *js, off_t off, void *buf, int bytes); 19936d6bdeeSMatthew Dillon int jsreadcallback(struct jstream *js, ssize_t (*func)(int, const void *, size_t), int fd, off_t off, int bytes); 20036d6bdeeSMatthew Dillon 201