dafs-updates-20080612
[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     int i;
149     extern char cml_version_number[];
150
151     /* Initialize directory paths */
152     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
153 #ifdef AFS_NT40_ENV
154         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
155 #endif
156         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
157                 argv[0]);
158         exit(2);
159     }
160
161     
162     ts = cmd_CreateSyntax("online", VolOnline, NULL, "bring a volume online (FSYNC_VOL_ON opcode)");
163     VOLOP_PARMS_DECL(ts);
164
165     ts = cmd_CreateSyntax("offline", VolOffline, NULL, "take a volume offline (FSYNC_VOL_OFF opcode)");
166     VOLOP_PARMS_DECL(ts);
167
168     ts = cmd_CreateSyntax("mode", VolMode, NULL, "change volume attach mode (FSYNC_VOL_NEEDVOLUME opcode)");
169     VOLOP_PARMS_DECL(ts);
170     cmd_CreateAlias(ts, "needvolume");
171
172     ts = cmd_CreateSyntax("detach", VolDetach, NULL, "detach a volume (FSYNC_VOL_DONE opcode)");
173     VOLOP_PARMS_DECL(ts);
174
175     ts = cmd_CreateSyntax("callback", VolBreakCBKs, NULL, "break callbacks for volume (FSYNC_VOL_BREAKCBKS opcode)");
176     VOLOP_PARMS_DECL(ts);
177     cmd_CreateAlias(ts, "cbk");
178
179     ts = cmd_CreateSyntax("move", VolMove, NULL, "set volume moved flag (FSYNC_VOL_MOVE opcode)");
180     VOLOP_PARMS_DECL(ts);
181
182     ts = cmd_CreateSyntax("list", VolList, NULL, "sync local volume list (FSYNC_VOL_LISTVOLUMES opcode)");
183     VOLOP_PARMS_DECL(ts);
184     cmd_CreateAlias(ts, "ls");
185
186     ts = cmd_CreateSyntax("leaveoff", VolLeaveOff, 0, "leave volume offline (FSYNC_VOL_LEAVE_OFF opcode)");
187     VOLOP_PARMS_DECL(ts);
188
189     ts = cmd_CreateSyntax("attach", VolForceAttach, 0, "force full attachment (FSYNC_VOL_ATTACH opcode)");
190     VOLOP_PARMS_DECL(ts);
191
192     ts = cmd_CreateSyntax("error", VolForceError, 0, "force into hard error state (FSYNC_VOL_FORCE_ERROR opcode)");
193     VOLOP_PARMS_DECL(ts);
194
195     ts = cmd_CreateSyntax("query", VolQuery, NULL, "get volume structure (FSYNC_VOL_QUERY opcode)");
196     VOLOP_PARMS_DECL(ts);
197     cmd_CreateAlias(ts, "qry");
198
199     ts = cmd_CreateSyntax("header", VolHdrQuery, NULL, "get volume disk data structure (FSYNC_VOL_QUERY_HDR opcode)");
200     VOLOP_PARMS_DECL(ts);
201     cmd_CreateAlias(ts, "hdr");
202
203     ts = cmd_CreateSyntax("volop", VolOpQuery, NULL, "get pending volume operation info (FSYNC_VOL_QUERY_VOP opcode)");
204     VOLOP_PARMS_DECL(ts);
205     cmd_CreateAlias(ts, "vop");
206
207     ts = cmd_CreateSyntax("vnode", VnQuery, NULL, "get vnode structure (FSYNC_VOL_QUERY_VNODE opcode)");
208     cmd_Seek(ts, CUSTOM_PARMS_OFFSET);
209     cmd_AddParm(ts, "-volumeid", CMD_SINGLE, 0, "volume id");
210     cmd_AddParm(ts, "-vnodeid", CMD_SINGLE, 0, "vnode id");
211     cmd_AddParm(ts, "-unique", CMD_SINGLE, 0, "uniquifier");
212     cmd_AddParm(ts, "-partition", CMD_SINGLE, 0, "paritition name");
213     COMMON_PARMS_DECL(ts);
214
215     ts = cmd_CreateSyntax("stats", StatsQuery, NULL, "see 'stats help' for more information");
216     cmd_Seek(ts, CUSTOM_PARMS_OFFSET);
217     cmd_AddParm(ts, "-cmd", CMD_SINGLE, 0, "subcommand");
218     cmd_AddParm(ts, "-arg1", CMD_SINGLE, CMD_OPTIONAL, "arg1");
219     cmd_AddParm(ts, "-arg2", CMD_SINGLE, CMD_OPTIONAL, "arg2");
220     COMMON_PARMS_DECL(ts);
221
222     err = cmd_Dispatch(argc, argv);
223     exit(err);
224 }
225
226 static int
227 common_prolog(struct cmd_syndesc * as, struct state * state)
228 {
229     register struct cmd_item *ti;
230
231 #ifdef AFS_NT40_ENV
232     if (afs_winsockInit() < 0) {
233         Exit(1);
234     }
235 #endif
236
237     VInitVolumePackage(debugUtility, 1, 1,
238                        DONT_CONNECT_FS, 0);
239     DInit(1);
240
241     if ((ti = as->parms[COMMON_PARMS_OFFSET].items)) {  /* -reason */
242         state->reason = atoi(ti->data);
243     }
244     if ((ti = as->parms[COMMON_PARMS_OFFSET+1].items)) {        /* -programtype */
245         if (!strcmp(ti->data, "fileServer")) {
246             programType = fileServer;
247         } else if (!strcmp(ti->data, "volumeUtility")) {
248             programType = volumeUtility;
249         } else if (!strcmp(ti->data, "salvager")) {
250             programType = salvager;
251         } else if (!strcmp(ti->data, "salvageServer")) {
252             programType = salvageServer;
253         } else {
254             programType = (ProgramType) atoi(ti->data);
255         }
256     }
257
258     VConnectFS();
259
260     return 0;
261 }
262
263 static int
264 common_volop_prolog(struct cmd_syndesc * as, struct state * state)
265 {
266     register struct cmd_item *ti;
267     char pname[100], *temp;
268
269     state->vop = (struct volop_state *) calloc(1, sizeof(struct volop_state));
270     assert(state->vop != NULL);
271
272     if ((ti = as->parms[COMMON_VOLOP_PARMS_OFFSET].items)) {    /* -volumeid */
273         state->vop->volume = atoi(ti->data);
274     } else {
275         fprintf(stderr, "required argument -volumeid not given\n");
276     }
277
278     if ((ti = as->parms[COMMON_VOLOP_PARMS_OFFSET+1].items)) {  /* -partition */
279         strlcpy(state->vop->partName, ti->data, sizeof(state->vop->partName));
280     } else {
281         memset(state->vop->partName, 0, sizeof(state->vop->partName));
282     }
283
284     return 0;
285 }
286
287 static int
288 do_volop(struct state * state, afs_int32 command, SYNC_response * res)
289 {
290     afs_int32 code;
291     SYNC_PROTO_BUF_DECL(res_buf);
292     SYNC_response res_l;
293
294     if (!res) {
295         res = &res_l;
296         res->payload.len = SYNC_PROTO_MAX_LEN;
297         res->payload.buf = res_buf;
298     }
299
300     fprintf(stderr, "calling FSYNC_VolOp with command code %d (%s)\n", 
301             command, command_code_to_string(command));
302
303     code = FSYNC_VolOp(state->vop->volume,
304                        state->vop->partName,
305                        command,
306                        state->reason,
307                        res);
308
309     switch (code) {
310     case SYNC_OK:
311     case SYNC_DENIED:
312         break;
313     default:
314         fprintf(stderr, "possible sync protocol error. return code was %d\n", code);
315     }
316
317     fprintf(stderr, "FSYNC_VolOp returned %d (%s)\n", code, response_code_to_string(code));
318     fprintf(stderr, "protocol response code was %d (%s)\n", 
319             res->hdr.response, response_code_to_string(res->hdr.response));
320     fprintf(stderr, "protocol reason code was %d (%s)\n", 
321             res->hdr.reason, reason_code_to_string(res->hdr.reason));
322
323     VDisconnectFS();
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     int hi, lo;
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          = 0x%x\n", v.header);
678         printf("\tdevice          = %d\n", v.device);
679         printf("\tpartition       = 0x%x\n", v.partition);
680         printf("\tlinkHandle      = 0x%x\n", v.linkHandle);
681         printf("\tnextVnodeUnique = %u\n", v.nextVnodeUnique);
682         printf("\tdiskDataHandle  = 0x%x\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       = 0x%x\n", v.vnodeIndex[vSmall].handle);
694         printf("\t\tbitmap       = 0x%x\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       = 0x%x\n", v.vnodeIndex[vLarge].handle);
700         printf("\t\tbitmap       = 0x%x\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  = 0x%x\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     int i;
867
868     res.hdr.response_len = sizeof(res.hdr);
869     res.payload.buf = res_buf;
870     res.payload.len = SYNC_PROTO_MAX_LEN;
871
872     common_prolog(as, &state);
873     common_volop_prolog(as, &state);
874
875     do_volop(&state, FSYNC_VOL_QUERY_VOP, &res);
876
877     if (!(res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS)) {
878         printf("*** file server not compiled with demand attach extensions.\n");
879         printf("*** pending volume operation metadata not available.\n");
880     }
881
882     if (res.hdr.response == SYNC_OK) {
883         memcpy(&vop, res.payload.buf, sizeof(FSSYNC_VolOp_info));
884
885         printf("pending_vol_op = {\n");
886
887         printf("\tcom = {\n");
888         printf("\t\tproto_version  = %u\n", vop.com.proto_version);
889         printf("\t\tpkt_seq        = %u\n", vop.com.pkt_seq);
890         printf("\t\tcom_seq        = %u\n", vop.com.com_seq);
891         printf("\t\tprogramType    = %d (%s)\n", 
892                vop.com.programType, program_type_to_string(vop.com.programType));
893         printf("\t\tpid            = %d\n", vop.com.pid);
894         printf("\t\ttid            = %d\n", vop.com.tid);
895         printf("\t\tcommand        = %d (%s)\n", 
896                vop.com.command, command_code_to_string(vop.com.command));
897         printf("\t\treason         = %d (%s)\n", 
898                vop.com.reason, reason_code_to_string(vop.com.reason));
899         printf("\t\tcommand_len    = %u\n", vop.com.command_len);
900         printf("\t\tflags          = 0x%x\n", vop.com.flags);
901         printf("\t}\n");
902
903         printf("\tvop = {\n");
904         printf("\t\tvolume         = %u\n", vop.vop.volume);
905         if (afs_strnlen(vop.vop.partName, sizeof(vop.vop.partName)) <
906             sizeof(vop.vop.partName)) {
907             printf("\t\tpartName       = '%s'\n", vop.vop.partName);
908         } else {
909             printf("\t\tpartName       = (illegal string)\n");
910         }
911         printf("\t}\n");
912
913         printf("}\n");
914     }
915
916     return 0;
917 }
918
919 static int
920 vn_prolog(struct cmd_syndesc * as, struct state * state)
921 {
922     register struct cmd_item *ti;
923     char pname[100], *temp;
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     int hi, lo;
1000
1001     res.hdr.response_len = sizeof(res.hdr);
1002     res.payload.buf = res_buf;
1003     res.payload.len = SYNC_PROTO_MAX_LEN;
1004
1005     common_prolog(as, &state);
1006     vn_prolog(as, &state);
1007
1008     do_vnqry(&state, &res);
1009
1010     if (res.hdr.response == SYNC_OK) {
1011         memcpy(&v, res.payload.buf, sizeof(Volume));
1012
1013         printf("vnode = {\n");
1014
1015         printf("\tvid_hash = {\n");
1016         printf("\t\tnext = 0x%lx\n", v.vid_hash.next);
1017         printf("\t\tprev = 0x%lx\n", v.vid_hash.prev);
1018         printf("\t}\n");
1019
1020         printf("\thashNext        = 0x%lx\n", v.hashNext);
1021         printf("\tlruNext         = 0x%lx\n", v.lruNext);
1022         printf("\tlruPrev         = 0x%lx\n", v.lruPrev);
1023         printf("\thashIndex       = %hu\n", v.hashIndex);
1024         printf("\tchanged_newTime = %u\n", (unsigned int) v.changed_newTime);
1025         printf("\tchanged_oldTime = %u\n", (unsigned int) v.changed_oldTime);
1026         printf("\tdelete          = %u\n", (unsigned int) v.delete);
1027         printf("\tvnodeNumber     = %u\n", v.vnodeNumber);
1028         printf("\tvolumePtr       = 0x%lx\n", v.volumePtr);
1029         printf("\tnUsers          = %u\n", v.nUsers);
1030         printf("\tcacheCheck      = %u\n", v.cacheCheck);
1031
1032 #ifdef AFS_DEMAND_ATTACH_FS
1033         if (!(res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS)) {
1034             printf("*** fssync-debug built to expect demand attach extensions.  server asserted\n");
1035             printf("*** that it was not compiled with demand attach turned on.  please recompile\n");
1036             printf("*** fssync-debug to match your server\n");
1037             goto done;
1038         }
1039
1040         printf("\tnReaders        = %u\n", v.nReaders);
1041         printf("\tvn_state_flags  = %s\n", vn_flags_to_string(v.vn_state_flags));
1042         printf("\tvn_state        = %s\n", vn_state_to_string(v.vn_state));
1043 #else
1044         if (res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS) {
1045             printf("*** server asserted demand attach extensions. fssync-debug not built to\n");
1046             printf("*** recognize those extensions. please recompile fssync-debug if you need\n");
1047             printf("*** to dump dafs extended state\n");
1048             goto done;
1049         }
1050 #endif /* !AFS_DEMAND_ATTACH_FS */
1051
1052         printf("\twriter          = %u\n", v.writer);
1053         printf("\tvcp             = 0x%lx\n", v.vcp);
1054         printf("\thandle          = 0x%lx\n", v.handle);
1055
1056         printf("\tdisk = {\n");
1057         printf("\t\ttype              = %u\n", v.disk.type);
1058         printf("\t\tcloned            = %u\n", v.disk.cloned);
1059         printf("\t\tmodeBits          = %u\n", v.disk.modeBits);
1060         printf("\t\tlinkCount         = %d\n", v.disk.linkCount);
1061         printf("\t\tlength            = %u\n", v.disk.length);
1062         printf("\t\tuniquifier        = %u\n", v.disk.uniquifier);
1063         printf("\t\tdataVersion       = %u\n", v.disk.dataVersion);
1064         printf("\t\tvn_ino_lo         = %u\n", v.disk.vn_ino_lo);
1065         printf("\t\tunixModifyTime    = %u\n", v.disk.unixModifyTime);
1066         printf("\t\tauthor            = %u\n", v.disk.author);
1067         printf("\t\towner             = %u\n", v.disk.owner);
1068         printf("\t\tparent            = %u\n", v.disk.parent);
1069         printf("\t\tvnodeMagic        = %u\n", v.disk.vnodeMagic);
1070
1071         printf("\t\tlock = {\n");
1072         printf("\t\t\tlockCount   = %d\n", v.disk.lock.lockCount);
1073         printf("\t\t\tlockTime    = %d\n", v.disk.lock.lockTime);
1074         printf("\t\t}\n");
1075
1076         printf("\t\tserverModifyTime  = %u\n", v.disk.serverModifyTime);
1077         printf("\t\tgroup             = %d\n", v.disk.group);
1078         printf("\t\tvn_ino_hi         = %d\n", v.disk.vn_ino_hi);
1079         printf("\t\treserved6         = %u\n", v.disk.reserved6);
1080         printf("\t}\n");
1081
1082         printf("}\n");
1083     }
1084
1085  done:
1086     return 0;
1087 }
1088
1089
1090 static int
1091 StatsQuery(struct cmd_syndesc * as, void * rock)
1092 {
1093     afs_int32 code;
1094     int command;
1095     struct cmd_item *ti;
1096     struct state state;
1097     SYNC_PROTO_BUF_DECL(res_buf);
1098     SYNC_response res;
1099     FSSYNC_StatsOp_hdr scom;
1100     union {
1101         void * ptr;
1102         struct VolPkgStats * vol_stats;
1103         struct VolumeHashChainStats * hash_stats;
1104 #ifdef AFS_DEMAND_ATTACH_FS
1105         struct volume_hdr_LRU_stats * hdr_stats;
1106 #endif
1107         struct DiskPartitionStats64 * vicep_stats;
1108     } sres;
1109
1110     sres.ptr = res_buf;
1111     res.hdr.response_len = sizeof(res.hdr);
1112     res.payload.buf = res_buf;
1113     res.payload.len = SYNC_PROTO_MAX_LEN;
1114
1115     if ((ti = as->parms[CUSTOM_PARMS_OFFSET].items)) {  /* -subcommand */
1116         if (!strcasecmp(ti->data, "vicep")) {
1117             command = FSYNC_VOL_STATS_VICEP;
1118         } else if (!strcasecmp(ti->data, "hash")) {
1119             command = FSYNC_VOL_STATS_HASH;
1120 #ifdef AFS_DEMAND_ATTACH_FS
1121         } else if (!strcasecmp(ti->data, "hdr")) {
1122             command = FSYNC_VOL_STATS_HDR;
1123         } else if (!strcasecmp(ti->data, "vlru")) {
1124             command = FSYNC_VOL_STATS_VLRU;
1125 #endif
1126         } else if (!strcasecmp(ti->data, "pkg")) {
1127             command = FSYNC_VOL_STATS_GENERAL;
1128         } else if (!strcasecmp(ti->data, "help")) {
1129             fprintf(stderr, "fssync-debug stats subcommands:\n");
1130             fprintf(stderr, "\tpkg\tgeneral volume package stats\n");
1131             fprintf(stderr, "\tvicep\tvice partition stats\n");
1132             fprintf(stderr, "\thash\tvolume hash chain stats\n");
1133 #ifdef AFS_DEMAND_ATTACH_FS
1134             fprintf(stderr, "\thdr\tvolume header cache stats\n");
1135             fprintf(stderr, "\tvlru\tvlru generation stats\n");
1136 #endif
1137             exit(0);
1138         } else {
1139             fprintf(stderr, "invalid stats subcommand");
1140             exit(1);
1141         }
1142     } else {
1143         command = FSYNC_VOL_STATS_GENERAL;
1144     }
1145
1146     if ((ti = as->parms[CUSTOM_PARMS_OFFSET+1].items)) {        /* -arg1 */
1147         switch (command) {
1148         case FSYNC_VOL_STATS_VICEP:
1149             strlcpy(scom.args.partName, ti->data, sizeof(state.vop->partName));
1150             break;
1151         case FSYNC_VOL_STATS_HASH:
1152             scom.args.hash_bucket = atoi(ti->data);
1153             break;
1154         case FSYNC_VOL_STATS_VLRU:
1155             scom.args.vlru_generation = atoi(ti->data);
1156             break;
1157         default:
1158             fprintf(stderr, "unrecognized arguments\n");
1159             exit(1);
1160         }
1161     } else {
1162         switch (command) {
1163         case FSYNC_VOL_STATS_VICEP:
1164         case FSYNC_VOL_STATS_HASH:
1165         case FSYNC_VOL_STATS_VLRU:
1166             fprintf(stderr, "this subcommand requires more parameters\n");
1167             exit(1);
1168         }
1169     }
1170
1171     common_prolog(as, &state);
1172
1173     fprintf(stderr, "calling FSYNC_askfs with command code %d (%s)\n", 
1174             command, command_code_to_string(command));
1175
1176     code = FSYNC_StatsOp(&scom, command, FSYNC_WHATEVER, &res);
1177
1178     switch (code) {
1179     case SYNC_OK:
1180     case SYNC_DENIED:
1181         break;
1182     default:
1183         fprintf(stderr, "possible sync protocol error. return code was %d\n", code);
1184     }
1185
1186     fprintf(stderr, "FSYNC_VolOp returned %d (%s)\n", code, response_code_to_string(code));
1187     fprintf(stderr, "protocol response code was %d (%s)\n", 
1188             res.hdr.response, response_code_to_string(res.hdr.response));
1189     fprintf(stderr, "protocol reason code was %d (%s)\n", 
1190             res.hdr.reason, reason_code_to_string(res.hdr.reason));
1191
1192     VDisconnectFS();
1193
1194     if (res.hdr.response == SYNC_OK) {
1195         switch (command) {
1196         case FSYNC_VOL_STATS_GENERAL:
1197             print_vol_stats_general(sres.vol_stats);
1198             break;
1199         case FSYNC_VOL_STATS_VICEP:
1200             print_vol_stats_viceP(sres.vicep_stats);
1201             break;
1202         case FSYNC_VOL_STATS_HASH:
1203             print_vol_stats_hash(sres.hash_stats);
1204             break;
1205 #ifdef AFS_DEMAND_ATTACH_FS
1206         case FSYNC_VOL_STATS_HDR:
1207             print_vol_stats_hdr(sres.hdr_stats);
1208             break;
1209 #endif /* AFS_DEMAND_ATTACH_FS */
1210         }
1211     }
1212
1213     return 0;
1214 }
1215
1216 static void
1217 print_vol_stats_general(VolPkgStats * stats)
1218 {
1219     int i;
1220     afs_uint32 hi, lo;
1221
1222     printf("VolPkgStats = {\n");
1223 #ifdef AFS_DEMAND_ATTACH_FS
1224     for (i = 0; i < VOL_STATE_COUNT; i++) {
1225         printf("\tvol_state_count[%s] = %d\n", 
1226                vol_state_to_string(i),
1227                stats->state_levels[i]);
1228     }
1229
1230     SplitInt64(stats->hash_looks, hi, lo);
1231     printf("\thash_looks = {\n");
1232     printf("\t\thi = %u\n", hi);
1233     printf("\t\tlo = %u\n", lo);
1234     printf("\t}\n");
1235
1236     SplitInt64(stats->hash_reorders, hi, lo);
1237     printf("\thash_reorders = {\n");
1238     printf("\t\thi = %u\n", hi);
1239     printf("\t\tlo = %u\n", lo);
1240     printf("\t}\n");
1241
1242     SplitInt64(stats->salvages, hi, lo);
1243     printf("\tsalvages = {\n");
1244     printf("\t\thi = %u\n", hi);
1245     printf("\t\tlo = %u\n", lo);
1246     printf("\t}\n");
1247
1248     SplitInt64(stats->vol_ops, hi, lo);
1249     printf("\tvol_ops = {\n");
1250     printf("\t\thi = %u\n", hi);
1251     printf("\t\tlo = %u\n", lo);
1252     printf("\t}\n");
1253 #endif
1254     SplitInt64(stats->hdr_loads, hi, lo);
1255     printf("\thdr_loads = {\n");
1256     printf("\t\thi = %u\n", hi);
1257     printf("\t\tlo = %u\n", lo);
1258     printf("\t}\n");
1259
1260     SplitInt64(stats->hdr_gets, hi, lo);
1261     printf("\thdr_gets = {\n");
1262     printf("\t\thi = %u\n", hi);
1263     printf("\t\tlo = %u\n", lo);
1264     printf("\t}\n");
1265
1266     SplitInt64(stats->attaches, hi, lo);
1267     printf("\tattaches = {\n");
1268     printf("\t\thi = %u\n", hi);
1269     printf("\t\tlo = %u\n", lo);
1270     printf("\t}\n");
1271
1272     SplitInt64(stats->soft_detaches, hi, lo);
1273     printf("\tsoft_detaches = {\n");
1274     printf("\t\thi = %u\n", hi);
1275     printf("\t\tlo = %u\n", lo);
1276     printf("\t}\n");
1277
1278     printf("\thdr_cache_size = %d\n", stats->hdr_cache_size);
1279             
1280     printf("}\n");
1281 }
1282
1283 static void
1284 print_vol_stats_viceP(struct DiskPartitionStats64 * stats)
1285 {
1286     printf("DiskPartitionStats64 = {\n");
1287     printf("\tfree = %lld\n", stats->free);
1288     printf("\tminFree = %lld\n", stats->minFree);
1289     printf("\ttotalUsable = %lld\n", stats->totalUsable);
1290     printf("\tf_files = %lld\n", stats->f_files);
1291 #ifdef AFS_DEMAND_ATTACH_FS
1292     printf("\tvol_list_len = %d\n", stats->vol_list_len);
1293 #endif
1294     printf("}\n");
1295 }
1296
1297 static void
1298 print_vol_stats_hash(struct VolumeHashChainStats * stats)
1299 {
1300     afs_uint32 hi, lo;
1301
1302     printf("DiskPartitionStats = {\n");
1303     printf("\ttable_size = %d\n", stats->table_size);
1304     printf("\tchain_len = %d\n", stats->chain_len);
1305
1306 #ifdef AFS_DEMAND_ATTACH_FS
1307     printf("\tchain_cacheCheck = %d\n", stats->chain_cacheCheck);
1308     printf("\tchain_busy = %d\n", stats->chain_busy);
1309
1310     SplitInt64(stats->chain_looks, hi, lo);
1311     printf("\tchain_looks = {\n");
1312     printf("\t\thi = %u\n", hi);
1313     printf("\t\tlo = %u\n", lo);
1314     printf("\t}\n");
1315
1316     SplitInt64(stats->chain_gets, hi, lo);
1317     printf("\tchain_gets = {\n");
1318     printf("\t\thi = %u\n", hi);
1319     printf("\t\tlo = %u\n", lo);
1320     printf("\t}\n");
1321
1322     SplitInt64(stats->chain_reorders, hi, lo);
1323     printf("\tchain_reorders = {\n");
1324     printf("\t\thi = %u\n", hi);
1325     printf("\t\tlo = %u\n", lo);
1326     printf("\t}\n");
1327 #endif /* AFS_DEMAND_ATTACH_FS */
1328
1329     printf("}\n");
1330 }
1331
1332
1333 #ifdef AFS_DEMAND_ATTACH_FS
1334 static void
1335 print_vol_stats_hdr(struct volume_hdr_LRU_stats * stats)
1336 {
1337     printf("volume_hdr_LRU_stats = {\n");
1338     printf("\tfree = %d\n", stats->free);
1339     printf("\tused = %d\n", stats->used);
1340     printf("\tattached = %d\n", stats->attached);
1341     printf("}\n");
1342 }
1343 #endif /* AFS_DEMAND_ATTACH_FS */
1344