2 * Copyright 2006, Sine Nomine Associates and others.
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
10 /* Main program file. Define globals. */
18 #include <afsconfig.h>
19 #include <afs/param.h>
24 #include <WINNT/afsevent.h>
28 #include <rx/rx_queue.h>
29 #include <afs/afsint.h>
30 #include <afs/opr_assert.h>
34 #include <afs/osi_inode.h>
38 #include <afs/afsutil.h>
39 #include <afs/fileutil.h>
44 #include <afs/ihandle.h>
45 #include <afs/vnode.h>
46 #include <afs/volume.h>
47 #include <afs/partition.h>
48 #include <afs/daemon_com.h>
49 #include <afs/salvsync.h>
54 int VolumeChanged
; /* hack to make dir package happy */
57 #ifndef AFS_DEMAND_ATTACH_FS
59 main(int argc
, char ** argv
)
61 fprintf(stderr
, "*** salvsync-debug is only supported for OpenAFS builds with the demand-attach fileserver extension\n");
64 #else /* AFS_DEMAND_ATTACH_FS */
74 struct salv_state
* sop
;
77 static int common_prolog(struct cmd_syndesc
*, struct fssync_state
*);
78 static int common_salv_prolog(struct cmd_syndesc
*, struct fssync_state
*);
80 static int do_salvop(struct fssync_state
*, afs_int32 command
, SYNC_response
* res
);
82 static char * response_code_to_string(afs_int32
);
83 static char * command_code_to_string(afs_int32
);
84 static char * reason_code_to_string(afs_int32
);
85 static char * state_code_to_string(afs_int32
);
88 static int OpStats(struct cmd_syndesc
* as
, void * rock
);
89 static int OpSalvage(struct cmd_syndesc
* as
, void * rock
);
90 static int OpCancel(struct cmd_syndesc
* as
, void * rock
);
91 static int OpCancelAll(struct cmd_syndesc
* as
, void * rock
);
92 static int OpRaisePrio(struct cmd_syndesc
* as
, void * rock
);
93 static int OpQuery(struct cmd_syndesc
* as
, void * rock
);
97 #include "AFS_component_version_number.c"
101 #define COMMON_PARMS_OFFSET 13
102 #define COMMON_PARMS(ts) \
103 cmd_Seek(ts, COMMON_PARMS_OFFSET); \
104 cmd_AddParm(ts, "-reason", CMD_SINGLE, CMD_OPTIONAL, "sync protocol reason code"); \
105 cmd_AddParm(ts, "-programtype", CMD_SINGLE, CMD_OPTIONAL, "program type code")
107 #define COMMON_SALV_PARMS_OFFSET 10
108 #define COMMON_SALV_PARMS(ts) \
109 cmd_Seek(ts, COMMON_SALV_PARMS_OFFSET); \
110 cmd_AddParm(ts, "-volumeid", CMD_SINGLE, 0, "volume id"); \
111 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name"); \
112 cmd_AddParm(ts, "-priority", CMD_SINGLE, CMD_OPTIONAL, "priority")
114 #define SALV_PARMS_DECL(ts) \
115 COMMON_SALV_PARMS(ts); \
118 #define COMMON_PARMS_DECL(ts) \
122 main(int argc
, char **argv
)
124 struct cmd_syndesc
*ts
;
127 /* Initialize directory paths */
128 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK
)) {
130 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR
, 0, argv
[0], 0);
132 fprintf(stderr
, "%s: Unable to obtain AFS server directory.\n",
138 ts
= cmd_CreateSyntax("stats", OpStats
, NULL
, 0, "get salvageserver statistics (SALVSYNC_NOP opcode)");
139 COMMON_PARMS_DECL(ts
);
140 cmd_CreateAlias(ts
, "nop");
142 ts
= cmd_CreateSyntax("salvage", OpSalvage
, NULL
, 0, "schedule a salvage (SALVSYNC_SALVAGE opcode)");
145 ts
= cmd_CreateSyntax("cancel", OpCancel
, NULL
, 0, "cancel a salvage (SALVSYNC_CANCEL opcode)");
148 ts
= cmd_CreateSyntax("raiseprio", OpRaisePrio
, NULL
, 0, "raise a salvage priority (SALVSYNC_RAISEPRIO opcode)");
150 cmd_CreateAlias(ts
, "rp");
152 ts
= cmd_CreateSyntax("query", OpQuery
, NULL
, 0, "query salvage status (SALVSYNC_QUERY opcode)");
154 cmd_CreateAlias(ts
, "qry");
156 ts
= cmd_CreateSyntax("kill", OpCancelAll
, NULL
, 0, "cancel all scheduled salvages (SALVSYNC_CANCELALL opcode)");
157 COMMON_PARMS_DECL(ts
);
159 err
= cmd_Dispatch(argc
, argv
);
164 common_prolog(struct cmd_syndesc
* as
, struct fssync_state
* state
)
167 VolumePackageOptions opts
;
170 if (afs_winsockInit() < 0) {
175 VOptDefaults(debugUtility
, &opts
);
176 if (VInitVolumePackage2(debugUtility
, &opts
)) {
177 /* VInitVolumePackage2 can fail on e.g. partition attachment errors,
178 * but we don't really care, since all we're doing is trying to use
180 fprintf(stderr
, "errors encountered initializing volume package, but "
181 "trying to continue anyway\n");
185 if ((ti
= as
->parms
[COMMON_PARMS_OFFSET
].items
)) { /* -reason */
186 state
->reason
= atoi(ti
->data
);
188 state
->reason
= SALVSYNC_REASON_WHATEVER
;
191 if ((ti
= as
->parms
[COMMON_PARMS_OFFSET
+1].items
)) { /* -programtype */
192 if (!strcmp(ti
->data
, "fileServer")) {
193 programType
= fileServer
;
194 } else if (!strcmp(ti
->data
, "volumeUtility")) {
195 programType
= volumeUtility
;
196 } else if (!strcmp(ti
->data
, "salvager")) {
197 programType
= salvager
;
198 } else if (!strcmp(ti
->data
, "salvageServer")) {
199 programType
= salvageServer
;
200 } else if (!strcmp(ti
->data
, "volumeServer")) {
201 programType
= volumeServer
;
202 } else if (!strcmp(ti
->data
, "volumeSalvager")) {
203 programType
= volumeSalvager
;
205 programType
= (ProgramType
) atoi(ti
->data
);
215 common_salv_prolog(struct cmd_syndesc
* as
, struct fssync_state
* state
)
219 state
->sop
= (struct salv_state
*) calloc(1, sizeof(struct salv_state
));
220 assert(state
->sop
!= NULL
);
222 if ((ti
= as
->parms
[COMMON_SALV_PARMS_OFFSET
].items
)) { /* -volumeid */
223 state
->sop
->volume
= atoi(ti
->data
);
225 fprintf(stderr
, "required argument -volumeid not given\n");
228 if ((ti
= as
->parms
[COMMON_SALV_PARMS_OFFSET
+1].items
)) { /* -partition */
229 strlcpy(state
->sop
->partName
, ti
->data
, sizeof(state
->sop
->partName
));
231 memset(state
->sop
->partName
, 0, sizeof(state
->sop
->partName
));
234 if ((ti
= as
->parms
[COMMON_SALV_PARMS_OFFSET
+2].items
)) { /* -prio */
235 state
->sop
->prio
= atoi(ti
->data
);
237 state
->sop
->prio
= 0;
244 do_salvop(struct fssync_state
* state
, afs_int32 command
, SYNC_response
* res
)
247 SALVSYNC_response_hdr hdr_l
, *hdr
;
252 res
->payload
.len
= sizeof(hdr_l
);
253 res
->payload
.buf
= hdr
= &hdr_l
;
255 hdr
= (SALVSYNC_response_hdr
*) res
->payload
.buf
;
258 fprintf(stderr
, "calling SALVSYNC_SalvageVolume with command code %d (%s)\n",
259 command
, command_code_to_string(command
));
261 code
= SALVSYNC_SalvageVolume(state
->sop
->volume
,
262 state
->sop
->partName
,
273 fprintf(stderr
, "possible sync protocol error. return code was %d\n", code
);
276 fprintf(stderr
, "SALVSYNC_SalvageVolume returned %d (%s)\n", code
, response_code_to_string(code
));
277 fprintf(stderr
, "protocol response code was %d (%s)\n",
278 res
->hdr
.response
, response_code_to_string(res
->hdr
.response
));
279 fprintf(stderr
, "protocol reason code was %d (%s)\n",
280 res
->hdr
.reason
, reason_code_to_string(res
->hdr
.reason
));
282 printf("state = {\n");
283 if (res
->hdr
.flags
& SALVSYNC_FLAG_VOL_STATS_VALID
) {
284 printf("\tstate = %d (%s)\n",
285 hdr
->state
, state_code_to_string(hdr
->state
));
286 printf("\tprio = %d\n", hdr
->prio
);
288 printf("\tsq_len = %d\n", hdr
->sq_len
);
289 printf("\tpq_len = %d\n", hdr
->pq_len
);
298 response_code_to_string(afs_int32 response
)
304 return "SYNC_DENIED";
306 return "SYNC_COM_ERROR";
307 case SYNC_BAD_COMMAND
:
308 return "SYNC_BAD_COMMAND";
310 return "SYNC_FAILED";
312 return "**UNKNOWN**";
317 command_code_to_string(afs_int32 command
)
320 case SYNC_COM_CHANNEL_CLOSE
:
321 return "SYNC_COM_CHANNEL_CLOSE";
323 return "SALVSYNC_NOP";
324 case SALVSYNC_SALVAGE
:
325 return "SALVSYNC_SALVAGE";
326 case SALVSYNC_CANCEL
:
327 return "SALVSYNC_CANCEL";
328 case SALVSYNC_RAISEPRIO
:
329 return "SALVSYNC_RAISEPRIO";
331 return "SALVSYNC_QUERY";
332 case SALVSYNC_CANCELALL
:
333 return "SALVSYNC_CANCELLALL";
335 return "**UNKNOWN**";
340 reason_code_to_string(afs_int32 reason
)
343 case SALVSYNC_WHATEVER
:
344 return "SALVSYNC_WHATEVER";
346 return "SALVSYNC_ERROR";
347 case SALVSYNC_OPERATOR
:
348 return "SALVSYNC_OPERATOR";
349 case SALVSYNC_SHUTDOWN
:
350 return "SALVSYNC_SHUTDOWN";
351 case SALVSYNC_NEEDED
:
352 return "SALVSYNC_NEEDED";
354 return "**UNKNOWN**";
360 program_type_to_string(afs_int32 type
)
362 switch ((ProgramType
)type
) {
366 return "volumeUtility";
370 return "salvageServer";
372 return "**UNKNOWN**";
378 state_code_to_string(afs_int32 state
)
381 case SALVSYNC_STATE_UNKNOWN
:
382 return "SALVSYNC_STATE_UNKNOWN";
383 case SALVSYNC_STATE_QUEUED
:
384 return "SALVSYNC_STATE_QUEUED";
385 case SALVSYNC_STATE_SALVAGING
:
386 return "SALVSYNC_STATE_SALVAGING";
387 case SALVSYNC_STATE_ERROR
:
388 return "SALVSYNC_STATE_ERROR";
389 case SALVSYNC_STATE_DONE
:
390 return "SALVSYNC_STATE_DONE";
392 return "**UNKNOWN**";
397 OpStats(struct cmd_syndesc
* as
, void * rock
)
399 struct fssync_state state
;
401 common_prolog(as
, &state
);
402 common_salv_prolog(as
, &state
);
404 do_salvop(&state
, SALVSYNC_NOP
, NULL
);
410 OpSalvage(struct cmd_syndesc
* as
, void * rock
)
412 struct fssync_state state
;
414 common_prolog(as
, &state
);
415 common_salv_prolog(as
, &state
);
417 do_salvop(&state
, SALVSYNC_SALVAGE
, NULL
);
423 OpCancel(struct cmd_syndesc
* as
, void * rock
)
425 struct fssync_state state
;
427 common_prolog(as
, &state
);
428 common_salv_prolog(as
, &state
);
430 do_salvop(&state
, SALVSYNC_CANCEL
, NULL
);
436 OpCancelAll(struct cmd_syndesc
* as
, void * rock
)
438 struct fssync_state state
;
440 common_prolog(as
, &state
);
441 common_salv_prolog(as
, &state
);
443 do_salvop(&state
, SALVSYNC_CANCELALL
, NULL
);
449 OpRaisePrio(struct cmd_syndesc
* as
, void * rock
)
451 struct fssync_state state
;
453 common_prolog(as
, &state
);
454 common_salv_prolog(as
, &state
);
456 do_salvop(&state
, SALVSYNC_RAISEPRIO
, NULL
);
462 OpQuery(struct cmd_syndesc
* as
, void * rock
)
464 struct fssync_state state
;
466 common_prolog(as
, &state
);
467 common_salv_prolog(as
, &state
);
469 do_salvop(&state
, SALVSYNC_QUERY
, NULL
);
474 #endif /* AFS_DEMAND_ATTACH_FS */