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