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