volser: Tidy header includes
[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     } else {
337         state->reason = FSYNC_WHATEVER;
338     }
339
340     if ((ti = as->parms[COMMON_PARMS_OFFSET+1].items)) {        /* -programtype */
341         if (!strcmp(ti->data, "fileServer")) {
342             programType = fileServer;
343         } else if (!strcmp(ti->data, "volumeUtility")) {
344             programType = volumeUtility;
345         } else if (!strcmp(ti->data, "salvager")) {
346             programType = salvager;
347         } else if (!strcmp(ti->data, "salvageServer")) {
348             programType = salvageServer;
349         } else if (!strcmp(ti->data, "volumeServer")) {
350             programType = volumeServer;
351         } else if (!strcmp(ti->data, "volumeSalvager")) {
352             programType = volumeSalvager;
353         } else {
354             programType = (ProgramType) atoi(ti->data);
355         }
356     }
357
358     VConnectFS();
359
360     return 0;
361 }
362
363 static int
364 common_volop_prolog(struct cmd_syndesc * as, struct state * state)
365 {
366     struct cmd_item *ti;
367
368     state->vop = (struct volop_state *) calloc(1, sizeof(struct volop_state));
369     osi_Assert(state->vop != NULL);
370
371     if ((ti = as->parms[COMMON_VOLOP_PARMS_OFFSET].items)) {    /* -volumeid */
372         state->vop->volume = atoi(ti->data);
373     } else {
374         fprintf(stderr, "required argument -volumeid not given\n");
375     }
376
377     if ((ti = as->parms[COMMON_VOLOP_PARMS_OFFSET+1].items)) {  /* -partition */
378         strlcpy(state->vop->partName, ti->data, sizeof(state->vop->partName));
379     } else {
380         memset(state->vop->partName, 0, sizeof(state->vop->partName));
381     }
382
383     return 0;
384 }
385
386 static int
387 debug_response(afs_int32 code, SYNC_response * res)
388 {
389     switch (code) {
390     case SYNC_OK:
391     case SYNC_DENIED:
392         break;
393     default:
394         fprintf(stderr, "warning: response code indicates possible protocol error.\n");
395     }
396
397     fprintf(stderr, "FSSYNC service returned %d (%s)\n", code, SYNC_res2string(code));
398
399     if (res) {
400         fprintf(stderr, "protocol header response code was %d (%s)\n",
401                 res->hdr.response, SYNC_res2string(res->hdr.response));
402         fprintf(stderr, "protocol reason code was %d (%s)\n",
403                 res->hdr.reason, FSYNC_reason2string(res->hdr.reason));
404     }
405
406     return 0;
407 }
408
409 static int
410 do_volop(struct state * state, afs_int32 command, SYNC_response * res)
411 {
412     afs_int32 code;
413     SYNC_PROTO_BUF_DECL(res_buf);
414     SYNC_response res_l;
415
416     if (!res) {
417         res = &res_l;
418         res->payload.len = SYNC_PROTO_MAX_LEN;
419         res->payload.buf = res_buf;
420     }
421
422     fprintf(stderr, "calling FSYNC_VolOp with command code %d (%s)\n",
423             command, FSYNC_com2string(command));
424
425     code = FSYNC_VolOp(state->vop->volume,
426                        state->vop->partName,
427                        command,
428                        state->reason,
429                        res);
430
431     debug_response(code, res);
432
433     VDisconnectFS();
434
435     return 0;
436
437 }
438
439
440 #define ENUMTOSTRING(en)  #en
441 #define ENUMCASE(en) \
442     case en: return ENUMTOSTRING(en)
443
444 #define FLAGTOSTRING(fl)  #fl
445 #define FLAGCASE(bitstr, fl, str, count) \
446     do { \
447         if ((bitstr) & (fl)) { \
448             if (count) \
449                 strlcat((str), " | ", sizeof(str)); \
450             strlcat((str), FLAGTOSTRING(fl), sizeof(str)); \
451             (count)++; \
452         } \
453     } while (0)
454
455 static int
456 VolOnline(struct cmd_syndesc * as, void * rock)
457 {
458     struct state state;
459
460     common_prolog(as, &state);
461     common_volop_prolog(as, &state);
462
463     do_volop(&state, FSYNC_VOL_ON, NULL);
464
465     return 0;
466 }
467
468 static int
469 VolOffline(struct cmd_syndesc * as, void * rock)
470 {
471     struct state state;
472
473     common_prolog(as, &state);
474     common_volop_prolog(as, &state);
475
476     do_volop(&state, FSYNC_VOL_OFF, NULL);
477
478     return 0;
479 }
480
481 static int
482 VolMode(struct cmd_syndesc * as, void * rock)
483 {
484     struct state state;
485
486     common_prolog(as, &state);
487     common_volop_prolog(as, &state);
488
489     do_volop(&state, FSYNC_VOL_NEEDVOLUME, NULL);
490
491     return 0;
492 }
493
494 static int
495 VolDetach(struct cmd_syndesc * as, void * rock)
496 {
497     struct state state;
498
499     common_prolog(as, &state);
500     common_volop_prolog(as, &state);
501
502     do_volop(&state, FSYNC_VOL_DONE, NULL);
503
504     return 0;
505 }
506
507 static int
508 VolBreakCBKs(struct cmd_syndesc * as, void * rock)
509 {
510     struct state state;
511
512     common_prolog(as, &state);
513     common_volop_prolog(as, &state);
514
515     do_volop(&state, FSYNC_VOL_BREAKCBKS, NULL);
516
517     return 0;
518 }
519
520 static int
521 VolMove(struct cmd_syndesc * as, void * rock)
522 {
523     struct state state;
524
525     common_prolog(as, &state);
526     common_volop_prolog(as, &state);
527
528     do_volop(&state, FSYNC_VOL_MOVE, NULL);
529
530     return 0;
531 }
532
533 static int
534 VolList(struct cmd_syndesc * as, void * rock)
535 {
536     struct state state;
537
538     common_prolog(as, &state);
539     common_volop_prolog(as, &state);
540
541     do_volop(&state, FSYNC_VOL_LISTVOLUMES, NULL);
542
543     return 0;
544 }
545
546 static int
547 VolLeaveOff(struct cmd_syndesc * as, void * rock)
548 {
549     struct state state;
550
551     common_prolog(as, &state);
552     common_volop_prolog(as, &state);
553
554     do_volop(&state, FSYNC_VOL_LEAVE_OFF, NULL);
555
556     return 0;
557 }
558
559 static int
560 VolForceAttach(struct cmd_syndesc * as, void * rock)
561 {
562     struct state state;
563
564     common_prolog(as, &state);
565     common_volop_prolog(as, &state);
566
567     do_volop(&state, FSYNC_VOL_ATTACH, NULL);
568
569     return 0;
570 }
571
572 static int
573 VolForceError(struct cmd_syndesc * as, void * rock)
574 {
575     struct state state;
576
577     common_prolog(as, &state);
578     common_volop_prolog(as, &state);
579
580     do_volop(&state, FSYNC_VOL_FORCE_ERROR, NULL);
581
582     return 0;
583 }
584
585 #ifdef AFS_DEMAND_ATTACH_FS
586 static char *
587 vol_state_to_string(VolState state)
588 {
589     switch (state) {
590         ENUMCASE(VOL_STATE_UNATTACHED);
591         ENUMCASE(VOL_STATE_PREATTACHED);
592         ENUMCASE(VOL_STATE_ATTACHING);
593         ENUMCASE(VOL_STATE_ATTACHED);
594         ENUMCASE(VOL_STATE_UPDATING);
595         ENUMCASE(VOL_STATE_GET_BITMAP);
596         ENUMCASE(VOL_STATE_HDR_LOADING);
597         ENUMCASE(VOL_STATE_HDR_ATTACHING);
598         ENUMCASE(VOL_STATE_SHUTTING_DOWN);
599         ENUMCASE(VOL_STATE_GOING_OFFLINE);
600         ENUMCASE(VOL_STATE_OFFLINING);
601         ENUMCASE(VOL_STATE_DETACHING);
602         ENUMCASE(VOL_STATE_SALVSYNC_REQ);
603         ENUMCASE(VOL_STATE_SALVAGING);
604         ENUMCASE(VOL_STATE_ERROR);
605         ENUMCASE(VOL_STATE_VNODE_ALLOC);
606         ENUMCASE(VOL_STATE_VNODE_GET);
607         ENUMCASE(VOL_STATE_VNODE_CLOSE);
608         ENUMCASE(VOL_STATE_VNODE_RELEASE);
609         ENUMCASE(VOL_STATE_VLRU_ADD);
610         ENUMCASE(VOL_STATE_DELETED);
611         ENUMCASE(VOL_STATE_SALVAGE_REQ);
612         ENUMCASE(VOL_STATE_FREED);
613     default:
614         return "**UNKNOWN**";
615     }
616 }
617
618 static char *
619 vol_flags_to_string(afs_uint16 flags)
620 {
621     static char str[256];
622     int count = 0;
623     str[0]='\0';
624
625     FLAGCASE(flags, VOL_HDR_ATTACHED, str, count);
626     FLAGCASE(flags, VOL_HDR_LOADED, str, count);
627     FLAGCASE(flags, VOL_HDR_IN_LRU, str, count);
628     FLAGCASE(flags, VOL_IN_HASH, str, count);
629     FLAGCASE(flags, VOL_ON_VBYP_LIST, str, count);
630     FLAGCASE(flags, VOL_IS_BUSY, str, count);
631     FLAGCASE(flags, VOL_ON_VLRU, str, count);
632     FLAGCASE(flags, VOL_HDR_DONTSALV, str, count);
633     FLAGCASE(flags, VOL_LOCKED, str, count);
634
635     return str;
636 }
637
638 static char *
639 vlru_idx_to_string(int idx)
640 {
641     switch (idx) {
642         ENUMCASE(VLRU_QUEUE_NEW);
643         ENUMCASE(VLRU_QUEUE_MID);
644         ENUMCASE(VLRU_QUEUE_OLD);
645         ENUMCASE(VLRU_QUEUE_CANDIDATE);
646         ENUMCASE(VLRU_QUEUE_HELD);
647         ENUMCASE(VLRU_QUEUE_INVALID);
648     default:
649         return "**UNKNOWN**";
650     }
651 }
652
653
654 static char *
655 vn_state_to_string(VnState state)
656 {
657     switch (state) {
658         ENUMCASE(VN_STATE_INVALID);
659         ENUMCASE(VN_STATE_RELEASING);
660         ENUMCASE(VN_STATE_CLOSING);
661         ENUMCASE(VN_STATE_ALLOC);
662         ENUMCASE(VN_STATE_ONLINE);
663         ENUMCASE(VN_STATE_LOAD);
664         ENUMCASE(VN_STATE_EXCLUSIVE);
665         ENUMCASE(VN_STATE_STORE);
666         ENUMCASE(VN_STATE_READ);
667         ENUMCASE(VN_STATE_ERROR);
668     default:
669         return "**UNKNOWN**";
670     }
671 }
672
673 static char *
674 vn_flags_to_string(afs_uint32 flags)
675 {
676     static char str[128];
677     int count = 0;
678     str[0]='\0';
679
680     FLAGCASE(flags, VN_ON_HASH, str, count);
681     FLAGCASE(flags, VN_ON_LRU, str, count);
682     FLAGCASE(flags, VN_ON_VVN, str, count);
683
684     return str;
685 }
686 #endif
687
688 static int
689 VolQuery(struct cmd_syndesc * as, void * rock)
690 {
691     struct state state;
692     SYNC_PROTO_BUF_DECL(res_buf);
693     SYNC_response res;
694     Volume v;
695 #ifdef AFS_DEMAND_ATTACH_FS
696     int hi, lo;
697 #endif
698
699     dafs_prolog();
700
701     res.hdr.response_len = sizeof(res.hdr);
702     res.payload.buf = res_buf;
703     res.payload.len = SYNC_PROTO_MAX_LEN;
704
705     common_prolog(as, &state);
706     common_volop_prolog(as, &state);
707
708     do_volop(&state, FSYNC_VOL_QUERY, &res);
709
710     if (res.hdr.response == SYNC_OK) {
711         memcpy(&v, res.payload.buf, sizeof(Volume));
712
713         printf("volume = {\n");
714         printf("\thashid          = %u\n", v.hashid);
715         printf("\theader          = %p\n", v.header);
716         printf("\tdevice          = %d\n", v.device);
717         printf("\tpartition       = %p\n", v.partition);
718         printf("\tlinkHandle      = %p\n", v.linkHandle);
719         printf("\tnextVnodeUnique = %u\n", v.nextVnodeUnique);
720         printf("\tdiskDataHandle  = %p\n", v.diskDataHandle);
721         printf("\tvnodeHashOffset = %u\n", v.vnodeHashOffset);
722         printf("\tshuttingDown    = %d\n", v.shuttingDown);
723         printf("\tgoingOffline    = %d\n", v.goingOffline);
724         printf("\tcacheCheck      = %u\n", v.cacheCheck);
725         printf("\tnUsers          = %d\n", v.nUsers);
726         printf("\tneedsPutBack    = %d\n", v.needsPutBack);
727         printf("\tspecialStatus   = %d\n", v.specialStatus);
728         printf("\tupdateTime      = %u\n", v.updateTime);
729
730         printf("\tvnodeIndex[vSmall] = {\n");
731         printf("\t\thandle       = %p\n", v.vnodeIndex[vSmall].handle);
732         printf("\t\tbitmap       = %p\n", v.vnodeIndex[vSmall].bitmap);
733         printf("\t\tbitmapSize   = %u\n", v.vnodeIndex[vSmall].bitmapSize);
734         printf("\t\tbitmapOffset = %u\n", v.vnodeIndex[vSmall].bitmapOffset);
735         printf("\t}\n");
736         printf("\tvnodeIndex[vLarge] = {\n");
737         printf("\t\thandle       = %p\n", v.vnodeIndex[vLarge].handle);
738         printf("\t\tbitmap       = %p\n", v.vnodeIndex[vLarge].bitmap);
739         printf("\t\tbitmapSize   = %u\n", v.vnodeIndex[vLarge].bitmapSize);
740         printf("\t\tbitmapOffset = %u\n", v.vnodeIndex[vLarge].bitmapOffset);
741         printf("\t}\n");
742 #ifdef AFS_DEMAND_ATTACH_FS
743         if (res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS) {
744             printf("\tupdateTime      = %u\n", v.updateTime);
745             printf("\tattach_state    = %s\n", vol_state_to_string(v.attach_state));
746             printf("\tattach_flags    = %s\n", vol_flags_to_string(v.attach_flags));
747             printf("\tnWaiters        = %d\n", v.nWaiters);
748             printf("\tchainCacheCheck = %d\n", v.chainCacheCheck);
749
750             /* online salvage structure */
751             printf("\tsalvage = {\n");
752             printf("\t\tprio      = %u\n", v.salvage.prio);
753             printf("\t\treason    = %d\n", v.salvage.reason);
754             printf("\t\trequested = %d\n", v.salvage.requested);
755             printf("\t\tscheduled = %d\n", v.salvage.scheduled);
756             printf("\t}\n");
757
758             /* statistics structure */
759             printf("\tstats = {\n");
760
761             printf("\t\thash_lookups = {\n");
762             SplitInt64(v.stats.hash_lookups,hi,lo);
763             printf("\t\t\thi = %u\n", hi);
764             printf("\t\t\tlo = %u\n", lo);
765             printf("\t\t}\n");
766
767             printf("\t\thash_short_circuits = {\n");
768             SplitInt64(v.stats.hash_short_circuits,hi,lo);
769             printf("\t\t\thi = %u\n", hi);
770             printf("\t\t\tlo = %u\n", lo);
771             printf("\t\t}\n");
772
773             printf("\t\thdr_loads = {\n");
774             SplitInt64(v.stats.hdr_loads,hi,lo);
775             printf("\t\t\thi = %u\n", hi);
776             printf("\t\t\tlo = %u\n", lo);
777             printf("\t\t}\n");
778
779             printf("\t\thdr_gets = {\n");
780             SplitInt64(v.stats.hdr_gets,hi,lo);
781             printf("\t\t\thi = %u\n", hi);
782             printf("\t\t\tlo = %u\n", lo);
783             printf("\t\t}\n");
784
785             printf("\t\tattaches         = %u\n", v.stats.attaches);
786             printf("\t\tsoft_detaches    = %u\n", v.stats.soft_detaches);
787             printf("\t\tsalvages         = %u\n", v.stats.salvages);
788             printf("\t\tvol_ops          = %u\n", v.stats.vol_ops);
789
790             printf("\t\tlast_attach      = %u\n", v.stats.last_attach);
791             printf("\t\tlast_get         = %u\n", v.stats.last_get);
792             printf("\t\tlast_promote     = %u\n", v.stats.last_promote);
793             printf("\t\tlast_hdr_get     = %u\n", v.stats.last_hdr_get);
794             printf("\t\tlast_hdr_load    = %u\n", v.stats.last_hdr_load);
795             printf("\t\tlast_salvage     = %u\n", v.stats.last_salvage);
796             printf("\t\tlast_salvage_req = %u\n", v.stats.last_salvage_req);
797             printf("\t\tlast_vol_op      = %u\n", v.stats.last_vol_op);
798             printf("\t}\n");
799
800             /* VLRU state */
801             printf("\tvlru = {\n");
802             printf("\t\tidx = %d (%s)\n",
803                    v.vlru.idx, vlru_idx_to_string(v.vlru.idx));
804             printf("\t}\n");
805
806             /* volume op state */
807             printf("\tpending_vol_op  = %p\n", v.pending_vol_op);
808         }
809 #else /* !AFS_DEMAND_ATTACH_FS */
810         if (res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS) {
811             printf("*** server asserted demand attach extensions. this fssync-debug\n"
812                    "*** is not built to recognize those extensions. please use an\n"
813                    "*** fssync-debug with demand attach if you need to dump dafs\n"
814                    "*** extended state.\n");
815         }
816 #endif /* !AFS_DEMAND_ATTACH_FS */
817         printf("}\n");
818     }
819
820     return 0;
821 }
822
823 static int
824 VolHdrQuery(struct cmd_syndesc * as, void * rock)
825 {
826     struct state state;
827     SYNC_PROTO_BUF_DECL(res_buf);
828     SYNC_response res;
829     VolumeDiskData v;
830     int i;
831
832     res.hdr.response_len = sizeof(res.hdr);
833     res.payload.buf = res_buf;
834     res.payload.len = SYNC_PROTO_MAX_LEN;
835
836     common_prolog(as, &state);
837     common_volop_prolog(as, &state);
838
839     do_volop(&state, FSYNC_VOL_QUERY_HDR, &res);
840
841     if (res.hdr.response == SYNC_OK) {
842         memcpy(&v, res.payload.buf, sizeof(VolumeDiskData));
843
844         printf("VolumeDiskData = {\n");
845         printf("\tstamp = {\n");
846         printf("\t\tmagic   = 0x%x\n", v.stamp.magic);
847         printf("\t\tversion = %u\n", v.stamp.version);
848         printf("\t}\n");
849
850         printf("\tid               = %u\n", v.id);
851         printf("\tname             = '%s'\n", v.name);
852         if (v.inUse != 0) {
853             printf("\tinUse            = %d (%s)\n", v.inUse, VPTypeToString(v.inUse));
854         } else {
855             printf("\tinUse            = %d (no)\n", v.inUse);
856         }
857         printf("\tinService        = %d\n", v.inService);
858         printf("\tblessed          = %d\n", v.blessed);
859         printf("\tneedsSalvaged    = %d\n", v.needsSalvaged);
860         printf("\tuniquifier       = %u\n", v.uniquifier);
861         printf("\ttype             = %d\n", v.type);
862         printf("\tparentId         = %u\n", v.parentId);
863         printf("\tcloneId          = %u\n", v.cloneId);
864         printf("\tbackupId         = %u\n", v.backupId);
865         printf("\trestoredFromId   = %u\n", v.restoredFromId);
866         printf("\tneedsCallback    = %d\n", v.needsCallback);
867         printf("\tdestroyMe        = %d\n", v.destroyMe);
868         printf("\tdontSalvage      = %d\n", v.dontSalvage);
869         printf("\tmaxquota         = %d\n", v.maxquota);
870         printf("\tminquota         = %d\n", v.minquota);
871         printf("\tmaxfiles         = %d\n", v.maxfiles);
872         printf("\taccountNumber    = %u\n", v.accountNumber);
873         printf("\towner            = %u\n", v.owner);
874         printf("\tfilecount        = %d\n", v.filecount);
875         printf("\tdiskused         = %d\n", v.diskused);
876         printf("\tdayUse           = %d\n", v.dayUse);
877         for (i = 0; i < 7; i++) {
878             printf("\tweekUse[%d]       = %d\n", i, v.weekUse[i]);
879         }
880         printf("\tdayUseDate       = %u\n", v.dayUseDate);
881         printf("\tcreationDate     = %u\n", v.creationDate);
882         printf("\taccessDate       = %u\n", v.accessDate);
883         printf("\tupdateDate       = %u\n", v.updateDate);
884         printf("\texpirationDate   = %u\n", v.expirationDate);
885         printf("\tbackupDate       = %u\n", v.backupDate);
886         printf("\tcopyDate         = %u\n", v.copyDate);
887 #ifdef OPENAFS_VOL_STATS
888         printf("\tstat_initialized = %d\n", v.stat_initialized);
889 #else
890         printf("\tmtd              = '%s'\n", v.motd);
891 #endif
892         printf("}\n");
893     }
894
895     return 0;
896 }
897
898 static int
899 VolOpQuery(struct cmd_syndesc * as, void * rock)
900 {
901     struct state state;
902     SYNC_PROTO_BUF_DECL(res_buf);
903     SYNC_response res;
904     FSSYNC_VolOp_info vop;
905
906     res.hdr.response_len = sizeof(res.hdr);
907     res.payload.buf = res_buf;
908     res.payload.len = SYNC_PROTO_MAX_LEN;
909
910     common_prolog(as, &state);
911     common_volop_prolog(as, &state);
912
913     do_volop(&state, FSYNC_VOL_QUERY_VOP, &res);
914
915     if (!(res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS)) {
916         printf("*** file server not compiled with demand attach extensions.\n");
917         printf("*** pending volume operation metadata not available.\n");
918     }
919
920     if (res.hdr.response == SYNC_OK) {
921         memcpy(&vop, res.payload.buf, sizeof(FSSYNC_VolOp_info));
922
923         printf("pending_vol_op = {\n");
924
925         printf("\tcom = {\n");
926         printf("\t\tproto_version  = %u\n", vop.com.proto_version);
927         printf("\t\tpkt_seq        = %u\n", vop.com.pkt_seq);
928         printf("\t\tcom_seq        = %u\n", vop.com.com_seq);
929         printf("\t\tprogramType    = %d (%s)\n",
930                vop.com.programType, VPTypeToString(vop.com.programType));
931         printf("\t\tpid            = %d\n", vop.com.pid);
932         printf("\t\ttid            = %d\n", vop.com.tid);
933         printf("\t\tcommand        = %d (%s)\n",
934                vop.com.command, FSYNC_com2string(vop.com.command));
935         printf("\t\treason         = %d (%s)\n",
936                vop.com.reason, FSYNC_reason2string(vop.com.reason));
937         printf("\t\tcommand_len    = %u\n", vop.com.command_len);
938         printf("\t\tflags          = 0x%lux\n", afs_printable_uint32_lu(vop.com.flags));
939         printf("\t}\n");
940
941         printf("\tvop = {\n");
942         printf("\t\tvolume         = %u\n", vop.vop.volume);
943         if (strnlen(vop.vop.partName, sizeof(vop.vop.partName)) <
944             sizeof(vop.vop.partName)) {
945             printf("\t\tpartName       = '%s'\n", vop.vop.partName);
946         } else {
947             printf("\t\tpartName       = (illegal string)\n");
948         }
949         printf("\t}\n");
950
951         printf("}\n");
952     }
953
954     return 0;
955 }
956
957 static int
958 vn_prolog(struct cmd_syndesc * as, struct state * state)
959 {
960     struct cmd_item *ti;
961
962     state->vop = (struct volop_state *) calloc(1, sizeof(struct volop_state));
963     osi_Assert(state->vop != NULL);
964
965     if ((ti = as->parms[CUSTOM_PARMS_OFFSET].items)) {  /* -volumeid */
966         state->vop->volume = atoi(ti->data);
967     } else {
968         fprintf(stderr, "required argument -volumeid not given\n");
969     }
970
971     if ((ti = as->parms[CUSTOM_PARMS_OFFSET+1].items)) {        /* -vnodeid */
972         state->vop->vnode = atoi(ti->data);
973     } else {
974         fprintf(stderr, "required argument -vnodeid not given\n");
975     }
976
977     if ((ti = as->parms[CUSTOM_PARMS_OFFSET+2].items)) {        /* -unique */
978         state->vop->unique = atoi(ti->data);
979     } else {
980         state->vop->unique = 0;
981     }
982
983     if ((ti = as->parms[COMMON_VOLOP_PARMS_OFFSET+3].items)) {  /* -partition */
984         strlcpy(state->vop->partName, ti->data, sizeof(state->vop->partName));
985     } else {
986         memset(state->vop->partName, 0, sizeof(state->vop->partName));
987     }
988
989     return 0;
990 }
991
992 static int
993 do_vnqry(struct state * state, SYNC_response * res)
994 {
995     afs_int32 code;
996     int command = FSYNC_VOL_QUERY_VNODE;
997     FSSYNC_VnQry_hdr qry;
998
999     qry.volume = state->vop->volume;
1000     qry.vnode = state->vop->vnode;
1001     qry.unique = state->vop->unique;
1002     qry.spare = 0;
1003     strlcpy(qry.partName, state->vop->partName, sizeof(qry.partName));
1004
1005     fprintf(stderr, "calling FSYNC_GenericOp with command code %d (%s)\n",
1006             command, FSYNC_com2string(command));
1007
1008     code = FSYNC_GenericOp(&qry, sizeof(qry), command, FSYNC_OPERATOR, res);
1009
1010     debug_response(code, res);
1011
1012     VDisconnectFS();
1013
1014     return 0;
1015 }
1016
1017 static int
1018 VnQuery(struct cmd_syndesc * as, void * rock)
1019 {
1020     struct state state;
1021     SYNC_PROTO_BUF_DECL(res_buf);
1022     SYNC_response res;
1023     Vnode v;
1024
1025     dafs_prolog();
1026
1027     res.hdr.response_len = sizeof(res.hdr);
1028     res.payload.buf = res_buf;
1029     res.payload.len = SYNC_PROTO_MAX_LEN;
1030
1031     common_prolog(as, &state);
1032     vn_prolog(as, &state);
1033
1034     do_vnqry(&state, &res);
1035
1036     if (res.hdr.response == SYNC_OK) {
1037         memcpy(&v, res.payload.buf, sizeof(Vnode));
1038
1039         printf("vnode = {\n");
1040
1041         printf("\tvid_hash = {\n");
1042         printf("\t\tnext = %p\n", v.vid_hash.next);
1043         printf("\t\tprev = %p\n", v.vid_hash.prev);
1044         printf("\t}\n");
1045
1046         printf("\thashNext        = %p\n", v.hashNext);
1047         printf("\tlruNext         = %p\n", v.lruNext);
1048         printf("\tlruPrev         = %p\n", v.lruPrev);
1049         printf("\thashIndex       = %hu\n", v.hashIndex);
1050         printf("\tchanged_newTime = %u\n", (unsigned int) v.changed_newTime);
1051         printf("\tchanged_oldTime = %u\n", (unsigned int) v.changed_oldTime);
1052         printf("\tdelete          = %u\n", (unsigned int) v.delete);
1053         printf("\tvnodeNumber     = %u\n", v.vnodeNumber);
1054         printf("\tvolumePtr       = %p\n", v.volumePtr);
1055         printf("\tnUsers          = %u\n", v.nUsers);
1056         printf("\tcacheCheck      = %u\n", v.cacheCheck);
1057
1058 #ifdef AFS_DEMAND_ATTACH_FS
1059         if (!(res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS)) {
1060             printf("*** this fssync-debug is built to expect demand attach extensions.\n"
1061                    "*** server asserted that is was not compiled with demand attach.\n"
1062                    "*** please use an fssync-debug without demand attach to match your\n"
1063                    "*** server.\n");
1064             goto done;
1065         }
1066
1067         printf("\tnReaders        = %u\n", v.nReaders);
1068         printf("\tvn_state_flags  = %s\n", vn_flags_to_string(v.vn_state_flags));
1069         printf("\tvn_state        = %s\n", vn_state_to_string(v.vn_state));
1070 #else
1071         if (res.hdr.flags & SYNC_FLAG_DAFS_EXTENSIONS) {
1072             printf("*** server asserted demand attach extensions. this fssync-debug is not\n"
1073                    "*** built to recognize those extensions. please use an fssync-debug\n"
1074                    "*** with demand attach to match your server.\n");
1075             goto done;
1076         }
1077 #endif /* !AFS_DEMAND_ATTACH_FS */
1078
1079         printf("\tvcp             = %p\n", v.vcp);
1080         printf("\thandle          = %p\n", v.handle);
1081
1082         printf("\tdisk = {\n");
1083         printf("\t\ttype              = %u\n", v.disk.type);
1084         printf("\t\tcloned            = %u\n", v.disk.cloned);
1085         printf("\t\tmodeBits          = %u\n", v.disk.modeBits);
1086         printf("\t\tlinkCount         = %d\n", v.disk.linkCount);
1087         printf("\t\tlength            = %u\n", v.disk.length);
1088         printf("\t\tuniquifier        = %u\n", v.disk.uniquifier);
1089         printf("\t\tdataVersion       = %u\n", v.disk.dataVersion);
1090         printf("\t\tvn_ino_lo         = %u\n", v.disk.vn_ino_lo);
1091         printf("\t\tunixModifyTime    = %u\n", v.disk.unixModifyTime);
1092         printf("\t\tauthor            = %u\n", v.disk.author);
1093         printf("\t\towner             = %u\n", v.disk.owner);
1094         printf("\t\tparent            = %u\n", v.disk.parent);
1095         printf("\t\tvnodeMagic        = %u\n", v.disk.vnodeMagic);
1096
1097         printf("\t\tlock = {\n");
1098         printf("\t\t\tlockCount   = %d\n", v.disk.lock.lockCount);
1099         printf("\t\t\tlockTime    = %d\n", v.disk.lock.lockTime);
1100         printf("\t\t}\n");
1101
1102         printf("\t\tserverModifyTime  = %u\n", v.disk.serverModifyTime);
1103         printf("\t\tgroup             = %d\n", v.disk.group);
1104         printf("\t\tvn_ino_hi         = %d\n", v.disk.vn_ino_hi);
1105         printf("\t\tvn_length_hi      = %u\n", v.disk.vn_length_hi);
1106         printf("\t}\n");
1107
1108         printf("}\n");
1109     }
1110
1111  done:
1112     return 0;
1113 }
1114
1115
1116 static int
1117 StatsQuery(struct cmd_syndesc * as, void * rock)
1118 {
1119     afs_int32 code;
1120     int command;
1121     struct cmd_item *ti;
1122     struct state state;
1123     SYNC_PROTO_BUF_DECL(res_buf);
1124     SYNC_response res;
1125     FSSYNC_StatsOp_hdr scom;
1126
1127     res.hdr.response_len = sizeof(res.hdr);
1128     res.payload.buf = res_buf;
1129     res.payload.len = SYNC_PROTO_MAX_LEN;
1130
1131     if ((ti = as->parms[CUSTOM_PARMS_OFFSET].items)) {  /* -subcommand */
1132         if (!strcasecmp(ti->data, "vicep")) {
1133             command = FSYNC_VOL_STATS_VICEP;
1134         } else if (!strcasecmp(ti->data, "hash")) {
1135             command = FSYNC_VOL_STATS_HASH;
1136 #ifdef AFS_DEMAND_ATTACH_FS
1137         } else if (!strcasecmp(ti->data, "hdr")) {
1138             command = FSYNC_VOL_STATS_HDR;
1139         } else if (!strcasecmp(ti->data, "vlru")) {
1140             command = FSYNC_VOL_STATS_VLRU;
1141 #endif
1142         } else if (!strcasecmp(ti->data, "pkg")) {
1143             command = FSYNC_VOL_STATS_GENERAL;
1144         } else if (!strcasecmp(ti->data, "help")) {
1145             fprintf(stderr, "fssync-debug stats subcommands:\n");
1146             fprintf(stderr, "\tpkg\tgeneral volume package stats\n");
1147             fprintf(stderr, "\tvicep\tvice partition stats\n");
1148             fprintf(stderr, "\thash\tvolume hash chain stats\n");
1149 #ifdef AFS_DEMAND_ATTACH_FS
1150             fprintf(stderr, "\thdr\tvolume header cache stats\n");
1151             fprintf(stderr, "\tvlru\tvlru generation stats\n");
1152 #endif
1153             exit(0);
1154         } else {
1155             fprintf(stderr, "invalid stats subcommand");
1156             exit(1);
1157         }
1158     } else {
1159         command = FSYNC_VOL_STATS_GENERAL;
1160     }
1161
1162     if ((ti = as->parms[CUSTOM_PARMS_OFFSET+1].items)) {        /* -arg1 */
1163         switch (command) {
1164         case FSYNC_VOL_STATS_VICEP:
1165             strlcpy(scom.args.partName, ti->data, sizeof(state.vop->partName));
1166             break;
1167         case FSYNC_VOL_STATS_HASH:
1168             scom.args.hash_bucket = atoi(ti->data);
1169             break;
1170         case FSYNC_VOL_STATS_VLRU:
1171             scom.args.vlru_generation = atoi(ti->data);
1172             break;
1173         default:
1174             fprintf(stderr, "unrecognized arguments\n");
1175             exit(1);
1176         }
1177     } else {
1178         switch (command) {
1179         case FSYNC_VOL_STATS_VICEP:
1180         case FSYNC_VOL_STATS_HASH:
1181         case FSYNC_VOL_STATS_VLRU:
1182             fprintf(stderr, "this subcommand requires more parameters\n");
1183             exit(1);
1184         }
1185     }
1186
1187     common_prolog(as, &state);
1188
1189     fprintf(stderr, "calling FSYNC_askfs with command code %d (%s)\n",
1190             command, FSYNC_com2string(command));
1191
1192     code = FSYNC_StatsOp(&scom, command, FSYNC_WHATEVER, &res);
1193
1194     switch (code) {
1195     case SYNC_OK:
1196     case SYNC_DENIED:
1197         break;
1198     default:
1199         fprintf(stderr, "possible sync protocol error. return code was %d\n", code);
1200     }
1201
1202     fprintf(stderr, "FSYNC_VolOp returned %d (%s)\n", code, SYNC_res2string(code));
1203     fprintf(stderr, "protocol response code was %d (%s)\n",
1204             res.hdr.response, SYNC_res2string(res.hdr.response));
1205     fprintf(stderr, "protocol reason code was %d (%s)\n",
1206             res.hdr.reason, FSYNC_reason2string(res.hdr.reason));
1207
1208     VDisconnectFS();
1209
1210     if (res.hdr.response == SYNC_OK) {
1211         switch (command) {
1212         case FSYNC_VOL_STATS_GENERAL:
1213             {
1214                 struct VolPkgStats vol_stats;
1215                 memcpy(&vol_stats, res_buf, sizeof(vol_stats));
1216                 print_vol_stats_general(&vol_stats);
1217                 break;
1218             }
1219         case FSYNC_VOL_STATS_VICEP:
1220             {
1221                 struct DiskPartitionStats64 vicep_stats;
1222                 memcpy(&vicep_stats, res_buf, sizeof(vicep_stats));
1223                 print_vol_stats_viceP(&vicep_stats);
1224                 break;
1225             }
1226         case FSYNC_VOL_STATS_HASH:
1227             {
1228                 struct VolumeHashChainStats hash_stats;
1229                 memcpy(&hash_stats, res_buf, sizeof(hash_stats));
1230                 print_vol_stats_hash(&hash_stats);
1231                 break;
1232             }
1233 #ifdef AFS_DEMAND_ATTACH_FS
1234         case FSYNC_VOL_STATS_HDR:
1235             {
1236                 struct volume_hdr_LRU_stats hdr_stats;
1237                 memcpy(&hdr_stats, res_buf, sizeof(hdr_stats));
1238                 print_vol_stats_hdr(&hdr_stats);
1239                 break;
1240             }
1241 #endif /* AFS_DEMAND_ATTACH_FS */
1242         }
1243     }
1244
1245     return 0;
1246 }
1247
1248 static void
1249 print_vol_stats_general(VolPkgStats * stats)
1250 {
1251 #ifdef AFS_DEMAND_ATTACH_FS
1252     int i;
1253 #endif
1254     afs_uint32 hi, lo;
1255
1256     printf("VolPkgStats = {\n");
1257 #ifdef AFS_DEMAND_ATTACH_FS
1258     for (i = 0; i < VOL_STATE_COUNT; i++) {
1259         printf("\tvol_state_count[%s] = %d\n",
1260                vol_state_to_string(i),
1261                stats->state_levels[i]);
1262     }
1263
1264     SplitInt64(stats->hash_looks, hi, lo);
1265     printf("\thash_looks = {\n");
1266     printf("\t\thi = %u\n", hi);
1267     printf("\t\tlo = %u\n", lo);
1268     printf("\t}\n");
1269
1270     SplitInt64(stats->hash_reorders, hi, lo);
1271     printf("\thash_reorders = {\n");
1272     printf("\t\thi = %u\n", hi);
1273     printf("\t\tlo = %u\n", lo);
1274     printf("\t}\n");
1275
1276     SplitInt64(stats->salvages, hi, lo);
1277     printf("\tsalvages = {\n");
1278     printf("\t\thi = %u\n", hi);
1279     printf("\t\tlo = %u\n", lo);
1280     printf("\t}\n");
1281
1282     SplitInt64(stats->vol_ops, hi, lo);
1283     printf("\tvol_ops = {\n");
1284     printf("\t\thi = %u\n", hi);
1285     printf("\t\tlo = %u\n", lo);
1286     printf("\t}\n");
1287 #endif
1288     SplitInt64(stats->hdr_loads, hi, lo);
1289     printf("\thdr_loads = {\n");
1290     printf("\t\thi = %u\n", hi);
1291     printf("\t\tlo = %u\n", lo);
1292     printf("\t}\n");
1293
1294     SplitInt64(stats->hdr_gets, hi, lo);
1295     printf("\thdr_gets = {\n");
1296     printf("\t\thi = %u\n", hi);
1297     printf("\t\tlo = %u\n", lo);
1298     printf("\t}\n");
1299
1300     SplitInt64(stats->attaches, hi, lo);
1301     printf("\tattaches = {\n");
1302     printf("\t\thi = %u\n", hi);
1303     printf("\t\tlo = %u\n", lo);
1304     printf("\t}\n");
1305
1306     SplitInt64(stats->soft_detaches, hi, lo);
1307     printf("\tsoft_detaches = {\n");
1308     printf("\t\thi = %u\n", hi);
1309     printf("\t\tlo = %u\n", lo);
1310     printf("\t}\n");
1311
1312     printf("\thdr_cache_size = %d\n", stats->hdr_cache_size);
1313
1314     printf("}\n");
1315 }
1316
1317 static void
1318 print_vol_stats_viceP(struct DiskPartitionStats64 * stats)
1319 {
1320     printf("DiskPartitionStats64 = {\n");
1321     printf("\tfree = %" AFS_INT64_FMT "\n", stats->free);
1322     printf("\tminFree = %" AFS_INT64_FMT "\n", stats->minFree);
1323     printf("\ttotalUsable = %" AFS_INT64_FMT "\n", stats->totalUsable);
1324     printf("\tf_files = %" AFS_INT64_FMT "\n", stats->f_files);
1325 #ifdef AFS_DEMAND_ATTACH_FS
1326     printf("\tvol_list_len = %d\n", stats->vol_list_len);
1327 #endif
1328     printf("}\n");
1329 }
1330
1331 static void
1332 print_vol_stats_hash(struct VolumeHashChainStats * stats)
1333 {
1334 #ifdef AFS_DEMAND_ATTACH_FS
1335     afs_uint32 hi, lo;
1336 #endif
1337
1338     printf("DiskPartitionStats = {\n");
1339     printf("\ttable_size = %d\n", stats->table_size);
1340     printf("\tchain_len = %d\n", stats->chain_len);
1341
1342 #ifdef AFS_DEMAND_ATTACH_FS
1343     printf("\tchain_cacheCheck = %d\n", stats->chain_cacheCheck);
1344     printf("\tchain_busy = %d\n", stats->chain_busy);
1345
1346     SplitInt64(stats->chain_looks, hi, lo);
1347     printf("\tchain_looks = {\n");
1348     printf("\t\thi = %u\n", hi);
1349     printf("\t\tlo = %u\n", lo);
1350     printf("\t}\n");
1351
1352     SplitInt64(stats->chain_gets, hi, lo);
1353     printf("\tchain_gets = {\n");
1354     printf("\t\thi = %u\n", hi);
1355     printf("\t\tlo = %u\n", lo);
1356     printf("\t}\n");
1357
1358     SplitInt64(stats->chain_reorders, hi, lo);
1359     printf("\tchain_reorders = {\n");
1360     printf("\t\thi = %u\n", hi);
1361     printf("\t\tlo = %u\n", lo);
1362     printf("\t}\n");
1363 #endif /* AFS_DEMAND_ATTACH_FS */
1364
1365     printf("}\n");
1366 }
1367
1368
1369 #ifdef AFS_DEMAND_ATTACH_FS
1370 static void
1371 print_vol_stats_hdr(struct volume_hdr_LRU_stats * stats)
1372 {
1373     printf("volume_hdr_LRU_stats = {\n");
1374     printf("\tfree = %d\n", stats->free);
1375     printf("\tused = %d\n", stats->used);
1376     printf("\tattached = %d\n", stats->attached);
1377     printf("}\n");
1378 }
1379 #endif /* AFS_DEMAND_ATTACH_FS */
1380
1381
1382 /**
1383  * query VGC.
1384  *
1385  * @notes args:
1386  *    - CUSTOM_PARMS_OFFSET+0 is partition string
1387  *    - CUSTOM_PARMS_OFFSET+1 is volume id
1388  *
1389  * @return operation status
1390  *    @retval 0 success
1391  */
1392 static int
1393 VGCQuery(struct cmd_syndesc * as, void * rock)
1394 {
1395     afs_int32 code;
1396     struct state state;
1397     char * partName;
1398     VolumeId volid;
1399     FSSYNC_VGQry_response_t q_res;
1400     SYNC_response res;
1401     int i;
1402     struct cmd_item *ti;
1403
1404     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+0].items)) {       /* -partition */
1405         return -1;
1406     }
1407     partName = ti->data;
1408
1409     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+1].items)) {       /* -volumeid */
1410         return -1;
1411     }
1412     volid = atoi(ti->data);
1413
1414     common_prolog(as, &state);
1415
1416     fprintf(stderr, "calling FSYNC_VCGQuery\n");
1417
1418     code = FSYNC_VGCQuery(partName, volid, &q_res, &res);
1419
1420     debug_response(code, &res);
1421
1422     if (code == SYNC_OK) {
1423         printf("VG = {\n");
1424         printf("\trw\t=\t%u\n", q_res.rw);
1425         printf("\tchildren\t= (\n");
1426         for (i = 0; i < VOL_VG_MAX_VOLS; i++) {
1427             if (q_res.children[i]) {
1428                 printf("\t\t%u\n", q_res.children[i]);
1429             }
1430         }
1431         printf("\t)\n");
1432     }
1433
1434     VDisconnectFS();
1435
1436     return 0;
1437 }
1438
1439 static int
1440 VGCAdd(struct cmd_syndesc * as, void * rock)
1441 {
1442     afs_int32 code;
1443     struct state state;
1444     char * partName;
1445     VolumeId parent, child;
1446     struct cmd_item *ti;
1447     SYNC_response res;
1448
1449     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+0].items)) {       /* -partition */
1450         return -1;
1451     }
1452     partName = ti->data;
1453
1454     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+1].items)) {       /* -parent */
1455         return -1;
1456     }
1457     parent = atoi(ti->data);
1458
1459     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+2].items)) {       /* -child */
1460         return -1;
1461     }
1462     child = atoi(ti->data);
1463
1464     common_prolog(as, &state);
1465     fprintf(stderr, "calling FSYNC_VCGAdd\n");
1466     code = FSYNC_VGCAdd(partName, parent, child, state.reason, &res);
1467     debug_response(code, &res);
1468
1469     VDisconnectFS();
1470
1471     return 0;
1472 }
1473
1474 static int
1475 VGCDel(struct cmd_syndesc * as, void * rock)
1476 {
1477     afs_int32 code;
1478     struct state state;
1479     char * partName;
1480     VolumeId parent, child;
1481     struct cmd_item *ti;
1482     SYNC_response res;
1483
1484     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+0].items)) {       /* -partition */
1485         return -1;
1486     }
1487     partName = ti->data;
1488
1489     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+1].items)) {       /* -parent */
1490         return -1;
1491     }
1492     parent = atoi(ti->data);
1493
1494     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+2].items)) {       /* -child */
1495         return -1;
1496     }
1497     child = atoi(ti->data);
1498
1499     common_prolog(as, &state);
1500     fprintf(stderr, "calling FSYNC_VCGDel\n");
1501     code = FSYNC_VGCDel(partName, parent, child, state.reason, &res);
1502     debug_response(code, &res);
1503
1504     VDisconnectFS();
1505
1506     return 0;
1507 }
1508
1509 static int
1510 VGCScan(struct cmd_syndesc * as, void * rock)
1511 {
1512     afs_int32 code;
1513     struct state state;
1514     char * partName;
1515     struct cmd_item *ti;
1516
1517     if (!(ti = as->parms[CUSTOM_PARMS_OFFSET+0].items)) {       /* -partition */
1518         return -1;
1519     }
1520     partName = ti->data;
1521
1522     common_prolog(as, &state);
1523     fprintf(stderr, "calling FSYNC_VCGScan\n");
1524     code = FSYNC_VGCScan(partName, state.reason);
1525     debug_response(code, NULL);
1526
1527     VDisconnectFS();
1528
1529     return 0;
1530 }
1531
1532 static int
1533 VGCScanAll(struct cmd_syndesc * as, void * rock)
1534 {
1535     afs_int32 code;
1536     struct state state;
1537
1538     common_prolog(as, &state);
1539     fprintf(stderr, "calling FSYNC_VCGScanAll\n");
1540     code = FSYNC_VGCScan(NULL, state.reason);
1541     debug_response(code, NULL);
1542
1543     VDisconnectFS();
1544
1545     return 0;
1546 }