2 * Copyright 2000, International Business Machines Corporation 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
13 * Institution: The Information Technology Center, Carnegie-Mellon University
17 /* Main program file. Define globals. */
20 #include <afsconfig.h>
21 #include <afs/param.h>
23 #include <afs/procmgmt.h>
26 #ifdef HAVE_SYS_FILE_H
31 #include <WINNT/afsevent.h>
35 #define WCOREDUMP(x) ((x) & 0200)
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>
44 #include <sys/fs/ufs_inode.h>
46 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
47 #include <ufs/ufs/dinode.h>
48 #include <ufs/ffs/fs.h>
50 #include <ufs/inode.h>
53 #else /* AFS_VFSINCL_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>
61 #endif /* AFS_VFSINCL_ENV */
62 #endif /* AFS_SGI_ENV */
65 #include <sys/lockf.h>
68 #include <checklist.h>
70 #if defined(AFS_SGI_ENV)
73 #if defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
75 #include <sys/mnttab.h>
76 #include <sys/mntent.h>
81 #endif /* AFS_SGI_ENV */
82 #endif /* AFS_HPUX_ENV */
86 #include <afs/osi_inode.h>
90 #include <afs/afsutil.h>
91 #include <afs/fileutil.h>
92 #include <rx/rx_queue.h>
97 #include <afs/afssyscalls.h>
101 #include "partition.h"
102 #include "daemon_com.h"
104 #include "salvsync.h"
105 #include "viceinode.h"
107 #include "vol-salvage.h"
111 pthread_t main_thread;
114 extern char *ShowLogFilename;
115 extern char cml_version_number[];
116 static int get_salvage_lock = 0;
125 TimeStampLogFile(char **logfile)
132 lt = localtime(&now);
133 if (asprintf(&stampSlvgLog,
134 "%s.%04d-%02d-%02d.%02d:%02d:%02d",
135 AFSDIR_SERVER_SLVGLOG_FILEPATH,
136 lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour,
137 lt->tm_min, lt->tm_sec) < 0) {
140 free(*logfile); /* free the default name */
141 *logfile = stampSlvgLog;
147 handleit(struct cmd_syndesc *as, void *arock)
149 struct CmdLine *cmdline = (struct CmdLine*)arock;
151 char pname[100], *temp;
152 afs_int32 seenpart = 0, seenvol = 0;
155 char *logfile = strdup(AFSDIR_SERVER_SLVGLOG_FILEPATH);
158 afs_int32 seenany = 0;
161 VolumePackageOptions opts;
162 struct DiskPartition64 *partP;
164 #ifdef AFS_SGI_VNODE_GLUE
165 if (afs_init_kernel_config(-1) < 0) {
167 ("Can't determine NUMA configuration, not starting salvager.\n");
175 for (i = 0; i < CMD_MAXPARMS; i++) {
176 if (as->parms[i].items) {
184 ("Exiting immediately without salvage. "
185 "Look into the FileLog to find volumes which really need to be salvaged!\n");
188 #endif /* FAST_RESTART */
189 if ((ti = as->parms[0].items)) { /* -partition */
191 strncpy(pname, ti->data, 100);
193 if ((ti = as->parms[1].items)) { /* -volumeid */
198 ("You must also specify '-partition' option with the '-volumeid' option\n");
202 vid_l = strtoul(ti->data, &end, 10);
203 if (vid_l >= MAX_AFS_UINT32 || vid_l == ULONG_MAX || *end != '\0') {
204 Log("salvage: invalid volume id specified; salvage aborted\n");
207 vid = (VolumeId)vid_l;
209 if (as->parms[2].items) /* -debug */
211 if (as->parms[3].items) /* -nowrite */
213 if (as->parms[4].items) /* -inodes */
215 if (as->parms[5].items || as->parms[21].items) /* -force, -f */
217 if (as->parms[6].items) /* -oktozap */
219 if (as->parms[7].items) /* -rootinodes */
221 if (as->parms[8].items) /* -RebuildDirs */
223 if (as->parms[9].items) /* -ForceReads */
225 if ((ti = as->parms[10].items)) { /* -Parallel # */
227 if (strncmp(temp, "all", 3) == 0) {
231 if (strlen(temp) != 0) {
232 Parallel = atoi(temp);
235 if (Parallel > MAXPARALLEL) {
236 printf("Setting parallel salvages to maximum of %d \n",
238 Parallel = MAXPARALLEL;
242 if ((ti = as->parms[11].items)) { /* -tmpdir */
246 dirp = opendir(tmpdir);
249 ("Can't open temporary placeholder dir %s; using current partition \n",
255 if ((ti = as->parms[12].items)) /* -showlog */
257 if ((ti = as->parms[13].items)) { /* -showsuid */
262 if ((ti = as->parms[14].items)) { /* -showmounts */
267 if ((ti = as->parms[15].items)) { /* -orphans */
269 orphans = ORPH_IGNORE;
270 else if (strcmp(ti->data, "remove") == 0
271 || strcmp(ti->data, "r") == 0)
272 orphans = ORPH_REMOVE;
273 else if (strcmp(ti->data, "attach") == 0
274 || strcmp(ti->data, "a") == 0)
275 orphans = ORPH_ATTACH;
277 #ifndef AFS_NT40_ENV /* ignore options on NT */
278 if ((ti = as->parms[16].items)) { /* -syslog */
280 fprintf(stderr, "Invalid options: -syslog and -showlog are exclusive.\n");
285 if ((ti = as->parms[17].items)) { /* -syslogfacility */
286 serverLogSyslogFacility = atoi(ti->data);
289 if ((ti = as->parms[18].items)) { /* -datelogs */
290 int code = TimeStampLogFile(&logfile);
292 fprintf(stderr, "Failed to format log file name for -datelogs; code=%d\n", code);
295 ShowLogFilename = logfile;
302 Log("%s\n", cml_version_number);
303 LogCommandLine(cmdline->argc, cmdline->argv, "SALVAGER", SalvageVersion, "STARTING AFS", Log);
306 if (ti = as->parms[19].items) { /* -DontSalvage */
308 "Exiting immediately without salvage. Look into the FileLog to find volumes which really need to be salvaged!";
315 /* Note: if seenvol we initialize this as a standard volume utility: this has the
316 * implication that the file server may be running; negotations have to be made with
317 * the file server in this case to take the read write volume and associated read-only
318 * volumes off line before salvaging */
321 if (afs_winsockInit() < 0) {
322 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0,
323 AFSDIR_SALVAGER_FILE, 0);
324 Log("Failed to initailize winsock, exiting.\n");
336 VOptDefaults(pt, &opts);
337 if (VInitVolumePackage2(pt, &opts)) {
338 Log("errors encountered initializing volume package; salvage aborted\n");
342 /* defer lock until we init volume package */
343 if (get_salvage_lock) {
344 if (seenvol && AskDAFS()) /* support forceDAFS */
345 ObtainSharedSalvageLock();
351 * Ok to defer this as Exit will clean up and no real work is done
352 * init'ing volume package
356 #ifdef AFS_DEMAND_ATTACH_FS
359 "The DAFS dasalvager cannot be run with a non-DAFS fileserver. Please use 'salvager'.";
361 if (!msg && !as->parms[20].items) {
363 "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.";
368 "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.";
381 if (myjob.cj_number != NOT_CHILD) {
384 (void)strcpy(pname, myjob.cj_part);
389 for (partP = DiskPartitionList; partP; partP = partP->next) {
390 SalvageFileSysParallel(partP);
392 SalvageFileSysParallel(0);
394 partP = VGetPartition(pname, 0);
396 Log("salvage: Unknown or unmounted partition %s; salvage aborted\n", pname);
400 SalvageFileSys(partP, 0);
402 /* Salvage individual volume */
403 SalvageFileSys(partP, vid);
411 #include "AFS_component_version_number.c"
415 main(int argc, char **argv)
417 struct CmdLine cmdline;
418 struct cmd_syndesc *ts;
423 * The following signal action for AIX is necessary so that in case of a
424 * crash (i.e. core is generated) we can include the user's data section
425 * in the core dump. Unfortunately, by default, only a partial core is
426 * generated which, in many cases, isn't too useful.
428 struct sigaction nsa;
430 sigemptyset(&nsa.sa_mask);
431 nsa.sa_handler = SIG_DFL;
432 nsa.sa_flags = SA_FULLDUMP;
433 sigaction(SIGABRT, &nsa, NULL);
434 sigaction(SIGSEGV, &nsa, NULL);
437 /* Initialize directory paths */
438 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
440 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
442 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
447 /* Default to binary mode for fopen() */
448 _set_fmode(_O_BINARY);
450 main_thread = pthread_self();
451 if (spawnDatap && spawnDataLen) {
452 /* This is a child per partition salvager. Don't setup log or
453 * try to lock the salvager lock.
455 if (nt_SetupPartitionSalvage(spawnDatap, spawnDataLen) < 0)
461 if (geteuid() != 0) {
462 printf("Salvager must be run as root.\n");
468 /* Get and hold a lock for the duration of the salvage to make sure
469 * that no other salvage runs at the same time. The routine
470 * VInitVolumePackage2 (called below) makes sure that a file server or
471 * other volume utilities don't interfere with the salvage.
473 get_salvage_lock = 1;
480 ts = cmd_CreateSyntax("initcmd", handleit, &cmdline, 0, "initialize the program");
481 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL,
482 "Name of partition to salvage");
483 cmd_AddParm(ts, "-volumeid", CMD_SINGLE, CMD_OPTIONAL,
484 "Volume Id to salvage");
485 cmd_AddParm(ts, "-debug", CMD_FLAG, CMD_OPTIONAL,
486 "Run in Debugging mode");
487 cmd_AddParm(ts, "-nowrite", CMD_FLAG, CMD_OPTIONAL,
488 "Run readonly/test mode");
489 cmd_AddParm(ts, "-inodes", CMD_FLAG, CMD_OPTIONAL,
490 "Just list affected afs inodes - debugging flag");
491 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "Force full salvaging");
492 cmd_AddParm(ts, "-oktozap", CMD_FLAG, CMD_OPTIONAL,
493 "Give permission to destroy bogus inodes/volumes - debugging flag");
494 cmd_AddParm(ts, "-rootinodes", CMD_FLAG, CMD_OPTIONAL,
495 "Show inodes owned by root - debugging flag");
496 cmd_AddParm(ts, "-salvagedirs", CMD_FLAG, CMD_OPTIONAL,
497 "Force rebuild/salvage of all directories");
498 cmd_AddParm(ts, "-blockreads", CMD_FLAG, CMD_OPTIONAL,
499 "Read smaller blocks to handle IO/bad blocks");
500 cmd_AddParm(ts, "-parallel", CMD_SINGLE, CMD_OPTIONAL,
501 "# of max parallel partition salvaging");
502 cmd_AddParm(ts, "-tmpdir", CMD_SINGLE, CMD_OPTIONAL,
503 "Name of dir to place tmp files ");
504 cmd_AddParm(ts, "-showlog", CMD_FLAG, CMD_OPTIONAL,
505 "Show log file upon completion");
506 cmd_AddParm(ts, "-showsuid", CMD_FLAG, CMD_OPTIONAL,
507 "Report on suid/sgid files");
508 cmd_AddParm(ts, "-showmounts", CMD_FLAG, CMD_OPTIONAL,
509 "Report on mountpoints");
510 cmd_AddParm(ts, "-orphans", CMD_SINGLE, CMD_OPTIONAL,
511 "ignore | remove | attach");
513 /* note - syslog isn't avail on NT, but if we make it conditional, have
514 * to deal with screwy offsets for cmd params */
515 cmd_AddParm(ts, "-syslog", CMD_FLAG, CMD_OPTIONAL,
516 "Write salvage log to syslogs");
517 cmd_AddParm(ts, "-syslogfacility", CMD_SINGLE, CMD_OPTIONAL,
518 "Syslog facility number to use");
519 cmd_AddParm(ts, "-datelogs", CMD_FLAG, CMD_OPTIONAL,
520 "Include timestamp in logfile filename");
522 cmd_AddParm(ts, "-DontSalvage", CMD_FLAG, CMD_OPTIONAL,
523 "Don't salvage. This my be set in BosConfig to let the fileserver restart immediately after a crash. Bad volumes will be taken offline");
524 #elif defined(AFS_DEMAND_ATTACH_FS)
525 cmd_Seek(ts, 20); /* skip DontSalvage */
526 cmd_AddParm(ts, "-forceDAFS", CMD_FLAG, CMD_OPTIONAL,
527 "For Demand Attach Fileserver, permit a manual volume salvage outside of the salvageserver");
528 #endif /* FAST_RESTART */
529 cmd_Seek(ts, 21); /* skip DontSalvage and forceDAFS if needed */
530 cmd_AddParm(ts, "-f", CMD_FLAG, CMD_OPTIONAL, "Alias for -force");
531 err = cmd_Dispatch(argc, argv);
533 return 0; /* not reached */