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