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