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