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