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 */
54 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_XBSD_ENV) && !defined(AFS_DARWIN_ENV)
55 #include <sys/inode.h>
57 #endif /* AFS_VFSINCL_ENV */
58 #endif /* AFS_SGI_ENV */
61 #include <sys/lockf.h>
64 #include <checklist.h>
66 #if defined(AFS_SGI_ENV)
69 #if defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
71 #include <sys/mnttab.h>
72 #include <sys/mntent.h>
77 #endif /* AFS_SGI_ENV */
78 #endif /* AFS_HPUX_ENV */
82 #include <afs/osi_inode.h>
86 #include <afs/afsutil.h>
87 #include <afs/fileutil.h>
88 #include <rx/rx_queue.h>
93 #include <afs/afssyscalls.h>
97 #include "partition.h"
98 #include "daemon_com.h"
100 #include "salvsync.h"
101 #include "viceinode.h"
103 #include "vol-salvage.h"
107 pthread_t main_thread;
110 extern char cml_version_number[];
111 static int get_salvage_lock = 0;
119 TimeStampLogFile(char **logfile)
126 lt = localtime(&now);
127 if (asprintf(&stampSlvgLog,
128 "%s.%04d-%02d-%02d.%02d:%02d:%02d",
129 AFSDIR_SERVER_SLVGLOG_FILEPATH,
130 lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour,
131 lt->tm_min, lt->tm_sec) < 0) {
134 *logfile = stampSlvgLog;
139 handleit(struct cmd_syndesc *as, void *arock)
141 struct CmdLine *cmdline = (struct CmdLine*)arock;
143 char pname[100], *temp;
144 afs_int32 seenpart = 0, seenvol = 0;
149 afs_int32 seenany = 0;
152 char *filename = NULL;
153 struct logOptions logopts;
154 VolumePackageOptions opts;
155 struct DiskPartition64 *partP;
157 memset(&logopts, 0, sizeof(logopts));
159 #ifdef AFS_SGI_VNODE_GLUE
160 if (afs_init_kernel_config(-1) < 0) {
162 ("Can't determine NUMA configuration, not starting salvager.\n");
170 for (i = 0; i < CMD_MAXPARMS; i++) {
171 if (as->parms[i].items) {
179 ("Exiting immediately without salvage. "
180 "Look into the FileLog to find volumes which really need to be salvaged!\n");
183 #endif /* FAST_RESTART */
184 if ((ti = as->parms[0].items)) { /* -partition */
186 strncpy(pname, ti->data, 100);
188 if ((ti = as->parms[1].items)) { /* -volumeid */
193 ("You must also specify '-partition' option with the '-volumeid' option\n");
197 vid_l = strtoul(ti->data, &end, 10);
198 if (vid_l >= MAX_AFS_UINT32 || vid_l == ULONG_MAX || *end != '\0') {
199 fprintf(stderr, "salvage: invalid volume id specified; salvage aborted\n");
202 vid = (VolumeId)vid_l;
204 if (as->parms[2].items) /* -debug */
206 if (as->parms[3].items) /* -nowrite */
208 if (as->parms[4].items) /* -inodes */
210 if (as->parms[5].items || as->parms[21].items) /* -force, -f */
212 if (as->parms[6].items) /* -oktozap */
214 if (as->parms[7].items) /* -rootinodes */
216 if (as->parms[8].items) /* -RebuildDirs */
218 if (as->parms[9].items) /* -ForceReads */
220 if ((ti = as->parms[10].items)) { /* -Parallel # */
222 if (strncmp(temp, "all", 3) == 0) {
226 if (strlen(temp) != 0) {
227 Parallel = atoi(temp);
230 if (Parallel > MAXPARALLEL) {
231 printf("Setting parallel salvages to maximum of %d \n",
233 Parallel = MAXPARALLEL;
237 if ((ti = as->parms[11].items)) { /* -tmpdir */
241 dirp = opendir(tmpdir);
244 ("Can't open temporary placeholder dir %s; using current partition \n",
250 if ((ti = as->parms[12].items)) /* -showlog */
252 if ((ti = as->parms[13].items)) { /* -showsuid */
257 if ((ti = as->parms[14].items)) { /* -showmounts */
262 if ((ti = as->parms[15].items)) { /* -orphans */
264 orphans = ORPH_IGNORE;
265 else if (strcmp(ti->data, "remove") == 0
266 || strcmp(ti->data, "r") == 0)
267 orphans = ORPH_REMOVE;
268 else if (strcmp(ti->data, "attach") == 0
269 || strcmp(ti->data, "a") == 0)
270 orphans = ORPH_ATTACH;
273 if ((ti = as->parms[16].items)) { /* -syslog */
275 fprintf(stderr, "Invalid options: -syslog and -showlog are exclusive.\n");
278 if ((ti = as->parms[18].items)) { /* -datelogs */
279 fprintf(stderr, "Invalid option: -syslog and -datelogs are exclusive.\n");
283 /* Do not silently ignore. */
284 fprintf(stderr, "Invalid option: -syslog is not available on this platform.\n");
287 logopts.lopt_dest = logDest_syslog;
288 logopts.lopt_tag = "salvager";
290 if ((ti = as->parms[17].items)) /* -syslogfacility */
291 logopts.lopt_facility = atoi(ti->data);
293 logopts.lopt_facility = LOG_DAEMON; /* default value */
296 logopts.lopt_dest = logDest_file;
298 if ((ti = as->parms[18].items)) { /* -datelogs */
299 int code = TimeStampLogFile(&filename);
301 fprintf(stderr, "Failed to format log file name for -datelogs; code=%d\n", code);
304 logopts.lopt_filename = filename;
306 logopts.lopt_filename = AFSDIR_SERVER_SLVGLOG_FILEPATH;
312 free(filename); /* Free string created by -datelogs, if one. */
314 Log("%s\n", cml_version_number);
315 LogCommandLine(cmdline->argc, cmdline->argv, "SALVAGER", SalvageVersion, "STARTING AFS", Log);
318 if (ti = as->parms[19].items) { /* -DontSalvage */
320 "Exiting immediately without salvage. Look into the FileLog to find volumes which really need to be salvaged!";
327 /* Note: if seenvol we initialize this as a standard volume utility: this has the
328 * implication that the file server may be running; negotations have to be made with
329 * the file server in this case to take the read write volume and associated read-only
330 * volumes off line before salvaging */
333 if (afs_winsockInit() < 0) {
334 ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0,
335 AFSDIR_SALVAGER_FILE, 0);
336 Log("Failed to initailize winsock, exiting.\n");
348 VOptDefaults(pt, &opts);
349 if (VInitVolumePackage2(pt, &opts)) {
350 Log("errors encountered initializing volume package; salvage aborted\n");
354 /* defer lock until we init volume package */
355 if (get_salvage_lock) {
356 if (seenvol && AskDAFS()) /* support forceDAFS */
357 ObtainSharedSalvageLock();
363 * Ok to defer this as Exit will clean up and no real work is done
364 * init'ing volume package
368 #ifdef AFS_DEMAND_ATTACH_FS
371 "The DAFS dasalvager cannot be run with a non-DAFS fileserver. Please use 'salvager'.";
373 if (!msg && !as->parms[20].items) {
375 "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 "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.";
393 if (myjob.cj_number != NOT_CHILD) {
396 (void)strcpy(pname, myjob.cj_part);
401 for (partP = DiskPartitionList; partP; partP = partP->next) {
402 SalvageFileSysParallel(partP);
404 SalvageFileSysParallel(0);
406 partP = VGetPartition(pname, 0);
408 Log("salvage: Unknown or unmounted partition %s; salvage aborted\n", pname);
412 SalvageFileSys(partP, 0);
414 /* Salvage individual volume */
415 SalvageFileSys(partP, vid);
423 #include "AFS_component_version_number.c"
427 main(int argc, char **argv)
429 struct CmdLine cmdline;
430 struct cmd_syndesc *ts;
435 * The following signal action for AIX is necessary so that in case of a
436 * crash (i.e. core is generated) we can include the user's data section
437 * in the core dump. Unfortunately, by default, only a partial core is
438 * generated which, in many cases, isn't too useful.
440 struct sigaction nsa;
442 sigemptyset(&nsa.sa_mask);
443 nsa.sa_handler = SIG_DFL;
444 nsa.sa_flags = SA_FULLDUMP;
445 sigaction(SIGABRT, &nsa, NULL);
446 sigaction(SIGSEGV, &nsa, NULL);
449 /* Initialize directory paths */
450 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
452 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
454 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
459 /* Default to binary mode for fopen() */
460 _set_fmode(_O_BINARY);
462 main_thread = pthread_self();
463 if (spawnDatap && spawnDataLen) {
464 /* This is a child per partition salvager. Don't setup log or
465 * try to lock the salvager lock.
467 if (nt_SetupPartitionSalvage(spawnDatap, spawnDataLen) < 0)
473 if (geteuid() != 0) {
474 printf("Salvager must be run as root.\n");
480 /* Get and hold a lock for the duration of the salvage to make sure
481 * that no other salvage runs at the same time. The routine
482 * VInitVolumePackage2 (called below) makes sure that a file server or
483 * other volume utilities don't interfere with the salvage.
485 get_salvage_lock = 1;
492 ts = cmd_CreateSyntax("initcmd", handleit, &cmdline, 0, "initialize the program");
493 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL,
494 "Name of partition to salvage");
495 cmd_AddParm(ts, "-volumeid", CMD_SINGLE, CMD_OPTIONAL,
496 "Volume Id to salvage");
497 cmd_AddParm(ts, "-debug", CMD_FLAG, CMD_OPTIONAL,
498 "Run in Debugging mode");
499 cmd_AddParm(ts, "-nowrite", CMD_FLAG, CMD_OPTIONAL,
500 "Run readonly/test mode");
501 cmd_AddParm(ts, "-inodes", CMD_FLAG, CMD_OPTIONAL,
502 "Just list affected afs inodes - debugging flag");
503 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "Force full salvaging");
504 cmd_AddParm(ts, "-oktozap", CMD_FLAG, CMD_OPTIONAL,
505 "Give permission to destroy bogus inodes/volumes - debugging flag");
506 cmd_AddParm(ts, "-rootinodes", CMD_FLAG, CMD_OPTIONAL,
507 "Show inodes owned by root - debugging flag");
508 cmd_AddParm(ts, "-salvagedirs", CMD_FLAG, CMD_OPTIONAL,
509 "Force rebuild/salvage of all directories");
510 cmd_AddParm(ts, "-blockreads", CMD_FLAG, CMD_OPTIONAL,
511 "Read smaller blocks to handle IO/bad blocks");
512 cmd_AddParm(ts, "-parallel", CMD_SINGLE, CMD_OPTIONAL,
513 "# of max parallel partition salvaging");
514 cmd_AddParm(ts, "-tmpdir", CMD_SINGLE, CMD_OPTIONAL,
515 "Name of dir to place tmp files ");
516 cmd_AddParm(ts, "-showlog", CMD_FLAG, CMD_OPTIONAL,
517 "Show log file upon completion");
518 cmd_AddParm(ts, "-showsuid", CMD_FLAG, CMD_OPTIONAL,
519 "Report on suid/sgid files");
520 cmd_AddParm(ts, "-showmounts", CMD_FLAG, CMD_OPTIONAL,
521 "Report on mountpoints");
522 cmd_AddParm(ts, "-orphans", CMD_SINGLE, CMD_OPTIONAL,
523 "ignore | remove | attach");
525 /* note - syslog isn't avail on NT, but if we make it conditional, have
526 * to deal with screwy offsets for cmd params */
527 cmd_AddParm(ts, "-syslog", CMD_FLAG, CMD_OPTIONAL,
528 "Write salvage log to syslogs");
529 cmd_AddParm(ts, "-syslogfacility", CMD_SINGLE, CMD_OPTIONAL,
530 "Syslog facility number to use");
531 cmd_AddParm(ts, "-datelogs", CMD_FLAG, CMD_OPTIONAL,
532 "Include timestamp in logfile filename");
534 cmd_AddParm(ts, "-DontSalvage", CMD_FLAG, CMD_OPTIONAL,
535 "Don't salvage. This my be set in BosConfig to let the fileserver restart immediately after a crash. Bad volumes will be taken offline");
536 #elif defined(AFS_DEMAND_ATTACH_FS)
537 cmd_Seek(ts, 20); /* skip DontSalvage */
538 cmd_AddParm(ts, "-forceDAFS", CMD_FLAG, CMD_OPTIONAL,
539 "For Demand Attach Fileserver, permit a manual volume salvage outside of the salvageserver");
540 #endif /* FAST_RESTART */
541 cmd_Seek(ts, 21); /* skip DontSalvage and forceDAFS if needed */
542 cmd_AddParm(ts, "-f", CMD_FLAG, CMD_OPTIONAL, "Alias for -force");
543 err = cmd_Dispatch(argc, argv);
545 AFS_UNREACHED(return 0);