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