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