revert-salvager-verbosity-entirely-20050319
[openafs.git] / src / vol / vol-salvage.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:         vol-salvage.c
13  *      Institution:    The Information Technology Center, Carnegie-Mellon University
14  */
15
16 /*  1.2 features:
17         Correct handling of bad "." and ".." entries.
18         Message if volume has "destroyMe" flag set--but doesn't delete yet.
19         Link count bug fixed--bug was that vnodeEssence link count was unsigned
20         14 bits.  Needs to be signed.
21
22     1.3 features:
23         Change to DirHandle stuff to make sure that cache entries are reused at the
24         right time (this parallels the file server change, but is not identical).
25
26         Added calls to directory salvager routines; doesn't salvage dir unless debug=1.
27
28     1.4 features:
29         Fixed bug which was causing inode link counts to go bad (thus leaking
30         disk blocks).
31 Vnodes with 0 inode pointers in RW volumes are now deleted.
32         An inode with a matching inode number to the vnode is preferred to an
33         inode with a higer data version.
34         Bug is probably fixed that was causing data version to remain wrong,
35         despite assurances from the salvager to the contrary.
36
37     1.5 features:
38         Added limited salvaging:  unless ForceSalvage is on, then the volume will
39         not be salvaged if the dontSalvage flag is set in the Volume Header.
40         The ForceSalvage flag is turned on if an individual volume is salvaged or
41         if the file FORCESALVAGE exists in the partition header of the file system
42         being salvaged.  This isn't used for anything but could be set by vfsck.
43         A -f flag was also added to force salvage.
44
45     1.6 features:
46         It now deletes obsolete volume inodes without complaining
47
48     1.7 features:
49         Repairs rw volume headers (again).
50
51     1.8 features:
52         Correlates volume headers & inodes correctly, thus preventing occasional deletion
53         of read-only volumes...
54         No longer forces a directory salvage for volume 144 (which may be a good volume
55         at some other site!)
56         Some of the messages are cleaned up or made more explicit.  One or two added.
57         Logging cleaned up.
58         A bug was fixed which forced salvage of read-only volumes without a corresponding
59         read/write volume.
60
61     1.9 features:
62         When a volume header is recreated, the new name will be "bogus.volume#"
63
64     2.0 features:
65         Directory salvaging turned on!!!
66
67     2.1 features:
68         Prints warning messages for setuid programs.
69
70     2.2 features:
71         Logs missing inode numbers.
72
73     2.3 features:
74             Increments directory version number by 200 (rather than by 1) when it is salvaged, in order to prevent problems due to the fact that a version number can be promised to a workstation before it is written to disk.  If the server crashes, it may have an older version.  Salvaging it could bring the version number up to the same version the workstation believed it already had a call back on. 
75
76     2.4 features:
77             Locks the file /vice/vol/salvage.lock before starting.  Aborts if it can't acquire the lock.
78             Time stamps on log entries.
79             Fcntl on stdout to cause all entries to be appended.
80             Problems writing to temporary files are now all detected.
81             Inode summary files are now dynamically named (so that multiple salvagers wouldn't conflict).
82             Some cleanup of error messages.
83 */
84
85
86 #define SalvageVersion "2.4"
87
88 /* Main program file. Define globals. */
89 #define MAIN 1
90
91 #include <afsconfig.h>
92 #include <afs/param.h>
93
94 RCSID
95     ("$Header$");
96
97 #include <stdlib.h>
98 #include <stdio.h>
99 #include <string.h>
100 #include <dirent.h>
101 #include <sys/stat.h>
102 #include <time.h>
103 #include <errno.h>
104 #ifdef AFS_NT40_ENV
105 #include <io.h>
106 #include <WINNT/afsevent.h>
107 #else
108 #include <sys/param.h>
109 #include <sys/file.h>
110 #ifndef ITIMER_REAL
111 #include <sys/time.h>
112 #endif /* ITIMER_REAL */
113 #endif
114 #if     defined(AFS_AIX_ENV) || defined(AFS_SUN4_ENV)
115 #define WCOREDUMP(x)    (x & 0200)
116 #endif
117 #include <rx/xdr.h>
118 #include <afs/afsint.h>
119 #include <afs/assert.h>
120 #if !defined(AFS_SGI_ENV) && !defined(AFS_NT40_ENV)
121 #if defined(AFS_VFSINCL_ENV)
122 #include <sys/vnode.h>
123 #ifdef  AFS_SUN5_ENV
124 #include <sys/fs/ufs_inode.h>
125 #else
126 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
127 #include <ufs/ufs/dinode.h>
128 #include <ufs/ffs/fs.h>
129 #else
130 #include <ufs/inode.h>
131 #endif
132 #endif
133 #else /* AFS_VFSINCL_ENV */
134 #ifdef  AFS_OSF_ENV
135 #include <ufs/inode.h>
136 #else /* AFS_OSF_ENV */
137 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_XBSD_ENV)
138 #include <sys/inode.h>
139 #endif
140 #endif
141 #endif /* AFS_VFSINCL_ENV */
142 #endif /* AFS_SGI_ENV */
143 #ifdef  AFS_AIX_ENV
144 #include <sys/vfs.h>
145 #include <sys/lockf.h>
146 #else
147 #ifdef  AFS_HPUX_ENV
148 #include <unistd.h>
149 #include <checklist.h>
150 #else
151 #if defined(AFS_SGI_ENV)
152 #include <unistd.h>
153 #include <fcntl.h>
154 #include <mntent.h>
155 #else
156 #if     defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
157 #ifdef    AFS_SUN5_ENV
158 #include <unistd.h>
159 #include <sys/mnttab.h>
160 #include <sys/mntent.h>
161 #else
162 #include <mntent.h>
163 #endif
164 #else
165 #endif /* AFS_SGI_ENV */
166 #endif /* AFS_HPUX_ENV */
167 #endif
168 #endif
169 #include <fcntl.h>
170 #ifndef AFS_NT40_ENV
171 #include <afs/osi_inode.h>
172 #endif
173 #include <afs/cmd.h>
174 #include <afs/afsutil.h>
175 #include <afs/fileutil.h>
176 #include <afs/procmgmt.h>       /* signal(), kill(), wait(), etc. */
177 #ifndef AFS_NT40_ENV
178 #include <syslog.h>
179 #endif
180
181 #include "nfs.h"
182 #include "lwp.h"
183 #include "lock.h"
184 #include <afs/afssyscalls.h>
185 #include "ihandle.h"
186 #include "vnode.h"
187 #include "volume.h"
188 #include "partition.h"
189 #include "fssync.h"
190 #include "viceinode.h"
191 #include "salvage.h"
192 #include "volinodes.h"          /* header magic number, etc. stuff */
193 #ifdef AFS_NT40_ENV
194 #include <pthread.h>
195 #endif
196
197 /*@+fcnmacros +macrofcndecl@*/
198 #ifdef O_LARGEFILE
199 #ifdef S_SPLINT_S
200 extern off64_t afs_lseek(int FD, off64_t O, int F);
201 #endif /*S_SPLINT_S */
202 #define afs_lseek(FD, O, F)     lseek64(FD, (off64_t) (O), F)
203 #define afs_stat        stat64
204 #define afs_fstat       fstat64
205 #define afs_open        open64
206 #define afs_fopen       fopen64
207 #else /* !O_LARGEFILE */
208 #ifdef S_SPLINT_S
209 extern off_t afs_lseek(int FD, off_t O, int F);
210 #endif /*S_SPLINT_S */
211 #define afs_lseek(FD, O, F)     lseek(FD, (off_t) (O), F)
212 #define afs_stat        stat
213 #define afs_fstat       fstat
214 #define afs_open        open
215 #define afs_fopen       fopen
216 #endif /* !O_LARGEFILE */
217 /*@=fcnmacros =macrofcndecl@*/
218
219 #ifdef  AFS_OSF_ENV
220 extern void *calloc();
221 #endif
222 static char *TimeStamp(time_t clock, int precision);
223
224 #define ORPH_IGNORE 0
225 #define ORPH_REMOVE 1
226 #define ORPH_ATTACH 2
227
228
229 int debug;                      /* -d flag */
230 int Testing = 0;                /* -n flag */
231 int ListInodeOption;            /* -i flag */
232 int ShowRootFiles;              /* -r flag */
233 int RebuildDirs;                /* -sal flag */
234 int Parallel = 4;               /* -para X flag */
235 int PartsPerDisk = 8;           /* Salvage up to 8 partitions on same disk sequentially */
236 int forceR = 0;                 /* -b flag */
237 int ShowLog = 0;                /* -showlog flag */
238 int ShowSuid = 0;               /* -showsuid flag */
239 int ShowMounts = 0;             /* -showmounts flag */
240 int orphans = ORPH_IGNORE;      /* -orphans option */
241 int Showmode = 0;
242
243 #ifndef AFS_NT40_ENV
244 int useSyslog = 0;              /* -syslog flag */
245 int useSyslogFacility = LOG_DAEMON;     /* -syslogfacility option */
246 #endif
247
248 #define MAXPARALLEL     32
249
250 int OKToZap;                    /* -o flag */
251 int ForceSalvage;               /* If salvage should occur despite the DONT_SALVAGE flag
252                                  * in the volume header */
253
254 static FILE *logFile = 0;       /* one of {/usr/afs/logs,/vice/file}/SalvageLog */
255
256 #define ROOTINODE       2       /* Root inode of a 4.2 Unix file system
257                                  * partition */
258 Device fileSysDevice;           /* The device number of the current
259                                  * partition being salvaged */
260 #ifdef AFS_NT40_ENV
261 char fileSysPath[8];
262 #else
263 char *fileSysPath;              /* The path of the mounted partition currently
264                                  * being salvaged, i.e. the directory
265                                  * containing the volume headers */
266 #endif
267 char *fileSysPathName;          /* NT needs this to make name pretty in log. */
268 IHandle_t *VGLinkH;             /* Link handle for current volume group. */
269 int VGLinkH_cnt;                /* # of references to lnk handle. */
270 struct DiskPartition *fileSysPartition; /* Partition  being salvaged */
271 #ifndef AFS_NT40_ENV
272 char *fileSysDeviceName;        /* The block device where the file system
273                                  * being salvaged was mounted */
274 char *filesysfulldev;
275 #endif
276 int VolumeChanged;              /* Set by any routine which would change the volume in
277                                  * a way which would require callback is to be broken if the
278                                  * volume was put back on line by an active file server */
279
280 VolumeDiskData VolInfo;         /* A copy of the last good or salvaged volume header dealt with */
281
282 struct InodeSummary {           /* Inode summary file--an entry for each
283                                  * volume in the inode file for a partition */
284     VolId volumeId;             /* Volume id */
285     VolId RWvolumeId;           /* RW volume associated */
286     int index;                  /* index into inode file (0, 1, 2 ...) */
287     int nInodes;                /* Number of inodes for this volume */
288     int nSpecialInodes;         /* Number of special inodes, i.e.  volume
289                                  * header, index, etc.  These are all
290                                  * marked (viceinode.h) and will all be sorted
291                                  * to the beginning of the information for
292                                  * this volume.  Read-only volumes should
293                                  * ONLY have special inodes (all the other
294                                  * inodes look as if they belong to the
295                                  * original RW volume). */
296     Unique maxUniquifier;       /* The maximum uniquifier found in all the inodes.
297                                  * This is only useful for RW volumes and is used
298                                  * to compute a new volume uniquifier in the event
299                                  * that the header needs to be recreated. The inode
300                                  * uniquifier may be a truncated version of vnode
301                                  * uniquifier (AFS_3DISPARES). The real maxUniquifer
302                                  * is from the vnodes and later calcuated from it */
303     struct VolumeSummary *volSummary;
304     /* Either a pointer to the original volume
305      * header summary, or constructed summary
306      * information */
307 } *inodeSummary;
308 #define readOnly(isp)   ((isp)->volumeId != (isp)->RWvolumeId)
309 int nVolumesInInodeFile;        /* Number of read-write volumes summarized */
310 int inodeFd;                    /* File descriptor for inode file */
311
312
313 struct VolumeSummary {          /* Volume summary an entry for each
314                                  * volume in a volume directory.
315                                  * Assumption: one volume directory per
316                                  * partition */
317     char *fileName;             /* File name on the partition for the volume
318                                  * header */
319     struct VolumeHeader header;
320     /* volume number, rw volume number, inode
321      * numbers of each major component of
322      * the volume */
323     IHandle_t *volumeInfoHandle;
324     byte wouldNeedCallback;     /* set if the file server should issue
325                                  * call backs for all the files in this volume when
326                                  * the volume goes back on line */
327 };
328
329 struct VnodeInfo {
330     IHandle_t *handle;          /* Inode containing this index */
331     int nVnodes;                /* Total number of vnodes in index */
332     int nAllocatedVnodes;       /* Total number actually used */
333     int volumeBlockCount;       /* Total number of blocks used by volume */
334     Inode *inodes;              /* Directory only */
335     struct VnodeEssence {
336         short count;            /* Number of references to vnode; MUST BE SIGNED */
337         unsigned claimed:1;     /* Set when a parent directory containing an entry
338                                  * referencing this vnode is found.  The claim
339                                  * is that the parent in "parent" can point to
340                                  * this vnode, and no other */
341         unsigned changed:1;     /* Set if any parameters (other than the count)
342                                  * in the vnode change.   It is determined if the
343                                  * link count has changed by noting whether it is
344                                  * 0 after scanning all directories */
345         unsigned salvaged:1;    /* Set if this directory vnode has already been salvaged. */
346         unsigned todelete:1;    /* Set if this vnode is to be deleted (should not be claimed) */
347         afs_fsize_t blockCount;
348         /* Number of blocks (1K) used by this vnode,
349          * approximately */
350         VnodeId parent;         /* parent in vnode */
351         Unique unique;          /* Must match entry! */
352         char *name;             /* Name of directory entry */
353         int modeBits;           /* File mode bits */
354         Inode InodeNumber;      /* file's inode */
355         int type;               /* File type */
356         int author;             /* File author */
357         int owner;              /* File owner */
358         int group;              /* File group */
359     } *vnodes;
360 } vnodeInfo[nVNODECLASSES];
361
362 struct DirSummary {
363     struct DirHandle dirHandle;
364     VnodeId vnodeNumber;
365     Unique unique;
366     unsigned haveDot, haveDotDot;
367     VolumeId rwVid;
368     int copied;                 /* If the copy-on-write stuff has been applied */
369     VnodeId parent;
370     char *name;
371     char *vname;
372     IHandle_t *ds_linkH;
373 };
374
375
376 struct VolumeSummary *volumeSummaryp;   /* Holds all the volumes in a part */
377 int nVolumes;                   /* Number of volumes (read-write and read-only)
378                                  * in volume summary */
379
380 #ifdef AFS_NT40_ENV
381 /* For NT, we can fork the per partition salvagers to gain the required
382  * safety against Aborts. But there's too many complex data structures at
383  * the per volume salvager layer to easilty copy the data across.
384  * childJobNumber is resset from -1 to the job number if this is a
385  * per partition child of the main salvager. This information is passed
386  * out-of-band in the extra data area setup for the now unused parent/child
387  * data transfer.
388  */
389 #define SALVAGER_MAGIC 0x00BBaaDD
390 #define NOT_CHILD -1            /* job numbers start at 0 */
391 /* If new options need to be passed to child, add them here. */
392 typedef struct {
393     int cj_magic;
394     int cj_number;
395     char cj_part[32];
396 } childJob_t;
397
398 /* Child job this process is running. */
399 childJob_t myjob = { SALVAGER_MAGIC, NOT_CHILD, "" };
400
401 int nt_SalvagePartition(char *partName, int jobn);
402 int nt_SetupPartitionSalvage(void *datap, int len);
403
404 typedef struct {
405     struct InodeSummary *svgp_inodeSummaryp;
406     int svgp_count;
407 } SVGParms_t;
408 #define canfork 0
409 #else
410 #define canfork 1
411 #endif
412
413
414
415 /* Forward declarations */
416 /*@printflike@*/ void Log(const char *format, ...);
417 /*@printflike@*/ void Abort(const char *format, ...);
418 void Exit(int code);
419 int Fork(void);
420 int Wait(char *prog);
421 char *ToString(char *s);
422 void AskOffline(VolumeId volumeId);
423 void AskOnline(VolumeId volumeId, char *partition);
424 void CheckLogFile(void);
425 #ifndef AFS_NT40_ENV
426 void TimeStampLogFile(void);
427 #endif
428 void ClearROInUseBit(struct VolumeSummary *summary);
429 void CopyAndSalvage(register struct DirSummary *dir);
430 int CopyInode(Device device, Inode inode1, Inode inode2, int rwvolume);
431 void CopyOnWrite(register struct DirSummary *dir);
432 void CountVolumeInodes(register struct ViceInodeInfo *ip, int maxInodes,
433                        register struct InodeSummary *summary);
434 void DeleteExtraVolumeHeaderFile(register struct VolumeSummary *vsp);
435 void DistilVnodeEssence(VolumeId vid, VnodeClass class, Inode ino,
436                         Unique * maxu);
437 int GetInodeSummary(char *path, VolumeId singleVolumeNumber);
438 void GetVolumeSummary(VolumeId singleVolumeNumber);
439 void JudgeEntry(struct DirSummary *dir, char *name, VnodeId vnodeNumber,
440                 Unique unique);
441 void MaybeZapVolume(register struct InodeSummary *isp, char *message,
442                     int deleteMe, int check);
443 void ObtainSalvageLock(void);
444 void PrintInodeList(void);
445 void PrintInodeSummary(void);
446 void PrintVolumeSummary(void);
447 int QuickCheck(register struct InodeSummary *isp, int nVols);
448 void RemoveTheForce(char *path);
449 void SalvageDir(char *name, VolumeId rwVid, struct VnodeInfo *dirVnodeInfo,
450                 IHandle_t * alinkH, int i, struct DirSummary *rootdir,
451                 int *rootdirfound);
452 void SalvageFileSysParallel(struct DiskPartition *partP);
453 void SalvageFileSys(struct DiskPartition *partP, VolumeId singleVolumeNumber);
454 void SalvageFileSys1(struct DiskPartition *partP,
455                      VolumeId singleVolumeNumber);
456 int SalvageHeader(register struct stuff *sp, struct InodeSummary *isp,
457                   int check, int *deleteMe);
458 int SalvageIndex(Inode ino, VnodeClass class, int RW,
459                  register struct ViceInodeInfo *ip, int nInodes,
460                  struct VolumeSummary *volSummary, int check);
461 int SalvageVnodes(register struct InodeSummary *rwIsp,
462                   register struct InodeSummary *thisIsp,
463                   register struct ViceInodeInfo *inodes, int check);
464 int SalvageVolume(register struct InodeSummary *rwIsp, IHandle_t * alinkH);
465 void DoSalvageVolumeGroup(register struct InodeSummary *isp, int nVols);
466 #ifdef AFS_NT40_ENV
467 void SalvageVolumeGroup(register struct InodeSummary *isp, int nVols);
468 #else
469 #define SalvageVolumeGroup DoSalvageVolumeGroup
470 #endif
471 int SalvageVolumeHeaderFile(register struct InodeSummary *isp,
472                             register struct ViceInodeInfo *inodes, int RW,
473                             int check, int *deleteMe);
474 void showlog(void);
475 int UseTheForceLuke(char *path);
476
477 static int IsVnodeOrphaned(VnodeId vnode);
478
479 /* Uniquifier stored in the Inode */
480 static Unique
481 IUnique(Unique u)
482 {
483 #ifdef  AFS_3DISPARES
484     return (u & 0x3fffff);
485 #else
486 #if defined(AFS_SGI_EXMAG)
487     return (u & SGI_UNIQMASK);
488 #else
489     return (u);
490 #endif /* AFS_SGI_EXMAG */
491 #endif
492 }
493
494 static int
495 BadError(register int aerror)
496 {
497     if (aerror == EPERM || aerror == ENXIO || aerror == ENOENT)
498         return 1;
499     return 0;                   /* otherwise may be transient, e.g. EMFILE */
500 }
501
502
503 char *tmpdir = 0;
504 static int
505 handleit(struct cmd_syndesc *as)
506 {
507     register struct cmd_item *ti;
508     char pname[100], *temp;
509     afs_int32 seenpart = 0, seenvol = 0, vid = 0, seenany = 0;
510     struct DiskPartition *partP;
511
512 #ifdef AFS_SGI_VNODE_GLUE
513     if (afs_init_kernel_config(-1) < 0) {
514         printf
515             ("Can't determine NUMA configuration, not starting salvager.\n");
516         exit(1);
517     }
518 #endif
519
520 #ifdef FAST_RESTART
521     {
522         afs_int32 i;
523         for (i = 0; i < CMD_MAXPARMS; i++) {
524             if (as->parms[i].items) {
525                 seenany = 1;
526                 break;
527             }
528         }
529     }
530     if (!seenany) {
531         char *msg =
532             "Exiting immediately without salvage. Look into the FileLog to find volumes which really need to be salvaged!";
533
534         if (useSyslog)
535             Log(msg);
536         else
537             printf("%s\n", msg);
538
539         Exit(0);
540     }
541 #endif /* FAST_RESTART */
542     if ((ti = as->parms[0].items)) {    /* -partition */
543         seenpart = 1;
544         strncpy(pname, ti->data, 100);
545     }
546     if ((ti = as->parms[1].items)) {    /* -volumeid */
547         if (!seenpart) {
548             printf
549                 ("You must also specify '-partition' option with the '-volumeid' option\n");
550             exit(-1);
551         }
552         seenvol = 1;
553         vid = atoi(ti->data);
554     }
555     if (as->parms[2].items)     /* -debug */
556         debug = 1;
557     if (as->parms[3].items)     /* -nowrite */
558         Testing = 1;
559     if (as->parms[4].items)     /* -inodes */
560         ListInodeOption = 1;
561     if (as->parms[5].items)     /* -force */
562         ForceSalvage = 1;
563     if (as->parms[6].items)     /* -oktozap */
564         OKToZap = 1;
565     if (as->parms[7].items)     /* -rootinodes */
566         ShowRootFiles = 1;
567     if (as->parms[8].items)     /* -RebuildDirs */
568         RebuildDirs = 1;
569     if (as->parms[9].items)     /* -ForceReads */
570         forceR = 1;
571     if ((ti = as->parms[10].items)) {   /* -Parallel # */
572         temp = ti->data;
573         if (strncmp(temp, "all", 3) == 0) {
574             PartsPerDisk = 1;
575             temp += 3;
576         }
577         if (strlen(temp) != 0) {
578             Parallel = atoi(temp);
579             if (Parallel < 1)
580                 Parallel = 1;
581             if (Parallel > MAXPARALLEL) {
582                 printf("Setting parallel salvages to maximum of %d \n",
583                        MAXPARALLEL);
584                 Parallel = MAXPARALLEL;
585             }
586         }
587     }
588     if ((ti = as->parms[11].items)) {   /* -tmpdir */
589         DIR *dirp;
590
591         tmpdir = ti->data;
592         dirp = opendir(tmpdir);
593         if (!dirp) {
594             printf
595                 ("Can't open temporary placeholder dir %s; using current partition \n",
596                  tmpdir);
597             tmpdir = NULL;
598         } else
599             closedir(dirp);
600     }
601     if ((ti = as->parms[12].items))     /* -showlog */
602         ShowLog = 1;
603     if ((ti = as->parms[13].items)) {   /* -log */
604         Testing = 1;
605         ShowSuid = 1;
606         Showmode = 1;
607     }
608     if ((ti = as->parms[14].items)) {   /* -showmounts */
609         Testing = 1;
610         Showmode = 1;
611         ShowMounts = 1;
612     }
613     if ((ti = as->parms[15].items)) {   /* -orphans */
614         if (Testing)
615             orphans = ORPH_IGNORE;
616         else if (strcmp(ti->data, "remove") == 0
617                  || strcmp(ti->data, "r") == 0)
618             orphans = ORPH_REMOVE;
619         else if (strcmp(ti->data, "attach") == 0
620                  || strcmp(ti->data, "a") == 0)
621             orphans = ORPH_ATTACH;
622     }
623 #ifndef AFS_NT40_ENV            /* ignore options on NT */
624     if ((ti = as->parms[16].items)) {   /* -syslog */
625         useSyslog = 1;
626         ShowLog = 0;
627     }
628     if ((ti = as->parms[17].items)) {   /* -syslogfacility */
629         useSyslogFacility = atoi(ti->data);
630     }
631
632     if ((ti = as->parms[18].items)) {   /* -datelogs */
633         TimeStampLogFile();
634     }
635 #endif
636
637 #ifdef FAST_RESTART
638     if (ti = as->parms[19].items) {     /* -DontSalvage */
639         char *msg =
640             "Exiting immediately without salvage. Look into the FileLog to find volumes which really need to be salvaged!";
641
642         if (useSyslog)
643             Log(msg);
644         else
645             printf("%s\n", msg);
646         Exit(0);
647     }
648 #endif /* FAST_RESTART */
649
650     /* Note:  if seemvol we initialize this as a standard volume utility:  this has the
651      * implication that the file server may be running; negotations have to be made with
652      * the file server in this case to take the read write volume and associated read-only
653      * volumes off line before salvaging */
654 #ifdef AFS_NT40_ENV
655     if (seenvol) {
656         if (afs_winsockInit() < 0) {
657             ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0,
658                                 AFSDIR_SALVAGER_FILE, 0);
659             Log("Failed to initailize winsock, exiting.\n");
660             Exit(1);
661         }
662     }
663 #endif
664     VInitVolumePackage(seenvol ? volumeUtility : salvager, 5, 5,
665                        DONT_CONNECT_FS, 0);
666     DInit(10);
667 #ifdef AFS_NT40_ENV
668     if (myjob.cj_number != NOT_CHILD) {
669         if (!seenpart) {
670             seenpart = 1;
671             (void)strcpy(pname, myjob.cj_part);
672         }
673     }
674 #endif
675     if (seenpart == 0) {
676         for (partP = DiskPartitionList; partP; partP = partP->next) {
677             SalvageFileSysParallel(partP);
678         }
679         SalvageFileSysParallel(0);
680     } else {
681         partP = VGetPartition(pname, 0);
682         if (!partP) {
683             Log("salvage: Unknown or unmounted partition %s; salvage aborted\n", pname);
684             Exit(1);
685         }
686         if (!seenvol)
687             SalvageFileSys(partP, 0);
688         else {
689             /* Salvage individual volume */
690             if (vid <= 0) {
691                 Log("salvage: invalid volume id specified; salvage aborted\n");
692                 Exit(1);
693             }
694             SalvageFileSys(partP, vid);
695         }
696     }
697     return (0);
698 }
699
700
701 #ifndef AFS_NT40_ENV
702 #include "AFS_component_version_number.c"
703 #endif
704 #define MAX_ARGS 128
705 #ifdef AFS_NT40_ENV
706 char *save_args[MAX_ARGS];
707 int n_save_args = 0;
708 pthread_t main_thread;
709 #endif
710
711 int
712 main(int argc, char **argv)
713 {
714     struct cmd_syndesc *ts;
715     int err = 0;
716     char commandLine[150];
717
718     int i;
719     extern char cml_version_number[];
720
721 #ifdef  AFS_AIX32_ENV
722     /*
723      * The following signal action for AIX is necessary so that in case of a 
724      * crash (i.e. core is generated) we can include the user's data section 
725      * in the core dump. Unfortunately, by default, only a partial core is
726      * generated which, in many cases, isn't too useful.
727      */
728     struct sigaction nsa;
729
730     sigemptyset(&nsa.sa_mask);
731     nsa.sa_handler = SIG_DFL;
732     nsa.sa_flags = SA_FULLDUMP;
733     sigaction(SIGABRT, &nsa, NULL);
734     sigaction(SIGSEGV, &nsa, NULL);
735 #endif
736
737     /* Initialize directory paths */
738     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
739 #ifdef AFS_NT40_ENV
740         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
741 #endif
742         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
743                 argv[0]);
744         exit(2);
745     }
746 #ifdef AFS_NT40_ENV
747     main_thread = pthread_self();
748     if (spawnDatap && spawnDataLen) {
749         /* This is a child per partition salvager. Don't setup log or
750          * try to lock the salvager lock.
751          */
752         if (nt_SetupPartitionSalvage(spawnDatap, spawnDataLen) < 0)
753             exit(3);
754     } else {
755 #endif
756         for (commandLine[0] = '\0', i = 0; i < argc; i++) {
757             if (i > 0)
758                 strcat(commandLine, " ");
759             strcat(commandLine, argv[i]);
760         }
761
762         /* All entries to the log will be appended.  Useful if there are
763          * multiple salvagers appending to the log.
764          */
765
766         CheckLogFile();
767 #ifndef AFS_NT40_ENV
768 #ifdef AFS_LINUX20_ENV
769         fcntl(fileno(logFile), F_SETFL, O_APPEND);      /* Isn't this redundant? */
770 #else
771         fcntl(fileno(logFile), F_SETFL, FAPPEND);       /* Isn't this redundant? */
772 #endif
773 #endif
774         setlinebuf(logFile);
775
776 #ifndef AFS_NT40_ENV
777         if (geteuid() != 0) {
778             printf("Salvager must be run as root.\n");
779             fflush(stdout);
780             Exit(0);
781         }
782 #endif
783
784         /* bad for normal help flag processing, but can do nada */
785
786         fprintf(logFile, "%s\n", cml_version_number);
787         Log("STARTING AFS SALVAGER %s (%s)\n", SalvageVersion, commandLine);
788
789         /* Get and hold a lock for the duration of the salvage to make sure
790          * that no other salvage runs at the same time.  The routine
791          * VInitVolumePackage (called below) makes sure that a file server or
792          * other volume utilities don't interfere with the salvage.
793          */
794         ObtainSalvageLock();
795 #ifdef AFS_NT40_ENV
796     }
797 #endif
798
799     ts = cmd_CreateSyntax("initcmd", handleit, 0, "initialize the program");
800     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL,
801                 "Name of partition to salvage");
802     cmd_AddParm(ts, "-volumeid", CMD_SINGLE, CMD_OPTIONAL,
803                 "Volume Id to salvage");
804     cmd_AddParm(ts, "-debug", CMD_FLAG, CMD_OPTIONAL,
805                 "Run in Debugging mode");
806     cmd_AddParm(ts, "-nowrite", CMD_FLAG, CMD_OPTIONAL,
807                 "Run readonly/test mode");
808     cmd_AddParm(ts, "-inodes", CMD_FLAG, CMD_OPTIONAL,
809                 "Just list affected afs inodes - debugging flag");
810     cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "Force full salvaging");
811     cmd_AddParm(ts, "-oktozap", CMD_FLAG, CMD_OPTIONAL,
812                 "Give permission to destroy bogus inodes/volumes - debugging flag");
813     cmd_AddParm(ts, "-rootinodes", CMD_FLAG, CMD_OPTIONAL,
814                 "Show inodes owned by root - debugging flag");
815     cmd_AddParm(ts, "-salvagedirs", CMD_FLAG, CMD_OPTIONAL,
816                 "Force rebuild/salvage of all directories");
817     cmd_AddParm(ts, "-blockreads", CMD_FLAG, CMD_OPTIONAL,
818                 "Read smaller blocks to handle IO/bad blocks");
819     cmd_AddParm(ts, "-parallel", CMD_SINGLE, CMD_OPTIONAL,
820                 "# of max parallel partition salvaging");
821     cmd_AddParm(ts, "-tmpdir", CMD_SINGLE, CMD_OPTIONAL,
822                 "Name of dir to place tmp files ");
823     cmd_AddParm(ts, "-showlog", CMD_FLAG, CMD_OPTIONAL,
824                 "Show log file upon completion");
825     cmd_AddParm(ts, "-showsuid", CMD_FLAG, CMD_OPTIONAL,
826                 "Report on suid/sgid files");
827     cmd_AddParm(ts, "-showmounts", CMD_FLAG, CMD_OPTIONAL,
828                 "Report on mountpoints");
829     cmd_AddParm(ts, "-orphans", CMD_SINGLE, CMD_OPTIONAL,
830                 "ignore | remove | attach");
831
832     /* note - syslog isn't avail on NT, but if we make it conditional, have
833      * to deal with screwy offsets for cmd params */
834     cmd_AddParm(ts, "-syslog", CMD_FLAG, CMD_OPTIONAL,
835                 "Write salvage log to syslogs");
836     cmd_AddParm(ts, "-syslogfacility", CMD_SINGLE, CMD_OPTIONAL,
837                 "Syslog facility number to use");
838     cmd_AddParm(ts, "-datelogs", CMD_FLAG, CMD_OPTIONAL,
839                 "Include timestamp in logfile filename");
840
841 #ifdef FAST_RESTART
842     cmd_AddParm(ts, "-DontSalvage", CMD_FLAG, CMD_OPTIONAL,
843                 "Don't salvage. This my be set in BosConfig to let the fileserver restart immediately after a crash. Bad volumes will be taken offline");
844 #endif /* FAST_RESTART */
845     err = cmd_Dispatch(argc, argv);
846     Exit(err);
847 }
848
849 /* Get the salvage lock if not already held. Hold until process exits. */
850 void
851 ObtainSalvageLock(void)
852 {
853     int salvageLock;
854
855 #ifdef AFS_NT40_ENV
856     salvageLock =
857         (int)CreateFile(AFSDIR_SERVER_SLVGLOCK_FILEPATH, 0, 0, NULL,
858                         OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
859     if (salvageLock == (int)INVALID_HANDLE_VALUE) {
860         fprintf(stderr,
861                 "salvager:  There appears to be another salvager running!  Aborted.\n");
862         Exit(1);
863     }
864 #else
865     salvageLock =
866         afs_open(AFSDIR_SERVER_SLVGLOCK_FILEPATH, O_CREAT | O_RDWR, 0666);
867     if (salvageLock < 0) {
868         fprintf(stderr,
869                 "salvager:  can't open salvage lock file %s, aborting\n",
870                 AFSDIR_SERVER_SLVGLOCK_FILEPATH);
871         Exit(1);
872     }
873 #ifdef AFS_DARWIN_ENV
874     if (flock(salvageLock, LOCK_EX) == -1) {
875 #else
876     if (lockf(salvageLock, F_LOCK, 0) == -1) {
877 #endif
878         fprintf(stderr,
879                 "salvager:  There appears to be another salvager running!  Aborted.\n");
880         Exit(1);
881     }
882 #endif
883 }
884
885
886 #ifdef AFS_SGI_XFS_IOPS_ENV
887 /* Check if the given partition is mounted. For XFS, the root inode is not a
888  * constant. So we check the hard way.
889  */
890 int
891 IsPartitionMounted(char *part)
892 {
893     FILE *mntfp;
894     struct mntent *mntent;
895
896     assert(mntfp = setmntent(MOUNTED, "r"));
897     while (mntent = getmntent(mntfp)) {
898         if (!strcmp(part, mntent->mnt_dir))
899             break;
900     }
901     endmntent(mntfp);
902
903     return mntent ? 1 : 1;
904 }
905 #endif
906 /* Check if the given inode is the root of the filesystem. */
907 #ifndef AFS_SGI_XFS_IOPS_ENV
908 int
909 IsRootInode(struct afs_stat *status)
910 {
911     /*
912      * The root inode is not a fixed value in XFS partitions. So we need to
913      * see if the partition is in the list of mounted partitions. This only 
914      * affects the SalvageFileSys path, so we check there.
915      */
916     return (status->st_ino == ROOTINODE);
917 }
918 #endif
919
920 #ifdef AFS_AIX42_ENV
921 #ifndef AFS_NAMEI_ENV
922 /* We don't want to salvage big files filesystems, since we can't put volumes on
923  * them.
924  */
925 int
926 CheckIfBigFilesFS(char *mountPoint, char *devName)
927 {
928     struct superblock fs;
929     char name[128];
930
931     if (strncmp(devName, "/dev/", 5)) {
932         (void)sprintf(name, "/dev/%s", devName);
933     } else {
934         (void)strcpy(name, devName);
935     }
936
937     if (ReadSuper(&fs, name) < 0) {
938         Log("Unable to read superblock. Not salvaging partition %s.\n",
939             mountPoint);
940         return 1;
941     }
942     if (IsBigFilesFileSystem(&fs)) {
943         Log("Partition %s is a big files filesystem, not salvaging.\n",
944             mountPoint);
945         return 1;
946     }
947     return 0;
948 }
949 #endif
950 #endif
951
952 #ifdef AFS_NT40_ENV
953 #define HDSTR "\\Device\\Harddisk"
954 #define HDLEN  (sizeof(HDSTR)-1)        /* Length of "\Device\Harddisk" */
955 int
956 SameDisk(struct DiskPartition *p1, struct DiskPartition *p2)
957 {
958 #define RES_LEN 256
959     char res[RES_LEN];
960     int d1, d2;
961     static int dowarn = 1;
962
963     if (!QueryDosDevice(p1->devName, res, RES_LEN - 1))
964         return 1;
965     if (strncmp(res, HDSTR, HDLEN)) {
966         if (dowarn) {
967             dowarn = 0;
968             Log("WARNING: QueryDosDevice is returning %s, not %s for %s\n",
969                 res, HDSTR, p1->devName);
970         }
971         return 1;
972     }
973     d1 = atoi(&res[HDLEN]);
974
975     if (!QueryDosDevice(p2->devName, res, RES_LEN - 1))
976         return 1;
977     if (strncmp(res, HDSTR, HDLEN)) {
978         if (dowarn) {
979             dowarn = 0;
980             Log("WARNING: QueryDosDevice is returning %s, not %s for %s\n",
981                 res, HDSTR, p2->devName);
982         }
983         return 1;
984     }
985     d2 = atoi(&res[HDLEN]);
986
987     return d1 == d2;
988 }
989 #else
990 #define SameDisk(P1, P2) ((P1)->device/PartsPerDisk == (P2)->device/PartsPerDisk)
991 #endif
992
993 /* This assumes that two partitions with the same device number divided by
994  * PartsPerDisk are on the same disk.
995  */
996 void
997 SalvageFileSysParallel(struct DiskPartition *partP)
998 {
999     struct job {
1000         struct DiskPartition *partP;
1001         int pid;                /* Pid for this job */
1002         int jobnumb;            /* Log file job number */
1003         struct job *nextjob;    /* Next partition on disk to salvage */
1004     };
1005     static struct job *jobs[MAXPARALLEL] = { 0 };       /* Need to zero this */
1006     struct job *thisjob = 0;
1007     static int numjobs = 0;
1008     static int jobcount = 0;
1009     char buf[1024];
1010     int wstatus;
1011     struct job *oldjob;
1012     int startjob;
1013     FILE *passLog;
1014     char logFileName[256];
1015     int i, j, pid;
1016
1017     if (partP) {
1018         /* We have a partition to salvage. Copy it into thisjob */
1019         thisjob = (struct job *)malloc(sizeof(struct job));
1020         if (!thisjob) {
1021             Log("Can't salvage '%s'. Not enough memory\n", partP->name);
1022             return;
1023         }
1024         memset(thisjob, 0, sizeof(struct job));
1025         thisjob->partP = partP;
1026         thisjob->jobnumb = jobcount;
1027         jobcount++;
1028     } else if (jobcount == 0) {
1029         /* We are asking to wait for all jobs (partp == 0), yet we never
1030          * started any.
1031          */
1032         Log("No file system partitions named %s* found; not salvaged\n",
1033             VICE_PARTITION_PREFIX);
1034         return;
1035     }
1036
1037     if (debug || Parallel == 1) {
1038         if (thisjob) {
1039             SalvageFileSys(thisjob->partP, 0);
1040             free(thisjob);
1041         }
1042         return;
1043     }
1044
1045     if (thisjob) {
1046         /* Check to see if thisjob is for a disk that we are already 
1047          * salvaging. If it is, link it in as the next job to do. The
1048          * jobs array has 1 entry per disk being salvages. numjobs is 
1049          * the total number of disks currently being salvaged. In 
1050          * order to keep thejobs array compact, when a disk is
1051          * completed, the hightest element in the jobs array is moved 
1052          * down to now open slot.
1053          */
1054         for (j = 0; j < numjobs; j++) {
1055             if (SameDisk(jobs[j]->partP, thisjob->partP)) {
1056                 /* On same disk, add it to this list and return */
1057                 thisjob->nextjob = jobs[j]->nextjob;
1058                 jobs[j]->nextjob = thisjob;
1059                 thisjob = 0;
1060                 break;
1061             }
1062         }
1063     }
1064
1065     /* Loop until we start thisjob or until all existing jobs are finished */
1066     while (thisjob || (!partP && (numjobs > 0))) {
1067         startjob = -1;          /* No new job to start */
1068
1069         if ((numjobs >= Parallel) || (!partP && (numjobs > 0))) {
1070             /* Either the max jobs are running or we have to wait for all
1071              * the jobs to finish. In either case, we wait for at least one
1072              * job to finish. When it's done, clean up after it.
1073              */
1074             pid = wait(&wstatus);
1075             assert(pid != -1);
1076             for (j = 0; j < numjobs; j++) {     /* Find which job it is */
1077                 if (pid == jobs[j]->pid)
1078                     break;
1079             }
1080             assert(j < numjobs);
1081             if (WCOREDUMP(wstatus)) {   /* Say if the job core dumped */
1082                 Log("Salvage of %s core dumped!\n", jobs[j]->partP->name);
1083             }
1084
1085             numjobs--;          /* job no longer running */
1086             oldjob = jobs[j];   /* remember */
1087             jobs[j] = jobs[j]->nextjob; /* Step to next part on same disk */
1088             free(oldjob);       /* free the old job */
1089
1090             /* If there is another partition on the disk to salvage, then 
1091              * say we will start it (startjob). If not, then put thisjob there
1092              * and say we will start it.
1093              */
1094             if (jobs[j]) {      /* Another partitions to salvage */
1095                 startjob = j;   /* Will start it */
1096             } else {            /* There is not another partition to salvage */
1097                 if (thisjob) {
1098                     jobs[j] = thisjob;  /* Add thisjob */
1099                     thisjob = 0;
1100                     startjob = j;       /* Will start it */
1101                 } else {
1102                     jobs[j] = jobs[numjobs];    /* Move last job up to this slot */
1103                     startjob = -1;      /* Don't start it - already running */
1104                 }
1105             }
1106         } else {
1107             /* We don't have to wait for a job to complete */
1108             if (thisjob) {
1109                 jobs[numjobs] = thisjob;        /* Add this job */
1110                 thisjob = 0;
1111                 startjob = numjobs;     /* Will start it */
1112             }
1113         }
1114
1115         /* Start up a new salvage job on a partition in job slot "startjob" */
1116         if (startjob != -1) {
1117             if (!Showmode)
1118                 Log("Starting salvage of file system partition %s\n",
1119                     jobs[startjob]->partP->name);
1120 #ifdef AFS_NT40_ENV
1121             /* For NT, we not only fork, but re-exec the salvager. Pass in the
1122              * commands and pass the child job number via the data path.
1123              */
1124             pid =
1125                 nt_SalvagePartition(jobs[startjob]->partP->name,
1126                                     jobs[startjob]->jobnumb);
1127             jobs[startjob]->pid = pid;
1128             numjobs++;
1129 #else
1130             pid = Fork();
1131             if (pid) {
1132                 jobs[startjob]->pid = pid;
1133                 numjobs++;
1134             } else {
1135                 int fd;
1136
1137                 ShowLog = 0;
1138                 for (fd = 0; fd < 16; fd++)
1139                     close(fd);
1140                 open("/", 0);
1141                 dup2(0, 1);
1142                 dup2(0, 2);
1143 #ifndef AFS_NT40_ENV
1144                 if (useSyslog) {
1145                     openlog("salvager", LOG_PID, useSyslogFacility);
1146                 } else
1147 #endif
1148                 {
1149                     (void)afs_snprintf(logFileName, sizeof logFileName,
1150                                        "%s.%d",
1151                                        AFSDIR_SERVER_SLVGLOG_FILEPATH,
1152                                        jobs[startjob]->jobnumb);
1153                     logFile = afs_fopen(logFileName, "w");
1154                 }
1155                 if (!logFile)
1156                     logFile = stdout;
1157
1158                 SalvageFileSys1(jobs[startjob]->partP, 0);
1159                 Exit(0);
1160             }
1161 #endif
1162         }
1163     }                           /* while ( thisjob || (!partP && numjobs > 0) ) */
1164
1165     /* If waited for all jobs to complete, now collect log files and return */
1166 #ifndef AFS_NT40_ENV
1167     if (!useSyslog)             /* if syslogging - no need to collect */
1168 #endif
1169         if (!partP) {
1170             for (i = 0; i < jobcount; i++) {
1171                 (void)afs_snprintf(logFileName, sizeof logFileName, "%s.%d",
1172                                    AFSDIR_SERVER_SLVGLOG_FILEPATH, i);
1173                 if ((passLog = afs_fopen(logFileName, "r"))) {
1174                     while (fgets(buf, sizeof(buf), passLog)) {
1175                         fputs(buf, logFile);
1176                     }
1177                     fclose(passLog);
1178                 }
1179                 (void)unlink(logFileName);
1180             }
1181             fflush(logFile);
1182         }
1183     return;
1184 }
1185
1186
1187 void
1188 SalvageFileSys(struct DiskPartition *partP, VolumeId singleVolumeNumber)
1189 {
1190     if (!canfork || debug || Fork() == 0) {
1191         SalvageFileSys1(partP, singleVolumeNumber);
1192         if (canfork && !debug) {
1193             ShowLog = 0;
1194             Exit(0);
1195         }
1196     } else
1197         Wait("SalvageFileSys");
1198 }
1199
1200 char *
1201 get_DevName(char *pbuffer, char *wpath)
1202 {
1203     char pbuf[128], *ptr;
1204     strcpy(pbuf, pbuffer);
1205     ptr = (char *)strrchr(pbuf, '/');
1206     if (ptr) {
1207         *ptr = '\0';
1208         strcpy(wpath, pbuf);
1209     } else
1210         return NULL;
1211     ptr = (char *)strrchr(pbuffer, '/');
1212     if (ptr) {
1213         strcpy(pbuffer, ptr + 1);
1214         return pbuffer;
1215     } else
1216         return NULL;
1217 }
1218
1219 void
1220 SalvageFileSys1(struct DiskPartition *partP, VolumeId singleVolumeNumber)
1221 {
1222     char *name, *tdir;
1223     char inodeListPath[256];
1224     static char tmpDevName[100];
1225     static char wpath[100];
1226     struct VolumeSummary *vsp, *esp;
1227     int i, j;
1228
1229     fileSysPartition = partP;
1230     fileSysDevice = fileSysPartition->device;
1231     fileSysPathName = VPartitionPath(fileSysPartition);
1232
1233 #ifdef AFS_NT40_ENV
1234     /* Opendir can fail on "C:" but not on "C:\" if C is empty! */
1235     (void)sprintf(fileSysPath, "%s\\", fileSysPathName);
1236     name = partP->devName;
1237 #else
1238     fileSysPath = fileSysPathName;
1239     strcpy(tmpDevName, partP->devName);
1240     name = get_DevName(tmpDevName, wpath);
1241     fileSysDeviceName = name;
1242     filesysfulldev = wpath;
1243 #endif
1244
1245     VLockPartition(partP->name);
1246     if (singleVolumeNumber || ForceSalvage)
1247         ForceSalvage = 1;
1248     else
1249         ForceSalvage = UseTheForceLuke(fileSysPath);
1250
1251     if (singleVolumeNumber) {
1252         if (!VConnectFS()) {
1253             Abort("Couldn't connect to file server\n");
1254         }
1255         AskOffline(singleVolumeNumber);
1256     } else {
1257         if (!Showmode)
1258             Log("SALVAGING FILE SYSTEM PARTITION %s (device=%s%s)\n",
1259                 partP->name, name, (Testing ? "(READONLY mode)" : ""));
1260         if (ForceSalvage)
1261             Log("***Forced salvage of all volumes on this partition***\n");
1262     }
1263
1264
1265     /*
1266      * Remove any leftover /vicepa/salvage.inodes.* or /vicepa/salvage.temp.* 
1267      * files 
1268      */
1269     {
1270         DIR *dirp;
1271         struct dirent *dp;
1272
1273         assert((dirp = opendir(fileSysPath)) != NULL);
1274         while ((dp = readdir(dirp))) {
1275             if (!strncmp(dp->d_name, "salvage.inodes.", 15)
1276                 || !strncmp(dp->d_name, "salvage.temp.", 13)) {
1277                 char npath[1024];
1278                 Log("Removing old salvager temp files %s\n", dp->d_name);
1279                 strcpy(npath, fileSysPath);
1280                 strcat(npath, "/");
1281                 strcat(npath, dp->d_name);
1282                 unlink(npath);
1283             }
1284         }
1285         closedir(dirp);
1286     }
1287     tdir = (tmpdir ? tmpdir : fileSysPath);
1288 #ifdef AFS_NT40_ENV
1289     (void)_putenv("TMP=");      /* If "TMP" is set, then that overrides tdir. */
1290     (void)strncpy(inodeListPath, _tempnam(tdir, "salvage.inodes."), 255);
1291 #else
1292     snprintf(inodeListPath, 255, "%s/salvage.inodes.%s.%d", tdir, name,
1293              getpid());
1294 #endif
1295     if (GetInodeSummary(inodeListPath, singleVolumeNumber) < 0) {
1296         unlink(inodeListPath);
1297         return;
1298     }
1299 #ifdef AFS_NT40_ENV
1300     /* Using nt_unlink here since we're really using the delete on close
1301      * semantics of unlink. In most places in the salvager, we really do
1302      * mean to unlink the file at that point. Those places have been
1303      * modified to actually do that so that the NT crt can be used there.
1304      */
1305     inodeFd =
1306         _open_osfhandle((long)nt_open(inodeListPath, O_RDWR, 0), O_RDWR);
1307     nt_unlink(inodeListPath);   /* NT's crt unlink won't if file is open. */
1308 #else
1309     inodeFd = afs_open(inodeListPath, O_RDONLY);
1310     unlink(inodeListPath);
1311 #endif
1312     if (inodeFd == -1)
1313         Abort("Temporary file %s is missing...\n", inodeListPath);
1314     if (ListInodeOption) {
1315         PrintInodeList();
1316         return;
1317     }
1318     /* enumerate volumes in the partition.
1319      * figure out sets of read-only + rw volumes.
1320      * salvage each set, read-only volumes first, then read-write.
1321      * Fix up inodes on last volume in set (whether it is read-write
1322      * or read-only).
1323      */
1324     GetVolumeSummary(singleVolumeNumber);
1325
1326     for (i = j = 0, vsp = volumeSummaryp, esp = vsp + nVolumes;
1327          i < nVolumesInInodeFile; i = j) {
1328         VolumeId rwvid = inodeSummary[i].RWvolumeId;
1329         for (j = i;
1330              j < nVolumesInInodeFile && inodeSummary[j].RWvolumeId == rwvid;
1331              j++) {
1332             VolumeId vid = inodeSummary[j].volumeId;
1333             struct VolumeSummary *tsp;
1334             /* Scan volume list (from partition root directory) looking for the
1335              * current rw volume number in the volume list from the inode scan.
1336              * If there is one here that is not in the inode volume list,
1337              * delete it now. */
1338             for (; vsp < esp && (vsp->header.parent < rwvid); vsp++) {
1339                 if (vsp->fileName)
1340                     DeleteExtraVolumeHeaderFile(vsp);
1341             }
1342             /* Now match up the volume summary info from the root directory with the
1343              * entry in the volume list obtained from scanning inodes */
1344             inodeSummary[j].volSummary = NULL;
1345             for (tsp = vsp; tsp < esp && (tsp->header.parent == rwvid); tsp++) {
1346                 if (tsp->header.id == vid) {
1347                     inodeSummary[j].volSummary = tsp;
1348                     tsp->fileName = 0;
1349                     break;
1350                 }
1351             }
1352         }
1353         /* Salvage the group of volumes (several read-only + 1 read/write)
1354          * starting with the current read-only volume we're looking at.
1355          */
1356         SalvageVolumeGroup(&inodeSummary[i], j - i);
1357     }
1358
1359     /* Delete any additional volumes that were listed in the partition but which didn't have any corresponding inodes */
1360     for (; vsp < esp; vsp++) {
1361         if (vsp->fileName)
1362             DeleteExtraVolumeHeaderFile(vsp);
1363     }
1364
1365     if (!singleVolumeNumber)    /* Remove the FORCESALVAGE file */
1366         RemoveTheForce(fileSysPath);
1367
1368     if (!Testing && singleVolumeNumber) {
1369         AskOnline(singleVolumeNumber, fileSysPartition->name);
1370
1371         /* Step through the volumeSummary list and set all volumes on-line.
1372          * The volumes were taken off-line in GetVolumeSummary.
1373          */
1374         for (j = 0; j < nVolumes; j++) {
1375             AskOnline(volumeSummaryp[j].header.id, fileSysPartition->name);
1376         }
1377     } else {
1378         if (!Showmode)
1379             Log("SALVAGING OF PARTITION %s%s COMPLETED\n",
1380                 fileSysPartition->name, (Testing ? " (READONLY mode)" : ""));
1381     }
1382
1383     close(inodeFd);             /* SalvageVolumeGroup was the last which needed it. */
1384 }
1385
1386 void
1387 DeleteExtraVolumeHeaderFile(register struct VolumeSummary *vsp)
1388 {
1389     if (!Showmode)
1390         Log("The volume header file %s is not associated with any actual data (%sdeleted)\n", vsp->fileName, (Testing ? "would have been " : ""));
1391     if (!Testing)
1392         unlink(vsp->fileName);
1393     vsp->fileName = 0;
1394 }
1395
1396 CompareInodes(const void *_p1, const void *_p2)
1397 {
1398     register const struct ViceInodeInfo *p1 = _p1;
1399     register const struct ViceInodeInfo *p2 = _p2;
1400     if (p1->u.vnode.vnodeNumber == INODESPECIAL
1401         || p2->u.vnode.vnodeNumber == INODESPECIAL) {
1402         VolumeId p1rwid, p2rwid;
1403         p1rwid =
1404             (p1->u.vnode.vnodeNumber ==
1405              INODESPECIAL ? p1->u.special.parentId : p1->u.vnode.volumeId);
1406         p2rwid =
1407             (p2->u.vnode.vnodeNumber ==
1408              INODESPECIAL ? p2->u.special.parentId : p2->u.vnode.volumeId);
1409         if (p1rwid < p2rwid)
1410             return -1;
1411         if (p1rwid > p2rwid)
1412             return 1;
1413         if (p1->u.vnode.vnodeNumber == INODESPECIAL
1414             && p2->u.vnode.vnodeNumber == INODESPECIAL) {
1415             if (p1->u.vnode.volumeId == p2->u.vnode.volumeId)
1416                 return (p1->u.special.type < p2->u.special.type ? -1 : 1);
1417             if (p1->u.vnode.volumeId == p1rwid)
1418                 return -1;
1419             if (p2->u.vnode.volumeId == p2rwid)
1420                 return 1;
1421             return (p1->u.vnode.volumeId < p2->u.vnode.volumeId ? -1 : 1);
1422         }
1423         if (p1->u.vnode.vnodeNumber != INODESPECIAL)
1424             return (p2->u.vnode.volumeId == p2rwid ? 1 : -1);
1425         return (p1->u.vnode.volumeId == p1rwid ? -1 : 1);
1426     }
1427     if (p1->u.vnode.volumeId < p2->u.vnode.volumeId)
1428         return -1;
1429     if (p1->u.vnode.volumeId > p2->u.vnode.volumeId)
1430         return 1;
1431     if (p1->u.vnode.vnodeNumber < p2->u.vnode.vnodeNumber)
1432         return -1;
1433     if (p1->u.vnode.vnodeNumber > p2->u.vnode.vnodeNumber)
1434         return 1;
1435     /* The following tests are reversed, so that the most desirable
1436      * of several similar inodes comes first */
1437     if (p1->u.vnode.vnodeUniquifier > p2->u.vnode.vnodeUniquifier) {
1438 #ifdef  AFS_3DISPARES
1439         if (p1->u.vnode.vnodeUniquifier > 3775414 /* 90% of 4.2M */  &&
1440             p2->u.vnode.vnodeUniquifier < 419490 /* 10% of 4.2M */ )
1441             return 1;
1442 #endif
1443 #ifdef  AFS_SGI_EXMAG
1444         if (p1->u.vnode.vnodeUniquifier > 15099494 /* 90% of 16M */  &&
1445             p2->u.vnode.vnodeUniquifier < 1677721 /* 10% of 16M */ )
1446             return 1;
1447 #endif
1448         return -1;
1449     }
1450     if (p1->u.vnode.vnodeUniquifier < p2->u.vnode.vnodeUniquifier) {
1451 #ifdef  AFS_3DISPARES
1452         if (p2->u.vnode.vnodeUniquifier > 3775414 /* 90% of 4.2M */  &&
1453             p1->u.vnode.vnodeUniquifier < 419490 /* 10% of 4.2M */ )
1454             return -1;
1455 #endif
1456 #ifdef  AFS_SGI_EXMAG
1457         if (p2->u.vnode.vnodeUniquifier > 15099494 /* 90% of 16M */  &&
1458             p1->u.vnode.vnodeUniquifier < 1677721 /* 10% of 16M */ )
1459             return 1;
1460 #endif
1461         return 1;
1462     }
1463     if (p1->u.vnode.inodeDataVersion > p2->u.vnode.inodeDataVersion) {
1464 #ifdef  AFS_3DISPARES
1465         if (p1->u.vnode.inodeDataVersion > 1887437 /* 90% of 2.1M */  &&
1466             p2->u.vnode.inodeDataVersion < 209716 /* 10% of 2.1M */ )
1467             return 1;
1468 #endif
1469 #ifdef  AFS_SGI_EXMAG
1470         if (p1->u.vnode.inodeDataVersion > 15099494 /* 90% of 16M */  &&
1471             p2->u.vnode.inodeDataVersion < 1677721 /* 10% of 16M */ )
1472             return 1;
1473 #endif
1474         return -1;
1475     }
1476     if (p1->u.vnode.inodeDataVersion < p2->u.vnode.inodeDataVersion) {
1477 #ifdef  AFS_3DISPARES
1478         if (p2->u.vnode.inodeDataVersion > 1887437 /* 90% of 2.1M */  &&
1479             p1->u.vnode.inodeDataVersion < 209716 /* 10% of 2.1M */ )
1480             return -1;
1481 #endif
1482 #ifdef  AFS_SGI_EXMAG
1483         if (p2->u.vnode.inodeDataVersion > 15099494 /* 90% of 16M */  &&
1484             p1->u.vnode.inodeDataVersion < 1677721 /* 10% of 16M */ )
1485             return 1;
1486 #endif
1487         return 1;
1488     }
1489     return 0;
1490 }
1491
1492 void
1493 CountVolumeInodes(register struct ViceInodeInfo *ip, int maxInodes,
1494                   register struct InodeSummary *summary)
1495 {
1496     int volume = ip->u.vnode.volumeId;
1497     int rwvolume = volume;
1498     register n, nSpecial;
1499     register Unique maxunique;
1500     n = nSpecial = 0;
1501     maxunique = 0;
1502     while (maxInodes-- && volume == ip->u.vnode.volumeId) {
1503         n++;
1504         if (ip->u.vnode.vnodeNumber == INODESPECIAL) {
1505             nSpecial++;
1506             rwvolume = ip->u.special.parentId;
1507             /* This isn't quite right, as there could (in error) be different
1508              * parent inodes in different special vnodes */
1509         } else {
1510             if (maxunique < ip->u.vnode.vnodeUniquifier)
1511                 maxunique = ip->u.vnode.vnodeUniquifier;
1512         }
1513         ip++;
1514     }
1515     summary->volumeId = volume;
1516     summary->RWvolumeId = rwvolume;
1517     summary->nInodes = n;
1518     summary->nSpecialInodes = nSpecial;
1519     summary->maxUniquifier = maxunique;
1520 }
1521
1522 int
1523 OnlyOneVolume(struct ViceInodeInfo *inodeinfo, VolumeId singleVolumeNumber, void *rock)
1524 {
1525     if (inodeinfo->u.vnode.vnodeNumber == INODESPECIAL)
1526         return (inodeinfo->u.special.parentId == singleVolumeNumber);
1527     return (inodeinfo->u.vnode.volumeId == singleVolumeNumber);
1528 }
1529
1530 /* GetInodeSummary
1531  *
1532  * Collect list of inodes in file named by path. If a truly fatal error,
1533  * unlink the file and abort. For lessor errors, return -1. The file will
1534  * be unlinked by the caller.
1535  */
1536 int
1537 GetInodeSummary(char *path, VolumeId singleVolumeNumber)
1538 {
1539     struct afs_stat status;
1540     int forceSal, err;
1541     struct ViceInodeInfo *ip;
1542     struct InodeSummary summary;
1543     char summaryFileName[50];
1544     FILE *summaryFile;
1545 #ifdef AFS_NT40_ENV
1546     char *dev = fileSysPath;
1547     char *wpath = fileSysPath;
1548 #else
1549     char *dev = fileSysDeviceName;
1550     char *wpath = filesysfulldev;
1551 #endif
1552     char *part = fileSysPath;
1553     char *tdir;
1554
1555     /* This file used to come from vfsck; cobble it up ourselves now... */
1556     if ((err =
1557          ListViceInodes(dev, fileSysPath, path,
1558                         singleVolumeNumber ? OnlyOneVolume : 0,
1559                         singleVolumeNumber, &forceSal, forceR, wpath, NULL)) < 0) {
1560         if (err == -2) {
1561             Log("*** I/O error %d when writing a tmp inode file %s; Not salvaged %s ***\nIncrease space on partition or use '-tmpdir'\n", errno, path, dev);
1562             return -1;
1563         }
1564         unlink(path);
1565         Abort("Unable to get inodes for \"%s\"; not salvaged\n", dev);
1566     }
1567     if (forceSal && !ForceSalvage) {
1568         Log("***Forced salvage of all volumes on this partition***\n");
1569         ForceSalvage = 1;
1570     }
1571     inodeFd = afs_open(path, O_RDWR);
1572     if (inodeFd == -1 || afs_fstat(inodeFd, &status) == -1) {
1573         unlink(path);
1574         Abort("No inode description file for \"%s\"; not salvaged\n", dev);
1575     }
1576     tdir = (tmpdir ? tmpdir : part);
1577 #ifdef AFS_NT40_ENV
1578     (void)_putenv("TMP=");      /* If "TMP" is set, then that overrides tdir. */
1579     (void)strcpy(summaryFileName, _tempnam(tdir, "salvage.temp"));
1580 #else
1581     (void)afs_snprintf(summaryFileName, sizeof summaryFileName,
1582                        "%s/salvage.temp.%d", tdir, getpid());
1583 #endif
1584     summaryFile = afs_fopen(summaryFileName, "a+");
1585     if (summaryFile == NULL) {
1586         close(inodeFd);
1587         unlink(path);
1588         Abort("Unable to create inode summary file\n");
1589     }
1590     if (!canfork || debug || Fork() == 0) {
1591         int nInodes;
1592         unsigned long st_size=(unsigned long) status.st_size;
1593         nInodes = st_size / sizeof(struct ViceInodeInfo);
1594         if (nInodes == 0) {
1595             fclose(summaryFile);
1596             close(inodeFd);
1597             unlink(summaryFileName);
1598             if (!singleVolumeNumber)    /* Remove the FORCESALVAGE file */
1599                 RemoveTheForce(fileSysPath);
1600             else {
1601                 struct VolumeSummary *vsp;
1602                 int i;
1603
1604                 GetVolumeSummary(singleVolumeNumber);
1605
1606                 for (i = 0, vsp = volumeSummaryp; i < nVolumes; i++) {
1607                     if (vsp->fileName)
1608                         DeleteExtraVolumeHeaderFile(vsp);
1609                 }
1610             }
1611             Log("%s vice inodes on %s; not salvaged\n",
1612                 singleVolumeNumber ? "No applicable" : "No", dev);
1613             return -1;
1614         }
1615         ip = (struct ViceInodeInfo *)malloc(nInodes*sizeof(struct ViceInodeInfo));
1616         if (ip == NULL) {
1617             fclose(summaryFile);
1618             close(inodeFd);
1619             unlink(path);
1620             unlink(summaryFileName);
1621             Abort
1622                 ("Unable to allocate enough space to read inode table; %s not salvaged\n",
1623                  dev);
1624         }
1625         if (read(inodeFd, ip, st_size) != st_size) {
1626             fclose(summaryFile);
1627             close(inodeFd);
1628             unlink(path);
1629             unlink(summaryFileName);
1630             Abort("Unable to read inode table; %s not salvaged\n", dev);
1631         }
1632         qsort(ip, nInodes, sizeof(struct ViceInodeInfo), CompareInodes);
1633         if (afs_lseek(inodeFd, 0, SEEK_SET) == -1
1634             || write(inodeFd, ip, st_size) != st_size) {
1635             fclose(summaryFile);
1636             close(inodeFd);
1637             unlink(path);
1638             unlink(summaryFileName);
1639             Abort("Unable to rewrite inode table; %s not salvaged\n", dev);
1640         }
1641         summary.index = 0;
1642         while (nInodes) {
1643             CountVolumeInodes(ip, nInodes, &summary);
1644             if (fwrite(&summary, sizeof(summary), 1, summaryFile) != 1) {
1645                 Log("Difficulty writing summary file (errno = %d); %s not salvaged\n", errno, dev);
1646                 fclose(summaryFile);
1647                 close(inodeFd);
1648                 return -1;
1649             }
1650             summary.index += (summary.nInodes);
1651             nInodes -= summary.nInodes;
1652             ip += summary.nInodes;
1653         }
1654         /* Following fflush is not fclose, because if it was debug mode would not work */
1655         if (fflush(summaryFile) == EOF || fsync(fileno(summaryFile)) == -1) {
1656             Log("Unable to write summary file (errno = %d); %s not salvaged\n", errno, dev);
1657             fclose(summaryFile);
1658             close(inodeFd);
1659             return -1;
1660         }
1661         if (canfork && !debug) {
1662             ShowLog = 0;
1663             Exit(0);
1664         }
1665     } else {
1666         if (Wait("Inode summary") == -1) {
1667             fclose(summaryFile);
1668             close(inodeFd);
1669             unlink(path);
1670             unlink(summaryFileName);
1671             Exit(1);            /* salvage of this partition aborted */
1672         }
1673     }
1674     assert(afs_fstat(fileno(summaryFile), &status) != -1);
1675     if (status.st_size != 0) {
1676         int ret;
1677         unsigned long st_status=(unsigned long)status.st_size;
1678         inodeSummary = (struct InodeSummary *)malloc(st_status);
1679         assert(inodeSummary != NULL);
1680         /* For GNU we need to do lseek to get the file pointer moved. */
1681         assert(afs_lseek(fileno(summaryFile), 0, SEEK_SET) == 0);
1682         ret = read(fileno(summaryFile), inodeSummary, st_status);
1683         assert(ret == st_status);
1684     }
1685     nVolumesInInodeFile =(unsigned long)(status.st_size) / sizeof(struct InodeSummary);
1686     Log("%d nVolumesInInodeFile %d \n",nVolumesInInodeFile,(unsigned long)(status.st_size));
1687     fclose(summaryFile);
1688     close(inodeFd);
1689     unlink(summaryFileName);
1690     return 0;
1691 }
1692
1693 /* Comparison routine for volume sort.
1694    This is setup so that a read-write volume comes immediately before
1695    any read-only clones of that volume */
1696 int
1697 CompareVolumes(const void *_p1, const void *_p2)
1698 {
1699     register const struct VolumeSummary *p1 = _p1;
1700     register const struct VolumeSummary *p2 = _p2;
1701     if (p1->header.parent != p2->header.parent)
1702         return p1->header.parent < p2->header.parent ? -1 : 1;
1703     if (p1->header.id == p1->header.parent)     /* p1 is rw volume */
1704         return -1;
1705     if (p2->header.id == p2->header.parent)     /* p2 is rw volume */
1706         return 1;
1707     return p1->header.id < p2->header.id ? -1 : 1;      /* Both read-only */
1708 }
1709
1710 void
1711 GetVolumeSummary(VolumeId singleVolumeNumber)
1712 {
1713     DIR *dirp;
1714     afs_int32 nvols = 0;
1715     struct VolumeSummary *vsp, vs;
1716     struct VolumeDiskHeader diskHeader;
1717     struct dirent *dp;
1718
1719     /* Get headers from volume directory */
1720     if (chdir(fileSysPath) == -1 || (dirp = opendir(".")) == NULL)
1721         Abort("Can't read directory %s; not salvaged\n", fileSysPath);
1722     if (!singleVolumeNumber) {
1723         while ((dp = readdir(dirp))) {
1724             char *p = dp->d_name;
1725             p = strrchr(dp->d_name, '.');
1726             if (p != NULL && strcmp(p, VHDREXT) == 0) {
1727                 int fd;
1728                 if ((fd = afs_open(dp->d_name, O_RDONLY)) != -1
1729                     && read(fd, (char *)&diskHeader, sizeof(diskHeader))
1730                     == sizeof(diskHeader)
1731                     && diskHeader.stamp.magic == VOLUMEHEADERMAGIC) {
1732                     DiskToVolumeHeader(&vs.header, &diskHeader);
1733                     nvols++;
1734                 }
1735                 close(fd);
1736             }
1737         }
1738 #ifdef AFS_NT40_ENV
1739         closedir(dirp);
1740         dirp = opendir(".");    /* No rewinddir for NT */
1741 #else
1742         rewinddir(dirp);
1743 #endif
1744         if (!nvols)
1745             nvols = 1;
1746         volumeSummaryp =
1747             (struct VolumeSummary *)malloc(nvols *
1748                                            sizeof(struct VolumeSummary));
1749     } else
1750         volumeSummaryp =
1751             (struct VolumeSummary *)malloc(20 * sizeof(struct VolumeSummary));
1752     assert(volumeSummaryp != NULL);
1753
1754     nVolumes = 0;
1755     vsp = volumeSummaryp;
1756     while ((dp = readdir(dirp))) {
1757         char *p = dp->d_name;
1758         p = strrchr(dp->d_name, '.');
1759         if (p != NULL && strcmp(p, VHDREXT) == 0) {
1760             int error = 0;
1761             int fd;
1762             if ((fd = afs_open(dp->d_name, O_RDONLY)) == -1
1763                 || read(fd, &diskHeader, sizeof(diskHeader))
1764                 != sizeof(diskHeader)
1765                 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
1766                 error = 1;
1767             }
1768             close(fd);
1769             if (error) {
1770                 if (!singleVolumeNumber) {
1771                     if (!Showmode)
1772                         Log("%s/%s is not a legitimate volume header file; %sdeleted\n", fileSysPathName, dp->d_name, (Testing ? "it would have been " : ""));
1773                     if (!Testing)
1774                         unlink(dp->d_name);
1775                 }
1776             } else {
1777                 char nameShouldBe[64];
1778                 DiskToVolumeHeader(&vsp->header, &diskHeader);
1779                 if (singleVolumeNumber && vsp->header.id == singleVolumeNumber
1780                     && vsp->header.parent != singleVolumeNumber) {
1781                     Log("%u is a read-only volume; not salvaged\n",
1782                         singleVolumeNumber);
1783                     Exit(1);
1784                 }
1785                 if (!singleVolumeNumber
1786                     || (vsp->header.id == singleVolumeNumber
1787                         || vsp->header.parent == singleVolumeNumber)) {
1788                     (void)afs_snprintf(nameShouldBe, sizeof nameShouldBe,
1789                                        VFORMAT, vsp->header.id);
1790                     if (singleVolumeNumber)
1791                         AskOffline(vsp->header.id);
1792                     if (strcmp(nameShouldBe, dp->d_name)) {
1793                         if (!Showmode)
1794                             Log("Volume header file %s is incorrectly named; %sdeleted (it will be recreated later, if necessary)\n", dp->d_name, (Testing ? "it would have been " : ""));
1795                         if (!Testing)
1796                             unlink(dp->d_name);
1797                     } else {
1798                         vsp->fileName = ToString(dp->d_name);
1799                         nVolumes++;
1800                         vsp++;
1801                     }
1802                 }
1803             }
1804             close(fd);
1805         }
1806     }
1807     closedir(dirp);
1808     qsort(volumeSummaryp, nVolumes, sizeof(struct VolumeSummary),
1809           CompareVolumes);
1810 }
1811
1812 /* Find the link table. This should be associated with the RW volume or, if
1813  * a RO only site, then the RO volume. For now, be cautious and hunt carefully.
1814  */
1815 Inode
1816 FindLinkHandle(register struct InodeSummary *isp, int nVols,
1817                struct ViceInodeInfo *allInodes)
1818 {
1819     int i, j;
1820     struct ViceInodeInfo *ip;
1821
1822     for (i = 0; i < nVols; i++) {
1823         ip = allInodes + isp[i].index;
1824         for (j = 0; j < isp[i].nSpecialInodes; j++) {
1825             if (ip[j].u.special.type == VI_LINKTABLE)
1826                 return ip[j].inodeNumber;
1827         }
1828     }
1829     return (Inode) - 1;
1830 }
1831
1832 int
1833 CreateLinkTable(register struct InodeSummary *isp, Inode ino)
1834 {
1835     struct versionStamp version;
1836     FdHandle_t *fdP;
1837
1838     if (!VALID_INO(ino))
1839         ino =
1840             IH_CREATE(NULL, fileSysDevice, fileSysPath, 0, isp->volumeId,
1841                       INODESPECIAL, VI_LINKTABLE, isp->RWvolumeId);
1842     if (!VALID_INO(ino))
1843         Abort
1844             ("Unable to allocate link table inode for volume %u (error = %d)\n",
1845              isp->RWvolumeId, errno);
1846     IH_INIT(VGLinkH, fileSysDevice, isp->RWvolumeId, ino);
1847     fdP = IH_OPEN(VGLinkH);
1848     if (fdP == NULL)
1849         Abort("Can't open link table for volume %u (error = %d)\n",
1850               isp->RWvolumeId, errno);
1851
1852     if (FDH_TRUNC(fdP, 0) < 0)
1853         Abort("Can't truncate link table for volume %u (error = %d)\n",
1854               isp->RWvolumeId, errno);
1855
1856     version.magic = LINKTABLEMAGIC;
1857     version.version = LINKTABLEVERSION;
1858
1859     if (FDH_WRITE(fdP, (char *)&version, sizeof(version))
1860         != sizeof(version))
1861         Abort("Can't truncate link table for volume %u (error = %d)\n",
1862               isp->RWvolumeId, errno);
1863
1864     FDH_REALLYCLOSE(fdP);
1865
1866     /* If the volume summary exits (i.e.,  the V*.vol header file exists),
1867      * then set this inode there as well.
1868      */
1869     if (isp->volSummary)
1870         isp->volSummary->header.linkTable = ino;
1871
1872     return 0;
1873 }
1874
1875 #ifdef AFS_NT40_ENV
1876 void *
1877 nt_SVG(void *arg)
1878 {
1879     SVGParms_t *parms = (SVGParms_t *) arg;
1880     DoSalvageVolumeGroup(parms->svgp_inodeSummaryp, parms->svgp_count);
1881     return NULL;
1882 }
1883
1884 void
1885 SalvageVolumeGroup(register struct InodeSummary *isp, int nVols)
1886 {
1887     pthread_t tid;
1888     pthread_attr_t tattr;
1889     int code;
1890     SVGParms_t parms;
1891
1892     /* Initialize per volume global variables, even if later code does so */
1893     VolumeChanged = 0;
1894     VGLinkH = NULL;
1895     VGLinkH_cnt = 0;
1896     memset(&VolInfo, 0, sizeof(VolInfo));
1897
1898     parms.svgp_inodeSummaryp = isp;
1899     parms.svgp_count = nVols;
1900     code = pthread_attr_init(&tattr);
1901     if (code) {
1902         Log("Failed to salvage volume group %u: pthread_attr_init()\n",
1903             isp->RWvolumeId);
1904         return;
1905     }
1906     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
1907     if (code) {
1908         Log("Failed to salvage volume group %u: pthread_attr_setdetachstate()\n", isp->RWvolumeId);
1909         return;
1910     }
1911     code = pthread_create(&tid, &tattr, nt_SVG, &parms);
1912     if (code) {
1913         Log("Failed to create thread to salvage volume group %u\n",
1914             isp->RWvolumeId);
1915         return;
1916     }
1917     (void)pthread_join(tid, NULL);
1918 }
1919 #endif /* AFS_NT40_ENV */
1920
1921 void
1922 DoSalvageVolumeGroup(register struct InodeSummary *isp, int nVols)
1923 {
1924     struct ViceInodeInfo *inodes, *allInodes, *ip;
1925     int i, totalInodes, size, salvageTo;
1926     int haveRWvolume;
1927     int check;
1928     Inode ino;
1929     int dec_VGLinkH = 0;
1930     int VGLinkH_p1;
1931     FdHandle_t *fdP = NULL;
1932
1933     VGLinkH_cnt = 0;
1934     haveRWvolume = (isp->volumeId == isp->RWvolumeId
1935                     && isp->nSpecialInodes > 0);
1936     if ((!ShowMounts) || (ShowMounts && !haveRWvolume)) {
1937         if (!ForceSalvage && QuickCheck(isp, nVols))
1938             return;
1939     }
1940     if (ShowMounts && !haveRWvolume)
1941         return;
1942     if (canfork && !debug && Fork() != 0) {
1943         (void)Wait("Salvage volume group");
1944         return;
1945     }
1946     for (i = 0, totalInodes = 0; i < nVols; i++)
1947         totalInodes += isp[i].nInodes;
1948     size = totalInodes * sizeof(struct ViceInodeInfo);
1949     inodes = (struct ViceInodeInfo *)malloc(size);
1950     allInodes = inodes - isp->index;    /* this would the base of all the inodes
1951                                          * for the partition, if all the inodes
1952                                          * had been read into memory */
1953     assert(afs_lseek
1954            (inodeFd, isp->index * sizeof(struct ViceInodeInfo),
1955             SEEK_SET) != -1);
1956     assert(read(inodeFd, inodes, size) == size);
1957
1958     /* Don't try to salvage a read write volume if there isn't one on this
1959      * partition */
1960     salvageTo = haveRWvolume ? 0 : 1;
1961
1962 #ifdef AFS_NAMEI_ENV
1963     ino = FindLinkHandle(isp, nVols, allInodes);
1964     if (VALID_INO(ino)) {
1965         IH_INIT(VGLinkH, fileSysDevice, isp->RWvolumeId, ino);
1966         fdP = IH_OPEN(VGLinkH);
1967     }
1968     if (!VALID_INO(ino) || fdP == NULL) {
1969         Log("%s link table for volume %u.\n",
1970             Testing ? "Would have recreated" : "Recreating", isp->RWvolumeId);
1971         if (Testing) {
1972             IH_INIT(VGLinkH, fileSysDevice, -1, -1);
1973         } else {
1974             CreateLinkTable(isp, ino);
1975         }
1976     }
1977     if (fdP)
1978         FDH_REALLYCLOSE(fdP);
1979 #else
1980     IH_INIT(VGLinkH, fileSysDevice, -1, -1);
1981 #endif
1982
1983     /* Salvage in reverse order--read/write volume last; this way any
1984      * Inodes not referenced by the time we salvage the read/write volume
1985      * can be picked up by the read/write volume */
1986     /* ACTUALLY, that's not done right now--the inodes just vanish */
1987     for (i = nVols - 1; i >= salvageTo; i--) {
1988         int rw = (i == 0);
1989         struct InodeSummary *lisp = &isp[i];
1990 #ifdef AFS_NAMEI_ENV
1991         /* If only the RO is present on this partition, the link table
1992          * shows up as a RW volume special file. Need to make sure the
1993          * salvager doesn't try to salvage the non-existent RW.
1994          */
1995         if (rw && nVols > 1 && isp[i].nSpecialInodes == 1) {
1996             /* If this only special inode is the link table, continue */
1997             if (inodes->u.special.type == VI_LINKTABLE) {
1998                 haveRWvolume = 0;
1999                 continue;
2000             }
2001         }
2002 #endif
2003         if (!Showmode)
2004             Log("%s VOLUME %u%s.\n", rw ? "SALVAGING" : "CHECKING CLONED",
2005                 lisp->volumeId, (Testing ? "(READONLY mode)" : ""));
2006         /* Check inodes twice.  The second time do things seriously.  This
2007          * way the whole RO volume can be deleted, below, if anything goes wrong */
2008         for (check = 1; check >= 0; check--) {
2009             int deleteMe;
2010             if (SalvageVolumeHeaderFile(lisp, allInodes, rw, check, &deleteMe)
2011                 == -1) {
2012                 MaybeZapVolume(lisp, "Volume header", deleteMe, check);
2013                 if (rw && deleteMe) {
2014                     haveRWvolume = 0;   /* This will cause its inodes to be deleted--since salvage
2015                                          * volume won't be called */
2016                     break;
2017                 }
2018                 if (!rw)
2019                     break;
2020             }
2021             if (rw && check == 1)
2022                 continue;
2023             if (SalvageVnodes(isp, lisp, allInodes, check) == -1) {
2024                 MaybeZapVolume(lisp, "Vnode index", 0, check);
2025                 break;
2026             }
2027         }
2028     }
2029
2030     /* Fix actual inode counts */
2031     if (!Showmode) {
2032         Log("totalInodes %d\n",totalInodes);
2033         for (ip = inodes; totalInodes; ip++, totalInodes--) {
2034             static int TraceBadLinkCounts = 0;
2035 #ifdef AFS_NAMEI_ENV
2036             if (VGLinkH->ih_ino == ip->inodeNumber) {
2037                 dec_VGLinkH = ip->linkCount - VGLinkH_cnt;
2038                 VGLinkH_p1 = ip->u.param[0];
2039                 continue;       /* Deal with this last. */
2040             }
2041 #endif
2042             if (ip->linkCount != 0 && TraceBadLinkCounts) {
2043                 TraceBadLinkCounts--;   /* Limit reports, per volume */
2044                 Log("#### DEBUG #### Link count incorrect by %d; inode %s, size %llu, p=(%u,%u,%u,%u)\n", ip->linkCount, PrintInode(NULL, ip->inodeNumber), (afs_uintmax_t) ip->byteCount, ip->u.param[0], ip->u.param[1], ip->u.param[2], ip->u.param[3]);
2045             }
2046             while (ip->linkCount > 0) {
2047                 /* below used to assert, not break */
2048                 if (!Testing) {
2049                     if (IH_DEC(VGLinkH, ip->inodeNumber, ip->u.param[0])) {
2050                         Log("idec failed. inode %s errno %d\n",
2051                             PrintInode(NULL, ip->inodeNumber), errno);
2052                         break;
2053                     }
2054                 }
2055                 ip->linkCount--;
2056             }
2057             while (ip->linkCount < 0) {
2058                 /* these used to be asserts */
2059                 if (!Testing) {
2060                     if (IH_INC(VGLinkH, ip->inodeNumber, ip->u.param[0])) {
2061                         Log("iinc failed. inode %s errno %d\n",
2062                             PrintInode(NULL, ip->inodeNumber), errno);
2063                         break;
2064                     }
2065                 }
2066                 ip->linkCount++;
2067             }
2068         }
2069 #ifdef AFS_NAMEI_ENV
2070         while (dec_VGLinkH > 0) {
2071             if (IH_DEC(VGLinkH, VGLinkH->ih_ino, VGLinkH_p1) < 0) {
2072                 Log("idec failed on link table, errno = %d\n", errno);
2073             }
2074             dec_VGLinkH--;
2075         }
2076         while (dec_VGLinkH < 0) {
2077             if (IH_INC(VGLinkH, VGLinkH->ih_ino, VGLinkH_p1) < 0) {
2078                 Log("iinc failed on link table, errno = %d\n", errno);
2079             }
2080             dec_VGLinkH++;
2081         }
2082 #endif
2083     }
2084     free(inodes);
2085     /* Directory consistency checks on the rw volume */
2086     if (haveRWvolume)
2087         SalvageVolume(isp, VGLinkH);
2088     IH_RELEASE(VGLinkH);
2089
2090     if (canfork && !debug) {
2091         ShowLog = 0;
2092         Exit(0);
2093     }
2094 }
2095
2096 int
2097 QuickCheck(register struct InodeSummary *isp, int nVols)
2098 {
2099     /* Check headers BEFORE forking */
2100     register int i;
2101     IHandle_t *h;
2102
2103     for (i = 0; i < nVols; i++) {
2104         struct VolumeSummary *vs = isp[i].volSummary;
2105         VolumeDiskData volHeader;
2106         if (!vs) {
2107             /* Don't salvage just because phantom rw volume is there... */
2108             /* (If a read-only volume exists, read/write inodes must also exist) */
2109             if (i == 0 && isp->nSpecialInodes == 0 && nVols > 1)
2110                 continue;
2111             return 0;
2112         }
2113         IH_INIT(h, fileSysDevice, vs->header.parent, vs->header.volumeInfo);
2114         if (IH_IREAD(h, 0, (char *)&volHeader, sizeof(volHeader))
2115             == sizeof(volHeader)
2116             && volHeader.stamp.magic == VOLUMEINFOMAGIC
2117             && volHeader.dontSalvage == DONT_SALVAGE
2118             && volHeader.needsSalvaged == 0 && volHeader.destroyMe == 0) {
2119             if (volHeader.inUse == 1) {
2120                 volHeader.inUse = 0;
2121                 volHeader.inService = 1;
2122                 if (!Testing) {
2123                     if (IH_IWRITE(h, 0, (char *)&volHeader, sizeof(volHeader))
2124                         != sizeof(volHeader)) {
2125                         IH_RELEASE(h);
2126                         return 0;
2127                     }
2128                 }
2129             }
2130             IH_RELEASE(h);
2131         } else {
2132             IH_RELEASE(h);
2133             return 0;
2134         }
2135     }
2136     return 1;
2137 }
2138
2139
2140 /* SalvageVolumeHeaderFile
2141  *
2142  * Salvage the top level V*.vol header file. Make sure the special files
2143  * exist and that there are no duplicates.
2144  *
2145  * Calls SalvageHeader for each possible type of volume special file.
2146  */
2147
2148 int
2149 SalvageVolumeHeaderFile(register struct InodeSummary *isp,
2150                         register struct ViceInodeInfo *inodes, int RW,
2151                         int check, int *deleteMe)
2152 {
2153     int headerFd = 0;
2154     int i;
2155     register struct ViceInodeInfo *ip;
2156     int allinodesobsolete = 1;
2157     struct VolumeDiskHeader diskHeader;
2158
2159     if (deleteMe)
2160         *deleteMe = 0;
2161     memset(&tempHeader, 0, sizeof(tempHeader));
2162     tempHeader.stamp.magic = VOLUMEHEADERMAGIC;
2163     tempHeader.stamp.version = VOLUMEHEADERVERSION;
2164     tempHeader.id = isp->volumeId;
2165     tempHeader.parent = isp->RWvolumeId;
2166     /* Check for duplicates (inodes are sorted by type field) */
2167     for (i = 0; i < isp->nSpecialInodes - 1; i++) {
2168         ip = &inodes[isp->index + i];
2169         if (ip->u.special.type == (ip + 1)->u.special.type) {
2170             if (!Showmode)
2171                 Log("Duplicate special inodes in volume header; salvage of volume %u aborted\n", isp->volumeId);
2172             return -1;
2173         }
2174     }
2175     for (i = 0; i < isp->nSpecialInodes; i++) {
2176         ip = &inodes[isp->index + i];
2177         if (ip->u.special.type <= 0 || ip->u.special.type > MAXINODETYPE) {
2178             if (check) {
2179                 Log("Rubbish header inode\n");
2180                 return -1;
2181             }
2182             Log("Rubbish header inode; deleted\n");
2183         } else if (!stuff[ip->u.special.type - 1].obsolete) {
2184             *(stuff[ip->u.special.type - 1].inode) = ip->inodeNumber;
2185             if (!check && ip->u.special.type != VI_LINKTABLE)
2186                 ip->linkCount--;        /* Keep the inode around */
2187             allinodesobsolete = 0;
2188         }
2189     }
2190
2191     if (allinodesobsolete) {
2192         if (deleteMe)
2193             *deleteMe = 1;
2194         return -1;
2195     }
2196
2197     if (!check)
2198         VGLinkH_cnt++;          /* one for every header. */
2199
2200     if (!RW && !check && isp->volSummary) {
2201         ClearROInUseBit(isp->volSummary);
2202         return 0;
2203     }
2204
2205     for (i = 0; i < MAXINODETYPE; i++) {
2206         if (stuff[i].inodeType == VI_LINKTABLE) {
2207             /* Gross hack: SalvageHeader does a bcmp on the volume header.
2208              * And we may have recreated the link table earlier, so set the
2209              * RW header as well.
2210              */
2211             if (VALID_INO(VGLinkH->ih_ino)) {
2212                 *stuff[i].inode = VGLinkH->ih_ino;
2213             }
2214             continue;
2215         }
2216         if (SalvageHeader(&stuff[i], isp, check, deleteMe) == -1 && check)
2217             return -1;
2218     }
2219
2220     if (isp->volSummary == NULL) {
2221         char name[64];
2222         (void)afs_snprintf(name, sizeof name, VFORMAT, isp->volumeId);
2223         if (check) {
2224             Log("No header file for volume %u\n", isp->volumeId);
2225             return -1;
2226         }
2227         if (!Showmode)
2228             Log("No header file for volume %u; %screating %s/%s\n",
2229                 isp->volumeId, (Testing ? "it would have been " : ""),
2230                 fileSysPathName, name);
2231         headerFd = afs_open(name, O_RDWR | O_CREAT | O_TRUNC, 0644);
2232         assert(headerFd != -1);
2233         isp->volSummary = (struct VolumeSummary *)
2234             malloc(sizeof(struct VolumeSummary));
2235         isp->volSummary->fileName = ToString(name);
2236     } else {
2237         char name[64];
2238         /* hack: these two fields are obsolete... */
2239         isp->volSummary->header.volumeAcl = 0;
2240         isp->volSummary->header.volumeMountTable = 0;
2241
2242         if (memcmp
2243             (&isp->volSummary->header, &tempHeader,
2244              sizeof(struct VolumeHeader))) {
2245             /* We often remove the name before calling us, so we make a fake one up */
2246             if (isp->volSummary->fileName) {
2247                 strcpy(name, isp->volSummary->fileName);
2248             } else {
2249                 (void)afs_snprintf(name, sizeof name, VFORMAT, isp->volumeId);
2250                 isp->volSummary->fileName = ToString(name);
2251             }
2252
2253             Log("Header file %s is damaged or no longer valid%s\n", name,
2254                 (check ? "" : "; repairing"));
2255             if (check)
2256                 return -1;
2257
2258             headerFd = afs_open(name, O_RDWR | O_TRUNC, 0644);
2259             assert(headerFd != -1);
2260         }
2261     }
2262     if (headerFd) {
2263         memcpy(&isp->volSummary->header, &tempHeader,
2264                sizeof(struct VolumeHeader));
2265         if (Testing) {
2266             if (!Showmode)
2267                 Log("It would have written a new header file for volume %u\n",
2268                     isp->volumeId);
2269         } else {
2270             VolumeHeaderToDisk(&diskHeader, &tempHeader);
2271             if (write(headerFd, &diskHeader, sizeof(struct VolumeDiskHeader))
2272                 != sizeof(struct VolumeDiskHeader)) {
2273                 Log("Couldn't rewrite volume header file!\n");
2274                 close(headerFd);
2275                 return -1;
2276             }
2277         }
2278         close(headerFd);
2279     }
2280     IH_INIT(isp->volSummary->volumeInfoHandle, fileSysDevice, isp->RWvolumeId,
2281             isp->volSummary->header.volumeInfo);
2282     return 0;
2283 }
2284
2285 int
2286 SalvageHeader(register struct stuff *sp, struct InodeSummary *isp, int check,
2287               int *deleteMe)
2288 {
2289     union {
2290         VolumeDiskData volumeInfo;
2291         struct versionStamp fileHeader;
2292     } header;
2293     IHandle_t *specH;
2294     int recreate = 0;
2295     afs_int32 code;
2296     FdHandle_t *fdP;
2297
2298     if (sp->obsolete)
2299         return 0;
2300 #ifndef AFS_NAMEI_ENV
2301     if (sp->inodeType == VI_LINKTABLE)
2302         return 0;
2303 #endif
2304     if (*(sp->inode) == 0) {
2305         if (check) {
2306             Log("Missing inode in volume header (%s)\n", sp->description);
2307             return -1;
2308         }
2309         if (!Showmode)
2310             Log("Missing inode in volume header (%s); %s\n", sp->description,
2311                 (Testing ? "it would have recreated it" : "recreating"));
2312         if (!Testing) {
2313             *(sp->inode) =
2314                 IH_CREATE(NULL, fileSysDevice, fileSysPath, 0, isp->volumeId,
2315                           INODESPECIAL, sp->inodeType, isp->RWvolumeId);
2316             if (!VALID_INO(*(sp->inode)))
2317                 Abort
2318                     ("Unable to allocate inode (%s) for volume header (error = %d)\n",
2319                      sp->description, errno);
2320         }
2321         recreate = 1;
2322     }
2323
2324     IH_INIT(specH, fileSysDevice, isp->RWvolumeId, *(sp->inode));
2325     fdP = IH_OPEN(specH);
2326     if (OKToZap && (fdP == NULL) && BadError(errno)) {
2327         /* bail out early and destroy the volume */
2328         if (!Showmode)
2329             Log("Still can't open volume header inode (%s), destroying volume\n", sp->description);
2330         if (deleteMe)
2331             *deleteMe = 1;
2332         IH_RELEASE(specH);
2333         return -1;
2334     }
2335     if (fdP == NULL)
2336         Abort("Unable to open inode (%s) of volume header (error = %d)\n",
2337               sp->description, errno);
2338
2339     if (!recreate
2340         && (FDH_READ(fdP, (char *)&header, sp->size) != sp->size
2341             || header.fileHeader.magic != sp->stamp.magic)) {
2342         if (check) {
2343             Log("Part of the header (%s) is corrupted\n", sp->description);
2344             FDH_REALLYCLOSE(fdP);
2345             IH_RELEASE(specH);
2346             return -1;
2347         }
2348         Log("Part of the header (%s) is corrupted; recreating\n",
2349             sp->description);
2350         recreate = 1;
2351     }
2352     if (sp->inodeType == VI_VOLINFO
2353         && header.volumeInfo.destroyMe == DESTROY_ME) {
2354         if (deleteMe)
2355             *deleteMe = 1;
2356         FDH_REALLYCLOSE(fdP);
2357         IH_RELEASE(specH);
2358         return -1;
2359     }
2360     if (recreate && !Testing) {
2361         if (check)
2362             Abort
2363                 ("Internal error: recreating volume header (%s) in check mode\n",
2364                  sp->description);
2365         code = FDH_TRUNC(fdP, 0);
2366         if (code == -1)
2367             Abort("Unable to truncate volume header file (%s) (error = %d)\n",
2368                   sp->description, errno);
2369
2370         /* The following code should be moved into vutil.c */
2371         if (sp->inodeType == VI_VOLINFO) {
2372             struct timeval tp;
2373             memset(&header.volumeInfo, 0, sizeof(header.volumeInfo));
2374             header.volumeInfo.stamp = sp->stamp;
2375             header.volumeInfo.id = isp->volumeId;
2376             header.volumeInfo.parentId = isp->RWvolumeId;
2377             sprintf(header.volumeInfo.name, "bogus.%u", isp->volumeId);
2378             Log("Warning: the name of volume %u is now \"bogus.%u\"\n",
2379                 isp->volumeId, isp->volumeId);
2380             header.volumeInfo.inService = 0;
2381             header.volumeInfo.blessed = 0;
2382             /* The + 1000 is a hack in case there are any files out in venus caches */
2383             header.volumeInfo.uniquifier = (isp->maxUniquifier + 1) + 1000;
2384             header.volumeInfo.type = (isp->volumeId == isp->RWvolumeId ? readwriteVolume : readonlyVolume);     /* XXXX */
2385             header.volumeInfo.needsCallback = 0;
2386             gettimeofday(&tp, 0);
2387             header.volumeInfo.creationDate = tp.tv_sec;
2388             if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
2389                 Abort
2390                     ("Unable to seek to beginning of volume header file (%s) (errno = %d)\n",
2391                      sp->description, errno);
2392             }
2393             code =
2394                 FDH_WRITE(fdP, (char *)&header.volumeInfo,
2395                           sizeof(header.volumeInfo));
2396             if (code != sizeof(header.volumeInfo)) {
2397                 if (code < 0)
2398                     Abort
2399                         ("Unable to write volume header file (%s) (errno = %d)\n",
2400                          sp->description, errno);
2401                 Abort("Unable to write entire volume header file (%s)\n",
2402                       sp->description);
2403             }
2404         } else {
2405             if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
2406                 Abort
2407                     ("Unable to seek to beginning of volume header file (%s) (errno = %d)\n",
2408                      sp->description, errno);
2409             }
2410             code = FDH_WRITE(fdP, (char *)&sp->stamp, sizeof(sp->stamp));
2411             if (code != sizeof(sp->stamp)) {
2412                 if (code < 0)
2413                     Abort
2414                         ("Unable to write version stamp in volume header file (%s) (errno = %d)\n",
2415                          sp->description, errno);
2416                 Abort
2417                     ("Unable to write entire version stamp in volume header file (%s)\n",
2418                      sp->description);
2419             }
2420         }
2421     }
2422     FDH_REALLYCLOSE(fdP);
2423     IH_RELEASE(specH);
2424     if (sp->inodeType == VI_VOLINFO) {
2425         VolInfo = header.volumeInfo;
2426         if (check) {
2427             char update[25];
2428             if (VolInfo.updateDate) {
2429                 strcpy(update, TimeStamp(VolInfo.updateDate, 0));
2430                 if (!Showmode)
2431                     Log("%s (%u) %supdated %s\n", VolInfo.name, VolInfo.id,
2432                         (Testing ? "it would have been " : ""), update);
2433             } else {
2434                 strcpy(update, TimeStamp(VolInfo.creationDate, 0));
2435                 if (!Showmode)
2436                     Log("%s (%u) not updated (created %s)\n", VolInfo.name,
2437                         VolInfo.id, update);
2438             }
2439
2440         }
2441     }
2442
2443     return 0;
2444 }
2445
2446 int
2447 SalvageVnodes(register struct InodeSummary *rwIsp,
2448               register struct InodeSummary *thisIsp,
2449               register struct ViceInodeInfo *inodes, int check)
2450 {
2451     int ilarge, ismall, ioffset, RW, nInodes;
2452     ioffset = rwIsp->index + rwIsp->nSpecialInodes;     /* first inode */
2453     if (Showmode)
2454         return 0;
2455     RW = (rwIsp == thisIsp);
2456     nInodes = (rwIsp->nInodes - rwIsp->nSpecialInodes);
2457     ismall =
2458         SalvageIndex(thisIsp->volSummary->header.smallVnodeIndex, vSmall, RW,
2459                      &inodes[ioffset], nInodes, thisIsp->volSummary, check);
2460     if (check && ismall == -1)
2461         return -1;
2462     ilarge =
2463         SalvageIndex(thisIsp->volSummary->header.largeVnodeIndex, vLarge, RW,
2464                      &inodes[ioffset], nInodes, thisIsp->volSummary, check);
2465     return (ilarge == 0 && ismall == 0 ? 0 : -1);
2466 }
2467
2468 int
2469 SalvageIndex(Inode ino, VnodeClass class, int RW,
2470              register struct ViceInodeInfo *ip, int nInodes,
2471              struct VolumeSummary *volSummary, int check)
2472 {
2473     VolumeId volumeNumber;
2474     char buf[SIZEOF_LARGEDISKVNODE];
2475     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
2476     int err = 0;
2477     StreamHandle_t *file;
2478     struct VnodeClassInfo *vcp;
2479     afs_sfsize_t size;
2480     afs_fsize_t vnodeLength;
2481     int vnodeIndex, nVnodes;
2482     afs_ino_str_t stmp1, stmp2;
2483     IHandle_t *handle;
2484     FdHandle_t *fdP;
2485
2486     volumeNumber = volSummary->header.id;
2487     IH_INIT(handle, fileSysDevice, volSummary->header.parent, ino);
2488     fdP = IH_OPEN(handle);
2489     assert(fdP != NULL);
2490     file = FDH_FDOPEN(fdP, "r+");
2491     assert(file != NULL);
2492     vcp = &VnodeClassInfo[class];
2493     size = OS_SIZE(fdP->fd_fd);
2494     assert(size != -1);
2495     nVnodes = (size / vcp->diskSize) - 1;
2496     if (nVnodes > 0) {
2497         assert((nVnodes + 1) * vcp->diskSize == size);
2498         assert(STREAM_SEEK(file, vcp->diskSize, 0) == 0);
2499     } else {
2500         nVnodes = 0;
2501     }
2502     for (vnodeIndex = 0;
2503          nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1;
2504          nVnodes--, vnodeIndex++) {
2505         if (vnode->type != vNull) {
2506             int vnodeChanged = 0;
2507             int vnodeNumber = bitNumberToVnodeNumber(vnodeIndex, class);
2508             /* Log programs that belong to root (potentially suid root);
2509              * don't bother for read-only or backup volumes */
2510 #ifdef  notdef                  /* This is done elsewhere */
2511             if (ShowRootFiles && RW && vnode->owner == 0 && vnodeNumber != 1)
2512                 Log("OWNER IS ROOT %s %u dir %u vnode %u author %u owner %u mode %o\n", VolInfo.name, volumeNumber, vnode->parent, vnodeNumber, vnode->author, vnode->owner, vnode->modeBits);
2513 #endif
2514             if (VNDISK_GET_INO(vnode) == 0) {
2515                 if (RW) {
2516                     /* Log("### DEBUG ### Deleted Vnode with 0 inode (vnode %d)\n", vnodeNumber); */
2517                     memset(vnode, 0, vcp->diskSize);
2518                     vnodeChanged = 1;
2519                 }
2520             } else {
2521                 if (vcp->magic != vnode->vnodeMagic) {
2522                     /* bad magic #, probably partially created vnode */
2523                     Log("Partially allocated vnode %d deleted.\n",
2524                         vnodeNumber);
2525                     memset(vnode, 0, vcp->diskSize);
2526                     vnodeChanged = 1;
2527                     goto vnodeDone;
2528                 }
2529                 /* ****** Should do a bit more salvage here:  e.g. make sure
2530                  * vnode type matches what it should be given the index */
2531                 while (nInodes && ip->u.vnode.vnodeNumber < vnodeNumber) {
2532 /*                  if (vnodeIdToClass(ip->u.vnode.vnodeNumber) == class && RW) {
2533  *                     Log("Inode %d: says it belongs to non-existing vnode %d\n",
2534  *                         ip->inodeNumber, ip->u.vnode.vnodeNumber);
2535  *                  }
2536  */
2537                     ip++;
2538                     nInodes--;
2539                 }
2540                 if (!RW) {
2541                     while (nInodes && ip->u.vnode.vnodeNumber == vnodeNumber) {
2542                         /* The following doesn't work, because the version number
2543                          * is not maintained correctly by the file server */
2544                         /*if (vnode->uniquifier == ip->u.vnode.vnodeUniquifier &&
2545                          * vnode->dataVersion == ip->u.vnode.inodeDataVersion)
2546                          * break; */
2547                         if (VNDISK_GET_INO(vnode) == ip->inodeNumber)
2548                             break;
2549                         ip++;
2550                         nInodes--;
2551                     }
2552                 } else {
2553                     /* For RW volume, look for vnode with matching inode number;
2554                      * if no such match, take the first determined by our sort
2555                      * order */
2556                     register struct ViceInodeInfo *lip = ip;
2557                     register lnInodes = nInodes;
2558                     while (lnInodes
2559                            && lip->u.vnode.vnodeNumber == vnodeNumber) {
2560                         if (VNDISK_GET_INO(vnode) == lip->inodeNumber) {
2561                             ip = lip;
2562                             nInodes = lnInodes;
2563                             break;
2564                         }
2565                         lip++;
2566                         lnInodes--;
2567                     }
2568                 }
2569                 if (nInodes && ip->u.vnode.vnodeNumber == vnodeNumber) {
2570                     /* "Matching" inode */
2571                     if (RW) {
2572                         Unique vu, iu;
2573                         FileVersion vd, id;
2574                         vu = vnode->uniquifier;
2575                         iu = ip->u.vnode.vnodeUniquifier;
2576                         vd = vnode->dataVersion;
2577                         id = ip->u.vnode.inodeDataVersion;
2578                         /*
2579                          * Because of the possibility of the uniquifier overflows (> 4M)
2580                          * we compare them modulo the low 22-bits; we shouldn't worry
2581                          * about mismatching since they shouldn't to many old 
2582                          * uniquifiers of the same vnode...
2583                          */
2584                         if (IUnique(vu) != IUnique(iu)) {
2585                             if (!Showmode) {
2586                                 Log("Vnode %u: vnode.unique, %u, does not match inode unique, %u; fixed, but status will be wrong\n", vnodeNumber, IUnique(vu), IUnique(iu));
2587                             }
2588
2589                             vnode->uniquifier = iu;
2590 #ifdef  AFS_3DISPARES
2591                             vnode->dataVersion = (id >= vd ?
2592                                                   /* 90% of 2.1M */
2593                                                   ((id - vd) >
2594                                                    1887437 ? vd : id) :
2595                                                   /* 90% of 2.1M */
2596                                                   ((vd - id) >
2597                                                    1887437 ? id : vd));
2598 #else
2599 #if defined(AFS_SGI_EXMAG)
2600                             vnode->dataVersion = (id >= vd ?
2601                                                   /* 90% of 16M */
2602                                                   ((id - vd) >
2603                                                    15099494 ? vd : id) :
2604                                                   /* 90% of 16M */
2605                                                   ((vd - id) >
2606                                                    15099494 ? id : vd));
2607 #else
2608                             vnode->dataVersion = (id > vd ? id : vd);
2609 #endif /* AFS_SGI_EXMAG */
2610 #endif /* AFS_3DISPARES */
2611                             vnodeChanged = 1;
2612                         } else {
2613                             /* don't bother checking for vd > id any more, since
2614                              * partial file transfers always result in this state,
2615                              * and you can't do much else anyway (you've already
2616                              * found the best data you can) */
2617 #ifdef  AFS_3DISPARES
2618                             if (!vnodeIsDirectory(vnodeNumber)
2619                                 && ((vd < id && (id - vd) < 1887437)
2620                                     || ((vd > id && (vd - id) > 1887437)))) {
2621 #else
2622 #if defined(AFS_SGI_EXMAG)
2623                             if (!vnodeIsDirectory(vnodeNumber)
2624                                 && ((vd < id && (id - vd) < 15099494)
2625                                     || ((vd > id && (vd - id) > 15099494)))) {
2626 #else
2627                             if (!vnodeIsDirectory(vnodeNumber) && vd < id) {
2628 #endif /* AFS_SGI_EXMAG */
2629 #endif
2630                                 if (!Showmode)
2631                                     Log("Vnode %d: version < inode version; fixed (old status)\n", vnodeNumber);
2632                                 vnode->dataVersion = id;
2633                                 vnodeChanged = 1;
2634                             }
2635                         }
2636                     }
2637                     if (ip->inodeNumber != VNDISK_GET_INO(vnode)) {
2638                         if (check) {
2639                             if (!Showmode) {
2640                                 Log("Vnode %d:  inode number incorrect (is %s should be %s). FileSize=%llu\n", vnodeNumber, PrintInode(stmp1, VNDISK_GET_INO(vnode)), PrintInode(stmp2, ip->inodeNumber), (afs_uintmax_t) ip->byteCount);
2641                             }
2642                             VNDISK_SET_INO(vnode, ip->inodeNumber);
2643                             err = -1;
2644                             goto zooks;
2645                         }
2646                         if (!Showmode) {
2647                             Log("Vnode %d: inode number incorrect; changed from %s to %s. FileSize=%llu\n", vnodeNumber, PrintInode(stmp1, VNDISK_GET_INO(vnode)), PrintInode(stmp2, ip->inodeNumber), (afs_uintmax_t) ip->byteCount);
2648                         }
2649                         VNDISK_SET_INO(vnode, ip->inodeNumber);
2650                         vnodeChanged = 1;
2651                     }
2652                     VNDISK_GET_LEN(vnodeLength, vnode);
2653                     if (ip->byteCount != vnodeLength) {
2654                         if (check) {
2655                             if (!Showmode)
2656                                 Log("Vnode %d: length incorrect; (is %llu should be %llu)\n", vnodeNumber, (afs_uintmax_t) vnodeLength, (afs_uintmax_t) ip->byteCount);
2657                             err = -1;
2658                             goto zooks;
2659                         }
2660                         if (!Showmode)
2661                             Log("Vnode %d: length incorrect; changed from %llu to %llu\n", vnodeNumber, (afs_uintmax_t) vnodeLength, (afs_uintmax_t) ip->byteCount);
2662                         VNDISK_SET_LEN(vnode, ip->byteCount);
2663                         vnodeChanged = 1;
2664                     }
2665                     if (!check)
2666                         ip->linkCount--;        /* Keep the inode around */
2667                     ip++;
2668                     nInodes--;
2669                 } else {        /* no matching inode */
2670                     if (VNDISK_GET_INO(vnode) != 0
2671                         || vnode->type == vDirectory) {
2672                         /* No matching inode--get rid of the vnode */
2673                         if (check) {
2674                             if (VNDISK_GET_INO(vnode)) {
2675                                 if (!Showmode) {
2676                                     Log("Vnode %d (unique %u): corresponding inode %s is missing\n", vnodeNumber, vnode->uniquifier, PrintInode(NULL, VNDISK_GET_INO(vnode)));
2677                                 }
2678                             } else {
2679                                 if (!Showmode)
2680                                     Log("Vnode %d (unique %u): bad directory vnode (no inode number listed)\n", vnodeNumber, vnode->uniquifier);
2681                             }
2682                             err = -1;
2683                             goto zooks;
2684                         }
2685                         if (VNDISK_GET_INO(vnode)) {
2686                             if (!Showmode) {
2687                                 Log("Vnode %d (unique %u): corresponding inode %s is missing; vnode deleted, vnode mod time=%s", vnodeNumber, vnode->uniquifier, PrintInode(NULL, VNDISK_GET_INO(vnode)), ctime((time_t *) & (vnode->serverModifyTime)));
2688                             }
2689                         } else {
2690                             if (!Showmode)
2691                                 Log("Vnode %d (unique %u): bad directory vnode (no inode number listed); vnode deleted, vnode mod time=%s", vnodeNumber, vnode->uniquifier, ctime((time_t *) & (vnode->serverModifyTime)));
2692                         }
2693                         memset(vnode, 0, vcp->diskSize);
2694                         vnodeChanged = 1;
2695                     } else {
2696                         /* Should not reach here becuase we checked for 
2697                          * (inodeNumber == 0) above. And where we zero the vnode,
2698                          * we also goto vnodeDone.
2699                          */
2700                     }
2701                 }
2702                 while (nInodes && ip->u.vnode.vnodeNumber == vnodeNumber) {
2703                     ip++;
2704                     nInodes--;
2705                 }
2706             }                   /* VNDISK_GET_INO(vnode) != 0 */
2707           vnodeDone:
2708             assert(!(vnodeChanged && check));
2709             if (vnodeChanged && !Testing) {
2710                 assert(IH_IWRITE
2711                        (handle, vnodeIndexOffset(vcp, vnodeNumber),
2712                         (char *)vnode, vcp->diskSize)
2713                        == vcp->diskSize);
2714                 VolumeChanged = 1;      /* For break call back */
2715             }
2716         }
2717     }
2718   zooks:
2719     STREAM_CLOSE(file);
2720     FDH_CLOSE(fdP);
2721     IH_RELEASE(handle);
2722     return err;
2723 }
2724
2725 struct VnodeEssence *
2726 CheckVnodeNumber(VnodeId vnodeNumber)
2727 {
2728     VnodeClass class;
2729     struct VnodeInfo *vip;
2730     int offset;
2731
2732     class = vnodeIdToClass(vnodeNumber);
2733     vip = &vnodeInfo[class];
2734     offset = vnodeIdToBitNumber(vnodeNumber);
2735     return (offset >= vip->nVnodes ? NULL : &vip->vnodes[offset]);
2736 }
2737
2738 void
2739 CopyOnWrite(register struct DirSummary *dir)
2740 {
2741     /* Copy the directory unconditionally if we are going to change it:
2742      * not just if was cloned.
2743      */
2744     struct VnodeDiskObject vnode;
2745     struct VnodeClassInfo *vcp = &VnodeClassInfo[vLarge];
2746     Inode oldinode, newinode;
2747     int code;
2748
2749     if (dir->copied || Testing)
2750         return;
2751     DFlush();                   /* Well justified paranoia... */
2752
2753     code =
2754         IH_IREAD(vnodeInfo[vLarge].handle,
2755                  vnodeIndexOffset(vcp, dir->vnodeNumber), (char *)&vnode,
2756                  sizeof(vnode));
2757     assert(code == sizeof(vnode));
2758     oldinode = VNDISK_GET_INO(&vnode);
2759     /* Increment the version number by a whole lot to avoid problems with
2760      * clients that were promised new version numbers--but the file server
2761      * crashed before the versions were written to disk.
2762      */
2763     newinode =
2764         IH_CREATE(dir->ds_linkH, fileSysDevice, fileSysPath, 0, dir->rwVid,
2765                   dir->vnodeNumber, vnode.uniquifier, vnode.dataVersion +=
2766                   200);
2767     assert(VALID_INO(newinode));
2768     assert(CopyInode(fileSysDevice, oldinode, newinode, dir->rwVid) == 0);
2769     vnode.cloned = 0;
2770     VNDISK_SET_INO(&vnode, newinode);
2771     code =
2772         IH_IWRITE(vnodeInfo[vLarge].handle,
2773                   vnodeIndexOffset(vcp, dir->vnodeNumber), (char *)&vnode,
2774                   sizeof(vnode));
2775     assert(code == sizeof(vnode));
2776
2777     SetSalvageDirHandle(&dir->dirHandle, dir->dirHandle.dirh_handle->ih_vid,
2778                         fileSysDevice, newinode);
2779     /* Don't delete the original inode right away, because the directory is
2780      * still being scanned.
2781      */
2782     dir->copied = 1;
2783 }
2784
2785 /*
2786  * This function should either successfully create a new dir, or give up 
2787  * and leave things the way they were.  In particular, if it fails to write 
2788  * the new dir properly, it should return w/o changing the reference to the 
2789  * old dir.
2790  */
2791 void
2792 CopyAndSalvage(register struct DirSummary *dir)
2793 {
2794     struct VnodeDiskObject vnode;
2795     struct VnodeClassInfo *vcp = &VnodeClassInfo[vLarge];
2796     Inode oldinode, newinode;
2797     DirHandle newdir;
2798     register afs_int32 code;
2799     afs_int32 parentUnique = 1;
2800     struct VnodeEssence *vnodeEssence;
2801
2802     if (Testing)
2803         return;
2804     Log("Salvaging directory %u...\n", dir->vnodeNumber);
2805     code =
2806         IH_IREAD(vnodeInfo[vLarge].handle,
2807                  vnodeIndexOffset(vcp, dir->vnodeNumber), (char *)&vnode,
2808                  sizeof(vnode));
2809     assert(code == sizeof(vnode));
2810     oldinode = VNDISK_GET_INO(&vnode);
2811     /* Increment the version number by a whole lot to avoid problems with
2812      * clients that were promised new version numbers--but the file server
2813      * crashed before the versions were written to disk.
2814      */
2815     newinode =
2816         IH_CREATE(dir->ds_linkH, fileSysDevice, fileSysPath, 0, dir->rwVid,
2817                   dir->vnodeNumber, vnode.uniquifier, vnode.dataVersion +=
2818                   200);
2819     assert(VALID_INO(newinode));
2820     SetSalvageDirHandle(&newdir, dir->rwVid, fileSysDevice, newinode);
2821
2822     /* Assign . and .. vnode numbers from dir and vnode.parent. 
2823      * The uniquifier for . is in the vnode.
2824      * The uniquifier for .. might be set to a bogus value of 1 and 
2825      * the salvager will later clean it up.
2826      */
2827     if (vnode.parent && (vnodeEssence = CheckVnodeNumber(vnode.parent))) {
2828         parentUnique = (vnodeEssence->unique ? vnodeEssence->unique : 1);
2829     }
2830     code =
2831         DirSalvage(&dir->dirHandle, &newdir, dir->vnodeNumber,
2832                    vnode.uniquifier,
2833                    (vnode.parent ? vnode.parent : dir->vnodeNumber),
2834                    parentUnique);
2835     if (code == 0)
2836         code = DFlush();
2837     if (code) {
2838         /* didn't really build the new directory properly, let's just give up. */
2839         code = IH_DEC(dir->ds_linkH, newinode, dir->rwVid);
2840         assert(code == 0);
2841         Log("Directory salvage returned code %d, continuing.\n", code);
2842         assert(1 == 2);
2843     }
2844     Log("Checking the results of the directory salvage...\n");
2845     if (!DirOK(&newdir)) {
2846         Log("Directory salvage failed!!!; restoring old version of the directory.\n");
2847         code = IH_DEC(dir->ds_linkH, newinode, dir->rwVid);
2848         assert(code == 0);
2849         assert(1 == 2);
2850     }
2851     vnode.cloned = 0;
2852     VNDISK_SET_INO(&vnode, newinode);
2853     VNDISK_SET_LEN(&vnode, Length(&newdir));
2854     code =
2855         IH_IWRITE(vnodeInfo[vLarge].handle,
2856                   vnodeIndexOffset(vcp, dir->vnodeNumber), (char *)&vnode,
2857                   sizeof(vnode));
2858     assert(code == sizeof(vnode));
2859 #ifdef AFS_NT40_ENV
2860     nt_sync(fileSysDevice);
2861 #else
2862     sync();                     /* this is slow, but hopefully rarely called.  We don't have
2863                                  * an open FD on the file itself to fsync.
2864                                  */
2865 #endif
2866     code = IH_DEC(dir->ds_linkH, oldinode, dir->rwVid);
2867     assert(code == 0);
2868     dir->dirHandle = newdir;
2869 }
2870
2871 void
2872 JudgeEntry(struct DirSummary *dir, char *name, VnodeId vnodeNumber,
2873            Unique unique)
2874 {
2875     struct VnodeEssence *vnodeEssence;
2876     afs_int32 dirOrphaned, todelete;
2877
2878     dirOrphaned = IsVnodeOrphaned(dir->vnodeNumber);
2879
2880     vnodeEssence = CheckVnodeNumber(vnodeNumber);
2881     if (vnodeEssence == NULL) {
2882         if (!Showmode) {
2883             Log("dir vnode %u: invalid entry deleted: %s/%s (vnode %u, unique %u)\n", dir->vnodeNumber, dir->name ? dir->name : "??", name, vnodeNumber, unique);
2884         }
2885         if (!Testing) {
2886             CopyOnWrite(dir);
2887             assert(Delete(&dir->dirHandle, name) == 0);
2888         }
2889         return;
2890     }
2891 #ifdef AFS_AIX_ENV
2892 #ifndef AFS_NAMEI_ENV
2893     /* On AIX machines, don't allow entries to point to inode 0. That is a special 
2894      * mount inode for the partition. If this inode were deleted, it would crash
2895      * the machine.
2896      */
2897     if (vnodeEssence->InodeNumber == 0) {
2898         Log("dir vnode %d: invalid entry: %s/%s has no inode (vnode %d, unique %d)%s\n", dir->vnodeNumber, (dir->name ? dir->name : "??"), name, vnodeNumber, unique, (Testing ? "-- would have deleted" : " -- deleted"));
2899         if (!Testing) {
2900             CopyOnWrite(dir);
2901             assert(Delete(&dir->dirHandle, name) == 0);
2902         }
2903         return;
2904     }
2905 #endif
2906 #endif
2907
2908     if (!(vnodeNumber & 1) && !Showmode
2909         && !(vnodeEssence->count || vnodeEssence->unique
2910              || vnodeEssence->modeBits)) {
2911         Log("dir vnode %u: invalid entry: %s/%s (vnode %u, unique %u)%s\n",
2912             dir->vnodeNumber, (dir->name ? dir->name : "??"), name,
2913             vnodeNumber, unique,
2914             ((!unique) ? (Testing ? "-- would have deleted" : " -- deleted") :
2915              ""));
2916         if (!unique) {
2917             if (!Testing) {
2918                 CopyOnWrite(dir);
2919                 assert(Delete(&dir->dirHandle, name) == 0);
2920             }
2921             return;
2922         }
2923     }
2924
2925     /* Check if the Uniquifiers match. If not, change the directory entry
2926      * so its unique matches the vnode unique. Delete if the unique is zero
2927      * or if the directory is orphaned.
2928      */
2929     if (!vnodeEssence->unique || (vnodeEssence->unique) != unique) {
2930         if (!vnodeEssence->unique
2931             && ((strcmp(name, "..") == 0) || (strcmp(name, ".") == 0))) {
2932             /* This is an orphaned directory. Don't delete the . or ..
2933              * entry. Otherwise, it will get created in the next 
2934              * salvage and deleted again here. So Just skip it.
2935              */
2936             return;
2937         }
2938
2939         todelete = ((!vnodeEssence->unique || dirOrphaned) ? 1 : 0);
2940
2941         if (!Showmode) {
2942             Log("dir vnode %u: %s/%s (vnode %u): unique changed from %u to %u %s\n", dir->vnodeNumber, (dir->name ? dir->name : "??"), name, vnodeNumber, unique, vnodeEssence->unique, (!todelete ? "" : (Testing ? "-- would have deleted" : "-- deleted")));
2943         }
2944         if (!Testing) {
2945             ViceFid fid;
2946             fid.Vnode = vnodeNumber;
2947             fid.Unique = vnodeEssence->unique;
2948             CopyOnWrite(dir);
2949             assert(Delete(&dir->dirHandle, name) == 0);
2950             if (!todelete)
2951                 assert(Create(&dir->dirHandle, name, &fid) == 0);
2952         }
2953         if (todelete)
2954             return;             /* no need to continue */
2955     }
2956
2957     if (strcmp(name, ".") == 0) {
2958         if (dir->vnodeNumber != vnodeNumber || (dir->unique != unique)) {
2959             ViceFid fid;
2960             if (!Showmode)
2961                 Log("directory vnode %u.%u: bad '.' entry (was %u.%u); fixed\n", dir->vnodeNumber, dir->unique, vnodeNumber, unique);
2962             if (!Testing) {
2963                 CopyOnWrite(dir);
2964                 assert(Delete(&dir->dirHandle, ".") == 0);
2965                 fid.Vnode = dir->vnodeNumber;
2966                 fid.Unique = dir->unique;
2967                 assert(Create(&dir->dirHandle, ".", &fid) == 0);
2968             }
2969
2970             vnodeNumber = fid.Vnode;    /* Get the new Essence */
2971             unique = fid.Unique;
2972             vnodeEssence = CheckVnodeNumber(vnodeNumber);
2973         }
2974         dir->haveDot = 1;
2975     } else if (strcmp(name, "..") == 0) {
2976         ViceFid pa;
2977         if (dir->parent) {
2978             struct VnodeEssence *dotdot;
2979             pa.Vnode = dir->parent;
2980             dotdot = CheckVnodeNumber(pa.Vnode);
2981             assert(dotdot != NULL);     /* XXX Should not be assert */
2982             pa.Unique = dotdot->unique;
2983         } else {
2984             pa.Vnode = dir->vnodeNumber;
2985             pa.Unique = dir->unique;
2986         }
2987         if ((pa.Vnode != vnodeNumber) || (pa.Unique != unique)) {
2988             if (!Showmode)
2989                 Log("directory vnode %u.%u: bad '..' entry (was %u.%u); fixed\n", dir->vnodeNumber, dir->unique, vnodeNumber, unique);
2990             if (!Testing) {
2991                 CopyOnWrite(dir);
2992                 assert(Delete(&dir->dirHandle, "..") == 0);
2993                 assert(Create(&dir->dirHandle, "..", &pa) == 0);
2994             }
2995
2996             vnodeNumber = pa.Vnode;     /* Get the new Essence */
2997             unique = pa.Unique;
2998             vnodeEssence = CheckVnodeNumber(vnodeNumber);
2999         }
3000         dir->haveDotDot = 1;
3001     } else if (strncmp(name, ".__afs", 6) == 0) {
3002         if (!Showmode) {
3003             Log("dir vnode %u: special old unlink-while-referenced file %s %s deleted (vnode %u)\n", dir->vnodeNumber, name, (Testing ? "would have been" : "is"), vnodeNumber);
3004         }
3005         if (!Testing) {
3006             CopyOnWrite(dir);
3007             assert(Delete(&dir->dirHandle, name) == 0);
3008         }
3009         vnodeEssence->claimed = 0;      /* Not claimed: Orphaned */
3010         vnodeEssence->todelete = 1;     /* Will later delete vnode and decr inode */
3011         return;
3012     } else {
3013         if (ShowSuid && (vnodeEssence->modeBits & 06000))
3014             Log("FOUND suid/sgid file: %s/%s (%u.%u %05o) author %u (vnode %u dir %u)\n", dir->name ? dir->name : "??", name, vnodeEssence->owner, vnodeEssence->group, vnodeEssence->modeBits, vnodeEssence->author, vnodeNumber, dir->vnodeNumber);
3015         if (ShowMounts && (vnodeEssence->type == vSymlink)
3016             && !(vnodeEssence->modeBits & 0111)) {
3017             int code, size;
3018             char buf[1024];
3019             IHandle_t *ihP;
3020             FdHandle_t *fdP;
3021
3022             IH_INIT(ihP, fileSysDevice, dir->dirHandle.dirh_handle->ih_vid,
3023                     vnodeEssence->InodeNumber);
3024             fdP = IH_OPEN(ihP);
3025             assert(fdP != NULL);
3026             size = FDH_SIZE(fdP);
3027             assert(size != -1);
3028             memset(buf, 0, 1024);
3029             if (size > 1024)
3030                 size = 1024;
3031             code = FDH_READ(fdP, buf, size);
3032             assert(code == size);
3033             Log("In volume %u (%s) found mountpoint %s/%s to '%s'\n",
3034                 dir->dirHandle.dirh_handle->ih_vid, dir->vname,
3035                 dir->name ? dir->name : "??", name, buf);
3036             FDH_REALLYCLOSE(fdP);
3037             IH_RELEASE(ihP);
3038         }
3039         if (ShowRootFiles && vnodeEssence->owner == 0 && vnodeNumber != 1)
3040             Log("FOUND root file: %s/%s (%u.%u %05o) author %u (vnode %u dir %u)\n", dir->name ? dir->name : "??", name, vnodeEssence->owner, vnodeEssence->group, vnodeEssence->modeBits, vnodeEssence->author, vnodeNumber, dir->vnodeNumber);
3041         if (vnodeIdToClass(vnodeNumber) == vLarge
3042             && vnodeEssence->name == NULL) {
3043             char *n;
3044             if ((n = (char *)malloc(strlen(name) + 1)))
3045                 strcpy(n, name);
3046             vnodeEssence->name = n;
3047         }
3048
3049         /* The directory entry points to the vnode. Check to see if the
3050          * vnode points back to the directory. If not, then let the 
3051          * directory claim it (else it might end up orphaned). Vnodes 
3052          * already claimed by another directory are deleted from this
3053          * directory: hardlinks to the same vnode are not allowed
3054          * from different directories.
3055          */
3056         if (vnodeEssence->parent != dir->vnodeNumber) {
3057             if (!vnodeEssence->claimed && !dirOrphaned && vnodeNumber != 1) {
3058                 /* Vnode does not point back to this directory.
3059                  * Orphaned dirs cannot claim a file (it may belong to
3060                  * another non-orphaned dir).
3061                  */
3062                 if (!Showmode) {
3063                     Log("dir vnode %u: %s/%s (vnode %u, unique %u) -- parent vnode %schanged from %u to %u\n", dir->vnodeNumber, (dir->name ? dir->name : "??"), name, vnodeNumber, unique, (Testing ? "would have been " : ""), vnodeEssence->parent, dir->vnodeNumber);
3064                 }
3065                 vnodeEssence->parent = dir->vnodeNumber;
3066                 vnodeEssence->changed = 1;
3067             } else {
3068                 /* Vnode was claimed by another directory */
3069                 if (!Showmode) {
3070                     if (dirOrphaned) {
3071                         Log("dir vnode %u: %s/%s parent vnode is %u (vnode %u, unique %u) -- %sdeleted\n", dir->vnodeNumber, (dir->name ? dir->name : "??"), name, vnodeEssence->parent, vnodeNumber, unique, (Testing ? "would have been " : ""));
3072                     } else if (vnodeNumber == 1) {
3073                         Log("dir vnode %d: %s/%s is invalid (vnode %d, unique %d) -- %sdeleted\n", dir->vnodeNumber, (dir->name ? dir->name : "??"), name, vnodeNumber, unique, (Testing ? "would have been " : ""));
3074                     } else {
3075                         Log("dir vnode %u: %s/%s already claimed by directory vnode %u (vnode %u, unique %u) -- %sdeleted\n", dir->vnodeNumber, (dir->name ? dir->name : "??"), name, vnodeEssence->parent, vnodeNumber, unique, (Testing ? "would have been " : ""));
3076                     }
3077                 }
3078                 if (!Testing) {
3079                     CopyOnWrite(dir);
3080                     assert(Delete(&dir->dirHandle, name) == 0);
3081                 }
3082                 return;
3083             }
3084         }
3085         /* This directory claims the vnode */
3086         vnodeEssence->claimed = 1;
3087     }
3088     vnodeEssence->count--;
3089 }
3090
3091 void
3092 DistilVnodeEssence(VolumeId rwVId, VnodeClass class, Inode ino, Unique * maxu)
3093 {
3094     register struct VnodeInfo *vip = &vnodeInfo[class];
3095     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
3096     char buf[SIZEOF_LARGEDISKVNODE];
3097     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
3098     int size;
3099     StreamHandle_t *file;
3100     int vnodeIndex;
3101     int nVnodes;
3102     FdHandle_t *fdP;
3103
3104     IH_INIT(vip->handle, fileSysDevice, rwVId, ino);
3105     fdP = IH_OPEN(vip->handle);
3106     assert(fdP != NULL);
3107     file = FDH_FDOPEN(fdP, "r+");
3108     assert(file != NULL);
3109     size = OS_SIZE(fdP->fd_fd);
3110     assert(size != -1);
3111     vip->nVnodes = (size / vcp->diskSize) - 1;
3112     if (vip->nVnodes > 0) {
3113         assert((vip->nVnodes + 1) * vcp->diskSize == size);
3114         assert(STREAM_SEEK(file, vcp->diskSize, 0) == 0);
3115         assert((vip->vnodes = (struct VnodeEssence *)
3116                 calloc(vip->nVnodes, sizeof(struct VnodeEssence))) != NULL);
3117         if (class == vLarge) {
3118             assert((vip->inodes = (Inode *)
3119                     calloc(vip->nVnodes, sizeof(Inode))) != NULL);
3120         } else {
3121             vip->inodes = NULL;
3122         }
3123     } else {
3124         vip->nVnodes = 0;
3125         vip->vnodes = NULL;
3126         vip->inodes = NULL;
3127     }
3128     vip->volumeBlockCount = vip->nAllocatedVnodes = 0;
3129     for (vnodeIndex = 0, nVnodes = vip->nVnodes;
3130          nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1;
3131          nVnodes--, vnodeIndex++) {
3132         if (vnode->type != vNull) {
3133             register struct VnodeEssence *vep = &vip->vnodes[vnodeIndex];
3134             afs_fsize_t vnodeLength;
3135             vip->nAllocatedVnodes++;
3136             vep->count = vnode->linkCount;
3137             VNDISK_GET_LEN(vnodeLength, vnode);
3138             vep->blockCount = nBlocks(vnodeLength);
3139             vip->volumeBlockCount += vep->blockCount;
3140             vep->parent = vnode->parent;
3141             vep->unique = vnode->uniquifier;
3142             if (*maxu < vnode->uniquifier)
3143                 *maxu = vnode->uniquifier;
3144             vep->modeBits = vnode->modeBits;
3145             vep->InodeNumber = VNDISK_GET_INO(vnode);
3146             vep->type = vnode->type;
3147             vep->author = vnode->author;
3148             vep->owner = vnode->owner;
3149             vep->group = vnode->group;
3150             if (vnode->type == vDirectory) {
3151                 assert(class == vLarge);
3152                 vip->inodes[vnodeIndex] = VNDISK_GET_INO(vnode);
3153             }
3154         }
3155     }
3156     STREAM_CLOSE(file);
3157     FDH_CLOSE(fdP);
3158 }
3159
3160 static char *
3161 GetDirName(VnodeId vnode, struct VnodeEssence *vp, char *path)
3162 {
3163     struct VnodeEssence *parentvp;
3164
3165     if (vnode == 1) {
3166         strcpy(path, ".");
3167         return path;
3168     }
3169     if (vp->parent && vp->name && (parentvp = CheckVnodeNumber(vp->parent))
3170         && GetDirName(vp->parent, parentvp, path)) {
3171         strcat(path, "/");
3172         strcat(path, vp->name);
3173         return path;
3174     }
3175     return 0;
3176 }
3177
3178 /* To determine if a vnode is orhpaned or not, the vnode and all its parent
3179  * vnodes must be "claimed". The vep->claimed flag is set in JudgeEntry().
3180  */
3181 static int
3182 IsVnodeOrphaned(VnodeId vnode)
3183 {
3184     struct VnodeEssence *vep;
3185
3186     if (vnode == 0)
3187         return (1);             /* Vnode zero does not exist */
3188     if (vnode == 1)
3189         return (0);             /* The root dir vnode is always claimed */
3190     vep = CheckVnodeNumber(vnode);      /* Get the vnode essence */
3191     if (!vep || !vep->claimed)
3192         return (1);             /* Vnode is not claimed - it is orphaned */
3193
3194     return (IsVnodeOrphaned(vep->parent));
3195 }
3196
3197 void
3198 SalvageDir(char *name, VolumeId rwVid, struct VnodeInfo *dirVnodeInfo,
3199            IHandle_t * alinkH, int i, struct DirSummary *rootdir,
3200            int *rootdirfound)
3201 {
3202     static struct DirSummary dir;
3203     static struct DirHandle dirHandle;
3204     struct VnodeEssence *parent;
3205     static char path[MAXPATHLEN];
3206     int dirok, code;
3207
3208     if (dirVnodeInfo->vnodes[i].salvaged)
3209         return;                 /* already salvaged */
3210
3211     dir.rwVid = rwVid;
3212     dirVnodeInfo->vnodes[i].salvaged = 1;
3213
3214     if (dirVnodeInfo->inodes[i] == 0)
3215         return;                 /* Not allocated to a directory */
3216
3217     if (bitNumberToVnodeNumber(i, vLarge) == 1) {
3218         if (dirVnodeInfo->vnodes[i].parent) {
3219             Log("Bad parent, vnode 1; %s...\n",
3220                 (Testing ? "skipping" : "salvaging"));
3221             dirVnodeInfo->vnodes[i].parent = 0;
3222             dirVnodeInfo->vnodes[i].changed = 1;
3223         }
3224     } else {
3225         parent = CheckVnodeNumber(dirVnodeInfo->vnodes[i].parent);
3226         if (parent && parent->salvaged == 0)
3227             SalvageDir(name, rwVid, dirVnodeInfo, alinkH,
3228                        vnodeIdToBitNumber(dirVnodeInfo->vnodes[i].parent),
3229                        rootdir, rootdirfound);
3230     }
3231
3232     dir.vnodeNumber = bitNumberToVnodeNumber(i, vLarge);
3233     dir.unique = dirVnodeInfo->vnodes[i].unique;
3234     dir.copied = 0;
3235     dir.vname = name;
3236     dir.parent = dirVnodeInfo->vnodes[i].parent;
3237     dir.haveDot = dir.haveDotDot = 0;
3238     dir.ds_linkH = alinkH;
3239     SetSalvageDirHandle(&dir.dirHandle, dir.rwVid, fileSysDevice,
3240                         dirVnodeInfo->inodes[i]);
3241
3242     dirok = ((RebuildDirs && !Testing) ? 0 : DirOK(&dir.dirHandle));
3243     if (!dirok) {
3244         if (!RebuildDirs) {
3245             Log("Directory bad, vnode %u; %s...\n", dir.vnodeNumber,
3246                 (Testing ? "skipping" : "salvaging"));
3247         }
3248         if (!Testing) {
3249             CopyAndSalvage(&dir);
3250             dirok = 1;
3251         }
3252     }
3253     dirHandle = dir.dirHandle;
3254
3255     dir.name =
3256         GetDirName(bitNumberToVnodeNumber(i, vLarge),
3257                    &dirVnodeInfo->vnodes[i], path);
3258
3259     if (dirok) {
3260         /* If enumeration failed for random reasons, we will probably delete
3261          * too much stuff, so we guard against this instead.
3262          */
3263         assert(EnumerateDir(&dirHandle, JudgeEntry, &dir) == 0);
3264     }
3265
3266     /* Delete the old directory if it was copied in order to salvage.
3267      * CopyOnWrite has written the new inode # to the disk, but we still
3268      * have the old one in our local structure here.  Thus, we idec the
3269      * local dude.
3270      */
3271     DFlush();
3272     if (dir.copied && !Testing) {
3273         code = IH_DEC(dir.ds_linkH, dirHandle.dirh_handle->ih_ino, rwVid);
3274         assert(code == 0);
3275         dirVnodeInfo->inodes[i] = dir.dirHandle.dirh_inode;
3276     }
3277
3278     /* Remember rootdir DirSummary _after_ it has been judged */
3279     if (dir.vnodeNumber == 1 && dir.unique == 1) {
3280         memcpy(rootdir, &dir, sizeof(struct DirSummary));
3281         *rootdirfound = 1;
3282     }
3283
3284     return;
3285 }
3286
3287 int
3288 SalvageVolume(register struct InodeSummary *rwIsp, IHandle_t * alinkH)
3289 {
3290     /* This routine, for now, will only be called for read-write volumes */
3291     int i, j, code;
3292     int BlocksInVolume = 0, FilesInVolume = 0;
3293     register VnodeClass class;
3294     struct DirSummary rootdir, oldrootdir;
3295     struct VnodeInfo *dirVnodeInfo;
3296     struct VnodeDiskObject vnode;
3297     VolumeDiskData volHeader;
3298     VolumeId vid;
3299     int orphaned, rootdirfound = 0;
3300     Unique maxunique = 0;       /* the maxUniquifier from the vnodes */
3301     afs_int32 ofiles = 0, oblocks = 0;  /* Number of orphaned files/blocks */
3302     struct VnodeEssence *vep;
3303     afs_int32 v, pv;
3304     IHandle_t *h;
3305     int nBytes;
3306     ViceFid pa;
3307     VnodeId LFVnode, ThisVnode;
3308     Unique LFUnique, ThisUnique;
3309     char npath[128];
3310
3311     vid = rwIsp->volSummary->header.id;
3312     IH_INIT(h, fileSysDevice, vid, rwIsp->volSummary->header.volumeInfo);
3313     nBytes = IH_IREAD(h, 0, (char *)&volHeader, sizeof(volHeader));
3314     assert(nBytes == sizeof(volHeader));
3315     assert(volHeader.stamp.magic == VOLUMEINFOMAGIC);
3316     assert(volHeader.destroyMe != DESTROY_ME);
3317     /* (should not have gotten this far with DESTROY_ME flag still set!) */
3318
3319     DistilVnodeEssence(vid, vLarge, rwIsp->volSummary->header.largeVnodeIndex,
3320                        &maxunique);
3321     DistilVnodeEssence(vid, vSmall, rwIsp->volSummary->header.smallVnodeIndex,
3322                        &maxunique);
3323
3324     dirVnodeInfo = &vnodeInfo[vLarge];
3325     for (i = 0; i < dirVnodeInfo->nVnodes; i++) {
3326         SalvageDir(volHeader.name, vid, dirVnodeInfo, alinkH, i, &rootdir,
3327                    &rootdirfound);
3328     }
3329     if (Showmode) {
3330         IH_RELEASE(h);
3331         return 0;
3332     }
3333
3334     /* Parse each vnode looking for orphaned vnodes and
3335      * connect them to the tree as orphaned (if requested).
3336      */
3337     oldrootdir = rootdir;
3338     for (class = 0; class < nVNODECLASSES; class++) {
3339         for (v = 0; v < vnodeInfo[class].nVnodes; v++) {
3340             vep = &(vnodeInfo[class].vnodes[v]);
3341             ThisVnode = bitNumberToVnodeNumber(v, class);
3342             ThisUnique = vep->unique;
3343
3344             if ((vep->type == 0) || vep->claimed || ThisVnode == 1)
3345                 continue;       /* Ignore unused, claimed, and root vnodes */
3346
3347             /* This vnode is orphaned. If it is a directory vnode, then the '..'
3348              * entry in this vnode had incremented the parent link count (In
3349              * JudgeEntry()). We need to go to the parent and decrement that
3350              * link count. But if the parent's unique is zero, then the parent
3351              * link count was not incremented in JudgeEntry().
3352              */
3353             if (class == vLarge) {      /* directory vnode */
3354                 pv = vnodeIdToBitNumber(vep->parent);
3355                 if (vnodeInfo[vLarge].vnodes[pv].unique != 0)
3356                     vnodeInfo[vLarge].vnodes[pv].count++;
3357             }
3358
3359             if (!rootdirfound)
3360                 continue;       /* If no rootdir, can't attach orphaned files */
3361
3362             /* Here we attach orphaned files and directories into the
3363              * root directory, LVVnode, making sure link counts stay correct.
3364              */
3365             if ((orphans == ORPH_ATTACH) && !vep->todelete && !Testing) {
3366                 LFVnode = rootdir.vnodeNumber;  /* Lost+Found vnode number */
3367                 LFUnique = rootdir.unique;      /* Lost+Found uniquifier */
3368
3369                 /* Update this orphaned vnode's info. Its parent info and 
3370                  * link count (do for orphaned directories and files).
3371                  */
3372                 vep->parent = LFVnode;  /* Parent is the root dir */
3373                 vep->unique = LFUnique;
3374                 vep->changed = 1;
3375                 vep->claimed = 1;
3376                 vep->count--;   /* Inc link count (root dir will pt to it) */
3377
3378                 /* If this orphaned vnode is a directory, change '..'. 
3379                  * The name of the orphaned dir/file is unknown, so we
3380                  * build a unique name. No need to CopyOnWrite the directory
3381                  * since it is not connected to tree in BK or RO volume and
3382                  * won't be visible there.
3383                  */
3384                 if (class == vLarge) {
3385                     ViceFid pa;
3386                     DirHandle dh;
3387
3388                     /* Remove and recreate the ".." entry in this orphaned directory */
3389                     SetSalvageDirHandle(&dh, vid, fileSysDevice,
3390                                         vnodeInfo[class].inodes[v]);
3391                     pa.Vnode = LFVnode;
3392                     pa.Unique = LFUnique;
3393                     assert(Delete(&dh, "..") == 0);
3394                     assert(Create(&dh, "..", &pa) == 0);
3395
3396                     /* The original parent's link count was decremented above.
3397                      * Here we increment the new parent's link count.
3398                      */
3399                     pv = vnodeIdToBitNumber(LFVnode);
3400                     vnodeInfo[vLarge].vnodes[pv].count--;
3401
3402                 }
3403
3404                 /* Go to the root dir and add this entry. The link count of the
3405                  * root dir was incremented when ".." was created. Try 10 times.
3406                  */
3407                 for (j = 0; j < 10; j++) {
3408                     pa.Vnode = ThisVnode;
3409                     pa.Unique = ThisUnique;
3410
3411                     (void)afs_snprintf(npath, sizeof npath, "%s.%u.%u",
3412                                        ((class ==
3413                                          vLarge) ? "__ORPHANDIR__" :
3414                                         "__ORPHANFILE__"), ThisVnode,
3415                                        ThisUnique);
3416
3417                     CopyOnWrite(&rootdir);
3418                     code = Create(&rootdir.dirHandle, npath, &pa);
3419                     if (!code)
3420                         break;
3421
3422                     ThisUnique += 50;   /* Try creating a different file */
3423                 }
3424                 assert(code == 0);
3425                 Log("Attaching orphaned %s to volume's root dir as %s\n",
3426                     ((class == vLarge) ? "directory" : "file"), npath);
3427             }
3428         }                       /* for each vnode in the class */
3429     }                           /* for each class of vnode */
3430
3431     /* Delete the old rootinode directory if the rootdir was CopyOnWrite */
3432     DFlush();
3433     if (!oldrootdir.copied && rootdir.copied) {
3434         code =
3435             IH_DEC(oldrootdir.ds_linkH, oldrootdir.dirHandle.dirh_inode,
3436                    oldrootdir.rwVid);
3437         assert(code == 0);
3438         /* dirVnodeInfo->inodes[?] is not updated with new inode number */
3439     }
3440
3441     DFlush();                   /* Flush the changes */
3442     if (!rootdirfound && (orphans == ORPH_ATTACH)) {
3443         Log("Cannot attach orphaned files and directories: Root directory not found\n");
3444         orphans = ORPH_IGNORE;
3445     }
3446
3447     /* Write out all changed vnodes. Orphaned files and directories
3448      * will get removed here also (if requested).
3449      */
3450     for (class = 0; class < nVNODECLASSES; class++) {
3451         int nVnodes = vnodeInfo[class].nVnodes;
3452         struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
3453         struct VnodeEssence *vnodes = vnodeInfo[class].vnodes;
3454         FilesInVolume += vnodeInfo[class].nAllocatedVnodes;
3455         BlocksInVolume += vnodeInfo[class].volumeBlockCount;
3456         for (i = 0; i < nVnodes; i++) {
3457             register struct VnodeEssence *vnp = &vnodes[i];
3458             VnodeId vnodeNumber = bitNumberToVnodeNumber(i, class);
3459
3460             /* If the vnode is good but is unclaimed (not listed in
3461              * any directory entries), then it is orphaned.
3462              */
3463             orphaned = -1;
3464             if ((vnp->type != 0) && (orphaned = IsVnodeOrphaned(vnodeNumber))) {
3465                 vnp->claimed = 0;       /* Makes IsVnodeOrphaned calls faster */
3466                 vnp->changed = 1;
3467             }
3468
3469             if (vnp->changed || vnp->count) {
3470                 int oldCount;
3471                 int code;
3472                 nBytes =
3473                     IH_IREAD(vnodeInfo[class].handle,
3474                              vnodeIndexOffset(vcp, vnodeNumber),
3475                              (char *)&vnode, sizeof(vnode));
3476                 assert(nBytes == sizeof(vnode));
3477
3478                 vnode.parent = vnp->parent;
3479                 oldCount = vnode.linkCount;
3480                 vnode.linkCount = vnode.linkCount - vnp->count;
3481
3482                 if (orphaned == -1)
3483                     orphaned = IsVnodeOrphaned(vnodeNumber);
3484                 if (orphaned) {
3485                     if (!vnp->todelete) {
3486                         /* Orphans should have already been attached (if requested) */
3487                         assert(orphans != ORPH_ATTACH);
3488                         oblocks += vnp->blockCount;
3489                         ofiles++;
3490                     }
3491                     if (((orphans == ORPH_REMOVE) || vnp->todelete)
3492                         && !Testing) {
3493                         BlocksInVolume -= vnp->blockCount;
3494                         FilesInVolume--;
3495                         if (VNDISK_GET_INO(&vnode)) {
3496                             code =
3497                                 IH_DEC(alinkH, VNDISK_GET_INO(&vnode), vid);
3498                             assert(code == 0);
3499                         }
3500                         memset(&vnode, 0, sizeof(vnode));
3501                     }
3502                 } else if (vnp->count) {
3503                     if (!Showmode) {
3504                         Log("Vnode %u: link count incorrect (was %d, %s %d)\n", vnodeNumber, oldCount, (Testing ? "would have changed to" : "now"), vnode.linkCount);
3505                     }
3506                 }
3507
3508                 vnode.dataVersion++;
3509                 if (!Testing) {
3510                     nBytes =
3511                         IH_IWRITE(vnodeInfo[class].handle,
3512                                   vnodeIndexOffset(vcp, vnodeNumber),
3513                                   (char *)&vnode, sizeof(vnode));
3514                     assert(nBytes == sizeof(vnode));
3515                 }
3516                 VolumeChanged = 1;
3517             }
3518         }
3519     }
3520     if (!Showmode && ofiles) {
3521         Log("%s %d orphaned files and directories (approx. %u KB)\n",
3522             (!Testing
3523              && (orphans == ORPH_REMOVE)) ? "Removed" : "Found", ofiles,
3524             oblocks);
3525     }
3526
3527     for (class = 0; class < nVNODECLASSES; class++) {
3528         register struct VnodeInfo *vip = &vnodeInfo[class];
3529         for (i = 0; i < vip->nVnodes; i++)
3530             if (vip->vnodes[i].name)
3531                 free(vip->vnodes[i].name);
3532         if (vip->vnodes)
3533             free(vip->vnodes);
3534         if (vip->inodes)
3535             free(vip->inodes);
3536     }
3537
3538     /* Set correct resource utilization statistics */
3539     volHeader.filecount = FilesInVolume;
3540     volHeader.diskused = BlocksInVolume;
3541
3542     /* Make sure the uniquifer is big enough: maxunique is the real maxUniquifier */
3543     if (volHeader.uniquifier < (maxunique + 1)) {
3544         if (!Showmode)
3545             Log("Volume uniquifier is too low; fixed\n");
3546         /* Plus 2,000 in case there are workstations out there with
3547          * cached vnodes that have since been deleted
3548          */
3549         volHeader.uniquifier = (maxunique + 1 + 2000);
3550     }
3551
3552     /* Turn off the inUse bit; the volume's been salvaged! */
3553     volHeader.inUse = 0;        /* clear flag indicating inUse@last crash */
3554     volHeader.needsSalvaged = 0;        /* clear 'damaged' flag */
3555     volHeader.inService = 1;    /* allow service again */
3556     volHeader.needsCallback = (VolumeChanged != 0);
3557     volHeader.dontSalvage = DONT_SALVAGE;
3558     VolumeChanged = 0;
3559     if (!Testing) {
3560         nBytes = IH_IWRITE(h, 0, (char *)&volHeader, sizeof(volHeader));
3561         assert(nBytes == sizeof(volHeader));
3562     }
3563     if (!Showmode) {
3564         Log("%sSalvaged %s (%u): %d files, %d blocks\n",
3565             (Testing ? "It would have " : ""), volHeader.name, volHeader.id,
3566             FilesInVolume, BlocksInVolume);
3567     }
3568     IH_RELEASE(vnodeInfo[vSmall].handle);
3569     IH_RELEASE(vnodeInfo[vLarge].handle);
3570     IH_RELEASE(h);
3571     return 0;
3572 }
3573
3574 void
3575 ClearROInUseBit(struct VolumeSummary *summary)
3576 {
3577     IHandle_t *h = summary->volumeInfoHandle;
3578     int nBytes;
3579
3580     VolumeDiskData volHeader;
3581
3582     nBytes = IH_IREAD(h, 0, (char *)&volHeader, sizeof(volHeader));
3583     assert(nBytes == sizeof(volHeader));
3584     assert(volHeader.stamp.magic == VOLUMEINFOMAGIC);
3585     volHeader.inUse = 0;
3586     volHeader.needsSalvaged = 0;
3587     volHeader.inService = 1;
3588     volHeader.dontSalvage = DONT_SALVAGE;
3589     if (!Testing) {
3590         nBytes = IH_IREAD(h, 0, (char *)&volHeader, sizeof(volHeader));
3591         assert(nBytes == sizeof(volHeader));
3592     }
3593 }
3594
3595 /* MaybeZapVolume
3596  * Possible delete the volume.
3597  *
3598  * deleteMe - Always do so, only a partial volume.
3599  */
3600 void
3601 MaybeZapVolume(register struct InodeSummary *isp, char *message, int deleteMe,
3602                int check)
3603 {
3604     if (readOnly(isp) || deleteMe) {
3605         if (isp->volSummary && isp->volSummary->fileName) {
3606             if (deleteMe) {
3607                 if (!Showmode)
3608                     Log("Volume %u (is only a partial volume--probably an attempt was made to move/restore it when a machine crash occured.\n", isp->volumeId);
3609                 if (!Showmode)
3610                     Log("It will be deleted on this server (you may find it elsewhere)\n");
3611             } else {
3612                 if (!Showmode)
3613                     Log("Volume %u needs to be salvaged.  Since it is read-only, however,\n", isp->volumeId);
3614                 if (!Showmode)
3615                     Log("it will be deleted instead.  It should be recloned.\n");
3616             }
3617             if (!Testing)
3618                 unlink(isp->volSummary->fileName);
3619         }
3620     } else if (!check) {
3621         Log("%s salvage was unsuccessful: read-write volume %u\n", message,
3622             isp->volumeId);
3623         Abort("Salvage of volume %u aborted\n", isp->volumeId);
3624     }
3625 }
3626
3627
3628 void
3629 AskOffline(VolumeId volumeId)
3630 {
3631     if (FSYNC_askfs(volumeId, NULL, FSYNC_OFF, FSYNC_SALVAGE) == FSYNC_DENIED) {
3632         Log("AskOffline:  file server denied offline request; a general salvage is required.\n");
3633         Abort("Salvage aborted\n");
3634     }
3635 }
3636
3637 void
3638 AskOnline(VolumeId volumeId, char *partition)
3639 {
3640     if (FSYNC_askfs(volumeId, partition, FSYNC_ON, 0) == FSYNC_DENIED) {
3641         Log("AskOnline:  file server denied online request to volume %u partition %s\n", volumeId, partition);
3642     }
3643 }
3644
3645 int
3646 CopyInode(Device device, Inode inode1, Inode inode2, int rwvolume)
3647 {
3648     /* Volume parameter is passed in case iopen is upgraded in future to
3649      * require a volume Id to be passed
3650      */
3651     char buf[4096];
3652     IHandle_t *srcH, *destH;
3653     FdHandle_t *srcFdP, *destFdP;
3654     register int n = 0;
3655
3656     IH_INIT(srcH, device, rwvolume, inode1);
3657     srcFdP = IH_OPEN(srcH);
3658     assert(srcFdP != NULL);
3659     IH_INIT(destH, device, rwvolume, inode2);
3660     destFdP = IH_OPEN(destH);
3661     assert(n != -1);
3662     while ((n = FDH_READ(srcFdP, buf, sizeof(buf))) > 0)
3663         assert(FDH_WRITE(destFdP, buf, n) == n);
3664     assert(n == 0);
3665     FDH_REALLYCLOSE(srcFdP);
3666     FDH_REALLYCLOSE(destFdP);
3667     IH_RELEASE(srcH);
3668     IH_RELEASE(destH);
3669     return 0;
3670 }
3671
3672 void
3673 PrintInodeList(void)
3674 {
3675     register struct ViceInodeInfo *ip;
3676     struct ViceInodeInfo *buf;
3677     struct afs_stat status;
3678     register nInodes;
3679
3680     assert(afs_fstat(inodeFd, &status) == 0);
3681     buf = (struct ViceInodeInfo *)malloc(status.st_size);
3682     assert(buf != NULL);
3683     nInodes = status.st_size / sizeof(struct ViceInodeInfo);
3684     assert(read(inodeFd, buf, status.st_size) == status.st_size);
3685     for (ip = buf; nInodes--; ip++) {
3686         Log("Inode:%s, linkCount=%d, size=%#llx, p=(%u,%u,%u,%u)\n",
3687             PrintInode(NULL, ip->inodeNumber), ip->linkCount,
3688             (afs_uintmax_t) ip->byteCount, ip->u.param[0], ip->u.param[1],
3689             ip->u.param[2], ip->u.param[3]);
3690     }
3691     free(buf);
3692 }
3693
3694 void
3695 PrintInodeSummary(void)
3696 {
3697     int i;
3698     struct InodeSummary *isp;
3699
3700     for (i = 0; i < nVolumesInInodeFile; i++) {
3701         isp = &inodeSummary[i];
3702         Log("VID:%u, RW:%u, index:%d, nInodes:%d, nSpecialInodes:%d, maxUniquifier:%u, volSummary\n", isp->volumeId, isp->RWvolumeId, isp->index, isp->nInodes, isp->nSpecialInodes, isp->maxUniquifier);
3703     }
3704 }
3705
3706 void
3707 PrintVolumeSummary(void)
3708 {
3709     int i;
3710     struct VolumeSummary *vsp;
3711
3712     for (i = 0, vsp = volumeSummaryp; i < nVolumes; vsp++, i++) {
3713         Log("fileName:%s, header, wouldNeedCallback\n", vsp->fileName);
3714     }
3715 }
3716
3717 int
3718 Fork(void)
3719 {
3720     int f;
3721 #ifdef AFS_NT40_ENV
3722     f = 0;
3723     assert(0);                  /* Fork is never executed in the NT code path */
3724 #else
3725     f = fork();
3726     assert(f >= 0);
3727 #endif
3728     return f;
3729 }
3730
3731 void
3732 Exit(code)
3733      int code;
3734 {
3735     if (ShowLog)
3736         showlog();
3737 #ifdef AFS_NT40_ENV
3738     if (main_thread != pthread_self())
3739         pthread_exit((void *)code);
3740     else
3741         exit(code);
3742 #else
3743     exit(code);
3744 #endif
3745 }
3746
3747 int
3748 Wait(char *prog)
3749 {
3750     int status;
3751     int pid;
3752     pid = wait(&status);
3753     assert(pid != -1);
3754     if (WCOREDUMP(status))
3755         Log("\"%s\" core dumped!\n", prog);
3756     if (WIFSIGNALED(status) != 0 || WEXITSTATUS(status) != 0)
3757         return -1;
3758     return pid;
3759 }
3760
3761 static char *
3762 TimeStamp(time_t clock, int precision)
3763 {
3764     struct tm *lt;
3765     static char timestamp[20];
3766     lt = localtime(&clock);
3767     if (precision)
3768         (void)strftime(timestamp, 20, "%m/%d/%Y %T", lt);
3769     else
3770         (void)strftime(timestamp, 20, "%m/%d/%Y %H:%M", lt);
3771     return timestamp;
3772 }
3773
3774 void
3775 CheckLogFile(void)
3776 {
3777     char oldSlvgLog[AFSDIR_PATH_MAX];
3778
3779 #ifndef AFS_NT40_ENV
3780     if (useSyslog) {
3781         ShowLog = 0;
3782         return;
3783     }
3784 #endif
3785
3786     strcpy(oldSlvgLog, AFSDIR_SERVER_SLVGLOG_FILEPATH);
3787     strcat(oldSlvgLog, ".old");
3788     if (!logFile) {
3789         renamefile(AFSDIR_SERVER_SLVGLOG_FILEPATH, oldSlvgLog);
3790         logFile = afs_fopen(AFSDIR_SERVER_SLVGLOG_FILEPATH, "a");
3791
3792         if (!logFile) {         /* still nothing, use stdout */
3793             logFile = stdout;
3794             ShowLog = 0;
3795         }
3796 #ifndef AFS_NAMEI_ENV
3797         AFS_DEBUG_IOPS_LOG(logFile);
3798 #endif
3799     }
3800 }
3801
3802 #ifndef AFS_NT40_ENV
3803 void
3804 TimeStampLogFile(void)
3805 {
3806     char stampSlvgLog[AFSDIR_PATH_MAX];
3807     struct tm *lt;
3808     time_t now;
3809
3810     now = time(0);
3811     lt = localtime(&now);
3812     (void)afs_snprintf(stampSlvgLog, sizeof stampSlvgLog,
3813                        "%s.%04d-%02d-%02d.%02d:%02d:%02d",
3814                        AFSDIR_SERVER_SLVGLOG_FILEPATH, lt->tm_year + 1900,
3815                        lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min,
3816                        lt->tm_sec);
3817
3818     /* try to link the logfile to a timestamped filename */
3819     /* if it fails, oh well, nothing we can do */
3820     link(AFSDIR_SERVER_SLVGLOG_FILEPATH, stampSlvgLog);
3821 }
3822 #endif
3823
3824 void
3825 showlog(void)
3826 {
3827     char line[256];
3828
3829 #ifndef AFS_NT40_ENV
3830     if (useSyslog) {
3831         printf("Can't show log since using syslog.\n");
3832         fflush(stdout);
3833         return;
3834     }
3835 #endif
3836
3837     rewind(logFile);
3838     fclose(logFile);
3839
3840     logFile = afs_fopen(AFSDIR_SERVER_SLVGLOG_FILEPATH, "r");
3841
3842     if (!logFile)
3843         printf("Can't read %s, exiting\n", AFSDIR_SERVER_SLVGLOG_FILEPATH);
3844     else {
3845         rewind(logFile);
3846         while (fgets(line, sizeof(line), logFile))
3847             printf("%s", line);
3848         fflush(stdout);
3849     }
3850 }
3851
3852 void
3853 Log(const char *format, ...)
3854 {
3855     struct timeval now;
3856     char tmp[1024];
3857     va_list args;
3858
3859     va_start(args, format);
3860     (void)afs_vsnprintf(tmp, sizeof tmp, format, args);
3861     va_end(args);
3862 #ifndef AFS_NT40_ENV
3863     if (useSyslog) {
3864         syslog(LOG_INFO, "%s", tmp);
3865     } else
3866 #endif
3867     {
3868         gettimeofday(&now, 0);
3869         fprintf(logFile, "%s %s", TimeStamp(now.tv_sec, 1), tmp);
3870         fflush(logFile);
3871     }
3872 }
3873
3874 void
3875 Abort(const char *format, ...)
3876 {
3877     va_list args;
3878     char tmp[1024];
3879
3880     va_start(args, format);
3881     (void)afs_vsnprintf(tmp, sizeof tmp, format, args);
3882     va_end(args);
3883 #ifndef AFS_NT40_ENV
3884     if (useSyslog) {
3885         syslog(LOG_INFO, "%s", tmp);
3886     } else
3887 #endif
3888     {
3889         fprintf(logFile, "%s", tmp);
3890         fflush(logFile);
3891         if (ShowLog)
3892             showlog();
3893     }
3894
3895     if (debug)
3896         abort();
3897     Exit(1);
3898 }
3899
3900 char *
3901 ToString(char *s)
3902 {
3903     register char *p;
3904     p = (char *)malloc(strlen(s) + 1);
3905     assert(p != NULL);
3906     strcpy(p, s);
3907     return p;
3908
3909 }
3910
3911 /* Remove the FORCESALVAGE file */
3912 void
3913 RemoveTheForce(char *path)
3914 {
3915     if (!Testing && ForceSalvage) {
3916         if (chdir(path) == 0)
3917             unlink("FORCESALVAGE");
3918     }
3919 }
3920
3921 #ifndef AFS_AIX32_ENV
3922 /*
3923  * UseTheForceLuke -    see if we can use the force
3924  */
3925 int
3926 UseTheForceLuke(char *path)
3927 {
3928     struct afs_stat force;
3929
3930     assert(chdir(path) != -1);
3931
3932     return (afs_stat("FORCESALVAGE", &force) == 0);
3933 }
3934 #else
3935 /*
3936  * UseTheForceLuke -    see if we can use the force
3937  *
3938  * NOTE:
3939  *      The VRMIX fsck will not muck with the filesystem it is supposedly
3940  *      fixing and create a "FORCESAVAGE" file (by design).  Instead, we
3941  *      muck directly with the root inode, which is within the normal
3942  *      domain of fsck.
3943  *      ListViceInodes() has a side effect of setting ForceSalvage if
3944  *      it detects a need, based on root inode examination.
3945  */
3946 int
3947 UseTheForceLuke(char *path)
3948 {
3949
3950     return 0;                   /* sorry OB1    */
3951 }
3952 #endif
3953
3954 #ifdef AFS_NT40_ENV
3955 /* NT support routines */
3956
3957 static char execpathname[MAX_PATH];
3958 int
3959 nt_SalvagePartition(char *partName, int jobn)
3960 {
3961     int pid;
3962     int n;
3963     childJob_t job;
3964     if (!*execpathname) {
3965         n = GetModuleFileName(NULL, execpathname, MAX_PATH - 1);
3966         if (!n || n == 1023)
3967             return -1;
3968     }
3969     job.cj_magic = SALVAGER_MAGIC;
3970     job.cj_number = jobn;
3971     (void)strcpy(job.cj_part, partName);
3972     pid = (int)spawnprocveb(execpathname, save_args, NULL, &job, sizeof(job));
3973     return pid;
3974 }
3975
3976 int
3977 nt_SetupPartitionSalvage(void *datap, int len)
3978 {
3979     childJob_t *jobp = (childJob_t *) datap;
3980     char logname[AFSDIR_PATH_MAX];
3981
3982     if (len != sizeof(childJob_t))
3983         return -1;
3984     if (jobp->cj_magic != SALVAGER_MAGIC)
3985         return -1;
3986     myjob = *jobp;
3987
3988     /* Open logFile */
3989     (void)sprintf(logname, "%s.%d", AFSDIR_SERVER_SLVGLOG_FILEPATH,
3990                   myjob.cj_number);
3991     logFile = afs_fopen(logname, "w");
3992     if (!logFile)
3993         logFile = stdout;
3994
3995     return 0;
3996 }
3997
3998
3999 #endif /* AFS_NT40_ENV */