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