reindent-20030715
[openafs.git] / src / tests / dumpscan.h
1 /*
2  * CMUCS AFStools
3  * dumpscan - routines for scanning and manipulating AFS volume dumps
4  *
5  * Copyright (c) 1998 Carnegie Mellon University
6  * All Rights Reserved.
7  * 
8  * Permission to use, copy, modify and distribute this software and its
9  * documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software_Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie Mellon
26  * the rights to redistribute these changes.
27  */
28
29 /* dumpscan.h - Public interface */
30
31 #ifndef _DUMPSCAN_H_
32 #define _DUMPSCAN_H_
33
34 #include "intNN.h"
35 #include "xfiles.h"
36
37 #include <lock.h>
38 #include <afs/afsint.h>
39 #include <afs/nfs.h>
40 #include <afs/ihandle.h>
41 #include <afs/vnode.h>
42 #include <afs/cmd.h>
43 #include <afs/auth.h>
44 #include <afs/bnode.h>
45 #include <afs/cellconfig.h>
46 #include <afs/kautils.h>
47 #include <afs/pterror.h>
48 #include <afs/vlserver.h>
49 #include <afs/volser.h>
50 #include <ubik.h>
51
52 /* Random useful types */
53 typedef struct tagged_field tagged_field;
54 typedef struct tag_parse_info tag_parse_info;
55 typedef afs_uint32(*tag_parser) (XFILE *, unsigned char *, tagged_field *,
56                                  afs_uint32, tag_parse_info *, void *,
57                                  void *);
58
59 /* Error codes used within dumpscan.
60  * Any of the routines declared below, or callbacks used by them,
61  * may signal a system error by returning the error number, or
62  * some other error by returning a com_err code.  Note that
63  * ParseTaggedData does _not_ return DSERR_TAG; instead, it returns
64  * 0, assuming the tag will be handled at a higher level.
65  *
66  * In addition, these errors may be reported to the caller of
67  * ParseDumpFile using the error callback.  Such reports will be
68  * issued whether or not error recovery is possible or attempted.
69  *
70  * NB: These errors are now in dumpscan_errs.h
71  */
72
73
74 /* Backup system dump header */
75 /* Right now, this looks a lot like an old stage header.  Eventually, it
76  * should contain enough fields to fully represent headers from old or
77  * new stage, Transarc, or other backup systems, and the appropriate read
78  * functions should extract as much data as possible from the actual file
79  * to fill this in. */
80 typedef struct {
81     afs_uint32 version;
82     afs_uint32 from_date;
83     afs_uint32 to_date;
84     afs_uint32 dump_date;
85     afs_uint32 filenum;
86     unsigned char *server;
87     unsigned char *part;
88     unsigned char *volname;
89     afs_uint32 volid;
90     afs_uint32 dumplen;
91     afs_uint32 level;
92     afs_uint32 magic;
93     afs_uint32 cksum;
94     afs_uint32 flags;
95 } backup_system_header;
96
97
98 /** AFS dump header **/
99 #define F_DUMPHDR_VOLID       0x00000001
100 #define F_DUMPHDR_VOLNAME     0x00000002
101 #define F_DUMPHDR_FROM        0x00000004
102 #define F_DUMPHDR_TO          0x00000008
103 typedef struct {
104     u_int64 offset;             /* Where in the file is it? */
105     afs_uint32 field_mask;      /* What fields are present? */
106     afs_uint32 magic;           /* Magic number */
107     afs_uint32 version;         /* Dump format version */
108     afs_uint32 volid;           /* VolID of volume in dump */
109     unsigned char *volname;     /* Name of volume in dump */
110     afs_uint32 from_date;       /* Reference date */
111     afs_uint32 to_date;         /* Date of dump */
112 } afs_dump_header;
113
114
115 /** AFS volume header **/
116 #define F_VOLHDR_VOLID        0x00000001
117 #define F_VOLHDR_VOLVERS      0x00000002
118 #define F_VOLHDR_VOLNAME      0x00000004
119 #define F_VOLHDR_INSERV       0x00000008
120 #define F_VOLHDR_BLESSED      0x00000010
121 #define F_VOLHDR_VOLUNIQ      0x00000020
122 #define F_VOLHDR_VOLTYPE      0x00000040
123 #define F_VOLHDR_PARENT       0x00000080
124 #define F_VOLHDR_CLONE        0x00000100
125 #define F_VOLHDR_MAXQ         0x00000200
126 #define F_VOLHDR_MINQ         0x00000400
127 #define F_VOLHDR_DISKUSED     0x00000800
128 #define F_VOLHDR_NFILES       0x00001000
129 #define F_VOLHDR_ACCOUNT      0x00002000
130 #define F_VOLHDR_OWNER        0x00004000
131 #define F_VOLHDR_CREATE_DATE  0x00008000
132 #define F_VOLHDR_ACCESS_DATE  0x00010000
133 #define F_VOLHDR_UPDATE_DATE  0x00020000
134 #define F_VOLHDR_EXPIRE_DATE  0x00040000
135 #define F_VOLHDR_BACKUP_DATE  0x00080000
136 #define F_VOLHDR_OFFLINE_MSG  0x00100000
137 #define F_VOLHDR_MOTD         0x00200000
138 #define F_VOLHDR_WEEKUSE      0x00400000
139 #define F_VOLHDR_DAYUSE       0x00800000
140 #define F_VOLHDR_DAYUSE_DATE  0x01000000
141 typedef struct {
142     u_int64 offset;             /* Where in the file is it? */
143     afs_uint32 field_mask;      /* What fields are present? */
144     afs_uint32 volid;           /* Volume ID */
145     afs_uint32 volvers;         /* ?? */
146     unsigned char *volname;     /* Volume Name */
147     int flag_inservice;         /* Inservice flag (0 or not) */
148     int flag_blessed;           /* Blessed to come online (0 or not) */
149     afs_uint32 voluniq;         /* Volume uniquifier */
150     int voltype;                /* Volume type */
151     afs_uint32 parent_volid;    /* Parent volume ID */
152     afs_uint32 clone_volid;     /* Clone volume ID */
153     afs_uint32 maxquota;        /* Max quota */
154     afs_uint32 minquota;        /* Min quota (obsolete) */
155     afs_uint32 diskused;        /* Disk blocks used */
156     afs_uint32 nfiles;          /* Number of files in volume */
157     afs_uint32 account_no;      /* Account number (unused) */
158     afs_uint32 owner;           /* Volume owner */
159     afs_uint32 create_date;     /* Creation date of this copy */
160     afs_uint32 access_date;     /* Last access */
161     afs_uint32 update_date;     /* Last modification */
162     afs_uint32 expire_date;     /* Expiration (unused) */
163     afs_uint32 backup_date;     /* Last backup clone */
164     unsigned char *offline_msg; /* Offline message */
165     unsigned char *motd_msg;    /* Volume MOTD */
166     afs_uint32 weekuse[7];      /* Weekuse data */
167     afs_uint32 dayuse;          /* # accesses in last day */
168     afs_uint32 dayuse_date;     /* Date for which dayuse is valid */
169 } afs_vol_header;
170
171
172 /** AFS vnode **/
173 #define F_VNODE_TYPE          0x00000001
174 #define F_VNODE_NLINKS        0x00000002
175 #define F_VNODE_PARENT        0x00000004
176 #define F_VNODE_DVERS         0x00000008
177 #define F_VNODE_AUTHOR        0x00000010
178 #define F_VNODE_OWNER         0x00000020
179 #define F_VNODE_GROUP         0x00000040
180 #define F_VNODE_MODE          0x00000080
181 #define F_VNODE_CDATE         0x00000100
182 #define F_VNODE_SDATE         0x00000200
183 #define F_VNODE_SIZE          0x00000800
184 #define F_VNODE_DATA          0x00001000
185 #define F_VNODE_ACL           0x00000400
186 typedef struct {
187     u_int64 offset;             /* Where in the file is it? */
188     afs_uint32 field_mask;      /* What fields are present? */
189     afs_uint32 vnode;           /* Vnode number */
190     afs_uint32 vuniq;           /* Uniquifier */
191     int type;                   /* Vnode type */
192     afs_uint16 nlinks;          /* Number of links (should be in 1 dir!) */
193     afs_uint32 parent;          /* Parent vnode */
194     afs_uint32 datavers;        /* Data version */
195     afs_uint32 author;          /* Last writer */
196     afs_uint32 owner;           /* Owner UID */
197     afs_uint32 group;           /* Owning group */
198     afs_uint16 mode;            /* UNIX mode bits */
199     afs_uint32 client_date;     /* Last modified date from client */
200     afs_uint32 server_date;     /* Last modified date on server */
201     afs_uint32 size;            /* Size of data */
202     u_int64 d_offset;           /* Where in the file is the data? */
203     unsigned char acl[SIZEOF_LARGEDISKVNODE - SIZEOF_SMALLDISKVNODE];
204 } afs_vnode;
205
206
207 /** AFS directory entry **/
208 typedef struct {
209     int slot;                   /* Directory slot # (info only) */
210     char *name;                 /* Name of entry */
211     afs_uint32 vnode;           /* Vnode number */
212     afs_uint32 uniq;            /* Uniquifier */
213 } afs_dir_entry;
214
215
216 /** Tagged field definitions **/
217 #define DKIND_NOOP      0x00    /* No data */
218 #define DKIND_BYTE      0x10    /* 1 byte  - decimal */
219 #define DKIND_HEX8      0x11    /* 1 byte  - hex */
220 #define DKIND_CHAR      0x12    /* 1 byte  - character */
221 #define DKIND_FLAG      0x13    /* 1 byte  - true/false */
222 #define DKIND_INT16     0x20    /* 2 bytes - decimal */
223 #define DKIND_HEX16     0x21    /* 2 bytes - hex */
224 #define DKIND_INT32     0x30    /* 4 bytes - decimal */
225 #define DKIND_HEX32     0x31    /* 4 bytes - hex */
226 #define DKIND_TIME      0x32    /* 4 bytes - time */
227 #define DKIND_STRING    0x40    /* ASCIIZ string */
228 #define DKIND_SPECIAL   0x50    /* Custom parser */
229 #define DKIND_MASK     (~0x0f)
230 struct tag_parse_info {
231     void *err_refcon;
232       afs_uint32(*cb_error) (afs_uint32, int, void *, char *, ...);
233     afs_uint32 flags;
234 #define TPFLAG_SKIP   0x0001
235 #define TPFLAG_RSKIP  0x0002
236     int shift_offset;
237     u_int64 shift_start;
238 };
239 struct tagged_field {
240     char tag;                   /* Tag character */
241     int kind;                   /* Kind of object */
242     char *label;                /* Label to use (for debugging) */
243     tag_parser func;            /* Parser function (for DKIND_SPECIAL) */
244     void *refptr;               /* Reference pointer (for parser's use) */
245     int refarg;                 /* Reference argument (for parser's use) */
246 };
247
248
249 /** Control structure for parsing volume dumps **/
250 typedef struct {
251     /* Callback functions:
252      * Whenever a "complex" object is parsed, we call a callback function.
253      * The callback gets a pointer to the complex object, the file pointer
254      * for the dump we're parsing, and the value of refcon in this structure.
255      * Callbacks should return 0 if all is well, non-0 to abort the dump.
256      * By convention, positive numbers should be errno values, and negative
257      * numbers can be used for other things.  It is OK to _try_ to seek anywhere
258      * in the file.  Beware, though, that the input is not always seekable.
259      * Also, note that the structures passed to these callbacks are going to
260      * go away after the callback returns.  There is no way to prevent this;
261      * make a copy if you want one.
262      */
263     void *refcon;
264       afs_uint32(*cb_bckhdr) (backup_system_header *, XFILE *, void *); /* Backup   */
265       afs_uint32(*cb_dumphdr) (afs_dump_header *, XFILE *, void *);     /* Dump hdr     */
266       afs_uint32(*cb_volhdr) (afs_vol_header *, XFILE *, void *);       /* Volume hdr   */
267       afs_uint32(*cb_vnode_dir) (afs_vnode *, XFILE *, void *); /* Directory    */
268       afs_uint32(*cb_vnode_file) (afs_vnode *, XFILE *, void *);        /* File         */
269       afs_uint32(*cb_vnode_link) (afs_vnode *, XFILE *, void *);        /* Symlink      */
270       afs_uint32(*cb_vnode_empty) (afs_vnode *, XFILE *, void *);       /* vnode+uniq   */
271       afs_uint32(*cb_vnode_wierd) (afs_vnode *, XFILE *, void *);       /* Unknown type */
272
273     /* This function is called when there is an error in the dump. */
274     /* (cb_error)(errno, fatal, refcon, msg_fmt, msg_args...) */
275     void *err_refcon;           /* If set, use instead of refcon for dir entries */
276       afs_uint32(*cb_error) (afs_uint32, int, void *, char *, ...);
277
278     /* This function is called for each directory entry, if set */
279       afs_uint32(*cb_dirent) (afs_vnode *, afs_dir_entry *, XFILE *, void *);
280
281     int flags;                  /* Flags and options */
282 #define DSFLAG_SEEK     0x0001  /* Input file is seekable */
283
284     int print_flags;            /* Flags to control what is printed */
285 #define DSPRINT_BCKHDR  0x0001  /* Print backup system header */
286 #define DSPRINT_DUMPHDR 0x0002  /* Print AFS dump header */
287 #define DSPRINT_VOLHDR  0x0004  /* Print AFS volume header */
288 #define DSPRINT_ITEM    0x0010  /* Print top-level tags */
289 #define DSPRINT_VNODE   0x0020  /* Print vnode attributes */
290 #define DSPRINT_ACL     0x0040  /* Print directory ACL's */
291 #define DSPRINT_DIR     0x0080  /* Print directory contents */
292 #define DSPRINT_DEBUG   0x0100  /* Print debugging info */
293 #define DSPRINT_PATH    0x0200  /* Print vnode paths */
294
295     int repair_flags;           /* Flags to control what is repaired.
296                                  * Most of these _require_ DSFLAG_SEEK */
297 #define DSFIX_SKIP      0x0001  /* Try to skip null tags */
298 #define DSFIX_RSKIP     0x0002  /* Seek back to fing skipped tags */
299 #define DSFIX_VDSYNC    0x0004  /* Resync location after vnode data */
300 #define DSFIX_VFSYNC    0x0008  /* Try to resync after bad vnode */
301
302   /** Things below this point for internal use only **/
303     afs_uint32 vol_uniquifier;
304 } dump_parser;
305
306
307 /** Hash table and control info for pathname manipulation **/
308 typedef struct vhash_ent {
309     struct vhash_ent *next;     /* Pointer to next entry */
310     afs_uint32 vnode;           /* VNode number */
311     afs_uint32 parent;          /* Parent VNode number */
312     u_int64 v_offset;           /* Offset to start of vnode */
313     u_int64 d_offset;           /* Offset to data (0 if none) */
314     afs_uint32 d_size;          /* Size of data */
315 } vhash_ent;
316 typedef struct {
317     afs_uint32 n_vnodes;        /* Number of vnodes in volume */
318     afs_uint32 n_dirs;          /* Number of file vnodes */
319     afs_uint32 n_files;         /* Number of directory vnodes */
320     int hash_size;              /* Hash table size (bits) */
321     vhash_ent **hash_table;     /* Hash table */
322     dump_parser *p;             /* Dump parser to use */
323 } path_hashinfo;
324
325
326 /** Function prototypes **/
327 /** Only the functions declared below are public interfaces **/
328 /** Maybe someday, I'll write man pages for these **/
329
330 /* primitive.c - I/O primitives */
331 extern afs_uint32 ReadByte(XFILE *, unsigned char *);
332 extern afs_uint32 ReadInt16(XFILE *, afs_uint16 *);
333 extern afs_uint32 ReadInt32(XFILE *, afs_uint32 *);
334 extern afs_uint32 ReadString(XFILE *, unsigned char **);
335 extern afs_uint32 WriteByte(XFILE *, unsigned char);
336 extern afs_uint32 WriteInt16(XFILE *, afs_uint16);
337 extern afs_uint32 WriteInt32(XFILE *, afs_uint32);
338 extern afs_uint32 WriteString(XFILE *, unsigned char *);
339 extern afs_uint32 WriteTagByte(XFILE *, unsigned char, unsigned char);
340 extern afs_uint32 WriteTagInt16(XFILE *, unsigned char, afs_uint16);
341 extern afs_uint32 WriteTagInt32(XFILE *, unsigned char, afs_uint32);
342 extern afs_uint32 WriteTagInt32Pair(XFILE *, unsigned char, afs_uint32,
343                                     afs_uint32);
344
345 /* parsetag.c - Parse tagged data */
346 extern afs_uint32 ParseTaggedData(XFILE *, tagged_field *, unsigned char *,
347                                   tag_parse_info *, void *, void *);
348
349 /* stagehdr.c - Parse and dump Stage dump headers */
350 extern afs_uint32 ParseStageHdr(XFILE *, unsigned char *,
351                                 backup_system_header *);
352 extern afs_uint32 DumpStagehdr(XFILE *, backup_system_header *);
353
354 /* backuphdr.c - Parse and print backup system headers */
355 extern void PrintBackupHdr(backup_system_header *);
356
357 /* parsedump.c - Parse all or part of a volume dump */
358 extern afs_uint32 ParseDumpFile(XFILE *, dump_parser *);
359 extern afs_uint32 ParseDumpHeader(XFILE *, dump_parser *);
360 extern afs_uint32 ParseVolumeHeader(XFILE *, dump_parser *);
361 extern afs_uint32 ParseVNode(XFILE *, dump_parser *);
362
363
364 /* directory.c - Directory parsing and lookup */
365 extern afs_uint32 ParseDirectory(XFILE *, dump_parser *, afs_uint32, int);
366 extern afs_uint32 DirectoryLookup(XFILE *, dump_parser *, afs_uint32, char **,
367                                   afs_uint32 *, afs_uint32 *);
368
369 /* dump.c - Dump parts of a volume dump */
370 extern afs_uint32 DumpDumpHeader(XFILE *, afs_dump_header *);
371 extern afs_uint32 DumpVolumeHeader(XFILE *, afs_vol_header *);
372 extern afs_uint32 DumpVNode(XFILE *, afs_vnode *);
373 extern afs_uint32 DumpVnodeData(XFILE *, char *, afs_uint32);
374 extern afs_uint32 CopyVnodeData(XFILE *, XFILE *, afs_uint32);
375
376 /* pathname.c - Follow and construct pathnames */
377 extern afs_uint32 Path_PreScan(XFILE *, path_hashinfo *, int);
378 extern void Path_FreeHashTable(path_hashinfo *);
379 extern afs_uint32 Path_Follow(XFILE *, path_hashinfo *, char *, vhash_ent *);
380 extern afs_uint32 Path_Build(XFILE *, path_hashinfo *, afs_uint32, char **,
381                              int);
382
383 #endif