Eliminate some "implicit function declaration" warnings
[openafs.git] / src / vol / fssync-debug.c
1 /*
2  * Copyright 2006-2008, 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 /* Main program file. Define globals. */
11 #define MAIN 1
12
13 /*
14  * fssync administration tool
15  */
16
17
18 #include <afsconfig.h>
19 #include <afs/param.h>
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <dirent.h>
26 #include <sys/stat.h>
27 #include <time.h>
28 #include <errno.h>
29 #ifdef AFS_NT40_ENV
30 #include <io.h>
31 #include <WINNT/afsevent.h>
32 #else
33 #include <sys/param.h>
34 #include <sys/file.h>
35 #ifndef ITIMER_REAL
36 #include <sys/time.h>
37 #endif /* ITIMER_REAL */
38 #endif
39 #include <rx/xdr.h>
40 #include <afs/afsint.h>
41 #include <afs/assert.h>
42
43
44 #include <fcntl.h>
45
46 #ifndef AFS_NT40_ENV
47 #include <afs/osi_inode.h>
48 #endif
49
50 #include <afs/cmd.h>
51 #include <afs/dir.h>
52 #include <afs/afsutil.h>
53 #include <afs/fileutil.h>
54
55 #include "nfs.h"
56 #include "lwp.h"
57 #include "lock.h"
58 #include "ihandle.h"
59 #include "vnode.h"
60 #include "volume.h"
61 #include "partition.h"
62 #include "daemon_com.h"
63 #include "fssync.h"
64 #ifdef AFS_NT40_ENV
65 #include <pthread.h>
66 #endif
67
68 int VolumeChanged; /* hack to make dir package happy */
69
70
71 struct volop_state {
72     afs_uint32 volume;
73     afs_uint32 vnode;
74     afs_uint32 unique;
75     char partName[16];
76 };
77
78 struct state {
79     afs_int32 reason;
80     struct volop_state * vop;
81 };
82
83 static int common_prolog(struct cmd_syndesc *, struct state *);
84 static int common_volop_prolog(struct cmd_syndesc *, struct state *);
85
86 static int do_volop(struct state *, afs_int32 command, SYNC_response * res);
87
88 static char * response_code_to_string(afs_int32);
89 static char * command_code_to_string(afs_int32);
90 static char * reason_code_to_string(afs_int32);
91 static char * program_type_to_string(afs_int32);
92
93 static int VolOnline(struct cmd_syndesc * as, void * rock);
94 static int VolOffline(struct cmd_syndesc * as, void * rock);
95 static int VolMode(struct cmd_syndesc * as, void * rock);
96 static int VolDetach(struct cmd_syndesc * as, void * rock);
97 static int VolBreakCBKs(struct cmd_syndesc * as, void * rock);
98 static int VolMove(struct cmd_syndesc * as, void * rock);
99 static int VolList(struct cmd_syndesc * as, void * rock);
100 static int VolLeaveOff(struct cmd_syndesc * as, void * rock);
101 static int VolForceAttach(struct cmd_syndesc * as, void * rock);
102 static int VolForceError(struct cmd_syndesc * as, void * rock);
103 static int VolQuery(struct cmd_syndesc * as, void * rock);
104 static int VolHdrQuery(struct cmd_syndesc * as, void * rock);
105 static int VolOpQuery(struct cmd_syndesc * as, void * rock);
106 static int StatsQuery(struct cmd_syndesc * as, void * rock);
107 static int VnQuery(struct cmd_syndesc * as, void * rock);
108
109 static void print_vol_stats_general(VolPkgStats * stats);
110 static void print_vol_stats_viceP(struct DiskPartitionStats64 * stats);
111 static void print_vol_stats_hash(struct VolumeHashChainStats * stats);
112 #ifdef AFS_DEMAND_ATTACH_FS
113 static void print_vol_stats_hdr(struct volume_hdr_LRU_stats * stats);
114 #endif
115
116 #ifndef AFS_NT40_ENV
117 #include "AFS_component_version_number.c"
118 #endif
119 #define MAX_ARGS 128
120
121 #define COMMON_PARMS_OFFSET    12
122 #define COMMON_PARMS(ts) \
123     cmd_Seek(ts, COMMON_PARMS_OFFSET); \
124     cmd_AddParm(ts, "-reason", CMD_SINGLE, CMD_OPTIONAL, "sync protocol reason code"); \
125     cmd_AddParm(ts, "-programtype", CMD_SINGLE, CMD_OPTIONAL, "program type code")
126
127 #define COMMON_VOLOP_PARMS_OFFSET    10
128 #define COMMON_VOLOP_PARMS(ts) \
129     cmd_Seek(ts, COMMON_VOLOP_PARMS_OFFSET); \
130     cmd_AddParm(ts, "-volumeid", CMD_SINGLE, 0, "volume id"); \
131     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name")
132
133 #define CUSTOM_PARMS_OFFSET 1
134
135
136 #define VOLOP_PARMS_DECL(ts) \
137     COMMON_VOLOP_PARMS(ts); \
138     COMMON_PARMS(ts)
139 #define COMMON_PARMS_DECL(ts) \
140     COMMON_PARMS(ts)
141
142 int
143 main(int argc, char **argv)
144 {
145     struct cmd_syndesc *ts;
146     int err = 0;
147
148     /* Initialize directory paths */
149     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
150 #ifdef AFS_NT40_ENV
151         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
152 #endif
153         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
154                 argv[0]);
155         exit(2);
156     }
157
158     
159     ts = cmd_CreateSyntax("online", VolOnline, NULL, "bring a volume online (FSYNC_VOL_ON opcode)");
160     VOLOP_PARMS_DECL(ts);
161
162     ts = cmd_CreateSyntax("offline", VolOffline, NULL, "take a volume offline (FSYNC_VOL_OFF opcode)");
163     VOLOP_PARMS_DECL(ts);
164
165     ts = cmd_CreateSyntax("mode", VolMode, NULL, "change volume attach mode (FSYNC_VOL_NEEDVOLUME opcode)");
166     VOLOP_PARMS_DECL(ts);
167     cmd_CreateAlias(ts, "needvolume");
168
169     ts = cmd_CreateSyntax("detach", VolDetach, NULL, "detach a volume (FSYNC_VOL_DONE opcode)");
170     VOLOP_PARMS_DECL(ts);
171
172     ts = cmd_CreateSyntax("callback", VolBreakCBKs, NULL, "break callbacks for volume (FSYNC_VOL_BREAKCBKS opcode)");
173     VOLOP_PARMS_DECL(ts);
174     cmd_CreateAlias(ts, "cbk");
175
176     ts = cmd_CreateSyntax("move", VolMove, NULL, "set volume moved flag (FSYNC_VOL_MOVE opcode)");
177     VOLOP_PARMS_DECL(ts);
178
179     ts = cmd_CreateSyntax("list", VolList, NULL, "sync local volume list (FSYNC_VOL_LISTVOLUMES opcode)");
180     VOLOP_PARMS_DECL(ts);
181     cmd_CreateAlias(ts, "ls");
182
183     ts = cmd_CreateSyntax("leaveoff", VolLeaveOff, 0, "leave volume offline (FSYNC_VOL_LEAVE_OFF opcode)");
184     VOLOP_PARMS_DECL(ts);
185
186     ts = cmd_CreateSyntax("attach", VolForceAttach, 0, "force full attachment (FSYNC_VOL_ATTACH opcode)");
187     VOLOP_PARMS_DECL(ts);
188
189     ts = cmd_CreateSyntax("error", VolForceError, 0, "force into hard error state (FSYNC_VOL_FORCE_ERROR opcode)");
190     VOLOP_PARMS_DECL(ts);
191
192     ts = cmd_CreateSyntax("query", VolQuery, NULL, "get volume structure (FSYNC_VOL_QUERY opcode)");
193     VOLOP_PARMS_DECL(ts);
194     cmd_CreateAlias(ts, "qry");
195
196     ts = cmd_CreateSyntax("header", VolHdrQuery, NULL, "get volume disk data structure (FSYNC_VOL_QUERY_HDR opcode)");
197     VOLOP_PARMS_DECL(ts);
198     cmd_CreateAlias(ts, "hdr");
199
200     ts = cmd_CreateSyntax("volop", VolOpQuery, NULL, "get pending volume operation info (FSYNC_VOL_QUERY_VOP opcode)");
201     VOLOP_PARMS_DECL(ts);
202     cmd_CreateAlias(ts, "vop");
203
204     ts = cmd_CreateSyntax("vnode", VnQuery, NULL, "get vnode structure (FSYNC_VOL_QUERY_VNODE opcode)");
205     cmd_Seek(ts, CUSTOM_PARMS_OFFSET);
206     cmd_AddParm(ts, "-volumeid", CMD_SINGLE, 0, "volume id");
207     cmd_AddParm(ts, "-vnodeid", CMD_SINGLE, 0, "vnode id");
208     cmd_AddParm(ts, "-unique", CMD_SINGLE, 0, "uniquifier");
209     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "paritition name");
210     COMMON_PARMS_DECL(ts);
211
212     ts = cmd_CreateSyntax("stats", StatsQuery, NULL, "see 'stats help' for more information");
213     cmd_Seek(ts, CUSTOM_PARMS_OFFSET);
214     cmd_AddParm(ts, "-cmd", CMD_SINGLE, 0, "subcommand");
215     cmd_AddParm(ts, "-arg1", CMD_SINGLE, CMD_OPTIONAL, "arg1");
216     cmd_AddParm(ts, "-arg2", CMD_SINGLE, CMD_OPTIONAL, "arg2");
217     COMMON_PARMS_DECL(ts);
218
219     err = cmd_Dispatch(argc, argv);
220     exit(err);
221 }
222
223 static int
224 common_prolog(struct cmd_syndesc * as, struct state * state)
225 {
226     register struct cmd_item *ti;
227
228 #ifdef AFS_NT40_ENV
229     if (afs_winsockInit() < 0) {
230         Exit(1);
231     }
232 #endif
233
234     VInitVolumePackage(debugUtility, 1, 1,
235                        DONT_CONNECT_FS, 0);
236     DInit(1);
237
238     if ((ti = as->parms[COMMON_PARMS_OFFSET].items)) {  /* -reason */
239         state->reason = atoi(ti->data);
240     }
241     if ((ti = as->parms[COMMON_PARMS_OFFSET+1].items)) {        /* -programtype */
242         if (!strcmp(ti->data, "fileServer")) {
243             programType = fileServer;
244         } else if (!strcmp(ti->data, "volumeUtility")) {
245             programType = volumeUtility;
246         } else if (!strcmp(ti->data, "salvager")) {
247             programType = salvager;
248         } else if (!strcmp(ti->data, "salvageServer")) {
249             programType = salvageServer;
250         } else {
251             programType = (ProgramType) atoi(ti->data);
252         }
253     }
254
255     VConnectFS();
256
257     return 0;
258 }
259
260 static int
261 common_volop_prolog(struct cmd_syndesc * as, struct state * state)
262 {
263     register struct cmd_item *ti;
264
265     state->vop = (struct volop_state *) calloc(1, sizeof(struct volop_state));
266     assert(state->vop != NULL);
267
268     if ((ti = as->parms[COMMON_VOLOP_PARMS_OFFSET].items)) {    /* -volumeid */
269         state->vop->volume = atoi(ti->data);
270     } else {
271         fprintf(stderr, "required argument -volumeid not given\n");
272     }
273
274     if ((ti = as->parms[COMMON_VOLOP_PARMS_OFFSET+1].items)) {  /* -partition */
275         strlcpy(state->vop->partName, ti->data, sizeof(state->vop->partName));
276     } else {
277         memset(state->vop->partName, 0, sizeof(state->vop->partName));
278     }
279
280     return 0;
281 }
282
283 static int
284 do_volop(struct state * state, afs_int32 command, SYNC_response * res)
285 {
286     afs_int32 code;
287     SYNC_PROTO_BUF_DECL(res_buf);
288     SYNC_response res_l;
289
290     if (!res) {
291         res = &res_l;
292         res->payload.len = SYNC_PROTO_MAX_LEN;
293         res->payload.buf = res_buf;
294     }
295
296     fprintf(stderr, "calling FSYNC_VolOp with command code %d (%s)\n", 
297             command, command_code_to_string(command));
298
299     code = FSYNC_VolOp(state->vop->volume,
300                        state->vop->partName,
301                        command,
302                        state->reason,
303                        res);
304
305     switch (code) {
306     case SYNC_OK:
307     case SYNC_DENIED:
308         break;
309     default:
310         fprintf(stderr, "possible sync protocol error. return code was %d\n", code);
311     }
312
313     fprintf(stderr, "FSYNC_VolOp returned %d (%s)\n", code, response_code_to_string(code));
314     fprintf(stderr, "protocol response code was %d (%s)\n", 
315             res->hdr.response, response_code_to_string(res->hdr.response));
316     fprintf(stderr, "protocol reason code was %d (%s)\n", 
317             res->hdr.reason, reason_code_to_string(res->hdr.reason));
318
319     VDisconnectFS();
320
321     return 0;
322
323 }
324
325
326 #define ENUMTOSTRING(en)  #en
327 #define ENUMCASE(en) \
328     case en: \
329         return ENUMTOSTRING(en); \
330         break
331
332 #define FLAGTOSTRING(fl)  #fl
333 #define FLAGCASE(bitstr, fl, str, count) \
334     do { \
335         if ((bitstr) & (fl)) { \
336             if (count) \
337                 strlcat((str), " | ", sizeof(str)); \
338             strlcat((str), FLAGTOSTRING(fl), sizeof(str)); \
339             (count)++; \
340         } \
341     } while (0)
342
343 static char *
344 response_code_to_string(afs_int32 response)
345 {
346     switch (response) {
347         ENUMCASE(SYNC_OK);
348         ENUMCASE(SYNC_DENIED);
349         ENUMCASE(SYNC_COM_ERROR);
350         ENUMCASE(SYNC_BAD_COMMAND);
351         ENUMCASE(SYNC_FAILED);
352     default:
353         return "**UNKNOWN**";
354     }
355 }
356
357 static char *
358 command_code_to_string(afs_int32 command)
359 {
360     switch (command) {
361         ENUMCASE(SYNC_COM_CHANNEL_CLOSE);
362         ENUMCASE(FSYNC_VOL_ON);
363         ENUMCASE(FSYNC_VOL_OFF);
364         ENUMCASE(FSYNC_VOL_LISTVOLUMES);
365         ENUMCASE(FSYNC_VOL_NEEDVOLUME);
366         ENUMCASE(FSYNC_VOL_MOVE);
367         ENUMCASE(FSYNC_VOL_BREAKCBKS);
368         ENUMCASE(FSYNC_VOL_DONE);
369         ENUMCASE(FSYNC_VOL_QUERY);
370         ENUMCASE(FSYNC_VOL_QUERY_HDR);
371         ENUMCASE(FSYNC_VOL_QUERY_VOP);
372         ENUMCASE(FSYNC_VOL_STATS_GENERAL);
373         ENUMCASE(FSYNC_VOL_STATS_VICEP);
374         ENUMCASE(FSYNC_VOL_STATS_HASH);
375         ENUMCASE(FSYNC_VOL_STATS_HDR);
376         ENUMCASE(FSYNC_VOL_STATS_VLRU);
377         ENUMCASE(FSYNC_VOL_ATTACH);
378         ENUMCASE(FSYNC_VOL_FORCE_ERROR);
379         ENUMCASE(FSYNC_VOL_LEAVE_OFF);
380         ENUMCASE(FSYNC_VOL_QUERY_VNODE);
381     default:
382         return "**UNKNOWN**";
383     }
384 }
385
386 static char *
387 reason_code_to_string(afs_int32 reason)
388 {
389     switch (reason) {
390         ENUMCASE(SYNC_REASON_NONE);
391         ENUMCASE(SYNC_REASON_MALFORMED_PACKET);
392         ENUMCASE(SYNC_REASON_NOMEM);
393         ENUMCASE(SYNC_REASON_ENCODING_ERROR);
394         ENUMCASE(FSYNC_WHATEVER);
395         ENUMCASE(FSYNC_SALVAGE);
396         ENUMCASE(FSYNC_MOVE);
397         ENUMCASE(FSYNC_OPERATOR);
398         ENUMCASE(FSYNC_EXCLUSIVE);
399         ENUMCASE(FSYNC_UNKNOWN_VOLID);
400         ENUMCASE(FSYNC_HDR_NOT_ATTACHED);
401         ENUMCASE(FSYNC_NO_PENDING_VOL_OP);
402         ENUMCASE(FSYNC_VOL_PKG_ERROR);
403         ENUMCASE(FSYNC_UNKNOWN_VNID);
404     default:
405         return "**UNKNOWN**";
406     }
407 }
408
409 static char *
410 program_type_to_string(afs_int32 type)
411 {
412     switch ((ProgramType)type) {
413         ENUMCASE(fileServer);
414         ENUMCASE(volumeUtility);
415         ENUMCASE(salvager);
416         ENUMCASE(salvageServer);
417         ENUMCASE(debugUtility);
418     default:
419         return "**UNKNOWN**";
420     }
421 }
422
423 static int 
424 VolOnline(struct cmd_syndesc * as, void * rock)
425 {
426     struct state state;
427
428     common_prolog(as, &state);
429     common_volop_prolog(as, &state);
430
431     do_volop(&state, FSYNC_VOL_ON, NULL);
432
433     return 0;
434 }
435
436 static int 
437 VolOffline(struct cmd_syndesc * as, void * rock)
438 {
439     struct state state;
440
441     common_prolog(as, &state);
442     common_volop_prolog(as, &state);
443
444     do_volop(&state, FSYNC_VOL_OFF, NULL);
445
446     return 0;
447 }
448
449 static int
450 VolMode(struct cmd_syndesc * as, void * rock)
451 {
452     struct state state;
453
454     common_prolog(as, &state);
455     common_volop_prolog(as, &state);
456
457     do_volop(&state, FSYNC_VOL_NEEDVOLUME, NULL);
458
459     return 0;
460 }
461
462 static int
463 VolDetach(struct cmd_syndesc * as, void * rock)
464 {
465     struct state state;
466
467     common_prolog(as, &state);
468     common_volop_prolog(as, &state);
469
470     do_volop(&state, FSYNC_VOL_DONE, NULL);
471
472     return 0;
473 }
474
475 static int
476 VolBreakCBKs(struct cmd_syndesc * as, void * rock)
477 {
478     struct state state;
479
480     common_prolog(as, &state);
481     common_volop_prolog(as, &state);
482
483     do_volop(&state, FSYNC_VOL_BREAKCBKS, NULL);
484
485     return 0;
486 }
487
488 static int
489 VolMove(struct cmd_syndesc * as, void * rock)
490 {
491     struct state state;
492
493     common_prolog(as, &state);
494     common_volop_prolog(as, &state);
495
496     do_volop(&state, FSYNC_VOL_MOVE, NULL);
497
498     return 0;
499 }
500
501 static int
502 VolList(struct cmd_syndesc * as, void * rock)
503 {
504     struct state state;
505
506     common_prolog(as, &state);
507     common_volop_prolog(as, &state);
508
509     do_volop(&state, FSYNC_VOL_LISTVOLUMES, NULL);
510
511     return 0;
512 }
513
514 static int
515 VolLeaveOff(struct cmd_syndesc * as, void * rock)
516 {
517     struct state state;
518
519     common_prolog(as, &state);
520     common_volop_prolog(as, &state);
521
522     do_volop(&state, FSYNC_VOL_LEAVE_OFF, NULL);
523
524     return 0;
525 }
526
527 static int
528 VolForceAttach(struct cmd_syndesc * as, void * rock)
529 {
530     struct state state;
531
532     common_prolog(as, &state);
533     common_volop_prolog(as, &state);
534
535     do_volop(&state, FSYNC_VOL_ATTACH, NULL);
536
537     return 0;
538 }
539
540 static int
541 VolForceError(struct cmd_syndesc * as, void * rock)
542 {
543     struct state state;
544
545     common_prolog(as, &state);
546     common_volop_prolog(as, &state);
547
548     do_volop(&state, FSYNC_VOL_FORCE_ERROR, NULL);
549
550     return 0;
551 }
552
553 #ifdef AFS_DEMAND_ATTACH_FS
554 static char *
555 vol_state_to_string(VolState state)
556 {
557     switch (state) {
558         ENUMCASE(VOL_STATE_UNATTACHED);
559         ENUMCASE(VOL_STATE_PREATTACHED);
560         ENUMCASE(VOL_STATE_ATTACHING);
561         ENUMCASE(VOL_STATE_ATTACHED);
562         ENUMCASE(VOL_STATE_UPDATING);
563         ENUMCASE(VOL_STATE_GET_BITMAP);
564         ENUMCASE(VOL_STATE_HDR_LOADING);
565         ENUMCASE(VOL_STATE_HDR_ATTACHING);
566         ENUMCASE(VOL_STATE_SHUTTING_DOWN);
567         ENUMCASE(VOL_STATE_GOING_OFFLINE);
568         ENUMCASE(VOL_STATE_OFFLINING);
569         ENUMCASE(VOL_STATE_DETACHING);
570         ENUMCASE(VOL_STATE_SALVSYNC_REQ);
571         ENUMCASE(VOL_STATE_SALVAGING);
572         ENUMCASE(VOL_STATE_ERROR);
573         ENUMCASE(VOL_STATE_VNODE_ALLOC);
574         ENUMCASE(VOL_STATE_VNODE_GET);
575         ENUMCASE(VOL_STATE_VNODE_CLOSE);
576         ENUMCASE(VOL_STATE_VNODE_RELEASE);
577         ENUMCASE(VOL_STATE_VLRU_ADD);
578         ENUMCASE(VOL_STATE_FREED);
579     default:
580         return "**UNKNOWN**";
581     }
582 }
583
584 static char *
585 vol_flags_to_string(afs_uint16 flags)
586 {
587     static char str[128];
588     int count = 0;
589     str[0]='\0';
590
591     FLAGCASE(flags, VOL_HDR_ATTACHED, str, count);
592     FLAGCASE(flags, VOL_HDR_LOADED, str, count);
593     FLAGCASE(flags, VOL_HDR_IN_LRU, str, count);
594     FLAGCASE(flags, VOL_IN_HASH, str, count);
595     FLAGCASE(flags, VOL_ON_VBYP_LIST, str, count);
596     FLAGCASE(flags, VOL_IS_BUSY, str, count);
597     FLAGCASE(flags, VOL_ON_VLRU, str, count);
598     FLAGCASE(flags, VOL_HDR_DONTSALV, str, count);
599
600     return str;
601 }
602
603 static char *
604 vlru_idx_to_string(int idx)
605 {
606     switch (idx) {
607         ENUMCASE(VLRU_QUEUE_NEW);
608         ENUMCASE(VLRU_QUEUE_MID);
609         ENUMCASE(VLRU_QUEUE_OLD);
610         ENUMCASE(VLRU_QUEUE_CANDIDATE);
611         ENUMCASE(VLRU_QUEUE_HELD);
612         ENUMCASE(VLRU_QUEUE_INVALID);
613     default:
614         return "**UNKNOWN**";
615     }
616 }
617
618
619 static char *
620 vn_state_to_string(VnState state)
621 {
622     switch (state) {
623         ENUMCASE(VN_STATE_INVALID);
624         ENUMCASE(VN_STATE_RELEASING);
625         ENUMCASE(VN_STATE_CLOSING);
626         ENUMCASE(VN_STATE_ALLOC);
627         ENUMCASE(VN_STATE_ONLINE);
628         ENUMCASE(VN_STATE_LOAD);
629         ENUMCASE(VN_STATE_EXCLUSIVE);
630         ENUMCASE(VN_STATE_STORE);
631         ENUMCASE(VN_STATE_READ);
632         ENUMCASE(VN_STATE_ERROR);
633     default:
634         return "**UNKNOWN**";
635     }
636 }
637
638 static char *
639 vn_flags_to_string(afs_uint32 flags)
640 {
641     static char str[128];
642     int count = 0;
643     str[0]='\0';
644
645     FLAGCASE(flags, VN_ON_HASH, str, count);
646     FLAGCASE(flags, VN_ON_LRU, str, count);
647     FLAGCASE(flags, VN_ON_VVN, str, count);
648
649     return str;
650 }
651 #endif
652
653 static int
654 VolQuery(struct cmd_syndesc * as, void * rock)
655 {
656     struct state state;
657     SYNC_PROTO_BUF_DECL(res_buf);
658     SYNC_response res;
659     Volume v;
660 #ifdef AFS_DEMAND_ATTACH_FS
661     int hi, lo;
662 #endif
663
664     res.hdr.response_len = sizeof(res.hdr);
665     res.payload.buf = res_buf;
666     res.payload.len = SYNC_PROTO_MAX_LEN;
667
668     common_prolog(as, &state);
669     common_volop_prolog(as, &state);
670
671     do_volop(&state, FSYNC_VOL_QUERY, &res);
672
673     if (res.hdr.response == SYNC_OK) {
674         memcpy(&v, res.payload.buf, sizeof(Volume));
675
676         printf("volume = {\n");
677         printf("\thashid          = %u\n", v.hashid);
678         printf("\theader          = %p\n", v.header);
679         printf("\tdevice          = %d\n", v.device);
680         printf("\tpartition       = %p\n", v.partition);
681         printf("\tlinkHandle      = %p\n", v.linkHandle);
682         printf("\tnextVnodeUnique = %u\n", v.nextVnodeUnique);
683         printf("\tdiskDataHandle  = %p\n", v.diskDataHandle);
684         printf("\tvnodeHashOffset = %u\n", v.vnodeHashOffset);
685         printf("\tshuttingDown    = %d\n", v.shuttingDown);
686         printf("\tgoingOffline    = %d\n", v.goingOffline);
687         printf("\tcacheCheck      = %u\n", v.cacheCheck);
688         printf("\tnUsers          = %d\n", v.nUsers);
689         printf("\tneedsPutBack    = %d\n", v.needsPutBack);
690         printf("\tspecialStatus   = %d\n", v.specialStatus);
691         printf("\tupdateTime      = %u\n", v.updateTime);
692         
693         printf("\tvnodeIndex[vSmall] = {\n");
694         printf("\t\thandle       = %p\n", v.vnodeIndex[vSmall].handle);
695         printf("\t\tbitmap       = %p\n", v.vnodeIndex[vSmall].bitmap);
696         printf("\t\tbitmapSize   = %u\n", v.vnodeIndex[vSmall].bitmapSize);
697         printf("\t\tbitmapOffset = %u\n", v.vnodeIndex[vSmall].bitmapOffset);
698         printf("\t}\n");
699         printf("\tvnodeIndex[vLarge] = {\n");
700         printf("\t\thandle       = %p\n", v.vnodeIndex[vLarge].handle);
701         printf("\t\tbitmap       = %p\n", v.vnodeIndex[vLarge].bitmap);
702         printf("\t\tbitmapSize   = %u\n", v.vnodeIndex[vLarge].bitmapSize);
703         printf("\t\tbitmapOffset = %u\n", v.vnodeIndex[vLarge].bitmapOffset);
704         printf("\t}\n");
705 #ifdef AFS_DEMAND_ATTACH_FS
706         if (res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS) {
707             printf("\tupdateTime      = %u\n", v.updateTime);
708             printf("\tattach_state    = %s\n", vol_state_to_string(v.attach_state));
709             printf("\tattach_flags    = %s\n", vol_flags_to_string(v.attach_flags));
710             printf("\tnWaiters        = %d\n", v.nWaiters);
711             printf("\tchainCacheCheck = %d\n", v.chainCacheCheck);
712             
713             /* online salvage structure */
714             printf("\tsalvage = {\n");
715             printf("\t\tprio      = %u\n", v.salvage.prio);
716             printf("\t\treason    = %d\n", v.salvage.reason);
717             printf("\t\trequested = %d\n", v.salvage.requested);
718             printf("\t\tscheduled = %d\n", v.salvage.scheduled);
719             printf("\t}\n");
720             
721             /* statistics structure */
722             printf("\tstats = {\n");
723
724             printf("\t\thash_lookups = {\n");
725             SplitInt64(v.stats.hash_lookups,hi,lo);
726             printf("\t\t\thi = %u\n", hi);
727             printf("\t\t\tlo = %u\n", lo);
728             printf("\t\t}\n");
729
730             printf("\t\thash_short_circuits = {\n");
731             SplitInt64(v.stats.hash_short_circuits,hi,lo);
732             printf("\t\t\thi = %u\n", hi);
733             printf("\t\t\tlo = %u\n", lo);
734             printf("\t\t}\n");
735
736             printf("\t\thdr_loads = {\n");
737             SplitInt64(v.stats.hdr_loads,hi,lo);
738             printf("\t\t\thi = %u\n", hi);
739             printf("\t\t\tlo = %u\n", lo);
740             printf("\t\t}\n");
741
742             printf("\t\thdr_gets = {\n");
743             SplitInt64(v.stats.hdr_gets,hi,lo);
744             printf("\t\t\thi = %u\n", hi);
745             printf("\t\t\tlo = %u\n", lo);
746             printf("\t\t}\n");
747             
748             printf("\t\tattaches         = %u\n", v.stats.attaches);
749             printf("\t\tsoft_detaches    = %u\n", v.stats.soft_detaches);
750             printf("\t\tsalvages         = %u\n", v.stats.salvages);
751             printf("\t\tvol_ops          = %u\n", v.stats.vol_ops);
752             
753             printf("\t\tlast_attach      = %u\n", v.stats.last_attach);
754             printf("\t\tlast_get         = %u\n", v.stats.last_get);
755             printf("\t\tlast_promote     = %u\n", v.stats.last_promote);
756             printf("\t\tlast_hdr_get     = %u\n", v.stats.last_hdr_get);
757             printf("\t\tlast_hdr_load    = %u\n", v.stats.last_hdr_load);
758             printf("\t\tlast_salvage     = %u\n", v.stats.last_salvage);
759             printf("\t\tlast_salvage_req = %u\n", v.stats.last_salvage_req);
760             printf("\t\tlast_vol_op      = %u\n", v.stats.last_vol_op);
761             printf("\t}\n");
762             
763             /* VLRU state */
764             printf("\tvlru = {\n");
765             printf("\t\tidx = %d (%s)\n", 
766                    v.vlru.idx, vlru_idx_to_string(v.vlru.idx));
767             printf("\t}\n");
768
769             /* volume op state */
770             printf("\tpending_vol_op  = %p\n", v.pending_vol_op);
771         }
772 #else /* !AFS_DEMAND_ATTACH_FS */
773         if (res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS) {
774             printf("*** server asserted demand attach extensions. fssync-debug not built to\n");
775             printf("*** recognize those extensions. please recompile fssync-debug if you need\n");
776             printf("*** to dump dafs extended state\n");
777         }
778 #endif /* !AFS_DEMAND_ATTACH_FS */
779         printf("}\n");
780     }
781
782     return 0;
783 }
784
785 static int
786 VolHdrQuery(struct cmd_syndesc * as, void * rock)
787 {
788     struct state state;
789     SYNC_PROTO_BUF_DECL(res_buf);
790     SYNC_response res;
791     VolumeDiskData v;
792     int i;
793
794     res.hdr.response_len = sizeof(res.hdr);
795     res.payload.buf = res_buf;
796     res.payload.len = SYNC_PROTO_MAX_LEN;
797
798     common_prolog(as, &state);
799     common_volop_prolog(as, &state);
800
801     do_volop(&state, FSYNC_VOL_QUERY_HDR, &res);
802
803     if (res.hdr.response == SYNC_OK) {
804         memcpy(&v, res.payload.buf, sizeof(VolumeDiskData));
805
806         printf("VolumeDiskData = {\n");
807         printf("\tstamp = {\n");
808         printf("\t\tmagic   = 0x%x\n", v.stamp.magic);
809         printf("\t\tversion = %u\n", v.stamp.version);
810         printf("\t}\n");
811         
812         printf("\tid               = %u\n", v.id);
813         printf("\tname             = '%s'\n", v.name);
814         if (v.inUse != 0) {
815             printf("\tinUse            = %d (%s)\n", v.inUse, program_type_to_string(v.inUse));
816         } else {
817             printf("\tinUse            = %d (no)\n", v.inUse);
818         }
819         printf("\tinService        = %d\n", v.inService);
820         printf("\tblessed          = %d\n", v.blessed);
821         printf("\tneedsSalvaged    = %d\n", v.needsSalvaged);
822         printf("\tuniquifier       = %u\n", v.uniquifier);
823         printf("\ttype             = %d\n", v.type);
824         printf("\tparentId         = %u\n", v.parentId);
825         printf("\tcloneId          = %u\n", v.cloneId);
826         printf("\tbackupId         = %u\n", v.backupId);
827         printf("\trestoredFromId   = %u\n", v.restoredFromId);
828         printf("\tneedsCallback    = %d\n", v.needsCallback);
829         printf("\tdestroyMe        = %d\n", v.destroyMe);
830         printf("\tdontSalvage      = %d\n", v.dontSalvage);
831         printf("\tmaxquota         = %d\n", v.maxquota);
832         printf("\tminquota         = %d\n", v.minquota);
833         printf("\tmaxfiles         = %d\n", v.maxfiles);
834         printf("\taccountNumber    = %u\n", v.accountNumber);
835         printf("\towner            = %u\n", v.owner);
836         printf("\tfilecount        = %d\n", v.filecount);
837         printf("\tdiskused         = %d\n", v.diskused);
838         printf("\tdayUse           = %d\n", v.dayUse);
839         for (i = 0; i < 7; i++) {
840             printf("\tweekUse[%d]       = %d\n", i, v.weekUse[i]);
841         }
842         printf("\tdayUseDate       = %u\n", v.dayUseDate);
843         printf("\tcreationDate     = %u\n", v.creationDate);
844         printf("\taccessDate       = %u\n", v.accessDate);
845         printf("\tupdateDate       = %u\n", v.updateDate);
846         printf("\texpirationDate   = %u\n", v.expirationDate);
847         printf("\tbackupDate       = %u\n", v.backupDate);
848         printf("\tcopyDate         = %u\n", v.copyDate);
849 #ifdef OPENAFS_VOL_STATS
850         printf("\tstat_initialized = %d\n", v.stat_initialized);
851 #else
852         printf("\tmtd              = '%s'\n", v.motd);
853 #endif
854         printf("}\n");
855     }
856
857     return 0;
858 }
859
860 static int
861 VolOpQuery(struct cmd_syndesc * as, void * rock)
862 {
863     struct state state;
864     SYNC_PROTO_BUF_DECL(res_buf);
865     SYNC_response res;
866     FSSYNC_VolOp_info vop;
867
868     res.hdr.response_len = sizeof(res.hdr);
869     res.payload.buf = res_buf;
870     res.payload.len = SYNC_PROTO_MAX_LEN;
871
872     common_prolog(as, &state);
873     common_volop_prolog(as, &state);
874
875     do_volop(&state, FSYNC_VOL_QUERY_VOP, &res);
876
877     if (!(res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS)) {
878         printf("*** file server not compiled with demand attach extensions.\n");
879         printf("*** pending volume operation metadata not available.\n");
880     }
881
882     if (res.hdr.response == SYNC_OK) {
883         memcpy(&vop, res.payload.buf, sizeof(FSSYNC_VolOp_info));
884
885         printf("pending_vol_op = {\n");
886
887         printf("\tcom = {\n");
888         printf("\t\tproto_version  = %u\n", vop.com.proto_version);
889         printf("\t\tpkt_seq        = %u\n", vop.com.pkt_seq);
890         printf("\t\tcom_seq        = %u\n", vop.com.com_seq);
891         printf("\t\tprogramType    = %d (%s)\n", 
892                vop.com.programType, program_type_to_string(vop.com.programType));
893         printf("\t\tpid            = %d\n", vop.com.pid);
894         printf("\t\ttid            = %d\n", vop.com.tid);
895         printf("\t\tcommand        = %d (%s)\n", 
896                vop.com.command, command_code_to_string(vop.com.command));
897         printf("\t\treason         = %d (%s)\n", 
898                vop.com.reason, reason_code_to_string(vop.com.reason));
899         printf("\t\tcommand_len    = %u\n", vop.com.command_len);
900         printf("\t\tflags          = 0x%lux\n", afs_printable_uint32_lu(vop.com.flags));
901         printf("\t}\n");
902
903         printf("\tvop = {\n");
904         printf("\t\tvolume         = %u\n", vop.vop.volume);
905         if (afs_strnlen(vop.vop.partName, sizeof(vop.vop.partName)) <
906             sizeof(vop.vop.partName)) {
907             printf("\t\tpartName       = '%s'\n", vop.vop.partName);
908         } else {
909             printf("\t\tpartName       = (illegal string)\n");
910         }
911         printf("\t}\n");
912
913         printf("}\n");
914     }
915
916     return 0;
917 }
918
919 static int
920 vn_prolog(struct cmd_syndesc * as, struct state * state)
921 {
922     register struct cmd_item *ti;
923
924     state->vop = (struct volop_state *) calloc(1, sizeof(struct volop_state));
925     assert(state->vop != NULL);
926
927     if ((ti = as->parms[CUSTOM_PARMS_OFFSET].items)) {  /* -volumeid */
928         state->vop->volume = atoi(ti->data);
929     } else {
930         fprintf(stderr, "required argument -volumeid not given\n");
931     }
932
933     if ((ti = as->parms[CUSTOM_PARMS_OFFSET+1].items)) {        /* -vnodeid */
934         state->vop->vnode = atoi(ti->data);
935     } else {
936         fprintf(stderr, "required argument -vnodeid not given\n");
937     }
938
939     if ((ti = as->parms[CUSTOM_PARMS_OFFSET+2].items)) {        /* -unique */
940         state->vop->unique = atoi(ti->data);
941     } else {
942         state->vop->unique = 0;
943     }
944
945     if ((ti = as->parms[COMMON_VOLOP_PARMS_OFFSET+3].items)) {  /* -partition */
946         strlcpy(state->vop->partName, ti->data, sizeof(state->vop->partName));
947     } else {
948         memset(state->vop->partName, 0, sizeof(state->vop->partName));
949     }
950
951     return 0;
952 }
953
954 static int
955 do_vnqry(struct state * state, SYNC_response * res)
956 {
957     afs_int32 code;
958     int command = FSYNC_VOL_QUERY_VNODE;
959     FSSYNC_VnQry_hdr qry;
960
961     qry.volume = state->vop->volume;
962     qry.vnode = state->vop->vnode;
963     qry.unique = state->vop->unique;
964     qry.spare = 0;
965     strlcpy(qry.partName, state->vop->partName, sizeof(qry.partName));
966
967     fprintf(stderr, "calling FSYNC_GenericOp with command code %d (%s)\n", 
968             command, command_code_to_string(command));
969
970     code = FSYNC_GenericOp(&qry, sizeof(qry), command, FSYNC_OPERATOR, res);
971
972     switch (code) {
973     case SYNC_OK:
974     case SYNC_DENIED:
975         break;
976     default:
977         fprintf(stderr, "possible sync protocol error. return code was %d\n", code);
978     }
979
980     fprintf(stderr, "FSYNC_GenericOp returned %d (%s)\n", code, response_code_to_string(code));
981     fprintf(stderr, "protocol response code was %d (%s)\n", 
982             res->hdr.response, response_code_to_string(res->hdr.response));
983     fprintf(stderr, "protocol reason code was %d (%s)\n", 
984             res->hdr.reason, reason_code_to_string(res->hdr.reason));
985
986     VDisconnectFS();
987
988     return 0;
989 }
990
991 static int
992 VnQuery(struct cmd_syndesc * as, void * rock)
993 {
994     struct state state;
995     SYNC_PROTO_BUF_DECL(res_buf);
996     SYNC_response res;
997     Vnode v;
998
999     res.hdr.response_len = sizeof(res.hdr);
1000     res.payload.buf = res_buf;
1001     res.payload.len = SYNC_PROTO_MAX_LEN;
1002
1003     common_prolog(as, &state);
1004     vn_prolog(as, &state);
1005
1006     do_vnqry(&state, &res);
1007
1008     if (res.hdr.response == SYNC_OK) {
1009         memcpy(&v, res.payload.buf, sizeof(Volume));
1010
1011         printf("vnode = {\n");
1012
1013         printf("\tvid_hash = {\n");
1014         printf("\t\tnext = %p\n", v.vid_hash.next);
1015         printf("\t\tprev = %p\n", v.vid_hash.prev);
1016         printf("\t}\n");
1017
1018         printf("\thashNext        = %p\n", v.hashNext);
1019         printf("\tlruNext         = %p\n", v.lruNext);
1020         printf("\tlruPrev         = %p\n", v.lruPrev);
1021         printf("\thashIndex       = %hu\n", v.hashIndex);
1022         printf("\tchanged_newTime = %u\n", (unsigned int) v.changed_newTime);
1023         printf("\tchanged_oldTime = %u\n", (unsigned int) v.changed_oldTime);
1024         printf("\tdelete          = %u\n", (unsigned int) v.delete);
1025         printf("\tvnodeNumber     = %u\n", v.vnodeNumber);
1026         printf("\tvolumePtr       = %p\n", v.volumePtr);
1027         printf("\tnUsers          = %u\n", v.nUsers);
1028         printf("\tcacheCheck      = %u\n", v.cacheCheck);
1029
1030 #ifdef AFS_DEMAND_ATTACH_FS
1031         if (!(res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS)) {
1032             printf("*** fssync-debug built to expect demand attach extensions.  server asserted\n");
1033             printf("*** that it was not compiled with demand attach turned on.  please recompile\n");
1034             printf("*** fssync-debug to match your server\n");
1035             goto done;
1036         }
1037
1038         printf("\tnReaders        = %u\n", v.nReaders);
1039         printf("\tvn_state_flags  = %s\n", vn_flags_to_string(v.vn_state_flags));
1040         printf("\tvn_state        = %s\n", vn_state_to_string(v.vn_state));
1041 #else
1042         if (res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS) {
1043             printf("*** server asserted demand attach extensions. fssync-debug not built to\n");
1044             printf("*** recognize those extensions. please recompile fssync-debug if you need\n");
1045             printf("*** to dump dafs extended state\n");
1046             goto done;
1047         }
1048 #endif /* !AFS_DEMAND_ATTACH_FS */
1049
1050         printf("\twriter          = %u\n", (unsigned int) v.writer);
1051         printf("\tvcp             = %p\n", v.vcp);
1052         printf("\thandle          = %p\n", v.handle);
1053
1054         printf("\tdisk = {\n");
1055         printf("\t\ttype              = %u\n", v.disk.type);
1056         printf("\t\tcloned            = %u\n", v.disk.cloned);
1057         printf("\t\tmodeBits          = %u\n", v.disk.modeBits);
1058         printf("\t\tlinkCount         = %d\n", v.disk.linkCount);
1059         printf("\t\tlength            = %u\n", v.disk.length);
1060         printf("\t\tuniquifier        = %u\n", v.disk.uniquifier);
1061         printf("\t\tdataVersion       = %u\n", v.disk.dataVersion);
1062         printf("\t\tvn_ino_lo         = %u\n", v.disk.vn_ino_lo);
1063         printf("\t\tunixModifyTime    = %u\n", v.disk.unixModifyTime);
1064         printf("\t\tauthor            = %u\n", v.disk.author);
1065         printf("\t\towner             = %u\n", v.disk.owner);
1066         printf("\t\tparent            = %u\n", v.disk.parent);
1067         printf("\t\tvnodeMagic        = %u\n", v.disk.vnodeMagic);
1068
1069         printf("\t\tlock = {\n");
1070         printf("\t\t\tlockCount   = %d\n", v.disk.lock.lockCount);
1071         printf("\t\t\tlockTime    = %d\n", v.disk.lock.lockTime);
1072         printf("\t\t}\n");
1073
1074         printf("\t\tserverModifyTime  = %u\n", v.disk.serverModifyTime);
1075         printf("\t\tgroup             = %d\n", v.disk.group);
1076         printf("\t\tvn_ino_hi         = %d\n", v.disk.vn_ino_hi);
1077         printf("\t\tvn_length_hi      = %u\n", v.disk.vn_length_hi);
1078         printf("\t}\n");
1079
1080         printf("}\n");
1081     }
1082
1083  done:
1084     return 0;
1085 }
1086
1087
1088 static int
1089 StatsQuery(struct cmd_syndesc * as, void * rock)
1090 {
1091     afs_int32 code;
1092     int command;
1093     struct cmd_item *ti;
1094     struct state state;
1095     SYNC_PROTO_BUF_DECL(res_buf);
1096     SYNC_response res;
1097     FSSYNC_StatsOp_hdr scom;
1098     union {
1099         void * ptr;
1100         struct VolPkgStats * vol_stats;
1101         struct VolumeHashChainStats * hash_stats;
1102 #ifdef AFS_DEMAND_ATTACH_FS
1103         struct volume_hdr_LRU_stats * hdr_stats;
1104 #endif
1105         struct DiskPartitionStats64 * vicep_stats;
1106     } sres;
1107
1108     sres.ptr = res_buf;
1109     res.hdr.response_len = sizeof(res.hdr);
1110     res.payload.buf = res_buf;
1111     res.payload.len = SYNC_PROTO_MAX_LEN;
1112
1113     if ((ti = as->parms[CUSTOM_PARMS_OFFSET].items)) {  /* -subcommand */
1114         if (!strcasecmp(ti->data, "vicep")) {
1115             command = FSYNC_VOL_STATS_VICEP;
1116         } else if (!strcasecmp(ti->data, "hash")) {
1117             command = FSYNC_VOL_STATS_HASH;
1118 #ifdef AFS_DEMAND_ATTACH_FS
1119         } else if (!strcasecmp(ti->data, "hdr")) {
1120             command = FSYNC_VOL_STATS_HDR;
1121         } else if (!strcasecmp(ti->data, "vlru")) {
1122             command = FSYNC_VOL_STATS_VLRU;
1123 #endif
1124         } else if (!strcasecmp(ti->data, "pkg")) {
1125             command = FSYNC_VOL_STATS_GENERAL;
1126         } else if (!strcasecmp(ti->data, "help")) {
1127             fprintf(stderr, "fssync-debug stats subcommands:\n");
1128             fprintf(stderr, "\tpkg\tgeneral volume package stats\n");
1129             fprintf(stderr, "\tvicep\tvice partition stats\n");
1130             fprintf(stderr, "\thash\tvolume hash chain stats\n");
1131 #ifdef AFS_DEMAND_ATTACH_FS
1132             fprintf(stderr, "\thdr\tvolume header cache stats\n");
1133             fprintf(stderr, "\tvlru\tvlru generation stats\n");
1134 #endif
1135             exit(0);
1136         } else {
1137             fprintf(stderr, "invalid stats subcommand");
1138             exit(1);
1139         }
1140     } else {
1141         command = FSYNC_VOL_STATS_GENERAL;
1142     }
1143
1144     if ((ti = as->parms[CUSTOM_PARMS_OFFSET+1].items)) {        /* -arg1 */
1145         switch (command) {
1146         case FSYNC_VOL_STATS_VICEP:
1147             strlcpy(scom.args.partName, ti->data, sizeof(state.vop->partName));
1148             break;
1149         case FSYNC_VOL_STATS_HASH:
1150             scom.args.hash_bucket = atoi(ti->data);
1151             break;
1152         case FSYNC_VOL_STATS_VLRU:
1153             scom.args.vlru_generation = atoi(ti->data);
1154             break;
1155         default:
1156             fprintf(stderr, "unrecognized arguments\n");
1157             exit(1);
1158         }
1159     } else {
1160         switch (command) {
1161         case FSYNC_VOL_STATS_VICEP:
1162         case FSYNC_VOL_STATS_HASH:
1163         case FSYNC_VOL_STATS_VLRU:
1164             fprintf(stderr, "this subcommand requires more parameters\n");
1165             exit(1);
1166         }
1167     }
1168
1169     common_prolog(as, &state);
1170
1171     fprintf(stderr, "calling FSYNC_askfs with command code %d (%s)\n", 
1172             command, command_code_to_string(command));
1173
1174     code = FSYNC_StatsOp(&scom, command, FSYNC_WHATEVER, &res);
1175
1176     switch (code) {
1177     case SYNC_OK:
1178     case SYNC_DENIED:
1179         break;
1180     default:
1181         fprintf(stderr, "possible sync protocol error. return code was %d\n", code);
1182     }
1183
1184     fprintf(stderr, "FSYNC_VolOp returned %d (%s)\n", code, response_code_to_string(code));
1185     fprintf(stderr, "protocol response code was %d (%s)\n", 
1186             res.hdr.response, response_code_to_string(res.hdr.response));
1187     fprintf(stderr, "protocol reason code was %d (%s)\n", 
1188             res.hdr.reason, reason_code_to_string(res.hdr.reason));
1189
1190     VDisconnectFS();
1191
1192     if (res.hdr.response == SYNC_OK) {
1193         switch (command) {
1194         case FSYNC_VOL_STATS_GENERAL:
1195             print_vol_stats_general(sres.vol_stats);
1196             break;
1197         case FSYNC_VOL_STATS_VICEP:
1198             print_vol_stats_viceP(sres.vicep_stats);
1199             break;
1200         case FSYNC_VOL_STATS_HASH:
1201             print_vol_stats_hash(sres.hash_stats);
1202             break;
1203 #ifdef AFS_DEMAND_ATTACH_FS
1204         case FSYNC_VOL_STATS_HDR:
1205             print_vol_stats_hdr(sres.hdr_stats);
1206             break;
1207 #endif /* AFS_DEMAND_ATTACH_FS */
1208         }
1209     }
1210
1211     return 0;
1212 }
1213
1214 static void
1215 print_vol_stats_general(VolPkgStats * stats)
1216 {
1217 #ifdef AFS_DEMAND_ATTACH_FS
1218     int i;
1219 #endif
1220     afs_uint32 hi, lo;
1221
1222     printf("VolPkgStats = {\n");
1223 #ifdef AFS_DEMAND_ATTACH_FS
1224     for (i = 0; i < VOL_STATE_COUNT; i++) {
1225         printf("\tvol_state_count[%s] = %d\n", 
1226                vol_state_to_string(i),
1227                stats->state_levels[i]);
1228     }
1229
1230     SplitInt64(stats->hash_looks, hi, lo);
1231     printf("\thash_looks = {\n");
1232     printf("\t\thi = %u\n", hi);
1233     printf("\t\tlo = %u\n", lo);
1234     printf("\t}\n");
1235
1236     SplitInt64(stats->hash_reorders, hi, lo);
1237     printf("\thash_reorders = {\n");
1238     printf("\t\thi = %u\n", hi);
1239     printf("\t\tlo = %u\n", lo);
1240     printf("\t}\n");
1241
1242     SplitInt64(stats->salvages, hi, lo);
1243     printf("\tsalvages = {\n");
1244     printf("\t\thi = %u\n", hi);
1245     printf("\t\tlo = %u\n", lo);
1246     printf("\t}\n");
1247
1248     SplitInt64(stats->vol_ops, hi, lo);
1249     printf("\tvol_ops = {\n");
1250     printf("\t\thi = %u\n", hi);
1251     printf("\t\tlo = %u\n", lo);
1252     printf("\t}\n");
1253 #endif
1254     SplitInt64(stats->hdr_loads, hi, lo);
1255     printf("\thdr_loads = {\n");
1256     printf("\t\thi = %u\n", hi);
1257     printf("\t\tlo = %u\n", lo);
1258     printf("\t}\n");
1259
1260     SplitInt64(stats->hdr_gets, hi, lo);
1261     printf("\thdr_gets = {\n");
1262     printf("\t\thi = %u\n", hi);
1263     printf("\t\tlo = %u\n", lo);
1264     printf("\t}\n");
1265
1266     SplitInt64(stats->attaches, hi, lo);
1267     printf("\tattaches = {\n");
1268     printf("\t\thi = %u\n", hi);
1269     printf("\t\tlo = %u\n", lo);
1270     printf("\t}\n");
1271
1272     SplitInt64(stats->soft_detaches, hi, lo);
1273     printf("\tsoft_detaches = {\n");
1274     printf("\t\thi = %u\n", hi);
1275     printf("\t\tlo = %u\n", lo);
1276     printf("\t}\n");
1277
1278     printf("\thdr_cache_size = %d\n", stats->hdr_cache_size);
1279             
1280     printf("}\n");
1281 }
1282
1283 static void
1284 print_vol_stats_viceP(struct DiskPartitionStats64 * stats)
1285 {
1286     printf("DiskPartitionStats64 = {\n");
1287     printf("\tfree = %" AFS_INT64_FMT "\n", stats->free);
1288     printf("\tminFree = %" AFS_INT64_FMT "\n", stats->minFree);
1289     printf("\ttotalUsable = %" AFS_INT64_FMT "\n", stats->totalUsable);
1290     printf("\tf_files = %" AFS_INT64_FMT "\n", stats->f_files);
1291 #ifdef AFS_DEMAND_ATTACH_FS
1292     printf("\tvol_list_len = %d\n", stats->vol_list_len);
1293 #endif
1294     printf("}\n");
1295 }
1296
1297 static void
1298 print_vol_stats_hash(struct VolumeHashChainStats * stats)
1299 {
1300 #ifdef AFS_DEMAND_ATTACH_FS
1301     afs_uint32 hi, lo;
1302 #endif
1303
1304     printf("DiskPartitionStats = {\n");
1305     printf("\ttable_size = %d\n", stats->table_size);
1306     printf("\tchain_len = %d\n", stats->chain_len);
1307
1308 #ifdef AFS_DEMAND_ATTACH_FS
1309     printf("\tchain_cacheCheck = %d\n", stats->chain_cacheCheck);
1310     printf("\tchain_busy = %d\n", stats->chain_busy);
1311
1312     SplitInt64(stats->chain_looks, hi, lo);
1313     printf("\tchain_looks = {\n");
1314     printf("\t\thi = %u\n", hi);
1315     printf("\t\tlo = %u\n", lo);
1316     printf("\t}\n");
1317
1318     SplitInt64(stats->chain_gets, hi, lo);
1319     printf("\tchain_gets = {\n");
1320     printf("\t\thi = %u\n", hi);
1321     printf("\t\tlo = %u\n", lo);
1322     printf("\t}\n");
1323
1324     SplitInt64(stats->chain_reorders, hi, lo);
1325     printf("\tchain_reorders = {\n");
1326     printf("\t\thi = %u\n", hi);
1327     printf("\t\tlo = %u\n", lo);
1328     printf("\t}\n");
1329 #endif /* AFS_DEMAND_ATTACH_FS */
1330
1331     printf("}\n");
1332 }
1333
1334
1335 #ifdef AFS_DEMAND_ATTACH_FS
1336 static void
1337 print_vol_stats_hdr(struct volume_hdr_LRU_stats * stats)
1338 {
1339     printf("volume_hdr_LRU_stats = {\n");
1340     printf("\tfree = %d\n", stats->free);
1341     printf("\tused = %d\n", stats->used);
1342     printf("\tattached = %d\n", stats->attached);
1343     printf("}\n");
1344 }
1345 #endif /* AFS_DEMAND_ATTACH_FS */
1346