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