dafs-20060317
[openafs.git] / src / tviced / serialize_state.h
1 /*
2  * Copyright 2006, Sine Nomine Associates 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 /*
11  * demand attach fs
12  * fileserver state serialization
13  */
14
15 #ifndef _AFS_TVICED_SERIALIZE_STATE_H
16 #define _AFS_TVICED_SERIALIZE_STATE_H
17
18 #ifdef AFS_DEMAND_ATTACH_FS
19
20 #define FS_STATE_MAGIC 0x62FA841C
21 #define FS_STATE_VERSION 2
22
23 #define HOST_STATE_MAGIC 0x7B8C9DAE
24 #define HOST_STATE_VERSION 2
25
26 #define HOST_STATE_ENTRY_MAGIC 0xA8B9CADB
27
28 #define CALLBACK_STATE_MAGIC 0x89DE67BC
29 #define CALLBACK_STATE_VERSION 1
30
31 #define CALLBACK_STATE_TIMEOUT_MAGIC 0x99DD5511
32 #define CALLBACK_STATE_FEHASH_MAGIC 0x77BB33FF
33 #define CALLBACK_STATE_ENTRY_MAGIC 0x54637281
34
35 #define ACTIVE_VOLUME_STATE_MAGIC 0xAC7557CA
36 #define ACTIVE_VOLUME_STATE_VERSION 1
37
38 #define ACTIVE_VOLUME_STATE_AVEHASH_MAGIC 0xBADDF00D
39
40 #define HOST_STATE_VALID_WINDOW 1800 /* 30 minutes */
41
42 /*
43  * on-disk structures
44  */
45 struct disk_version_stamp {
46     afs_uint32 magic;
47     afs_uint32 version;
48 };
49
50 /* 1024 byte header structure */
51 struct fs_state_header {
52     struct disk_version_stamp stamp;  /* version stamp */
53     afs_uint32 timestamp;             /* timestamp of save */
54     afs_uint32 sys_name;              /* sys name id for this machine */
55     afsUUID server_uuid;              /* server's UUID */
56     byte valid;                       /* whether header contents are valid */
57     byte endianness;                  /* endianness sanity check (0 for LE, 1 for BE) */
58     byte stats_detailed;              /* fs stats detailed sanity check */
59     byte padding1[1];                 /* padding */
60     afs_uint32 reserved1[23];         /* for expansion */
61     afs_uint64 avol_offset;           /* offset of active volumes structure */
62     afs_uint64 h_offset;              /* offset of host_state_header structure */
63     afs_uint64 cb_offset;             /* offset of callback_state_header structure */
64     afs_uint64 vlru_offset;           /* offset of vlru state structure */
65     afs_uint32 reserved2[56];         /* for expansion */
66     char server_version_string[128];  /* version string from AFS_component_version_number.c */
67     afs_uint32 reserved3[128];        /* for expansion */
68 };
69
70 /*
71  * host package serialization
72  */
73
74 /* 256 byte header for the host state data */
75 struct host_state_header {
76     struct disk_version_stamp stamp;  /* host state version stamp */
77     afs_uint32 records;               /* number of stored host records */
78     afs_uint32 index_max;             /* max index value encountered */
79     afs_uint32 reserved[60];          /* for expansion */
80 };
81
82 /* 32 byte host entry header */
83 struct host_state_entry_header {
84     afs_uint32 magic;         /* stamp */
85     afs_uint32 len;           /* number of bytes in this record */
86     afs_uint32 interfaces;    /* number of interfaces included in record */
87     afs_uint32 hcps;          /* number of hcps entries in record */
88     afs_uint32 reserved[4];
89 };
90
91 /* 36 byte host entry structure */
92 struct hostDiskEntry {
93     afs_uint32 host;            /* IP address of host interface that is
94                                  * currently being used, in network
95                                  * byte order */
96     afs_uint16 port;            /* port address of host */
97     afs_uint16 hostFlags;       /*  bit map */
98     byte Console;               /* XXXX This host is a console */
99     byte hcpsfailed;            /* Retry the cps call next time */
100     byte hcps_valid;            /* prlist_val not null */
101 #if FS_STATS_DETAILED
102     byte InSameNetwork;         /*Is host's addr in the same network as
103                                  * the File Server's? */
104 #else
105     byte padding1[1];           /* for padding */
106 #endif                          /* FS_STATS_DETAILED */
107     afs_uint32 hcps_len;        /* length of hcps */
108     afs_uint32 LastCall;        /* time of last call from host */
109     afs_uint32 ActiveCall;      /* time of any call but gettime */
110     afs_uint32 cpsCall;         /* time of last cps call from this host */
111     afs_uint32 cblist;          /* Call back list for this host */
112     afs_uint32 index;           /* index for correlating w/ callback dumps */
113 };
114
115 /*
116  * callback package serialization
117  */
118
119 /* 512 byte header */
120 struct callback_state_header {
121     struct disk_version_stamp stamp;    /* callback state version stamp */
122     afs_uint32 nFEs;                    /* number of FileEntry records */
123     afs_uint32 nCBs;                    /* number of CallBack records */
124     afs_uint32 fe_max;                  /* max FileEntry index */
125     afs_uint32 cb_max;                  /* max CallBack index */
126     afs_int32 tfirst;                   /* first valid timeout */
127     afs_uint32 reserved[115];           /* for expansion */
128     afs_uint64 timeout_offset;          /* offset of timeout queue heads */
129     afs_uint64 fehash_offset;           /* offset of file entry hash buckets */
130     afs_uint64 fe_offset;               /* offset of first file entry */
131 };
132
133 /* 32 byte header */
134 struct callback_state_timeout_header {
135     afs_uint32 magic;         /* magic number for timeout header */
136     afs_uint32 len;           /* total length of header and timeout records */
137     afs_uint32 records;       /* number of timeout records */
138     afs_uint32 reserved[5];
139 };
140
141 /* 32 byte header */
142 struct callback_state_fehash_header {
143     afs_uint32 magic;         /* magic number for fehash header */
144     afs_uint32 len;           /* total length of header and fehash bucket heads */
145     afs_uint32 records;       /* number of hash buckets */
146     afs_uint32 reserved[5];
147 };
148
149 /* 32 byte header */
150 struct callback_state_entry_header {
151     afs_uint32 magic;         /* magic number for FE entry */
152     afs_uint32 len;           /* number of bytes in this record */
153     afs_uint32 nCBs;          /* number of callbacks for this FE */
154     afs_uint32 reserved[5];
155 };
156
157 struct FEDiskEntry {
158     struct FileEntry fe;
159     afs_uint32 index;
160 };
161
162 struct CBDiskEntry {
163     struct CallBack cb;
164     afs_uint32 index;
165 };
166
167 /*
168  * active volumes state serialization
169  *
170  * these structures are meant to support
171  * automated salvaging of active volumes
172  * in the event of a fileserver crash
173  */
174
175 /* 512 byte header */
176 struct active_volume_state_header {
177     struct disk_version_stamp stamp;    /* callback state version stamp */
178     afs_uint32 nAVEs;                   /* number of ActiveVolumeEntry records */
179     afs_uint32 init_timestamp;          /* timestamp of AVE initialization */
180     afs_uint32 update_timetamp;         /* timestamp of last AVE update */
181     afs_uint32 reserved[119];           /* for expansion */
182     afs_uint64 avehash_offset;          /* offset of active volume entry hash buckets */
183     afs_uint64 ave_offset;              /* offset of first active volume entry */
184 };
185
186 /* 32 byte header */
187 struct active_volume_state_avehash_header {
188     afs_uint32 magic;         /* magic number for avehash header */
189     afs_uint32 len;           /* total length of header and avehash bucket heads */
190     afs_uint32 records;       /* number of hash buckets */
191     afs_uint32 reserved[5];
192 };
193
194 typedef afs_uint32 active_volume_state_avehash_entry;
195
196 /* active volume entry */
197 struct AVDiskEntry {
198     afs_uint32 volume;
199     afs_uint32 partition;
200     afs_uint32 hash_next;
201 };
202
203
204 /*
205  * dump runtime state
206  */
207 struct idx_map_entry_t {
208     afs_uint32 old_idx;                    /* host hash id from last runtime */
209     afs_uint32 new_idx;                    /* host hash id for this runtime */
210 };
211
212
213 /* verification process sanity check constants
214  *
215  * make them fairly large so we don't get 
216  * false positives 
217  */
218 #define FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN    100000     /* max elements in a host uuid-hash chain */
219 #define FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN    2000000    /* max elements in a host ipv4-hash chain */
220 #define FS_STATE_FE_MAX_HASH_CHAIN_LEN        100000     /* max elements in a FE fid-hash chain */
221 #define FS_STATE_FCB_MAX_LIST_LEN             100000     /* max elements in a per-FE CB list */
222 #define FS_STATE_HCB_MAX_LIST_LEN             100000     /* max elements in a per-host CB list */
223 #define FS_STATE_TCB_MAX_LIST_LEN             100000     /* max elements in a per-timeout CB list */
224
225
226 /*
227  * main state serialization state structure
228  */
229
230 struct fs_dump_state {
231     enum {
232         FS_STATE_DUMP_MODE,
233         FS_STATE_LOAD_MODE
234     } mode;
235     struct {
236         byte do_host_restore;              /* whether host restore should be done */
237         byte some_steps_skipped;           /* whether some steps were skipped */
238         byte warnings_generated;           /* whether any warnings were generated during restore */
239     } flags;
240     afs_fsize_t file_len;
241     int fd;                                /* fd of the current dump file */
242     int bail;                              /* non-zero if something went wrong */
243     char * fn;                             /* name of the current dump file */
244     struct {                               /* memory map of dump file */
245         void * map;
246         void * cursor;
247         afs_foff_t offset;
248         afs_fsize_t size;
249     } mmap;
250     struct fs_state_header * hdr;          /* main header */
251     struct host_state_header * h_hdr;      /* header for host state data */
252     struct callback_state_header * cb_hdr; /* header for callback state data */
253     struct callback_state_timeout_header * cb_timeout_hdr;
254     struct callback_state_fehash_header * cb_fehash_hdr;
255     afs_uint64 eof_offset;                 /* current end of file offset */
256     struct {
257         int len;                           /* number of host entries in map */
258         struct idx_map_entry_t * entries;
259     } h_map;
260     struct {
261         int len;
262         struct idx_map_entry_t * entries;
263     } fe_map;
264     struct {
265         int len;
266         struct idx_map_entry_t * entries;
267     } cb_map;
268 };
269
270
271 /* prototypes */
272
273 /* serialize_state.c */
274 extern int fs_stateWrite(struct fs_dump_state * state,
275                          void * buf, size_t len);
276 extern int fs_stateRead(struct fs_dump_state * state,
277                         void * buf, size_t len);
278 extern int fs_stateWriteV(struct fs_dump_state * state,
279                           struct iovec * iov, int niov);
280 extern int fs_stateReadV(struct fs_dump_state * state,
281                          struct iovec * iov, int niov);
282 extern int fs_stateSync(struct fs_dump_state * state);
283 extern int fs_stateWriteHeader(struct fs_dump_state * state,
284                                afs_uint64 * offset,
285                                void * hdr, size_t len);
286 extern int fs_stateReadHeader(struct fs_dump_state * state,
287                               afs_uint64 * offset,
288                               void * hdr, size_t len);
289 extern int fs_stateIncEOF(struct fs_dump_state * state,
290                           afs_int32 len);
291 extern int fs_stateSeek(struct fs_dump_state * state,
292                         afs_uint64 * offset);
293
294 /* host.c */
295 extern int h_stateSave(struct fs_dump_state * state);
296 extern int h_stateRestore(struct fs_dump_state * state);
297 extern int h_stateRestoreIndices(struct fs_dump_state * state);
298 extern int h_stateVerify(struct fs_dump_state * state);
299 extern int h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new);
300
301 /* callback.c */
302 extern int cb_stateSave(struct fs_dump_state * state);
303 extern int cb_stateRestore(struct fs_dump_state * state);
304 extern int cb_stateRestoreIndices(struct fs_dump_state * state);
305 extern int cb_stateVerify(struct fs_dump_state * state);
306 extern int cb_stateVerifyHCBList(struct fs_dump_state * state, struct host * host);
307 extern int fe_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new);
308 extern int cb_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new);
309
310 #endif /* AFS_DEMAND_ATTACH_FS */
311 #endif /* _AFS_TVICED_SERIALIZE_STATE_H */