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