Switch to jhash for VNODE_HASH
[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("\tshuttingDown    = %d\n", v.shuttingDown);
710         printf("\tgoingOffline    = %d\n", v.goingOffline);
711         printf("\tcacheCheck      = %u\n", v.cacheCheck);
712         printf("\tnUsers          = %d\n", v.nUsers);
713         printf("\tneedsPutBack    = %d\n", v.needsPutBack);
714         printf("\tspecialStatus   = %d\n", v.specialStatus);
715         printf("\tupdateTime      = %u\n", v.updateTime);
716
717         printf("\tvnodeIndex[vSmall] = {\n");
718         printf("\t\thandle       = %p\n", v.vnodeIndex[vSmall].handle);
719         printf("\t\tbitmap       = %p\n", v.vnodeIndex[vSmall].bitmap);
720         printf("\t\tbitmapSize   = %u\n", v.vnodeIndex[vSmall].bitmapSize);
721         printf("\t\tbitmapOffset = %u\n", v.vnodeIndex[vSmall].bitmapOffset);
722         printf("\t}\n");
723         printf("\tvnodeIndex[vLarge] = {\n");
724         printf("\t\thandle       = %p\n", v.vnodeIndex[vLarge].handle);
725         printf("\t\tbitmap       = %p\n", v.vnodeIndex[vLarge].bitmap);
726         printf("\t\tbitmapSize   = %u\n", v.vnodeIndex[vLarge].bitmapSize);
727         printf("\t\tbitmapOffset = %u\n", v.vnodeIndex[vLarge].bitmapOffset);
728         printf("\t}\n");
729 #ifdef AFS_DEMAND_ATTACH_FS
730         if (res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS) {
731             printf("\tupdateTime      = %u\n", v.updateTime);
732             printf("\tattach_state    = %s\n", vol_state_to_string(v.attach_state));
733             printf("\tattach_flags    = %s\n", vol_flags_to_string(v.attach_flags));
734             printf("\tnWaiters        = %d\n", v.nWaiters);
735             printf("\tchainCacheCheck = %d\n", v.chainCacheCheck);
736
737             /* online salvage structure */
738             printf("\tsalvage = {\n");
739             printf("\t\tprio      = %u\n", v.salvage.prio);
740             printf("\t\treason    = %d\n", v.salvage.reason);
741             printf("\t\trequested = %d\n", v.salvage.requested);
742             printf("\t\tscheduled = %d\n", v.salvage.scheduled);
743             printf("\t}\n");
744
745             /* statistics structure */
746             printf("\tstats = {\n");
747
748             printf("\t\thash_lookups = %"AFS_INT64_FMT"\n",
749                    v.stats.hash_lookups);
750             printf("\t\thash_short_circuits = %"AFS_INT64_FMT"\n",
751                    v.stats.hash_short_circuits);
752             printf("\t\thdr_loads = %"AFS_INT64_FMT"\n",
753                    v.stats.hdr_loads);
754             printf("\t\thdr_gets = %"AFS_INT64_FMT"\n",
755                    v.stats.hdr_gets);
756             printf("\t\t}\n");
757
758             printf("\t\tattaches         = %u\n", v.stats.attaches);
759             printf("\t\tsoft_detaches    = %u\n", v.stats.soft_detaches);
760             printf("\t\tsalvages         = %u\n", v.stats.salvages);
761             printf("\t\tvol_ops          = %u\n", v.stats.vol_ops);
762
763             printf("\t\tlast_attach      = %u\n", v.stats.last_attach);
764             printf("\t\tlast_get         = %u\n", v.stats.last_get);
765             printf("\t\tlast_promote     = %u\n", v.stats.last_promote);
766             printf("\t\tlast_hdr_get     = %u\n", v.stats.last_hdr_get);
767             printf("\t\tlast_hdr_load    = %u\n", v.stats.last_hdr_load);
768             printf("\t\tlast_salvage     = %u\n", v.stats.last_salvage);
769             printf("\t\tlast_salvage_req = %u\n", v.stats.last_salvage_req);
770             printf("\t\tlast_vol_op      = %u\n", v.stats.last_vol_op);
771             printf("\t}\n");
772
773             /* VLRU state */
774             printf("\tvlru = {\n");
775             printf("\t\tidx = %d (%s)\n",
776                    v.vlru.idx, vlru_idx_to_string(v.vlru.idx));
777             printf("\t}\n");
778
779             /* volume op state */
780             printf("\tpending_vol_op  = %p\n", v.pending_vol_op);
781         }
782 #else /* !AFS_DEMAND_ATTACH_FS */
783         if (res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS) {
784             printf("*** server asserted demand attach extensions. this fssync-debug\n"
785                    "*** is not built to recognize those extensions. please use an\n"
786                    "*** fssync-debug with demand attach if you need to dump dafs\n"
787                    "*** extended state.\n");
788         }
789 #endif /* !AFS_DEMAND_ATTACH_FS */
790         printf("}\n");
791     }
792
793     return 0;
794 }
795
796 static int
797 VolHdrQuery(struct cmd_syndesc * as, void * rock)
798 {
799     struct fssync_state state;
800     SYNC_PROTO_BUF_DECL(res_buf);
801     SYNC_response res;
802     VolumeDiskData v;
803     int i;
804
805     res.hdr.response_len = sizeof(res.hdr);
806     res.payload.buf = res_buf;
807     res.payload.len = SYNC_PROTO_MAX_LEN;
808
809     common_prolog(as, &state);
810     common_volop_prolog(as, &state);
811
812     do_volop(&state, FSYNC_VOL_QUERY_HDR, &res);
813
814     if (res.hdr.response == SYNC_OK) {
815         memcpy(&v, res.payload.buf, sizeof(VolumeDiskData));
816
817         printf("VolumeDiskData = {\n");
818         printf("\tstamp = {\n");
819         printf("\t\tmagic   = 0x%x\n", v.stamp.magic);
820         printf("\t\tversion = %u\n", v.stamp.version);
821         printf("\t}\n");
822
823         printf("\tid               = %" AFS_VOLID_FMT "\n", afs_printable_VolumeId_lu(v.id));
824         printf("\tname             = '%s'\n", v.name);
825         if (v.inUse != 0) {
826             printf("\tinUse            = %d (%s)\n", v.inUse, VPTypeToString(v.inUse));
827         } else {
828             printf("\tinUse            = %d (no)\n", v.inUse);
829         }
830         printf("\tinService        = %d\n", v.inService);
831         printf("\tblessed          = %d\n", v.blessed);
832         printf("\tneedsSalvaged    = %d\n", v.needsSalvaged);
833         printf("\tuniquifier       = %u\n", v.uniquifier);
834         printf("\ttype             = %d\n", v.type);
835         printf("\tparentId         = %" AFS_VOLID_FMT "\n", afs_printable_VolumeId_lu(v.parentId));
836         printf("\tcloneId          = %" AFS_VOLID_FMT "\n", afs_printable_VolumeId_lu(v.cloneId));
837         printf("\tbackupId         = %" AFS_VOLID_FMT "\n", afs_printable_VolumeId_lu(v.backupId));
838         printf("\trestoredFromId   = %" AFS_VOLID_FMT "\n", afs_printable_VolumeId_lu(v.restoredFromId));
839         printf("\tneedsCallback    = %d\n", v.needsCallback);
840         printf("\tdestroyMe        = %d\n", v.destroyMe);
841         printf("\tdontSalvage      = %d\n", v.dontSalvage);
842         printf("\tmaxquota         = %d\n", v.maxquota);
843         printf("\tminquota         = %d\n", v.minquota);
844         printf("\tmaxfiles         = %d\n", v.maxfiles);
845         printf("\taccountNumber    = %u\n", v.accountNumber);
846         printf("\towner            = %u\n", v.owner);
847         printf("\tfilecount        = %d\n", v.filecount);
848         printf("\tdiskused         = %d\n", v.diskused);
849         printf("\tdayUse           = %d\n", v.dayUse);
850         for (i = 0; i < 7; i++) {
851             printf("\tweekUse[%d]       = %d\n", i, v.weekUse[i]);
852         }
853         printf("\tdayUseDate       = %u\n", v.dayUseDate);
854         printf("\tcreationDate     = %u\n", v.creationDate);
855         printf("\taccessDate       = %u\n", v.accessDate);
856         printf("\tupdateDate       = %u\n", v.updateDate);
857         printf("\texpirationDate   = %u\n", v.expirationDate);
858         printf("\tbackupDate       = %u\n", v.backupDate);
859         printf("\tcopyDate         = %u\n", v.copyDate);
860         printf("\tstat_initialized = %d\n", v.stat_initialized);
861         printf("}\n");
862     }
863
864     return 0;
865 }
866
867 static int
868 VolOpQuery(struct cmd_syndesc * as, void * rock)
869 {
870     struct fssync_state state;
871     SYNC_PROTO_BUF_DECL(res_buf);
872     SYNC_response res;
873     FSSYNC_VolOp_info vop;
874
875     res.hdr.response_len = sizeof(res.hdr);
876     res.payload.buf = res_buf;
877     res.payload.len = SYNC_PROTO_MAX_LEN;
878
879     common_prolog(as, &state);
880     common_volop_prolog(as, &state);
881
882     do_volop(&state, FSYNC_VOL_QUERY_VOP, &res);
883
884     if (!(res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS)) {
885         printf("*** file server not compiled with demand attach extensions.\n");
886         printf("*** pending volume operation metadata not available.\n");
887     }
888
889     if (res.hdr.response == SYNC_OK) {
890         memcpy(&vop, res.payload.buf, sizeof(FSSYNC_VolOp_info));
891
892         printf("pending_vol_op = {\n");
893
894         printf("\tcom = {\n");
895         printf("\t\tproto_version  = %u\n", vop.com.proto_version);
896         printf("\t\tpkt_seq        = %u\n", vop.com.pkt_seq);
897         printf("\t\tcom_seq        = %u\n", vop.com.com_seq);
898         printf("\t\tprogramType    = %d (%s)\n",
899                vop.com.programType, VPTypeToString(vop.com.programType));
900         printf("\t\tpid            = %d\n", vop.com.pid);
901         printf("\t\ttid            = %d\n", vop.com.tid);
902         printf("\t\tcommand        = %d (%s)\n",
903                vop.com.command, FSYNC_com2string(vop.com.command));
904         printf("\t\treason         = %d (%s)\n",
905                vop.com.reason, FSYNC_reason2string(vop.com.reason));
906         printf("\t\tcommand_len    = %u\n", vop.com.command_len);
907         printf("\t\tflags          = 0x%lux\n", afs_printable_uint32_lu(vop.com.flags));
908         printf("\t}\n");
909
910         printf("\tvop = {\n");
911         printf("\t\tvolume         = %" AFS_VOLID_FMT "\n", afs_printable_VolumeId_lu(vop.vop.volume));
912         if (strnlen(vop.vop.partName, sizeof(vop.vop.partName)) <
913             sizeof(vop.vop.partName)) {
914             printf("\t\tpartName       = '%s'\n", vop.vop.partName);
915         } else {
916             printf("\t\tpartName       = (illegal string)\n");
917         }
918         printf("\t}\n");
919
920         printf("}\n");
921     }
922
923     return 0;
924 }
925
926 static int
927 vn_prolog(struct cmd_syndesc * as, struct fssync_state * state)
928 {
929     struct cmd_item *ti;
930
931     state->vop = (struct volop_state *) calloc(1, sizeof(struct volop_state));
932     osi_Assert(state->vop != NULL);
933
934     if ((ti = as->parms[CUSTOM_PARMS_OFFSET].items)) {  /* -volumeid */
935         state->vop->volume = atoi(ti->data);
936     } else {
937         fprintf(stderr, "required argument -volumeid not given\n");
938     }
939
940     if ((ti = as->parms[CUSTOM_PARMS_OFFSET+1].items)) {        /* -vnodeid */
941         state->vop->vnode = atoi(ti->data);
942     } else {
943         fprintf(stderr, "required argument -vnodeid not given\n");
944     }
945
946     if ((ti = as->parms[CUSTOM_PARMS_OFFSET+2].items)) {        /* -unique */
947         state->vop->unique = atoi(ti->data);
948     } else {
949         state->vop->unique = 0;
950     }
951
952     if ((ti = as->parms[COMMON_VOLOP_PARMS_OFFSET+3].items)) {  /* -partition */
953         strlcpy(state->vop->partName, ti->data, sizeof(state->vop->partName));
954     } else {
955         memset(state->vop->partName, 0, sizeof(state->vop->partName));
956     }
957
958     return 0;
959 }
960
961 static int
962 do_vnqry(struct fssync_state * state, SYNC_response * res)
963 {
964     afs_int32 code;
965     int command = FSYNC_VOL_QUERY_VNODE;
966     FSSYNC_VnQry_hdr qry;
967
968     qry.volume = state->vop->volume;
969     qry.vnode = state->vop->vnode;
970     qry.unique = state->vop->unique;
971     qry.spare = 0;
972     strlcpy(qry.partName, state->vop->partName, sizeof(qry.partName));
973
974     fprintf(stderr, "calling FSYNC_GenericOp with command code %d (%s)\n",
975             command, FSYNC_com2string(command));
976
977     code = FSYNC_GenericOp(&qry, sizeof(qry), command, FSYNC_OPERATOR, res);
978
979     debug_response(code, res);
980
981     VDisconnectFS();
982
983     return 0;
984 }
985
986 static int
987 VnQuery(struct cmd_syndesc * as, void * rock)
988 {
989     struct fssync_state state;
990     SYNC_PROTO_BUF_DECL(res_buf);
991     SYNC_response res;
992     Vnode v;
993
994     dafs_prolog();
995
996     res.hdr.response_len = sizeof(res.hdr);
997     res.payload.buf = res_buf;
998     res.payload.len = SYNC_PROTO_MAX_LEN;
999
1000     common_prolog(as, &state);
1001     vn_prolog(as, &state);
1002
1003     do_vnqry(&state, &res);
1004
1005     if (res.hdr.response == SYNC_OK) {
1006         memcpy(&v, res.payload.buf, sizeof(Vnode));
1007
1008         printf("vnode = {\n");
1009
1010         printf("\tvid_hash = {\n");
1011         printf("\t\tnext = %p\n", v.vid_hash.next);
1012         printf("\t\tprev = %p\n", v.vid_hash.prev);
1013         printf("\t}\n");
1014
1015         printf("\thashNext        = %p\n", v.hashNext);
1016         printf("\tlruNext         = %p\n", v.lruNext);
1017         printf("\tlruPrev         = %p\n", v.lruPrev);
1018         printf("\thashIndex       = %hu\n", v.hashIndex);
1019         printf("\tchanged_newTime = %u\n", (unsigned int) v.changed_newTime);
1020         printf("\tchanged_oldTime = %u\n", (unsigned int) v.changed_oldTime);
1021         printf("\tdelete          = %u\n", (unsigned int) v.delete);
1022         printf("\tvnodeNumber     = %u\n", v.vnodeNumber);
1023         printf("\tvolumePtr       = %p\n", v.volumePtr);
1024         printf("\tnUsers          = %u\n", v.nUsers);
1025         printf("\tcacheCheck      = %u\n", v.cacheCheck);
1026
1027 #ifdef AFS_DEMAND_ATTACH_FS
1028         if (!(res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS)) {
1029             printf("*** this fssync-debug is built to expect demand attach extensions.\n"
1030                    "*** server asserted that is was not compiled with demand attach.\n"
1031                    "*** please use an fssync-debug without demand attach to match your\n"
1032                    "*** server.\n");
1033             goto done;
1034         }
1035
1036         printf("\tnReaders        = %u\n", v.nReaders);
1037         printf("\tvn_state_flags  = %s\n", vn_flags_to_string(v.vn_state_flags));
1038         printf("\tvn_state        = %s\n", vn_state_to_string(v.vn_state));
1039 #else
1040         if (res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS) {
1041             printf("*** server asserted demand attach extensions. this fssync-debug is not\n"
1042                    "*** built to recognize those extensions. please use an fssync-debug\n"
1043                    "*** with demand attach to match your server.\n");
1044             goto done;
1045         }
1046 #endif /* !AFS_DEMAND_ATTACH_FS */
1047
1048         printf("\tvcp             = %p\n", v.vcp);
1049         printf("\thandle          = %p\n", v.handle);
1050
1051         printf("\tdisk = {\n");
1052         printf("\t\ttype              = %u\n", v.disk.type);
1053         printf("\t\tcloned            = %u\n", v.disk.cloned);
1054         printf("\t\tmodeBits          = %u\n", v.disk.modeBits);
1055         printf("\t\tlinkCount         = %d\n", v.disk.linkCount);
1056         printf("\t\tlength            = %u\n", v.disk.length);
1057         printf("\t\tuniquifier        = %u\n", v.disk.uniquifier);
1058         printf("\t\tdataVersion       = %u\n", v.disk.dataVersion);
1059         printf("\t\tvn_ino_lo         = %u\n", v.disk.vn_ino_lo);
1060         printf("\t\tunixModifyTime    = %u\n", v.disk.unixModifyTime);
1061         printf("\t\tauthor            = %u\n", v.disk.author);
1062         printf("\t\towner             = %u\n", v.disk.owner);
1063         printf("\t\tparent            = %u\n", v.disk.parent);
1064         printf("\t\tvnodeMagic        = %u\n", v.disk.vnodeMagic);
1065
1066         printf("\t\tlock = {\n");
1067         printf("\t\t\tlockCount   = %d\n", v.disk.lock.lockCount);
1068         printf("\t\t\tlockTime    = %d\n", v.disk.lock.lockTime);
1069         printf("\t\t}\n");
1070
1071         printf("\t\tserverModifyTime  = %u\n", v.disk.serverModifyTime);
1072         printf("\t\tgroup             = %d\n", v.disk.group);
1073         printf("\t\tvn_ino_hi         = %d\n", v.disk.vn_ino_hi);
1074         printf("\t\tvn_length_hi      = %u\n", v.disk.vn_length_hi);
1075         printf("\t}\n");
1076
1077         printf("}\n");
1078     }
1079
1080  done:
1081     return 0;
1082 }
1083
1084
1085 static int
1086 StatsQuery(struct cmd_syndesc * as, void * rock)
1087 {
1088     afs_int32 code;
1089     int command;
1090     struct cmd_item *ti;
1091     struct fssync_state state;
1092     SYNC_PROTO_BUF_DECL(res_buf);
1093     SYNC_response res;
1094     FSSYNC_StatsOp_hdr scom;
1095
1096     res.hdr.response_len = sizeof(res.hdr);
1097     res.payload.buf = res_buf;
1098     res.payload.len = SYNC_PROTO_MAX_LEN;
1099
1100     if ((ti = as->parms[CUSTOM_PARMS_OFFSET].items)) {  /* -subcommand */
1101         if (!strcasecmp(ti->data, "vicep")) {
1102             command = FSYNC_VOL_STATS_VICEP;
1103         } else if (!strcasecmp(ti->data, "hash")) {
1104             command = FSYNC_VOL_STATS_HASH;
1105 #ifdef AFS_DEMAND_ATTACH_FS
1106         } else if (!strcasecmp(ti->data, "hdr")) {
1107             command = FSYNC_VOL_STATS_HDR;
1108         } else if (!strcasecmp(ti->data, "vlru")) {
1109             command = FSYNC_VOL_STATS_VLRU;
1110 #endif
1111         } else if (!strcasecmp(ti->data, "pkg")) {
1112             command = FSYNC_VOL_STATS_GENERAL;
1113         } else if (!strcasecmp(ti->data, "help")) {
1114             fprintf(stderr, "fssync-debug stats subcommands:\n");
1115             fprintf(stderr, "\tpkg\tgeneral volume package stats\n");
1116             fprintf(stderr, "\tvicep\tvice partition stats\n");
1117             fprintf(stderr, "\thash\tvolume hash chain stats\n");
1118 #ifdef AFS_DEMAND_ATTACH_FS
1119             fprintf(stderr, "\thdr\tvolume header cache stats\n");
1120             fprintf(stderr, "\tvlru\tvlru generation stats\n");
1121 #endif
1122             exit(0);
1123         } else {
1124             fprintf(stderr, "invalid stats subcommand");
1125             exit(1);
1126         }
1127     } else {
1128         command = FSYNC_VOL_STATS_GENERAL;
1129     }
1130
1131     if ((ti = as->parms[CUSTOM_PARMS_OFFSET+1].items)) {        /* -arg1 */
1132         switch (command) {
1133         case FSYNC_VOL_STATS_VICEP:
1134             strlcpy(scom.args.partName, ti->data, sizeof(state.vop->partName));
1135             break;
1136         case FSYNC_VOL_STATS_HASH:
1137             scom.args.hash_bucket = atoi(ti->data);
1138             break;
1139         case FSYNC_VOL_STATS_VLRU:
1140             scom.args.vlru_generation = atoi(ti->data);
1141             break;
1142         default:
1143             fprintf(stderr, "unrecognized arguments\n");
1144             exit(1);
1145         }
1146     } else {
1147         switch (command) {
1148         case FSYNC_VOL_STATS_VICEP:
1149         case FSYNC_VOL_STATS_HASH:
1150         case FSYNC_VOL_STATS_VLRU:
1151             fprintf(stderr, "this subcommand requires more parameters\n");
1152             exit(1);
1153         }
1154     }
1155
1156     common_prolog(as, &state);
1157
1158     fprintf(stderr, "calling FSYNC_askfs with command code %d (%s)\n",
1159             command, FSYNC_com2string(command));
1160
1161     code = FSYNC_StatsOp(&scom, command, FSYNC_WHATEVER, &res);
1162
1163     switch (code) {
1164     case SYNC_OK:
1165     case SYNC_DENIED:
1166         break;
1167     default:
1168         fprintf(stderr, "possible sync protocol error. return code was %d\n", code);
1169     }
1170
1171     fprintf(stderr, "FSYNC_VolOp returned %d (%s)\n", code, SYNC_res2string(code));
1172     fprintf(stderr, "protocol response code was %d (%s)\n",
1173             res.hdr.response, SYNC_res2string(res.hdr.response));
1174     fprintf(stderr, "protocol reason code was %d (%s)\n",
1175             res.hdr.reason, FSYNC_reason2string(res.hdr.reason));
1176
1177     VDisconnectFS();
1178
1179     if (res.hdr.response == SYNC_OK) {
1180         switch (command) {
1181         case FSYNC_VOL_STATS_GENERAL:
1182             {
1183                 struct VolPkgStats vol_stats;
1184                 memcpy(&vol_stats, res_buf, sizeof(vol_stats));
1185                 print_vol_stats_general(&vol_stats);
1186                 break;
1187             }
1188         case FSYNC_VOL_STATS_VICEP:
1189             {
1190                 struct DiskPartitionStats64 vicep_stats;
1191                 memcpy(&vicep_stats, res_buf, sizeof(vicep_stats));
1192                 print_vol_stats_viceP(&vicep_stats);
1193                 break;
1194             }
1195         case FSYNC_VOL_STATS_HASH:
1196             {
1197                 struct VolumeHashChainStats hash_stats;
1198                 memcpy(&hash_stats, res_buf, sizeof(hash_stats));
1199                 print_vol_stats_hash(&hash_stats);
1200                 break;
1201             }
1202 #ifdef AFS_DEMAND_ATTACH_FS
1203         case FSYNC_VOL_STATS_HDR:
1204             {
1205                 struct volume_hdr_LRU_stats hdr_stats;
1206                 memcpy(&hdr_stats, res_buf, sizeof(hdr_stats));
1207                 print_vol_stats_hdr(&hdr_stats);
1208                 break;
1209             }
1210 #endif /* AFS_DEMAND_ATTACH_FS */
1211         }
1212     }
1213
1214     return 0;
1215 }
1216
1217 static void
1218 print_vol_stats_general(VolPkgStats * stats)
1219 {
1220 #ifdef AFS_DEMAND_ATTACH_FS
1221     int i;
1222 #endif
1223
1224     printf("VolPkgStats = {\n");
1225 #ifdef AFS_DEMAND_ATTACH_FS
1226     for (i = 0; i < VOL_STATE_COUNT; i++) {
1227         printf("\tvol_state_count[%s] = %d\n",
1228                vol_state_to_string(i),
1229                stats->state_levels[i]);
1230     }
1231
1232     printf("\thash_looks = %"AFS_INT64_FMT"\n",
1233            stats->hash_looks);
1234     printf("\thash_reorders = %"AFS_INT64_FMT"\n",
1235            stats->hash_reorders);
1236     printf("\tsalvages = %"AFS_INT64_FMT"\n",
1237            stats->salvages);
1238     printf("\tvol_ops = %"AFS_INT64_FMT"\n",
1239            stats->vol_ops);
1240 #endif
1241     printf("\thdr_loads = %"AFS_INT64_FMT"\n",
1242            stats->hdr_loads);
1243     printf("\thdr_gets = %"AFS_INT64_FMT"\n",
1244            stats->hdr_gets);
1245     printf("\tattaches = %"AFS_INT64_FMT"\n",
1246            stats->attaches);
1247     printf("\tsoft_detaches = %"AFS_INT64_FMT"\n",
1248            stats->soft_detaches);
1249     printf("\thdr_cache_size = %d\n", stats->hdr_cache_size);
1250
1251     printf("}\n");
1252 }
1253
1254 static void
1255 print_vol_stats_viceP(struct DiskPartitionStats64 * stats)
1256 {
1257     printf("DiskPartitionStats64 = {\n");
1258     printf("\tfree = %" AFS_INT64_FMT "\n", stats->free);
1259     printf("\tminFree = %" AFS_INT64_FMT "\n", stats->minFree);
1260     printf("\ttotalUsable = %" AFS_INT64_FMT "\n", stats->totalUsable);
1261     printf("\tf_files = %" AFS_INT64_FMT "\n", stats->f_files);
1262 #ifdef AFS_DEMAND_ATTACH_FS
1263     printf("\tvol_list_len = %d\n", stats->vol_list_len);
1264 #endif
1265     printf("}\n");
1266 }
1267
1268 static void
1269 print_vol_stats_hash(struct VolumeHashChainStats * stats)
1270 {
1271     printf("DiskPartitionStats = {\n");
1272     printf("\ttable_size = %d\n", stats->table_size);
1273     printf("\tchain_len = %d\n", stats->chain_len);
1274
1275 #ifdef AFS_DEMAND_ATTACH_FS
1276     printf("\tchain_cacheCheck = %d\n", stats->chain_cacheCheck);
1277     printf("\tchain_busy = %d\n", stats->chain_busy);
1278
1279     printf("\tchain_looks = %"AFS_INT64_FMT"\n",
1280            stats->chain_looks);
1281     printf("\tchain_gets = %"AFS_INT64_FMT"\n",
1282            stats->chain_gets);
1283     printf("\tchain_reorders = %"AFS_INT64_FMT"\n",
1284            stats->chain_reorders);
1285 #endif /* AFS_DEMAND_ATTACH_FS */
1286
1287     printf("}\n");
1288 }
1289
1290
1291 #ifdef AFS_DEMAND_ATTACH_FS
1292 static void
1293 print_vol_stats_hdr(struct volume_hdr_LRU_stats * stats)
1294 {
1295     printf("volume_hdr_LRU_stats = {\n");
1296     printf("\tfree = %d\n", stats->free);
1297     printf("\tused = %d\n", stats->used);
1298     printf("\tattached = %d\n", stats->attached);
1299     printf("}\n");
1300 }
1301 #endif /* AFS_DEMAND_ATTACH_FS */
1302
1303
1304 /**
1305  * query VGC.
1306  *
1307  * @notes args:
1308  *    - CUSTOM_PARMS_OFFSET+0 is partition string
1309  *    - CUSTOM_PARMS_OFFSET+1 is volume id
1310  *
1311  * @return operation status
1312  *    @retval 0 success
1313  */
1314 static int
1315 VGCQuery(struct cmd_syndesc * as, void * rock)
1316 {
1317     afs_int32 code;
1318     struct fssync_state state;
1319     char * partName;
1320     VolumeId volid;
1321     FSSYNC_VGQry_response_t q_res;
1322     SYNC_response res;
1323     int i;
1324     struct cmd_item *ti;
1325
1326     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+0].items)) {       /* -partition */
1327         return -1;
1328     }
1329     partName = ti->data;
1330
1331     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+1].items)) {       /* -volumeid */
1332         return -1;
1333     }
1334     volid = atoi(ti->data);
1335
1336     common_prolog(as, &state);
1337
1338     fprintf(stderr, "calling FSYNC_VCGQuery\n");
1339
1340     code = FSYNC_VGCQuery(partName, volid, &q_res, &res);
1341
1342     debug_response(code, &res);
1343
1344     if (code == SYNC_OK) {
1345         printf("VG = {\n");
1346         printf("\trw\t=\t%u\n", q_res.rw);
1347         printf("\tchildren\t= (\n");
1348         for (i = 0; i < VOL_VG_MAX_VOLS; i++) {
1349             if (q_res.children[i]) {
1350                 printf("\t\t%u\n", q_res.children[i]);
1351             }
1352         }
1353         printf("\t)\n");
1354     }
1355
1356     VDisconnectFS();
1357
1358     return 0;
1359 }
1360
1361 static int
1362 VGCAdd(struct cmd_syndesc * as, void * rock)
1363 {
1364     afs_int32 code;
1365     struct fssync_state state;
1366     char * partName;
1367     VolumeId parent, child;
1368     struct cmd_item *ti;
1369     SYNC_response res;
1370
1371     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+0].items)) {       /* -partition */
1372         return -1;
1373     }
1374     partName = ti->data;
1375
1376     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+1].items)) {       /* -parent */
1377         return -1;
1378     }
1379     parent = atoi(ti->data);
1380
1381     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+2].items)) {       /* -child */
1382         return -1;
1383     }
1384     child = atoi(ti->data);
1385
1386     common_prolog(as, &state);
1387     fprintf(stderr, "calling FSYNC_VCGAdd\n");
1388     code = FSYNC_VGCAdd(partName, parent, child, state.reason, &res);
1389     debug_response(code, &res);
1390
1391     VDisconnectFS();
1392
1393     return 0;
1394 }
1395
1396 static int
1397 VGCDel(struct cmd_syndesc * as, void * rock)
1398 {
1399     afs_int32 code;
1400     struct fssync_state state;
1401     char * partName;
1402     VolumeId parent, child;
1403     struct cmd_item *ti;
1404     SYNC_response res;
1405
1406     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+0].items)) {       /* -partition */
1407         return -1;
1408     }
1409     partName = ti->data;
1410
1411     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+1].items)) {       /* -parent */
1412         return -1;
1413     }
1414     parent = atoi(ti->data);
1415
1416     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+2].items)) {       /* -child */
1417         return -1;
1418     }
1419     child = atoi(ti->data);
1420
1421     common_prolog(as, &state);
1422     fprintf(stderr, "calling FSYNC_VCGDel\n");
1423     code = FSYNC_VGCDel(partName, parent, child, state.reason, &res);
1424     debug_response(code, &res);
1425
1426     VDisconnectFS();
1427
1428     return 0;
1429 }
1430
1431 static int
1432 VGCScan(struct cmd_syndesc * as, void * rock)
1433 {
1434     afs_int32 code;
1435     struct fssync_state state;
1436     char * partName;
1437     struct cmd_item *ti;
1438
1439     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+0].items)) {       /* -partition */
1440         return -1;
1441     }
1442     partName = ti->data;
1443
1444     common_prolog(as, &state);
1445     fprintf(stderr, "calling FSYNC_VCGScan\n");
1446     code = FSYNC_VGCScan(partName, state.reason);
1447     debug_response(code, NULL);
1448
1449     VDisconnectFS();
1450
1451     return 0;
1452 }
1453
1454 static int
1455 VGCScanAll(struct cmd_syndesc * as, void * rock)
1456 {
1457     afs_int32 code;
1458     struct fssync_state state;
1459
1460     common_prolog(as, &state);
1461     fprintf(stderr, "calling FSYNC_VCGScanAll\n");
1462     code = FSYNC_VGCScan(NULL, state.reason);
1463     debug_response(code, NULL);
1464
1465     VDisconnectFS();
1466
1467     return 0;
1468 }