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