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