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 cml_version_number
[];
115 static int get_salvage_lock
= 0;
123 TimeStampLogFile(char **logfile
)
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) {
138 *logfile
= stampSlvgLog
;
143 handleit(struct cmd_syndesc
*as
, void *arock
)
145 struct CmdLine
*cmdline
= (struct CmdLine
*)arock
;
147 char pname
[100], *temp
;
148 afs_int32 seenpart
= 0, seenvol
= 0;
153 afs_int32 seenany
= 0;
156 char *filename
= NULL
;
157 struct logOptions logopts
;
158 VolumePackageOptions opts
;
159 struct DiskPartition64
*partP
;
161 memset(&logopts
, 0, sizeof(logopts
));
163 #ifdef AFS_SGI_VNODE_GLUE
164 if (afs_init_kernel_config(-1) < 0) {
166 ("Can't determine NUMA configuration, not starting salvager.\n");
174 for (i
= 0; i
< CMD_MAXPARMS
; i
++) {
175 if (as
->parms
[i
].items
) {
183 ("Exiting immediately without salvage. "
184 "Look into the FileLog to find volumes which really need to be salvaged!\n");
187 #endif /* FAST_RESTART */
188 if ((ti
= as
->parms
[0].items
)) { /* -partition */
190 strncpy(pname
, ti
->data
, 100);
192 if ((ti
= as
->parms
[1].items
)) { /* -volumeid */
197 ("You must also specify '-partition' option with the '-volumeid' option\n");
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");
206 vid
= (VolumeId
)vid_l
;
208 if (as
->parms
[2].items
) /* -debug */
210 if (as
->parms
[3].items
) /* -nowrite */
212 if (as
->parms
[4].items
) /* -inodes */
214 if (as
->parms
[5].items
|| as
->parms
[21].items
) /* -force, -f */
216 if (as
->parms
[6].items
) /* -oktozap */
218 if (as
->parms
[7].items
) /* -rootinodes */
220 if (as
->parms
[8].items
) /* -RebuildDirs */
222 if (as
->parms
[9].items
) /* -ForceReads */
224 if ((ti
= as
->parms
[10].items
)) { /* -Parallel # */
226 if (strncmp(temp
, "all", 3) == 0) {
230 if (strlen(temp
) != 0) {
231 Parallel
= atoi(temp
);
234 if (Parallel
> MAXPARALLEL
) {
235 printf("Setting parallel salvages to maximum of %d \n",
237 Parallel
= MAXPARALLEL
;
241 if ((ti
= as
->parms
[11].items
)) { /* -tmpdir */
245 dirp
= opendir(tmpdir
);
248 ("Can't open temporary placeholder dir %s; using current partition \n",
254 if ((ti
= as
->parms
[12].items
)) /* -showlog */
256 if ((ti
= as
->parms
[13].items
)) { /* -showsuid */
261 if ((ti
= as
->parms
[14].items
)) { /* -showmounts */
266 if ((ti
= as
->parms
[15].items
)) { /* -orphans */
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
;
277 if ((ti
= as
->parms
[16].items
)) { /* -syslog */
279 fprintf(stderr
, "Invalid options: -syslog and -showlog are exclusive.\n");
282 if ((ti
= as
->parms
[18].items
)) { /* -datelogs */
283 fprintf(stderr
, "Invalid option: -syslog and -datelogs are exclusive.\n");
287 /* Do not silently ignore. */
288 fprintf(stderr
, "Invalid option: -syslog is not available on this platform.\n");
291 logopts
.lopt_dest
= logDest_syslog
;
292 logopts
.lopt_tag
= "salvager";
294 if ((ti
= as
->parms
[17].items
)) /* -syslogfacility */
295 logopts
.lopt_facility
= atoi(ti
->data
);
297 logopts
.lopt_facility
= LOG_DAEMON
; /* default value */
300 logopts
.lopt_dest
= logDest_file
;
302 if ((ti
= as
->parms
[18].items
)) { /* -datelogs */
303 int code
= TimeStampLogFile(&filename
);
305 fprintf(stderr
, "Failed to format log file name for -datelogs; code=%d\n", code
);
308 logopts
.lopt_filename
= filename
;
310 logopts
.lopt_filename
= AFSDIR_SERVER_SLVGLOG_FILEPATH
;
316 free(filename
); /* Free string created by -datelogs, if one. */
318 Log("%s\n", cml_version_number
);
319 LogCommandLine(cmdline
->argc
, cmdline
->argv
, "SALVAGER", SalvageVersion
, "STARTING AFS", Log
);
322 if (ti
= as
->parms
[19].items
) { /* -DontSalvage */
324 "Exiting immediately without salvage. Look into the FileLog to find volumes which really need to be salvaged!";
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 */
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");
352 VOptDefaults(pt
, &opts
);
353 if (VInitVolumePackage2(pt
, &opts
)) {
354 Log("errors encountered initializing volume package; salvage aborted\n");
358 /* defer lock until we init volume package */
359 if (get_salvage_lock
) {
360 if (seenvol
&& AskDAFS()) /* support forceDAFS */
361 ObtainSharedSalvageLock();
367 * Ok to defer this as Exit will clean up and no real work is done
368 * init'ing volume package
372 #ifdef AFS_DEMAND_ATTACH_FS
375 "The DAFS dasalvager cannot be run with a non-DAFS fileserver. Please use 'salvager'.";
377 if (!msg
&& !as
->parms
[20].items
) {
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.";
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.";
397 if (myjob
.cj_number
!= NOT_CHILD
) {
400 (void)strcpy(pname
, myjob
.cj_part
);
405 for (partP
= DiskPartitionList
; partP
; partP
= partP
->next
) {
406 SalvageFileSysParallel(partP
);
408 SalvageFileSysParallel(0);
410 partP
= VGetPartition(pname
, 0);
412 Log("salvage: Unknown or unmounted partition %s; salvage aborted\n", pname
);
416 SalvageFileSys(partP
, 0);
418 /* Salvage individual volume */
419 SalvageFileSys(partP
, vid
);
427 #include "AFS_component_version_number.c"
431 main(int argc
, char **argv
)
433 struct CmdLine cmdline
;
434 struct cmd_syndesc
*ts
;
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.
444 struct sigaction nsa
;
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
);
453 /* Initialize directory paths */
454 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK
)) {
456 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR
, 0, argv
[0], 0);
458 fprintf(stderr
, "%s: Unable to obtain AFS server directory.\n",
463 /* Default to binary mode for fopen() */
464 _set_fmode(_O_BINARY
);
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.
471 if (nt_SetupPartitionSalvage(spawnDatap
, spawnDataLen
) < 0)
477 if (geteuid() != 0) {
478 printf("Salvager must be run as root.\n");
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.
489 get_salvage_lock
= 1;
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");
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");
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
);
549 return 0; /* not reached */