viced: make -vhashsize usable for non-DAFS
[openafs.git] / src / viced / 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     byte InSameNetwork;         /*Is host's addr in the same network as
106                                  * the File Server's? */
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     VolumeId 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     byte valid;                            /* whether or not this entry has been populated */
209     afs_uint32 old_idx;                    /* host hash id from last runtime */
210     afs_uint32 new_idx;                    /* host hash id for this runtime */
211 };
212
213
214 /* verification process sanity check constants
215  *
216  * make them fairly large so we don't get
217  * false positives
218  */
219 #define FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN    100000     /* max elements in a host uuid-hash chain */
220 #define FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN    2000000    /* max elements in a host ipv4-hash chain */
221 #define FS_STATE_FE_MAX_HASH_CHAIN_LEN        100000     /* max elements in a FE fid-hash chain */
222 #define FS_STATE_FCB_MAX_LIST_LEN             100000     /* max elements in a per-FE CB list */
223 #define FS_STATE_HCB_MAX_LIST_LEN             100000     /* max elements in a per-host CB list */
224 #define FS_STATE_TCB_MAX_LIST_LEN             100000     /* max elements in a per-timeout CB list */
225
226
227 /*
228  * main state serialization state structure
229  */
230
231 struct fs_dump_state {
232     enum {
233         FS_STATE_DUMP_MODE,
234         FS_STATE_LOAD_MODE
235     } mode;
236     struct {
237         byte do_host_restore;              /* whether host restore should be done */
238         byte some_steps_skipped;           /* whether some steps were skipped */
239         byte warnings_generated;           /* whether any warnings were generated during restore */
240     } flags;
241     afs_fsize_t file_len;
242     int fd;                                /* fd of the current dump file */
243     int bail;                              /* non-zero if something went wrong */
244     char * fn;                             /* name of the current dump file */
245     struct {                               /* memory map of dump file */
246         void * map;
247         void * cursor;
248         afs_foff_t offset;
249         afs_fsize_t size;
250     } mmap;
251     struct fs_state_header * hdr;          /* main header */
252     struct host_state_header * h_hdr;      /* header for host state data */
253     struct callback_state_header * cb_hdr; /* header for callback state data */
254     struct callback_state_timeout_header * cb_timeout_hdr;
255     struct callback_state_fehash_header * cb_fehash_hdr;
256     afs_uint64 eof_offset;                 /* current end of file offset */
257     struct {
258         int len;                           /* number of host entries in map */
259         struct idx_map_entry_t * entries;
260     } h_map;
261     struct {
262         int len;
263         struct idx_map_entry_t * entries;
264     } fe_map;
265     struct {
266         int len;
267         struct idx_map_entry_t * entries;
268     } cb_map;
269 };
270
271 /* prototypes */
272
273 /* serialize_state.c */
274 extern int fs_stateFileOpen(struct fs_dump_state *state);
275 extern int fs_stateWrite(struct fs_dump_state * state,
276                          void * buf, size_t len);
277 extern int fs_stateRead(struct fs_dump_state * state,
278                         void * buf, size_t len);
279 extern int fs_stateWriteV(struct fs_dump_state * state,
280                           struct iovec * iov, int niov);
281 extern int fs_stateReadV(struct fs_dump_state * state,
282                          struct iovec * iov, int niov);
283 extern int fs_stateSync(struct fs_dump_state * state);
284 extern int fs_stateWriteHeader(struct fs_dump_state * state,
285                                afs_uint64 * offset,
286                                void * hdr, size_t len);
287 extern int fs_stateReadHeader(struct fs_dump_state * state,
288                               afs_uint64 * offset,
289                               void * hdr, size_t len);
290 extern int fs_stateIncEOF(struct fs_dump_state * state,
291                           afs_int32 len);
292 extern int fs_stateSeek(struct fs_dump_state * state,
293                         afs_uint64 * offset);
294
295 /* host.c */
296 extern int h_stateSave(struct fs_dump_state * state);
297 extern int h_stateRestore(struct fs_dump_state * state);
298 extern int h_stateRestoreIndices(struct fs_dump_state * state);
299 extern int h_stateVerify(struct fs_dump_state * state);
300 extern int h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new);
301
302 /* callback.c */
303 extern int cb_stateSave(struct fs_dump_state * state);
304 extern int cb_stateRestore(struct fs_dump_state * state);
305 extern int cb_stateRestoreIndices(struct fs_dump_state * state);
306 extern int cb_stateVerify(struct fs_dump_state * state);
307 extern int cb_stateVerifyHCBList(struct fs_dump_state * state, struct host * host);
308 extern int fe_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new);
309 extern int cb_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new);
310
311 #endif /* AFS_DEMAND_ATTACH_FS */
312 #endif /* _AFS_TVICED_SERIALIZE_STATE_H */