reindent-20030715
[openafs.git] / src / budb / database.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afs/auth.h>
11 #include <afs/bubasics.h>
12 #include <lock.h>
13 #include "budb.h"
14
15 typedef afs_uint32 dbadr;
16
17 struct hashTable {
18     afs_int32 functionType;     /* type of hash function */
19     afs_int32 threadOffset;     /* Byte offset of hash thread */
20     afs_int32 entries;          /* number of entries in hash table */
21     afs_int32 length;           /* number of hash table slots */
22     dbadr table;                /* pointer to actual table */
23     afs_int32 progress;         /* number empty buckets in oldTable */
24     afs_int32 oldLength;        /* slots in old table */
25     dbadr oldTable;             /* old table */
26 };
27
28 /* text blocks and related structures */
29
30 struct textBlock {
31     afs_uint32 version;         /* for access consistency checks */
32     afs_int32 size;             /* size in bytes */
33     dbadr textAddr;             /* list of blocks */
34     afs_int32 newsize;          /* replacement size */
35     dbadr newTextAddr;          /* list of new blocks */
36 };
37
38 struct db_lockS {
39     afs_int32 type;             /* user defined - for consistency checks */
40     afs_int32 lockState;        /* locked/free */
41     afs_int32 lockTime;         /* when locked */
42     afs_int32 expires;          /* when timeout expires */
43     afs_int32 instanceId;       /* user instance id */
44     int lockHost;               /* locking host, if possible */
45 };
46
47 typedef struct db_lockS db_lockT;
48 typedef db_lockT *db_lockP;
49
50 /* hash table function types */
51 #define HT_dumpIden_FUNCTION    1
52 #define HT_dumpName_FUNCTION    2
53 #define HT_tapeName_FUNCTION    3
54 #define HT_volName_FUNCTION     4
55
56 #define HT_MAX_FUNCTION         4
57
58 /* block types */
59
60 #define free_BLOCK              0
61 #define volFragment_BLOCK       1
62 #define volInfo_BLOCK           2
63 #define tape_BLOCK              3
64 #define dump_BLOCK              4
65 #define MAX_STRUCTURE_BLOCK_TYPE 4
66 #define hashTable_BLOCK         5
67 #define text_BLOCK              6
68 #define NBLOCKTYPES             7
69
70 /* This defines root pointers to all parts of the database.  It is allocated
71    starting at location zero. */
72
73 #define BUDB_VERSION 1
74
75 struct dbHeader {
76     afs_int32 version;          /* database version number */
77     dumpId lastDumpId;          /* last dump id value */
78     afs_uint32 lastTapeId;      /* last tape id allocated */
79     afs_uint32 lastInstanceId;  /* last instance Id handed out */
80     dbadr freePtrs[NBLOCKTYPES];        /* pointers to free blocks */
81     dbadr eofPtr;               /* first free byte in db */
82     afs_int32 nHTBuckets;       /* size of hashtable blocks */
83     Date lastUpdate;            /* time of last change to database */
84     afs_int32 ptrs[10];         /* spare pointers */
85     struct hashTable volName;   /* hash tables */
86     struct hashTable tapeName;
87     struct hashTable dumpName;
88     struct hashTable dumpIden;
89     db_lockT textLocks[TB_MAX]; /* locks for text blocks */
90     struct textBlock textBlock[TB_MAX]; /* raw text information */
91     afs_int32 words[10];        /* spare words */
92     afs_int32 checkVersion;     /* version# for consistency checking */
93 };
94
95 /* The database is composed of several types of structures of different sizes.
96    To simplify recovery from crashes, allocation is done in fixed size blocks.
97    All structures in a block are of the same type.  Each block has a header
98    which identifies its type, a count of the number of free structures, and a
99    pointer to the next block of the same type. */
100
101 #define BLOCKSIZE 2048
102 #define BlockBase(a)                                                    \
103         ((((a) - sizeof(struct dbHeader)) & ~(BLOCKSIZE-1)) +           \
104          sizeof(struct dbHeader))
105
106 struct blockHeader {
107     char type;                  /* type: implies structure size */
108     char flags;                 /* miscellaneous bits */
109     short nFree;                /* number of free structures */
110     dbadr next;                 /* next free block same type */
111 };
112
113 #define BLOCK_DATA_SIZE (BLOCKSIZE-sizeof(struct blockHeader))
114
115 struct block {
116     struct blockHeader h;
117     char a[BLOCKSIZE - sizeof(struct blockHeader)];
118 };
119
120 #define NhtBucketS ((BLOCKSIZE-sizeof(struct blockHeader))/sizeof(dbadr))
121 struct htBlock {
122     struct blockHeader h;
123     dbadr bucket[NhtBucketS];
124 };
125
126 /* The database contains one volFragment for every occurence of volume data on
127  * any tape. */
128
129 #define VOLFRAGMENTFLAGS 0xffff
130 struct volFragment {
131     dbadr vol;                  /* full volume info */
132     dbadr sameNameChain;        /* next with same vol info */
133     dbadr tape;                 /* tape containing this fragment */
134     dbadr sameTapeChain;        /* next fragment on tape */
135
136     afs_int32 position;         /* on tape */
137     Date clone;                 /* clone date of volume */
138     Date incTime;               /* time for incremental; 0 => full */
139     afs_int32 startByte;        /* first byte of volume in this frag */
140     afs_uint32 nBytes;          /* bytes in fragment */
141     short flags;                /* miscellaneous bits */
142     short sequence;             /* seq of frag in dumped volume */
143 };
144 #define NvolFragmentS 45
145 struct vfBlock {
146     struct blockHeader h;
147     struct {
148         struct volFragment s;
149         char pad[4];
150     } a[NvolFragmentS];
151 };
152
153
154 /* This represents additional information about a file system volume that
155  * changes relatively infrequently.  Its purpose is to minimize the size of the
156  * folFragment structure. */
157
158 #define VOLINFOFLAGS 0xffff0000
159 struct volInfo {
160     char name[BU_MAXNAMELEN];   /* name of volume: the hash key */
161     afs_int32 flags;            /* miscellaneous bits */
162     afs_int32 id;               /* read-write volume's id */
163     char server[BU_MAXHOSTLEN]; /* file server */
164     afs_int32 partition;        /* disk partition on server */
165     afs_int32 nFrags;           /* number of fragments on list */
166
167     dbadr nameHashChain;        /* for volume name hash table */
168     dbadr sameNameHead;         /* volInfo of first struct this name */
169     dbadr sameNameChain;        /* next volume with this name */
170     dbadr firstFragment;        /* all volFragment with this volInfo */
171 };
172 #define NvolInfoS 20
173 struct viBlock {
174     struct blockHeader h;
175     struct {
176         struct volInfo s;
177         char pad[4];
178     } a[NvolInfoS];
179 };
180
181
182 /* The tape structure represents information of every physical tape in the
183  * backup system.  Some of the data may persist event though the tape's
184  * contents are repeatedly overwritten. */
185
186 struct tape {
187     char name[BU_MAXTAPELEN];   /* name of physical tape */
188     afs_int32 flags;            /* miscellaneous bits */
189     Date written;               /* tape writing started */
190     Date expires;               /* expiration date */
191     afs_uint32 nMBytes;         /* length of tape in Mbytes */
192     afs_uint32 nBytes;          /* remainder of Mbytes  */
193     afs_int32 nFiles;           /*  ditto for  EOFs */
194     afs_int32 nVolumes;         /*  ditto for  volume fragments */
195     afs_int32 seq;              /* sequence in tapeSet */
196
197     afs_uint32 labelpos;        /* Position of the tape label */
198     afs_int32 useCount;         /* # of times used */
199     afs_int32 useKBytes;        /* How much of tape is used (in KBytes) */
200
201     dbadr nameHashChain;        /* for tape name hash table */
202     dbadr dump;                 /* dump (tapeSet) this is part of */
203     dbadr nextTape;             /* next tape in dump (tapeSet) */
204     dbadr firstVol;             /* first volume fragment on tape */
205 };
206
207 #define NtapeS 20
208 struct tBlock {
209     struct blockHeader h;
210     struct {
211         struct tape s;
212         char pad[8];
213     } a[NtapeS];
214 };
215
216 /* The structure describes every dump operation whose contents are still availe
217  * on at least one tape.
218  */
219
220 struct dump {
221     /* similar to budb_dumpEntry */
222     afs_int32 id;               /* unique identifier for dump */
223     dumpId parent;              /* parent dump */
224     afs_int32 level;            /* dump level */
225     afs_int32 flags;            /* miscellaneous bits */
226     char volumeSet[BU_MAXNAMELEN];      /* name of volume that was dumped */
227     char dumpPath[BU_MAX_DUMP_PATH];    /* "path" name of dump level */
228     char dumpName[BU_MAXNAMELEN];       /* dump name */
229     Date created;               /* time dump initiated */
230 /*  Date  incTime;                      target time for incremental dumps */
231     afs_int32 nVolumes;         /* number of volumes in dump */
232     struct budb_tapeSet tapes;  /* tapes for this dump */
233     struct ktc_principal dumper;        /* user doing dump */
234     afs_uint32 initialDumpID;   /* The dumpid of the initisl dump (for appended dumps) */
235     dbadr appendedDumpChain;    /* Ptr to dump appended to this dump */
236
237     dbadr idHashChain;          /* for dump id hash table */
238     dbadr nameHashChain;        /* for dump name hash table */
239     dbadr firstTape;            /* first tape in dump (tapeSet) */
240 };
241
242 typedef struct dump dbDumpT;
243 typedef dbDumpT *dbDumpP;
244
245 #define NdumpS 3
246 struct dBlock {
247     struct blockHeader h;
248     struct {
249         struct dump s;
250         char pad[44];
251     } a[NdumpS];
252 };
253
254 /* This structure is used to cache the hash table blocks for a database hash
255    table.  The htBlock structure is modified slightly to accomodate this
256    mechanism.  On disk the next ptr links the consecutive blocks of the hash
257    table together.  In memory this information is inferred */
258
259 struct memoryHTBlock {
260     int valid;                  /* block needs to be read in */
261     dbadr a;                    /* where stored in db */
262     struct htBlock b;
263 };
264
265 struct memoryHashTable {
266     struct hashTable *ht;       /* ptr to appropriate db.h hashtable */
267     /* these are host byte order version of the corresponding HT fields */
268     int threadOffset;           /* Byte offset of hash thread */
269     int length;                 /* number of hash table slots */
270     int oldLength;              /* slots in old table */
271     int progress;               /* number empty buckets in oldTable */
272     /* these are the memory copies of the hash table buckets */
273     int size;                   /* allocated size of array */
274     struct memoryHTBlock *(*blocks);    /* ptr to array of ht block pointers */
275     int oldSize;                /*  ditto for old HT */
276     struct memoryHTBlock *(*oldBlocks);
277 };
278
279 struct memoryDB {               /* in core copies of database structures */
280     struct Lock lock;
281     Date readTime;
282     struct dbHeader h;
283     struct memoryHashTable volName;
284     struct memoryHashTable tapeName;
285     struct memoryHashTable dumpName;
286     struct memoryHashTable dumpIden;
287     struct textBlock textBlock[TB_NUM];
288 };
289 extern struct memoryDB db;
290
291 #define set_header_word(ut,field,value) \
292     dbwrite ((ut), ((char *)&(db.h.field) - (char *)&db.h), \
293              ((db.h.field = (value)), (char *)&(db.h.field)), \
294              sizeof(afs_int32))
295
296 #define set_word_offset(ut,a,b,offset,value)                          \
297     dbwrite ((ut), (a)+(offset),                                      \
298              (*(afs_int32 *)((char *)(b) + (offset)) = (value),       \
299               (char *)((char *)(b) + (offset))),                      \
300              sizeof(afs_int32))
301
302 #define set_word_addr(ut,a,b,addr,value)                              \
303     dbwrite ((ut), (a)+((char *)(addr) - (char *)(b)),                \
304              (*(afs_int32 *)(addr) = (value),                         \
305               (char *)(addr)),                                        \
306              sizeof(afs_int32))
307
308 #ifdef notdef
309 /* simple min/max macros */
310 #define MIN(x,y)        ((x) < (y) ? (x) : (y))
311 #define MAX(x,y)        ((x) > (y) ? (x) : (y))
312 #endif /* notdef */
313
314 struct memoryHashTable *ht_GetType();
315 extern afs_uint32 ht_HashEntry();
316 extern dbadr ht_LookupBucket();