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