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