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