Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / vol / salvager.c
1 /*
2 * Copyright 2000, International Business Machines Corporation 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 /*
11 * System: VICE-TWO
12 * Module: salvager.c
13 * Institution: The Information Technology Center, Carnegie-Mellon University
14 */
15
16
17 /* Main program file. Define globals. */
18 #define MAIN 1
19
20 #include <afsconfig.h>
21 #include <afs/param.h>
22
23 #include <afs/procmgmt.h>
24 #include <roken.h>
25
26 #ifdef HAVE_SYS_FILE_H
27 #include <sys/file.h>
28 #endif
29
30 #ifdef AFS_NT40_ENV
31 #include <WINNT/afsevent.h>
32 #endif
33
34 #ifndef WCOREDUMP
35 #define WCOREDUMP(x) ((x) & 0200)
36 #endif
37
38 #include <rx/xdr.h>
39 #include <afs/afsint.h>
40 #if !defined(AFS_SGI_ENV) && !defined(AFS_NT40_ENV)
41 #if defined(AFS_VFSINCL_ENV)
42 #include <sys/vnode.h>
43 #ifdef AFS_SUN5_ENV
44 #include <sys/fs/ufs_inode.h>
45 #else
46 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
47 #include <ufs/ufs/dinode.h>
48 #include <ufs/ffs/fs.h>
49 #else
50 #include <ufs/inode.h>
51 #endif
52 #endif
53 #else /* AFS_VFSINCL_ENV */
54 #ifdef AFS_OSF_ENV
55 #include <ufs/inode.h>
56 #else /* AFS_OSF_ENV */
57 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_XBSD_ENV) && !defined(AFS_DARWIN_ENV)
58 #include <sys/inode.h>
59 #endif
60 #endif
61 #endif /* AFS_VFSINCL_ENV */
62 #endif /* AFS_SGI_ENV */
63 #ifdef AFS_AIX_ENV
64 #include <sys/vfs.h>
65 #include <sys/lockf.h>
66 #else
67 #ifdef AFS_HPUX_ENV
68 #include <checklist.h>
69 #else
70 #if defined(AFS_SGI_ENV)
71 #include <mntent.h>
72 #else
73 #if defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
74 #ifdef AFS_SUN5_ENV
75 #include <sys/mnttab.h>
76 #include <sys/mntent.h>
77 #else
78 #include <mntent.h>
79 #endif
80 #else
81 #endif /* AFS_SGI_ENV */
82 #endif /* AFS_HPUX_ENV */
83 #endif
84 #endif
85 #ifndef AFS_NT40_ENV
86 #include <afs/osi_inode.h>
87 #endif
88 #include <afs/cmd.h>
89 #include <afs/dir.h>
90 #include <afs/afsutil.h>
91 #include <afs/fileutil.h>
92 #include <rx/rx_queue.h>
93
94 #include "nfs.h"
95 #include "lwp.h"
96 #include "lock.h"
97 #include <afs/afssyscalls.h>
98 #include "ihandle.h"
99 #include "vnode.h"
100 #include "volume.h"
101 #include "partition.h"
102 #include "daemon_com.h"
103 #include "fssync.h"
104 #include "salvsync.h"
105 #include "viceinode.h"
106 #include "salvage.h"
107 #include "vol-salvage.h"
108 #include "common.h"
109 #ifdef AFS_NT40_ENV
110 #include <pthread.h>
111 pthread_t main_thread;
112 #endif
113
114 extern char cml_version_number[];
115 static int get_salvage_lock = 0;
116
117 struct CmdLine {
118 int argc;
119 char **argv;
120 };
121
122 static int
123 TimeStampLogFile(char **logfile)
124 {
125 char *stampSlvgLog;
126 struct tm *lt;
127 time_t now;
128
129 now = time(0);
130 lt = localtime(&now);
131 if (asprintf(&stampSlvgLog,
132 "%s.%04d-%02d-%02d.%02d:%02d:%02d",
133 AFSDIR_SERVER_SLVGLOG_FILEPATH,
134 lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour,
135 lt->tm_min, lt->tm_sec) < 0) {
136 return ENOMEM;
137 }
138 *logfile = stampSlvgLog;
139 return 0;
140 }
141
142 static int
143 handleit(struct cmd_syndesc *as, void *arock)
144 {
145 struct CmdLine *cmdline = (struct CmdLine*)arock;
146 struct cmd_item *ti;
147 char pname[100], *temp;
148 afs_int32 seenpart = 0, seenvol = 0;
149 VolumeId vid = 0;
150 ProgramType pt;
151
152 #ifdef FAST_RESTART
153 afs_int32 seenany = 0;
154 #endif
155
156 char *filename = NULL;
157 struct logOptions logopts;
158 VolumePackageOptions opts;
159 struct DiskPartition64 *partP;
160
161 memset(&logopts, 0, sizeof(logopts));
162
163 #ifdef AFS_SGI_VNODE_GLUE
164 if (afs_init_kernel_config(-1) < 0) {
165 printf
166 ("Can't determine NUMA configuration, not starting salvager.\n");
167 exit(1);
168 }
169 #endif
170
171 #ifdef FAST_RESTART
172 {
173 afs_int32 i;
174 for (i = 0; i < CMD_MAXPARMS; i++) {
175 if (as->parms[i].items) {
176 seenany = 1;
177 break;
178 }
179 }
180 }
181 if (!seenany) {
182 printf
183 ("Exiting immediately without salvage. "
184 "Look into the FileLog to find volumes which really need to be salvaged!\n");
185 Exit(0);
186 }
187 #endif /* FAST_RESTART */
188 if ((ti = as->parms[0].items)) { /* -partition */
189 seenpart = 1;
190 strncpy(pname, ti->data, 100);
191 }
192 if ((ti = as->parms[1].items)) { /* -volumeid */
193 char *end;
194 unsigned long vid_l;
195 if (!seenpart) {
196 printf
197 ("You must also specify '-partition' option with the '-volumeid' option\n");
198 exit(-1);
199 }
200 seenvol = 1;
201 vid_l = strtoul(ti->data, &end, 10);
202 if (vid_l >= MAX_AFS_UINT32 || vid_l == ULONG_MAX || *end != '\0') {
203 fprintf(stderr, "salvage: invalid volume id specified; salvage aborted\n");
204 Exit(1);
205 }
206 vid = (VolumeId)vid_l;
207 }
208 if (as->parms[2].items) /* -debug */
209 debug = 1;
210 if (as->parms[3].items) /* -nowrite */
211 Testing = 1;
212 if (as->parms[4].items) /* -inodes */
213 ListInodeOption = 1;
214 if (as->parms[5].items || as->parms[21].items) /* -force, -f */
215 ForceSalvage = 1;
216 if (as->parms[6].items) /* -oktozap */
217 OKToZap = 1;
218 if (as->parms[7].items) /* -rootinodes */
219 ShowRootFiles = 1;
220 if (as->parms[8].items) /* -RebuildDirs */
221 RebuildDirs = 1;
222 if (as->parms[9].items) /* -ForceReads */
223 forceR = 1;
224 if ((ti = as->parms[10].items)) { /* -Parallel # */
225 temp = ti->data;
226 if (strncmp(temp, "all", 3) == 0) {
227 PartsPerDisk = 1;
228 temp += 3;
229 }
230 if (strlen(temp) != 0) {
231 Parallel = atoi(temp);
232 if (Parallel < 1)
233 Parallel = 1;
234 if (Parallel > MAXPARALLEL) {
235 printf("Setting parallel salvages to maximum of %d \n",
236 MAXPARALLEL);
237 Parallel = MAXPARALLEL;
238 }
239 }
240 }
241 if ((ti = as->parms[11].items)) { /* -tmpdir */
242 DIR *dirp;
243
244 tmpdir = ti->data;
245 dirp = opendir(tmpdir);
246 if (!dirp) {
247 printf
248 ("Can't open temporary placeholder dir %s; using current partition \n",
249 tmpdir);
250 tmpdir = NULL;
251 } else
252 closedir(dirp);
253 }
254 if ((ti = as->parms[12].items)) /* -showlog */
255 ShowLog = 1;
256 if ((ti = as->parms[13].items)) { /* -showsuid */
257 Testing = 1;
258 ShowSuid = 1;
259 Showmode = 1;
260 }
261 if ((ti = as->parms[14].items)) { /* -showmounts */
262 Testing = 1;
263 Showmode = 1;
264 ShowMounts = 1;
265 }
266 if ((ti = as->parms[15].items)) { /* -orphans */
267 if (Testing)
268 orphans = ORPH_IGNORE;
269 else if (strcmp(ti->data, "remove") == 0
270 || strcmp(ti->data, "r") == 0)
271 orphans = ORPH_REMOVE;
272 else if (strcmp(ti->data, "attach") == 0
273 || strcmp(ti->data, "a") == 0)
274 orphans = ORPH_ATTACH;
275 }
276
277 if ((ti = as->parms[16].items)) { /* -syslog */
278 if (ShowLog) {
279 fprintf(stderr, "Invalid options: -syslog and -showlog are exclusive.\n");
280 Exit(1);
281 }
282 if ((ti = as->parms[18].items)) { /* -datelogs */
283 fprintf(stderr, "Invalid option: -syslog and -datelogs are exclusive.\n");
284 Exit(1);
285 }
286 #ifndef HAVE_SYSLOG
287 /* Do not silently ignore. */
288 fprintf(stderr, "Invalid option: -syslog is not available on this platform.\n");
289 Exit(1);
290 #else
291 logopts.lopt_dest = logDest_syslog;
292 logopts.lopt_tag = "salvager";
293
294 if ((ti = as->parms[17].items)) /* -syslogfacility */
295 logopts.lopt_facility = atoi(ti->data);
296 else
297 logopts.lopt_facility = LOG_DAEMON; /* default value */
298 #endif
299 } else {
300 logopts.lopt_dest = logDest_file;
301
302 if ((ti = as->parms[18].items)) { /* -datelogs */
303 int code = TimeStampLogFile(&filename);
304 if (code != 0) {
305 fprintf(stderr, "Failed to format log file name for -datelogs; code=%d\n", code);
306 Exit(code);
307 }
308 logopts.lopt_filename = filename;
309 } else {
310 logopts.lopt_filename = AFSDIR_SERVER_SLVGLOG_FILEPATH;
311 }
312 }
313
314 OpenLog(&logopts);
315 SetupLogSignals();
316 free(filename); /* Free string created by -datelogs, if one. */
317
318 Log("%s\n", cml_version_number);
319 LogCommandLine(cmdline->argc, cmdline->argv, "SALVAGER", SalvageVersion, "STARTING AFS", Log);
320
321 #ifdef FAST_RESTART
322 if (ti = as->parms[19].items) { /* -DontSalvage */
323 char *msg =
324 "Exiting immediately without salvage. Look into the FileLog to find volumes which really need to be salvaged!";
325 Log("%s\n", msg);
326 printf("%s\n", msg);
327 Exit(0);
328 }
329 #endif
330
331 /* Note: if seenvol we initialize this as a standard volume utility: this has the
332 * implication that the file server may be running; negotations have to be made with
333 * the file server in this case to take the read write volume and associated read-only
334 * volumes off line before salvaging */
335 #ifdef AFS_NT40_ENV
336 if (seenvol) {
337 if (afs_winsockInit() < 0) {
338 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0,
339 AFSDIR_SALVAGER_FILE, 0);
340 Log("Failed to initailize winsock, exiting.\n");
341 Exit(1);
342 }
343 }
344 #endif
345
346 if (seenvol) {
347 pt = volumeSalvager;
348 } else {
349 pt = salvager;
350 }
351
352 VOptDefaults(pt, &opts);
353 if (VInitVolumePackage2(pt, &opts)) {
354 Log("errors encountered initializing volume package; salvage aborted\n");
355 Exit(1);
356 }
357
358 /* defer lock until we init volume package */
359 if (get_salvage_lock) {
360 if (seenvol && AskDAFS()) /* support forceDAFS */
361 ObtainSharedSalvageLock();
362 else
363 ObtainSalvageLock();
364 }
365
366 /*
367 * Ok to defer this as Exit will clean up and no real work is done
368 * init'ing volume package
369 */
370 if (seenvol) {
371 char *msg = NULL;
372 #ifdef AFS_DEMAND_ATTACH_FS
373 if (!AskDAFS()) {
374 msg =
375 "The DAFS dasalvager cannot be run with a non-DAFS fileserver. Please use 'salvager'.";
376 }
377 if (!msg && !as->parms[20].items) {
378 msg =
379 "The standalone salvager cannot be run concurrently with a Demand Attach Fileserver. Please use 'salvageserver -client <partition> <volume id>' to manually schedule volume salvages with the salvageserver (new versions of 'bos salvage' automatically do this for you). Or, if you insist on using the standalone salvager, add the -forceDAFS flag to your salvager command line.";
380 }
381 #else
382 if (AskDAFS()) {
383 msg =
384 "The non-DAFS salvager cannot be run with a Demand Attach Fileserver. Please use 'salvageserver -client <partition> <volume id>' to manually schedule volume salvages with the salvageserver (new versions of 'bos salvage' automatically do this for you). Or, if you insist on using the standalone salvager, run dasalvager with the -forceDAFS flag.";
385 }
386 #endif
387
388 if (msg) {
389 Log("%s\n", msg);
390 printf("%s\n", msg);
391 Exit(1);
392 }
393 }
394
395 DInit(10);
396 #ifdef AFS_NT40_ENV
397 if (myjob.cj_number != NOT_CHILD) {
398 if (!seenpart) {
399 seenpart = 1;
400 (void)strcpy(pname, myjob.cj_part);
401 }
402 }
403 #endif
404 if (seenpart == 0) {
405 for (partP = DiskPartitionList; partP; partP = partP->next) {
406 SalvageFileSysParallel(partP);
407 }
408 SalvageFileSysParallel(0);
409 } else {
410 partP = VGetPartition(pname, 0);
411 if (!partP) {
412 Log("salvage: Unknown or unmounted partition %s; salvage aborted\n", pname);
413 Exit(1);
414 }
415 if (!seenvol)
416 SalvageFileSys(partP, 0);
417 else {
418 /* Salvage individual volume */
419 SalvageFileSys(partP, vid);
420 }
421 }
422 return (0);
423 }
424
425
426 #ifndef AFS_NT40_ENV
427 #include "AFS_component_version_number.c"
428 #endif
429
430 int
431 main(int argc, char **argv)
432 {
433 struct CmdLine cmdline;
434 struct cmd_syndesc *ts;
435 int err = 0;
436
437 #ifdef AFS_AIX32_ENV
438 /*
439 * The following signal action for AIX is necessary so that in case of a
440 * crash (i.e. core is generated) we can include the user's data section
441 * in the core dump. Unfortunately, by default, only a partial core is
442 * generated which, in many cases, isn't too useful.
443 */
444 struct sigaction nsa;
445
446 sigemptyset(&nsa.sa_mask);
447 nsa.sa_handler = SIG_DFL;
448 nsa.sa_flags = SA_FULLDUMP;
449 sigaction(SIGABRT, &nsa, NULL);
450 sigaction(SIGSEGV, &nsa, NULL);
451 #endif
452
453 /* Initialize directory paths */
454 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
455 #ifdef AFS_NT40_ENV
456 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
457 #endif
458 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
459 argv[0]);
460 exit(2);
461 }
462 #ifdef AFS_NT40_ENV
463 /* Default to binary mode for fopen() */
464 _set_fmode(_O_BINARY);
465
466 main_thread = pthread_self();
467 if (spawnDatap && spawnDataLen) {
468 /* This is a child per partition salvager. Don't setup log or
469 * try to lock the salvager lock.
470 */
471 if (nt_SetupPartitionSalvage(spawnDatap, spawnDataLen) < 0)
472 exit(3);
473 } else {
474 #endif
475
476 #ifndef AFS_NT40_ENV
477 if (geteuid() != 0) {
478 printf("Salvager must be run as root.\n");
479 fflush(stdout);
480 Exit(0);
481 }
482 #endif
483
484 /* Get and hold a lock for the duration of the salvage to make sure
485 * that no other salvage runs at the same time. The routine
486 * VInitVolumePackage2 (called below) makes sure that a file server or
487 * other volume utilities don't interfere with the salvage.
488 */
489 get_salvage_lock = 1;
490 #ifdef AFS_NT40_ENV
491 }
492 #endif
493
494 cmdline.argc = argc;
495 cmdline.argv = argv;
496 ts = cmd_CreateSyntax("initcmd", handleit, &cmdline, 0, "initialize the program");
497 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL,
498 "Name of partition to salvage");
499 cmd_AddParm(ts, "-volumeid", CMD_SINGLE, CMD_OPTIONAL,
500 "Volume Id to salvage");
501 cmd_AddParm(ts, "-debug", CMD_FLAG, CMD_OPTIONAL,
502 "Run in Debugging mode");
503 cmd_AddParm(ts, "-nowrite", CMD_FLAG, CMD_OPTIONAL,
504 "Run readonly/test mode");
505 cmd_AddParm(ts, "-inodes", CMD_FLAG, CMD_OPTIONAL,
506 "Just list affected afs inodes - debugging flag");
507 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "Force full salvaging");
508 cmd_AddParm(ts, "-oktozap", CMD_FLAG, CMD_OPTIONAL,
509 "Give permission to destroy bogus inodes/volumes - debugging flag");
510 cmd_AddParm(ts, "-rootinodes", CMD_FLAG, CMD_OPTIONAL,
511 "Show inodes owned by root - debugging flag");
512 cmd_AddParm(ts, "-salvagedirs", CMD_FLAG, CMD_OPTIONAL,
513 "Force rebuild/salvage of all directories");
514 cmd_AddParm(ts, "-blockreads", CMD_FLAG, CMD_OPTIONAL,
515 "Read smaller blocks to handle IO/bad blocks");
516 cmd_AddParm(ts, "-parallel", CMD_SINGLE, CMD_OPTIONAL,
517 "# of max parallel partition salvaging");
518 cmd_AddParm(ts, "-tmpdir", CMD_SINGLE, CMD_OPTIONAL,
519 "Name of dir to place tmp files ");
520 cmd_AddParm(ts, "-showlog", CMD_FLAG, CMD_OPTIONAL,
521 "Show log file upon completion");
522 cmd_AddParm(ts, "-showsuid", CMD_FLAG, CMD_OPTIONAL,
523 "Report on suid/sgid files");
524 cmd_AddParm(ts, "-showmounts", CMD_FLAG, CMD_OPTIONAL,
525 "Report on mountpoints");
526 cmd_AddParm(ts, "-orphans", CMD_SINGLE, CMD_OPTIONAL,
527 "ignore | remove | attach");
528
529 /* note - syslog isn't avail on NT, but if we make it conditional, have
530 * to deal with screwy offsets for cmd params */
531 cmd_AddParm(ts, "-syslog", CMD_FLAG, CMD_OPTIONAL,
532 "Write salvage log to syslogs");
533 cmd_AddParm(ts, "-syslogfacility", CMD_SINGLE, CMD_OPTIONAL,
534 "Syslog facility number to use");
535 cmd_AddParm(ts, "-datelogs", CMD_FLAG, CMD_OPTIONAL,
536 "Include timestamp in logfile filename");
537 #ifdef FAST_RESTART
538 cmd_AddParm(ts, "-DontSalvage", CMD_FLAG, CMD_OPTIONAL,
539 "Don't salvage. This my be set in BosConfig to let the fileserver restart immediately after a crash. Bad volumes will be taken offline");
540 #elif defined(AFS_DEMAND_ATTACH_FS)
541 cmd_Seek(ts, 20); /* skip DontSalvage */
542 cmd_AddParm(ts, "-forceDAFS", CMD_FLAG, CMD_OPTIONAL,
543 "For Demand Attach Fileserver, permit a manual volume salvage outside of the salvageserver");
544 #endif /* FAST_RESTART */
545 cmd_Seek(ts, 21); /* skip DontSalvage and forceDAFS if needed */
546 cmd_AddParm(ts, "-f", CMD_FLAG, CMD_OPTIONAL, "Alias for -force");
547 err = cmd_Dispatch(argc, argv);
548 Exit(err);
549 return 0; /* not reached */
550 }
551