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