Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / budb / database.h
1 /* Copyright (C) 1989,1990 Transarc Corporation - All rights reserved */
2 /*
3  * (C) COPYRIGHT IBM CORPORATION 1989
4  * LICENSED MATERIALS - PROPERTY OF IBM
5  */
6
7
8 #include <afs/auth.h>
9 #include <afs/bubasics.h>
10 #include <lock.h>
11 #include "budb.h"
12
13 typedef afs_uint32 dbadr;
14
15 struct hashTable {
16     afs_int32 functionType;                     /* type of hash function */
17     afs_int32 threadOffset;                     /* Byte offset of hash thread */
18     afs_int32 entries;                  /* number of entries in hash table */
19     afs_int32 length;                   /* number of hash table slots */
20     dbadr table;                        /* pointer to actual table */
21     afs_int32 progress;                 /* number empty buckets in oldTable */
22     afs_int32 oldLength;                        /* slots in old table */
23     dbadr oldTable;                     /* old table */
24 };
25
26 /* text blocks and related structures */
27
28 struct  textBlock
29 {
30     afs_uint32  version;                        /* for access consistency checks */
31     afs_int32  size;                            /* size in bytes */
32     dbadr textAddr;                     /* list of blocks */ 
33     afs_int32  newsize;                 /* replacement size */
34     dbadr newTextAddr;                  /* list of new blocks */
35 };
36
37 struct db_lockS
38 {
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 {
188     char  name[BU_MAXTAPELEN];          /* name of physical tape */
189     afs_int32  flags;                   /* miscellaneous bits */
190     Date  written;                      /* tape writing started */
191     Date  expires;                      /* expiration date */
192     afs_uint32  nMBytes;                        /* length of tape in Mbytes */
193     afs_uint32  nBytes;                 /* remainder of Mbytes  */
194     afs_int32  nFiles;                  /*  ditto for  EOFs */
195     afs_int32  nVolumes;                        /*  ditto for  volume fragments */
196     afs_int32  seq;                             /* sequence in tapeSet */
197
198     afs_uint32  labelpos;                       /* Position of the tape label */
199     afs_int32  useCount;                        /* # of times used */
200     afs_int32  useKBytes;                    /* How much of tape is used (in KBytes) */
201
202     dbadr nameHashChain;                /* for tape name hash table */
203     dbadr dump;                         /* dump (tapeSet) this is part of */
204     dbadr nextTape;                     /* next tape in dump (tapeSet) */
205     dbadr firstVol;                     /* first volume fragment on tape */
206 };
207
208 #define NtapeS 20
209 struct tBlock {
210     struct blockHeader h;
211     struct {
212         struct tape s;
213         char        pad[8];
214     } a[NtapeS];
215 };
216
217 /* The structure describes every dump operation whose contents are still availe
218  * on at least one tape.
219  */
220
221 struct dump
222 {
223     /* similar to budb_dumpEntry */
224     afs_int32  id;                              /* unique identifier for dump */
225     dumpId parent;                      /* parent dump */
226     afs_int32  level;                   /* dump level */
227     afs_int32  flags;                   /* miscellaneous bits */
228     char  volumeSet[BU_MAXNAMELEN];     /* name of volume that was dumped */
229     char  dumpPath[BU_MAX_DUMP_PATH];   /* "path" name of dump level */
230     char  dumpName[BU_MAXNAMELEN];      /* dump name */
231     Date  created;                      /* time dump initiated */
232 /*  Date  incTime;                      target time for incremental dumps */
233     afs_int32  nVolumes;                        /* number of volumes in dump */
234     struct budb_tapeSet tapes;          /* tapes for this dump */
235     struct ktc_principal dumper;        /* user doing dump */
236     afs_uint32 initialDumpID;               /* The dumpid of the initisl dump (for appended dumps) */
237     dbadr  appendedDumpChain;           /* Ptr to dump appended to this dump */
238
239     dbadr idHashChain;                  /* for dump id hash table */
240     dbadr nameHashChain;                /* for dump name hash table */
241     dbadr firstTape;                    /* first tape in dump (tapeSet) */
242 };
243
244 typedef struct dump  dbDumpT;
245 typedef dbDumpT     *dbDumpP;
246
247 #define NdumpS 3
248 struct dBlock {
249     struct blockHeader h;
250     struct {
251         struct dump s;
252         char        pad[44];
253     } a[NdumpS];
254 };
255
256 /* This structure is used to cache the hash table blocks for a database hash
257    table.  The htBlock structure is modified slightly to accomodate this
258    mechanism.  On disk the next ptr links the consecutive blocks of the hash
259    table together.  In memory this information is inferred */
260
261 struct memoryHTBlock {
262     int   valid;                        /* block needs to be read in */
263     dbadr a;                            /* where stored in db */
264     struct htBlock b;
265 };
266
267 struct memoryHashTable {
268     struct hashTable *ht;               /* ptr to appropriate db.h hashtable */
269     /* these are host byte order version of the corresponding HT fields */
270     int threadOffset;                   /* Byte offset of hash thread */
271     int length;                         /* number of hash table slots */
272     int oldLength;                      /* slots in old table */
273     int progress;                       /* number empty buckets in oldTable */
274     /* these are the memory copies of the hash table buckets */
275     int size;                           /* allocated size of array */
276     struct memoryHTBlock *(*blocks);    /* ptr to array of ht block pointers */
277     int oldSize;                        /*  ditto for old HT */
278     struct memoryHTBlock *(*oldBlocks);
279 };
280
281 struct memoryDB { /* in core copies of database structures */
282     struct Lock lock;
283     Date  readTime;
284     struct dbHeader h;
285     struct memoryHashTable volName;
286     struct memoryHashTable tapeName;
287     struct memoryHashTable dumpName;
288     struct memoryHashTable dumpIden;
289     struct textBlock textBlock[TB_NUM];
290 };
291 extern struct memoryDB db;
292
293 #define set_header_word(ut,field,value) \
294     dbwrite ((ut), ((char *)&(db.h.field) - (char *)&db.h), \
295              ((db.h.field = (value)), (char *)&(db.h.field)), \
296              sizeof(afs_int32))
297
298 #define set_word_offset(ut,a,b,offset,value)                          \
299     dbwrite ((ut), (a)+(offset),                                      \
300              (*(afs_int32 *)((char *)(b) + (offset)) = (value),       \
301               (char *)((char *)(b) + (offset))),                      \
302              sizeof(afs_int32))
303
304 #define set_word_addr(ut,a,b,addr,value)                              \
305     dbwrite ((ut), (a)+((char *)(addr) - (char *)(b)),                \
306              (*(afs_int32 *)(addr) = (value),                         \
307               (char *)(addr)),                                        \
308              sizeof(afs_int32))
309
310 #ifdef notdef
311 /* simple min/max macros */
312 #define MIN(x,y)        ((x) < (y) ? (x) : (y))
313 #define MAX(x,y)        ((x) > (y) ? (x) : (y))
314 #endif /* notdef */
315
316 struct memoryHashTable *ht_GetType ();
317 extern afs_uint32 ht_HashEntry ();
318 extern dbadr ht_LookupBucket ();
319