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