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