1a5e92be70a491de696c3f35390ddfa6993bc3c5
[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 static int get_salvage_lock = 0;
115
116 static int
117 handleit(struct cmd_syndesc *as, void *arock)
118 {
119     struct cmd_item *ti;
120     char pname[100], *temp;
121     afs_int32 seenpart = 0, seenvol = 0;
122     VolumeId vid = 0;
123     ProgramType pt;
124
125 #ifdef FAST_RESTART
126     afs_int32  seenany = 0;
127 #endif
128
129     VolumePackageOptions opts;
130     struct DiskPartition64 *partP;
131
132 #ifdef AFS_SGI_VNODE_GLUE
133     if (afs_init_kernel_config(-1) < 0) {
134         printf
135             ("Can't determine NUMA configuration, not starting salvager.\n");
136         exit(1);
137     }
138 #endif
139
140 #ifdef FAST_RESTART
141     {
142         afs_int32 i;
143         for (i = 0; i < CMD_MAXPARMS; i++) {
144             if (as->parms[i].items) {
145                 seenany = 1;
146                 break;
147             }
148         }
149     }
150     if (!seenany) {
151         char *msg =
152             "Exiting immediately without salvage. Look into the FileLog to find volumes which really need to be salvaged!";
153
154 #ifndef AFS_NT40_ENV
155         if (useSyslog)
156             Log("%s", msg);
157         else
158 #endif
159             printf("%s\n", msg);
160
161         Exit(0);
162     }
163 #endif /* FAST_RESTART */
164     if ((ti = as->parms[0].items)) {    /* -partition */
165         seenpart = 1;
166         strncpy(pname, ti->data, 100);
167     }
168     if ((ti = as->parms[1].items)) {    /* -volumeid */
169         char *end;
170         unsigned long vid_l;
171         if (!seenpart) {
172             printf
173                 ("You must also specify '-partition' option with the '-volumeid' option\n");
174             exit(-1);
175         }
176         seenvol = 1;
177         vid_l = strtoul(ti->data, &end, 10);
178         if (vid_l >= MAX_AFS_UINT32 || vid_l == ULONG_MAX || *end != '\0') {
179             Log("salvage: invalid volume id specified; salvage aborted\n");
180             Exit(1);
181         }
182         vid = (VolumeId)vid_l;
183     }
184     if (as->parms[2].items)     /* -debug */
185         debug = 1;
186     if (as->parms[3].items)     /* -nowrite */
187         Testing = 1;
188     if (as->parms[4].items)     /* -inodes */
189         ListInodeOption = 1;
190     if (as->parms[5].items || as->parms[21].items)      /* -force, -f */
191         ForceSalvage = 1;
192     if (as->parms[6].items)     /* -oktozap */
193         OKToZap = 1;
194     if (as->parms[7].items)     /* -rootinodes */
195         ShowRootFiles = 1;
196     if (as->parms[8].items)     /* -RebuildDirs */
197         RebuildDirs = 1;
198     if (as->parms[9].items)     /* -ForceReads */
199         forceR = 1;
200     if ((ti = as->parms[10].items)) {   /* -Parallel # */
201         temp = ti->data;
202         if (strncmp(temp, "all", 3) == 0) {
203             PartsPerDisk = 1;
204             temp += 3;
205         }
206         if (strlen(temp) != 0) {
207             Parallel = atoi(temp);
208             if (Parallel < 1)
209                 Parallel = 1;
210             if (Parallel > MAXPARALLEL) {
211                 printf("Setting parallel salvages to maximum of %d \n",
212                        MAXPARALLEL);
213                 Parallel = MAXPARALLEL;
214             }
215         }
216     }
217     if ((ti = as->parms[11].items)) {   /* -tmpdir */
218         DIR *dirp;
219
220         tmpdir = ti->data;
221         dirp = opendir(tmpdir);
222         if (!dirp) {
223             printf
224                 ("Can't open temporary placeholder dir %s; using current partition \n",
225                  tmpdir);
226             tmpdir = NULL;
227         } else
228             closedir(dirp);
229     }
230     if ((ti = as->parms[12].items))     /* -showlog */
231         ShowLog = 1;
232     if ((ti = as->parms[13].items)) {   /* -showsuid */
233         Testing = 1;
234         ShowSuid = 1;
235         Showmode = 1;
236     }
237     if ((ti = as->parms[14].items)) {   /* -showmounts */
238         Testing = 1;
239         Showmode = 1;
240         ShowMounts = 1;
241     }
242     if ((ti = as->parms[15].items)) {   /* -orphans */
243         if (Testing)
244             orphans = ORPH_IGNORE;
245         else if (strcmp(ti->data, "remove") == 0
246                  || strcmp(ti->data, "r") == 0)
247             orphans = ORPH_REMOVE;
248         else if (strcmp(ti->data, "attach") == 0
249                  || strcmp(ti->data, "a") == 0)
250             orphans = ORPH_ATTACH;
251     }
252 #ifndef AFS_NT40_ENV            /* ignore options on NT */
253     if ((ti = as->parms[16].items)) {   /* -syslog */
254         useSyslog = 1;
255         ShowLog = 0;
256     }
257     if ((ti = as->parms[17].items)) {   /* -syslogfacility */
258         useSyslogFacility = atoi(ti->data);
259     }
260
261     if ((ti = as->parms[18].items)) {   /* -datelogs */
262       TimeStampLogFile((char *)AFSDIR_SERVER_SLVGLOG_FILEPATH);
263     }
264 #endif
265
266 #ifdef FAST_RESTART
267     if (ti = as->parms[19].items) {     /* -DontSalvage */
268         char *msg =
269             "Exiting immediately without salvage. Look into the FileLog to find volumes which really need to be salvaged!";
270
271 #ifndef AFS_NT40_ENV
272         if (useSyslog)
273             Log("%s", msg);
274         else
275 #endif
276             printf("%s\n", msg);
277         Exit(0);
278     }
279 #endif
280
281     /* Note:  if seenvol we initialize this as a standard volume utility:  this has the
282      * implication that the file server may be running; negotations have to be made with
283      * the file server in this case to take the read write volume and associated read-only
284      * volumes off line before salvaging */
285 #ifdef AFS_NT40_ENV
286     if (seenvol) {
287         if (afs_winsockInit() < 0) {
288             ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0,
289                                 AFSDIR_SALVAGER_FILE, 0);
290             Log("Failed to initailize winsock, exiting.\n");
291             Exit(1);
292         }
293     }
294 #endif
295
296     if (seenvol) {
297         pt = volumeSalvager;
298     } else {
299         pt = salvager;
300     }
301
302     VOptDefaults(pt, &opts);
303     if (VInitVolumePackage2(pt, &opts)) {
304         Log("errors encountered initializing volume package; salvage aborted\n");
305         Exit(1);
306     }
307
308     /* defer lock until we init volume package */
309     if (get_salvage_lock) {
310         if (seenvol && AskDAFS()) /* support forceDAFS */
311             ObtainSharedSalvageLock();
312         else
313             ObtainSalvageLock();
314     }
315
316     /*
317      * Ok to defer this as Exit will clean up and no real work is done
318      * init'ing volume package
319      */
320     if (seenvol) {
321         char *msg = NULL;
322 #ifdef AFS_DEMAND_ATTACH_FS
323         if (!AskDAFS()) {
324             msg =
325                 "The DAFS dasalvager cannot be run with a non-DAFS fileserver.  Please use 'salvager'.";
326         }
327         if (!msg && !as->parms[20].items) {
328             msg =
329                 "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.";
330         }
331 #else
332         if (AskDAFS()) {
333             msg =
334                 "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.";
335         }
336 #endif
337
338         if (msg) {
339 #ifndef AFS_NT40_ENV
340             if (useSyslog)
341                 Log("%s", msg);
342             else
343 #endif
344                 printf("%s\n", msg);
345             Exit(1);
346         }
347     }
348
349     DInit(10);
350 #ifdef AFS_NT40_ENV
351     if (myjob.cj_number != NOT_CHILD) {
352         if (!seenpart) {
353             seenpart = 1;
354             (void)strcpy(pname, myjob.cj_part);
355         }
356     }
357 #endif
358     if (seenpart == 0) {
359         for (partP = DiskPartitionList; partP; partP = partP->next) {
360             SalvageFileSysParallel(partP);
361         }
362         SalvageFileSysParallel(0);
363     } else {
364         partP = VGetPartition(pname, 0);
365         if (!partP) {
366             Log("salvage: Unknown or unmounted partition %s; salvage aborted\n", pname);
367             Exit(1);
368         }
369         if (!seenvol)
370             SalvageFileSys(partP, 0);
371         else {
372             /* Salvage individual volume */
373             SalvageFileSys(partP, vid);
374         }
375     }
376     return (0);
377 }
378
379
380 #ifndef AFS_NT40_ENV
381 #include "AFS_component_version_number.c"
382 #endif
383
384 int
385 main(int argc, char **argv)
386 {
387     struct cmd_syndesc *ts;
388     int err = 0;
389
390     extern char cml_version_number[];
391
392 #ifdef  AFS_AIX32_ENV
393     /*
394      * The following signal action for AIX is necessary so that in case of a
395      * crash (i.e. core is generated) we can include the user's data section
396      * in the core dump. Unfortunately, by default, only a partial core is
397      * generated which, in many cases, isn't too useful.
398      */
399     struct sigaction nsa;
400
401     sigemptyset(&nsa.sa_mask);
402     nsa.sa_handler = SIG_DFL;
403     nsa.sa_flags = SA_FULLDUMP;
404     sigaction(SIGABRT, &nsa, NULL);
405     sigaction(SIGSEGV, &nsa, NULL);
406 #endif
407
408     /* Initialize directory paths */
409     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
410 #ifdef AFS_NT40_ENV
411         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
412 #endif
413         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
414                 argv[0]);
415         exit(2);
416     }
417 #ifdef AFS_NT40_ENV
418     /* Default to binary mode for fopen() */
419     _set_fmode(_O_BINARY);
420
421     main_thread = pthread_self();
422     if (spawnDatap && spawnDataLen) {
423         /* This is a child per partition salvager. Don't setup log or
424          * try to lock the salvager lock.
425          */
426         if (nt_SetupPartitionSalvage(spawnDatap, spawnDataLen) < 0)
427             exit(3);
428     } else {
429 #endif
430         /* All entries to the log will be appended.  Useful if there are
431          * multiple salvagers appending to the log.
432          */
433
434         CheckLogFile((char *)AFSDIR_SERVER_SLVGLOG_FILEPATH);
435 #ifndef AFS_NT40_ENV
436 #ifdef AFS_LINUX20_ENV
437         fcntl(fileno(logFile), F_SETFL, O_APPEND);      /* Isn't this redundant? */
438 #else
439         fcntl(fileno(logFile), F_SETFL, FAPPEND);       /* Isn't this redundant? */
440 #endif
441 #endif
442         setlinebuf(logFile);
443
444 #ifndef AFS_NT40_ENV
445         if (geteuid() != 0) {
446             printf("Salvager must be run as root.\n");
447             fflush(stdout);
448             Exit(0);
449         }
450 #endif
451
452         /* bad for normal help flag processing, but can do nada */
453
454         fprintf(logFile, "%s\n", cml_version_number);
455         LogCommandLine(argc, argv, "SALVAGER", SalvageVersion, "STARTING AFS",
456                        Log);
457
458         /* Get and hold a lock for the duration of the salvage to make sure
459          * that no other salvage runs at the same time.  The routine
460          * VInitVolumePackage2 (called below) makes sure that a file server or
461          * other volume utilities don't interfere with the salvage.
462          */
463         get_salvage_lock = 1;
464 #ifdef AFS_NT40_ENV
465     }
466 #endif
467
468     ts = cmd_CreateSyntax("initcmd", handleit, NULL, "initialize the program");
469     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL,
470                 "Name of partition to salvage");
471     cmd_AddParm(ts, "-volumeid", CMD_SINGLE, CMD_OPTIONAL,
472                 "Volume Id to salvage");
473     cmd_AddParm(ts, "-debug", CMD_FLAG, CMD_OPTIONAL,
474                 "Run in Debugging mode");
475     cmd_AddParm(ts, "-nowrite", CMD_FLAG, CMD_OPTIONAL,
476                 "Run readonly/test mode");
477     cmd_AddParm(ts, "-inodes", CMD_FLAG, CMD_OPTIONAL,
478                 "Just list affected afs inodes - debugging flag");
479     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "Force full salvaging");
480     cmd_AddParm(ts, "-oktozap", CMD_FLAG, CMD_OPTIONAL,
481                 "Give permission to destroy bogus inodes/volumes - debugging flag");
482     cmd_AddParm(ts, "-rootinodes", CMD_FLAG, CMD_OPTIONAL,
483                 "Show inodes owned by root - debugging flag");
484     cmd_AddParm(ts, "-salvagedirs", CMD_FLAG, CMD_OPTIONAL,
485                 "Force rebuild/salvage of all directories");
486     cmd_AddParm(ts, "-blockreads", CMD_FLAG, CMD_OPTIONAL,
487                 "Read smaller blocks to handle IO/bad blocks");
488     cmd_AddParm(ts, "-parallel", CMD_SINGLE, CMD_OPTIONAL,
489                 "# of max parallel partition salvaging");
490     cmd_AddParm(ts, "-tmpdir", CMD_SINGLE, CMD_OPTIONAL,
491                 "Name of dir to place tmp files ");
492     cmd_AddParm(ts, "-showlog", CMD_FLAG, CMD_OPTIONAL,
493                 "Show log file upon completion");
494     cmd_AddParm(ts, "-showsuid", CMD_FLAG, CMD_OPTIONAL,
495                 "Report on suid/sgid files");
496     cmd_AddParm(ts, "-showmounts", CMD_FLAG, CMD_OPTIONAL,
497                 "Report on mountpoints");
498     cmd_AddParm(ts, "-orphans", CMD_SINGLE, CMD_OPTIONAL,
499                 "ignore | remove | attach");
500
501     /* note - syslog isn't avail on NT, but if we make it conditional, have
502      * to deal with screwy offsets for cmd params */
503     cmd_AddParm(ts, "-syslog", CMD_FLAG, CMD_OPTIONAL,
504                 "Write salvage log to syslogs");
505     cmd_AddParm(ts, "-syslogfacility", CMD_SINGLE, CMD_OPTIONAL,
506                 "Syslog facility number to use");
507     cmd_AddParm(ts, "-datelogs", CMD_FLAG, CMD_OPTIONAL,
508                 "Include timestamp in logfile filename");
509 #ifdef FAST_RESTART
510     cmd_AddParm(ts, "-DontSalvage", CMD_FLAG, CMD_OPTIONAL,
511                 "Don't salvage. This my be set in BosConfig to let the fileserver restart immediately after a crash. Bad volumes will be taken offline");
512 #elif defined(AFS_DEMAND_ATTACH_FS)
513     cmd_Seek(ts, 20); /* skip DontSalvage */
514     cmd_AddParm(ts, "-forceDAFS", CMD_FLAG, CMD_OPTIONAL,
515                 "For Demand Attach Fileserver, permit a manual volume salvage outside of the salvageserver");
516 #endif /* FAST_RESTART */
517     cmd_Seek(ts, 21); /* skip DontSalvage and forceDAFS if needed */
518     cmd_AddParm(ts, "-f", CMD_FLAG, CMD_OPTIONAL, "Alias for -force");
519     err = cmd_Dispatch(argc, argv);
520     Exit(err);
521     return 0; /* not reached */
522 }
523