Demand attach warning fixes
[openafs.git] / src / tsalvaged / salvsync-debug.c
1 /*
2  * Copyright 2006, 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  * salvsync debug 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 #include <afs/dir.h>
43
44 #include <fcntl.h>
45
46 #ifndef AFS_NT40_ENV
47 #include <afs/osi_inode.h>
48 #endif
49
50 #include <afs/cmd.h>
51 #include <afs/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 "partition.h"
61 #include "daemon_com.h"
62 #include "salvsync.h"
63 #ifdef AFS_NT40_ENV
64 #include <pthread.h>
65 #endif
66
67 int VolumeChanged; /* hack to make dir package happy */
68
69
70 #ifndef AFS_DEMAND_ATTACH_FS
71 int
72 main(int argc, char ** argv)
73 {
74     fprintf(stderr, "*** salvsync-debug is only supported for OpenAFS builds with the demand-attach fileserver extension\n");
75     return -1;
76 }
77 #else /* AFS_DEMAND_ATTACH_FS */
78
79 struct salv_state {
80     afs_uint32 prio;
81     afs_uint32 volume;
82     char partName[16];
83 };
84
85 struct state {
86     afs_int32 reason;
87     struct salv_state * sop;
88 };
89
90 static int common_prolog(struct cmd_syndesc *, struct state *);
91 static int common_salv_prolog(struct cmd_syndesc *, struct state *);
92
93 static int do_salvop(struct state *, afs_int32 command, SYNC_response * res);
94
95 static char * response_code_to_string(afs_int32);
96 static char * command_code_to_string(afs_int32);
97 static char * reason_code_to_string(afs_int32);
98 static char * state_code_to_string(afs_int32);
99
100
101 static int OpStats(struct cmd_syndesc * as, void * rock);
102 static int OpSalvage(struct cmd_syndesc * as, void * rock);
103 static int OpCancel(struct cmd_syndesc * as, void * rock);
104 static int OpCancelAll(struct cmd_syndesc * as, void * rock);
105 static int OpRaisePrio(struct cmd_syndesc * as, void * rock);
106 static int OpQuery(struct cmd_syndesc * as, void * rock);
107
108
109 #ifndef AFS_NT40_ENV
110 #include "AFS_component_version_number.c"
111 #endif
112 #define MAX_ARGS 128
113
114 #define COMMON_PARMS_OFFSET    13
115 #define COMMON_PARMS(ts) \
116     cmd_Seek(ts, COMMON_PARMS_OFFSET); \
117     cmd_AddParm(ts, "-reason", CMD_SINGLE, CMD_OPTIONAL, "sync protocol reason code"); \
118     cmd_AddParm(ts, "-programtype", CMD_SINGLE, CMD_OPTIONAL, "program type code")
119
120 #define COMMON_SALV_PARMS_OFFSET    10
121 #define COMMON_SALV_PARMS(ts) \
122     cmd_Seek(ts, COMMON_SALV_PARMS_OFFSET); \
123     cmd_AddParm(ts, "-volumeid", CMD_SINGLE, 0, "volume id"); \
124     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "partition name"); \
125     cmd_AddParm(ts, "-priority", CMD_SINGLE, CMD_OPTIONAL, "priority")
126
127 #define SALV_PARMS_DECL(ts) \
128     COMMON_SALV_PARMS(ts); \
129     COMMON_PARMS(ts)
130
131 #define COMMON_PARMS_DECL(ts) \
132     COMMON_PARMS(ts)
133
134 int
135 main(int argc, char **argv)
136 {
137     struct cmd_syndesc *ts;
138     int err = 0;
139
140     /* Initialize directory paths */
141     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
142 #ifdef AFS_NT40_ENV
143         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
144 #endif
145         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
146                 argv[0]);
147         exit(2);
148     }
149
150
151     ts = cmd_CreateSyntax("stats", OpStats, NULL, "get salvageserver statistics (SALVSYNC_NOP opcode)");
152     COMMON_PARMS_DECL(ts);
153     cmd_CreateAlias(ts, "nop");
154
155     ts = cmd_CreateSyntax("salvage", OpSalvage, NULL, "schedule a salvage (SALVSYNC_SALVAGE opcode)");
156     SALV_PARMS_DECL(ts);
157
158     ts = cmd_CreateSyntax("cancel", OpCancel, NULL, "cancel a salvage (SALVSYNC_CANCEL opcode)");
159     SALV_PARMS_DECL(ts);
160
161     ts = cmd_CreateSyntax("raiseprio", OpRaisePrio, NULL, "raise a salvage priority (SALVSYNC_RAISEPRIO opcode)");
162     SALV_PARMS_DECL(ts);
163     cmd_CreateAlias(ts, "rp");
164
165     ts = cmd_CreateSyntax("query", OpQuery, NULL, "query salvage status (SALVSYNC_QUERY opcode)");
166     SALV_PARMS_DECL(ts);
167     cmd_CreateAlias(ts, "qry");
168
169     ts = cmd_CreateSyntax("kill", OpCancelAll, NULL, "cancel all scheduled salvages (SALVSYNC_CANCELALL opcode)");
170     COMMON_PARMS_DECL(ts);
171
172     err = cmd_Dispatch(argc, argv);
173     exit(err);
174 }
175
176 static int
177 common_prolog(struct cmd_syndesc * as, struct state * state)
178 {
179     register struct cmd_item *ti;
180
181 #ifdef AFS_NT40_ENV
182     if (afs_winsockInit() < 0) {
183         Exit(1);
184     }
185 #endif
186
187     VInitVolumePackage(debugUtility, 1, 1,
188                        DONT_CONNECT_FS, 0);
189     DInit(1);
190
191     if ((ti = as->parms[COMMON_PARMS_OFFSET].items)) {  /* -reason */
192         state->reason = atoi(ti->data);
193     }
194     if ((ti = as->parms[COMMON_PARMS_OFFSET+1].items)) {        /* -programtype */
195         if (!strcmp(ti->data, "fileServer")) {
196             programType = fileServer;
197         } else if (!strcmp(ti->data, "volumeUtility")) {
198             programType = volumeUtility;
199         } else if (!strcmp(ti->data, "salvager")) {
200             programType = salvager;
201         } else if (!strcmp(ti->data, "salvageServer")) {
202             programType = salvageServer;
203         } else {
204             programType = (ProgramType) atoi(ti->data);
205         }
206     }
207
208     VConnectSALV();
209
210     return 0;
211 }
212
213 static int
214 common_salv_prolog(struct cmd_syndesc * as, struct state * state)
215 {
216     register struct cmd_item *ti;
217
218     state->sop = (struct salv_state *) calloc(1, sizeof(struct salv_state));
219     assert(state->sop != NULL);
220
221     if ((ti = as->parms[COMMON_SALV_PARMS_OFFSET].items)) {     /* -volumeid */
222         state->sop->volume = atoi(ti->data);
223     } else {
224         fprintf(stderr, "required argument -volumeid not given\n");
225     }
226
227     if ((ti = as->parms[COMMON_SALV_PARMS_OFFSET+1].items)) {   /* -partition */
228         strlcpy(state->sop->partName, ti->data, sizeof(state->sop->partName));
229     } else {
230         memset(state->sop->partName, 0, sizeof(state->sop->partName));
231     }
232
233     if ((ti = as->parms[COMMON_SALV_PARMS_OFFSET+2].items)) {   /* -prio */
234         state->sop->prio = atoi(ti->data);
235     } else {
236         state->sop->prio = 0;
237     }
238
239     return 0;
240 }
241
242 static int
243 do_salvop(struct state * state, afs_int32 command, SYNC_response * res)
244 {
245     afs_int32 code;
246     SALVSYNC_response_hdr hdr_l, *hdr;
247     SYNC_response res_l;
248
249     if (!res) {
250         res = &res_l;
251         res->payload.len = sizeof(hdr_l);
252         res->payload.buf = hdr = &hdr_l;
253     } else {
254         hdr = (SALVSYNC_response_hdr *) res->payload.buf;
255     }
256
257     fprintf(stderr, "calling SALVSYNC_SalvageVolume with command code %d (%s)\n", 
258             command, command_code_to_string(command));
259
260     code = SALVSYNC_SalvageVolume(state->sop->volume,
261                                   state->sop->partName,
262                                   command,
263                                   state->reason,
264                                   state->sop->prio,
265                                   res);
266
267     switch (code) {
268     case SYNC_OK:
269     case SYNC_DENIED:
270         break;
271     default:
272         fprintf(stderr, "possible sync protocol error. return code was %d\n", code);
273     }
274
275     fprintf(stderr, "SALVSYNC_SalvageVolume returned %d (%s)\n", code, response_code_to_string(code));
276     fprintf(stderr, "protocol response code was %d (%s)\n", 
277             res->hdr.response, response_code_to_string(res->hdr.response));
278     fprintf(stderr, "protocol reason code was %d (%s)\n", 
279             res->hdr.reason, reason_code_to_string(res->hdr.reason));
280
281     printf("state = {\n");
282     if (res->hdr.flags & SALVSYNC_FLAG_VOL_STATS_VALID) {
283         printf("\tstate = %d (%s)\n",
284                hdr->state, state_code_to_string(hdr->state));
285         printf("\tprio = %d\n", hdr->prio);
286     }
287     printf("\tsq_len = %d\n", hdr->sq_len);
288     printf("\tpq_len = %d\n", hdr->pq_len);
289     printf("}\n");
290
291     VDisconnectSALV();
292
293     return 0;
294 }
295
296 static char *
297 response_code_to_string(afs_int32 response)
298 {
299     switch (response) {
300     case SYNC_OK:
301         return "SYNC_OK";
302     case SYNC_DENIED:
303         return "SYNC_DENIED";
304     case SYNC_COM_ERROR:
305         return "SYNC_COM_ERROR";
306     case SYNC_BAD_COMMAND:
307         return "SYNC_BAD_COMMAND";
308     case SYNC_FAILED:
309         return "SYNC_FAILED";
310     default:
311         return "**UNKNOWN**";
312     }
313 }
314
315 static char *
316 command_code_to_string(afs_int32 command)
317 {
318     switch (command) {
319     case SYNC_COM_CHANNEL_CLOSE:
320         return "SYNC_COM_CHANNEL_CLOSE";
321     case SALVSYNC_NOP:
322         return "SALVSYNC_NOP";
323     case SALVSYNC_SALVAGE:
324         return "SALVSYNC_SALVAGE";
325     case SALVSYNC_CANCEL:
326         return "SALVSYNC_CANCEL";
327     case SALVSYNC_RAISEPRIO:
328         return "SALVSYNC_RAISEPRIO";
329     case SALVSYNC_QUERY:
330         return "SALVSYNC_QUERY";
331     case SALVSYNC_CANCELALL:
332         return "SALVSYNC_CANCELLALL";
333     default:
334         return "**UNKNOWN**";
335     }
336 }
337
338 static char *
339 reason_code_to_string(afs_int32 reason)
340 {
341     switch (reason) {
342     case SALVSYNC_WHATEVER:
343         return "SALVSYNC_WHATEVER";
344     case SALVSYNC_ERROR:
345         return "SALVSYNC_ERROR";
346     case SALVSYNC_OPERATOR:
347         return "SALVSYNC_OPERATOR";
348     case SALVSYNC_SHUTDOWN:
349         return "SALVSYNC_SHUTDOWN";
350     case SALVSYNC_NEEDED:
351         return "SALVSYNC_NEEDED";
352     default:
353         return "**UNKNOWN**";
354     }
355 }
356
357 #if 0
358 static char *
359 program_type_to_string(afs_int32 type)
360 {
361     switch ((ProgramType)type) {
362     case fileServer:
363         return "fileServer";
364     case volumeUtility:
365         return "volumeUtility";
366     case salvager:
367         return "salvager";
368     case salvageServer:
369         return "salvageServer";
370     default:
371         return "**UNKNOWN**";
372     }
373 }
374 #endif
375
376 static char *
377 state_code_to_string(afs_int32 state)
378 {
379     switch (state) {
380     case SALVSYNC_STATE_UNKNOWN:
381         return "SALVSYNC_STATE_UNKNOWN";
382     case SALVSYNC_STATE_QUEUED:
383         return "SALVSYNC_STATE_QUEUED";
384     case SALVSYNC_STATE_SALVAGING:
385         return "SALVSYNC_STATE_SALVAGING";
386     case SALVSYNC_STATE_ERROR:
387         return "SALVSYNC_STATE_ERROR";
388     case SALVSYNC_STATE_DONE:
389         return "SALVSYNC_STATE_DONE";
390     default:
391         return "**UNKNOWN**";
392     }
393 }
394
395 static int
396 OpStats(struct cmd_syndesc * as, void * rock)
397 {
398     struct state state;
399
400     common_prolog(as, &state);
401     common_salv_prolog(as, &state);
402
403     do_salvop(&state, SALVSYNC_NOP, NULL);
404
405     return 0;
406 }
407
408 static int
409 OpSalvage(struct cmd_syndesc * as, void * rock)
410 {
411     struct state state;
412
413     common_prolog(as, &state);
414     common_salv_prolog(as, &state);
415
416     do_salvop(&state, SALVSYNC_SALVAGE, NULL);
417
418     return 0;
419 }
420
421 static int
422 OpCancel(struct cmd_syndesc * as, void * rock)
423 {
424     struct state state;
425
426     common_prolog(as, &state);
427     common_salv_prolog(as, &state);
428
429     do_salvop(&state, SALVSYNC_CANCEL, NULL);
430
431     return 0;
432 }
433
434 static int
435 OpCancelAll(struct cmd_syndesc * as, void * rock)
436 {
437     struct state state;
438
439     common_prolog(as, &state);
440     common_salv_prolog(as, &state);
441
442     do_salvop(&state, SALVSYNC_CANCELALL, NULL);
443
444     return 0;
445 }
446
447 static int
448 OpRaisePrio(struct cmd_syndesc * as, void * rock)
449 {
450     struct state state;
451
452     common_prolog(as, &state);
453     common_salv_prolog(as, &state);
454
455     do_salvop(&state, SALVSYNC_RAISEPRIO, NULL);
456
457     return 0;
458 }
459
460 static int
461 OpQuery(struct cmd_syndesc * as, void * rock)
462 {
463     struct state state;
464
465     common_prolog(as, &state);
466     common_salv_prolog(as, &state);
467
468     do_salvop(&state, SALVSYNC_QUERY, NULL);
469
470     return 0;
471 }
472
473 #endif /* AFS_DEMAND_ATTACH_FS */