532e7b896c8a73559a028ddce004a269c7ce738e
[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 *ShowLogFilename;
115 extern char cml_version_number[];
116 static int get_salvage_lock = 0;
117
118 struct CmdLine {
119    int argc;
120    char **argv;
121 };
122
123 #ifndef AFS_NT40_ENV
124 static int
125 TimeStampLogFile(char **logfile)
126 {
127     char *stampSlvgLog;
128     struct tm *lt;
129     time_t now;
130
131     now = time(0);
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) {
138         return ENOMEM;
139     }
140     free(*logfile); /* free the default name */
141     *logfile = stampSlvgLog;
142     return 0;
143 }
144 #endif
145
146 static int
147 handleit(struct cmd_syndesc *as, void *arock)
148 {
149     struct CmdLine *cmdline = (struct CmdLine*)arock;
150     struct cmd_item *ti;
151     char pname[100], *temp;
152     afs_int32 seenpart = 0, seenvol = 0;
153     VolumeId vid = 0;
154     ProgramType pt;
155     char *logfile = strdup(AFSDIR_SERVER_SLVGLOG_FILEPATH);
156
157 #ifdef FAST_RESTART
158     afs_int32  seenany = 0;
159 #endif
160
161     VolumePackageOptions opts;
162     struct DiskPartition64 *partP;
163
164 #ifdef AFS_SGI_VNODE_GLUE
165     if (afs_init_kernel_config(-1) < 0) {
166         printf
167             ("Can't determine NUMA configuration, not starting salvager.\n");
168         exit(1);
169     }
170 #endif
171
172 #ifdef FAST_RESTART
173     {
174         afs_int32 i;
175         for (i = 0; i < CMD_MAXPARMS; i++) {
176             if (as->parms[i].items) {
177                 seenany = 1;
178                 break;
179             }
180         }
181     }
182     if (!seenany) {
183         printf
184             ("Exiting immediately without salvage. "
185              "Look into the FileLog to find volumes which really need to be salvaged!\n");
186         Exit(0);
187     }
188 #endif /* FAST_RESTART */
189     if ((ti = as->parms[0].items)) {    /* -partition */
190         seenpart = 1;
191         strncpy(pname, ti->data, 100);
192     }
193     if ((ti = as->parms[1].items)) {    /* -volumeid */
194         char *end;
195         unsigned long vid_l;
196         if (!seenpart) {
197             printf
198                 ("You must also specify '-partition' option with the '-volumeid' option\n");
199             exit(-1);
200         }
201         seenvol = 1;
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");
205             Exit(1);
206         }
207         vid = (VolumeId)vid_l;
208     }
209     if (as->parms[2].items)     /* -debug */
210         debug = 1;
211     if (as->parms[3].items)     /* -nowrite */
212         Testing = 1;
213     if (as->parms[4].items)     /* -inodes */
214         ListInodeOption = 1;
215     if (as->parms[5].items || as->parms[21].items)      /* -force, -f */
216         ForceSalvage = 1;
217     if (as->parms[6].items)     /* -oktozap */
218         OKToZap = 1;
219     if (as->parms[7].items)     /* -rootinodes */
220         ShowRootFiles = 1;
221     if (as->parms[8].items)     /* -RebuildDirs */
222         RebuildDirs = 1;
223     if (as->parms[9].items)     /* -ForceReads */
224         forceR = 1;
225     if ((ti = as->parms[10].items)) {   /* -Parallel # */
226         temp = ti->data;
227         if (strncmp(temp, "all", 3) == 0) {
228             PartsPerDisk = 1;
229             temp += 3;
230         }
231         if (strlen(temp) != 0) {
232             Parallel = atoi(temp);
233             if (Parallel < 1)
234                 Parallel = 1;
235             if (Parallel > MAXPARALLEL) {
236                 printf("Setting parallel salvages to maximum of %d \n",
237                        MAXPARALLEL);
238                 Parallel = MAXPARALLEL;
239             }
240         }
241     }
242     if ((ti = as->parms[11].items)) {   /* -tmpdir */
243         DIR *dirp;
244
245         tmpdir = ti->data;
246         dirp = opendir(tmpdir);
247         if (!dirp) {
248             printf
249                 ("Can't open temporary placeholder dir %s; using current partition \n",
250                  tmpdir);
251             tmpdir = NULL;
252         } else
253             closedir(dirp);
254     }
255     if ((ti = as->parms[12].items))     /* -showlog */
256         ShowLog = 1;
257     if ((ti = as->parms[13].items)) {   /* -showsuid */
258         Testing = 1;
259         ShowSuid = 1;
260         Showmode = 1;
261     }
262     if ((ti = as->parms[14].items)) {   /* -showmounts */
263         Testing = 1;
264         Showmode = 1;
265         ShowMounts = 1;
266     }
267     if ((ti = as->parms[15].items)) {   /* -orphans */
268         if (Testing)
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;
276     }
277 #ifndef AFS_NT40_ENV            /* ignore options on NT */
278     if ((ti = as->parms[16].items)) {   /* -syslog */
279         if (ShowLog) {
280             fprintf(stderr, "Invalid options: -syslog and -showlog are exclusive.\n");
281             Exit(1);
282         }
283         serverLogSyslog = 1;
284     }
285     if ((ti = as->parms[17].items)) {   /* -syslogfacility */
286         serverLogSyslogFacility = atoi(ti->data);
287     }
288
289     if ((ti = as->parms[18].items)) {   /* -datelogs */
290         int code = TimeStampLogFile(&logfile);
291         if (code != 0) {
292             fprintf(stderr, "Failed to format log file name for -datelogs; code=%d\n", code);
293             Exit(code);
294         }
295         ShowLogFilename = logfile;
296     }
297 #endif
298
299     OpenLog(logfile);
300     SetupLogSignals();
301
302     Log("%s\n", cml_version_number);
303     LogCommandLine(cmdline->argc, cmdline->argv, "SALVAGER", SalvageVersion, "STARTING AFS", Log);
304
305 #ifdef FAST_RESTART
306     if (ti = as->parms[19].items) {     /* -DontSalvage */
307         char *msg =
308             "Exiting immediately without salvage. Look into the FileLog to find volumes which really need to be salvaged!";
309         Log("%s\n", msg);
310         printf("%s\n", msg);
311         Exit(0);
312     }
313 #endif
314
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 */
319 #ifdef AFS_NT40_ENV
320     if (seenvol) {
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");
325             Exit(1);
326         }
327     }
328 #endif
329
330     if (seenvol) {
331         pt = volumeSalvager;
332     } else {
333         pt = salvager;
334     }
335
336     VOptDefaults(pt, &opts);
337     if (VInitVolumePackage2(pt, &opts)) {
338         Log("errors encountered initializing volume package; salvage aborted\n");
339         Exit(1);
340     }
341
342     /* defer lock until we init volume package */
343     if (get_salvage_lock) {
344         if (seenvol && AskDAFS()) /* support forceDAFS */
345             ObtainSharedSalvageLock();
346         else
347             ObtainSalvageLock();
348     }
349
350     /*
351      * Ok to defer this as Exit will clean up and no real work is done
352      * init'ing volume package
353      */
354     if (seenvol) {
355         char *msg = NULL;
356 #ifdef AFS_DEMAND_ATTACH_FS
357         if (!AskDAFS()) {
358             msg =
359                 "The DAFS dasalvager cannot be run with a non-DAFS fileserver.  Please use 'salvager'.";
360         }
361         if (!msg && !as->parms[20].items) {
362             msg =
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.";
364         }
365 #else
366         if (AskDAFS()) {
367             msg =
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.";
369         }
370 #endif
371
372         if (msg) {
373             Log("%s\n", msg);
374             printf("%s\n", msg);
375             Exit(1);
376         }
377     }
378
379     DInit(10);
380 #ifdef AFS_NT40_ENV
381     if (myjob.cj_number != NOT_CHILD) {
382         if (!seenpart) {
383             seenpart = 1;
384             (void)strcpy(pname, myjob.cj_part);
385         }
386     }
387 #endif
388     if (seenpart == 0) {
389         for (partP = DiskPartitionList; partP; partP = partP->next) {
390             SalvageFileSysParallel(partP);
391         }
392         SalvageFileSysParallel(0);
393     } else {
394         partP = VGetPartition(pname, 0);
395         if (!partP) {
396             Log("salvage: Unknown or unmounted partition %s; salvage aborted\n", pname);
397             Exit(1);
398         }
399         if (!seenvol)
400             SalvageFileSys(partP, 0);
401         else {
402             /* Salvage individual volume */
403             SalvageFileSys(partP, vid);
404         }
405     }
406     return (0);
407 }
408
409
410 #ifndef AFS_NT40_ENV
411 #include "AFS_component_version_number.c"
412 #endif
413
414 int
415 main(int argc, char **argv)
416 {
417     struct CmdLine cmdline;
418     struct cmd_syndesc *ts;
419     int err = 0;
420
421 #ifdef  AFS_AIX32_ENV
422     /*
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.
427      */
428     struct sigaction nsa;
429
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);
435 #endif
436
437     /* Initialize directory paths */
438     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
439 #ifdef AFS_NT40_ENV
440         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
441 #endif
442         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
443                 argv[0]);
444         exit(2);
445     }
446 #ifdef AFS_NT40_ENV
447     /* Default to binary mode for fopen() */
448     _set_fmode(_O_BINARY);
449
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.
454          */
455         if (nt_SetupPartitionSalvage(spawnDatap, spawnDataLen) < 0)
456             exit(3);
457     } else {
458 #endif
459
460 #ifndef AFS_NT40_ENV
461         if (geteuid() != 0) {
462             printf("Salvager must be run as root.\n");
463             fflush(stdout);
464             Exit(0);
465         }
466 #endif
467
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.
472          */
473         get_salvage_lock = 1;
474 #ifdef AFS_NT40_ENV
475     }
476 #endif
477
478     cmdline.argc = argc;
479     cmdline.argv = argv;
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");
512
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");
521 #ifdef FAST_RESTART
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);
532     Exit(err);
533     return 0; /* not reached */
534 }
535