Avoid 'salvageserver -client -showlog' segfault
[openafs.git] / src / vol / vol-salvage.c
index 04eb269..e5daf80 100644 (file)
@@ -83,17 +83,17 @@ Vnodes with 0 inode pointers in RW volumes are now deleted.
 */
 
 
-#define SalvageVersion "2.4"
-
-/* Main program file. Define globals. */
-#define MAIN 1
-
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
 
+#ifndef AFS_NT40_ENV
+#include <sys/param.h>
+#include <sys/file.h>
+#ifndef ITIMER_REAL
+#include <sys/time.h>
+#endif /* ITIMER_REAL */
+#endif
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -104,12 +104,6 @@ RCSID
 #ifdef AFS_NT40_ENV
 #include <io.h>
 #include <WINNT/afsevent.h>
-#else
-#include <sys/param.h>
-#include <sys/file.h>
-#ifndef ITIMER_REAL
-#include <sys/time.h>
-#endif /* ITIMER_REAL */
 #endif
 #if    defined(AFS_AIX_ENV) || defined(AFS_SUN4_ENV)
 #define WCOREDUMP(x)   (x & 0200)
@@ -171,6 +165,7 @@ RCSID
 #include <afs/osi_inode.h>
 #endif
 #include <afs/cmd.h>
+#include <afs/dir.h>
 #include <afs/afsutil.h>
 #include <afs/fileutil.h>
 #include <afs/procmgmt.h>      /* signal(), kill(), wait(), etc. */
@@ -186,10 +181,15 @@ RCSID
 #include "vnode.h"
 #include "volume.h"
 #include "partition.h"
+#include "daemon_com.h"
 #include "fssync.h"
+#include "salvsync.h"
 #include "viceinode.h"
 #include "salvage.h"
 #include "volinodes.h"         /* header magic number, etc. stuff */
+#include "vol-salvage.h"
+#include "vol_internal.h"
+
 #ifdef AFS_NT40_ENV
 #include <pthread.h>
 #endif
@@ -221,13 +221,9 @@ extern void *calloc();
 #endif
 static char *TimeStamp(time_t clock, int precision);
 
-#define ORPH_IGNORE 0
-#define ORPH_REMOVE 1
-#define ORPH_ATTACH 2
-
 
 int debug;                     /* -d flag */
-int Testing = 0;               /* -n flag */
+extern int Testing;            /* -n flag */
 int ListInodeOption;           /* -i flag */
 int ShowRootFiles;             /* -r flag */
 int RebuildDirs;               /* -sal flag */
@@ -240,18 +236,25 @@ int ShowMounts = 0;               /* -showmounts flag */
 int orphans = ORPH_IGNORE;     /* -orphans option */
 int Showmode = 0;
 
+
 #ifndef AFS_NT40_ENV
 int useSyslog = 0;             /* -syslog flag */
 int useSyslogFacility = LOG_DAEMON;    /* -syslogfacility option */
 #endif
 
+#ifdef AFS_NT40_ENV
+int canfork = 0;
+#else
+int canfork = 1;
+#endif
+
 #define        MAXPARALLEL     32
 
 int OKToZap;                   /* -o flag */
 int ForceSalvage;              /* If salvage should occur despite the DONT_SALVAGE flag
                                 * in the volume header */
 
-static FILE *logFile = 0;      /* one of {/usr/afs/logs,/vice/file}/SalvageLog */
+FILE *logFile = 0;     /* one of {/usr/afs/logs,/vice/file}/SalvageLog */
 
 #define ROOTINODE      2       /* Root inode of a 4.2 Unix file system
                                 * partition */
@@ -267,7 +270,7 @@ char *fileSysPath;          /* The path of the mounted partition currently
 char *fileSysPathName;         /* NT needs this to make name pretty in log. */
 IHandle_t *VGLinkH;            /* Link handle for current volume group. */
 int VGLinkH_cnt;               /* # of references to lnk handle. */
-struct DiskPartition *fileSysPartition;        /* Partition  being salvaged */
+struct DiskPartition64 *fileSysPartition;      /* Partition  being salvaged */
 #ifndef AFS_NT40_ENV
 char *fileSysDeviceName;       /* The block device where the file system
                                 * being salvaged was mounted */
@@ -279,201 +282,24 @@ int VolumeChanged;               /* Set by any routine which would change the volume in
 
 VolumeDiskData VolInfo;                /* A copy of the last good or salvaged volume header dealt with */
 
-struct InodeSummary {          /* Inode summary file--an entry for each
-                                * volume in the inode file for a partition */
-    VolId volumeId;            /* Volume id */
-    VolId RWvolumeId;          /* RW volume associated */
-    int index;                 /* index into inode file (0, 1, 2 ...) */
-    int nInodes;               /* Number of inodes for this volume */
-    int nSpecialInodes;                /* Number of special inodes, i.e.  volume
-                                * header, index, etc.  These are all
-                                * marked (viceinode.h) and will all be sorted
-                                * to the beginning of the information for
-                                * this volume.  Read-only volumes should
-                                * ONLY have special inodes (all the other
-                                * inodes look as if they belong to the
-                                * original RW volume). */
-    Unique maxUniquifier;      /* The maximum uniquifier found in all the inodes.
-                                * This is only useful for RW volumes and is used
-                                * to compute a new volume uniquifier in the event
-                                * that the header needs to be recreated. The inode
-                                * uniquifier may be a truncated version of vnode
-                                * uniquifier (AFS_3DISPARES). The real maxUniquifer
-                                * is from the vnodes and later calcuated from it */
-    struct VolumeSummary *volSummary;
-    /* Either a pointer to the original volume
-     * header summary, or constructed summary
-     * information */
-} *inodeSummary;
-#define readOnly(isp)  ((isp)->volumeId != (isp)->RWvolumeId)
 int nVolumesInInodeFile;       /* Number of read-write volumes summarized */
 int inodeFd;                   /* File descriptor for inode file */
 
 
-struct VolumeSummary {         /* Volume summary an entry for each
-                                * volume in a volume directory.
-                                * Assumption: one volume directory per
-                                * partition */
-    char *fileName;            /* File name on the partition for the volume
-                                * header */
-    struct VolumeHeader header;
-    /* volume number, rw volume number, inode
-     * numbers of each major component of
-     * the volume */
-    IHandle_t *volumeInfoHandle;
-    byte wouldNeedCallback;    /* set if the file server should issue
-                                * call backs for all the files in this volume when
-                                * the volume goes back on line */
-};
-
-struct VnodeInfo {
-    IHandle_t *handle;         /* Inode containing this index */
-    int nVnodes;               /* Total number of vnodes in index */
-    int nAllocatedVnodes;      /* Total number actually used */
-    int volumeBlockCount;      /* Total number of blocks used by volume */
-    Inode *inodes;             /* Directory only */
-    struct VnodeEssence {
-       short count;            /* Number of references to vnode; MUST BE SIGNED */
-       unsigned claimed:1;     /* Set when a parent directory containing an entry
-                                * referencing this vnode is found.  The claim
-                                * is that the parent in "parent" can point to
-                                * this vnode, and no other */
-       unsigned changed:1;     /* Set if any parameters (other than the count)
-                                * in the vnode change.   It is determined if the
-                                * link count has changed by noting whether it is
-                                * 0 after scanning all directories */
-       unsigned salvaged:1;    /* Set if this directory vnode has already been salvaged. */
-       unsigned todelete:1;    /* Set if this vnode is to be deleted (should not be claimed) */
-       afs_fsize_t blockCount;
-       /* Number of blocks (1K) used by this vnode,
-        * approximately */
-       VnodeId parent;         /* parent in vnode */
-       Unique unique;          /* Must match entry! */
-       char *name;             /* Name of directory entry */
-       int modeBits;           /* File mode bits */
-       Inode InodeNumber;      /* file's inode */
-       int type;               /* File type */
-       int author;             /* File author */
-       int owner;              /* File owner */
-       int group;              /* File group */
-    } *vnodes;
-} vnodeInfo[nVNODECLASSES];
-
-struct DirSummary {
-    struct DirHandle dirHandle;
-    VnodeId vnodeNumber;
-    Unique unique;
-    unsigned haveDot, haveDotDot;
-    VolumeId rwVid;
-    int copied;                        /* If the copy-on-write stuff has been applied */
-    VnodeId parent;
-    char *name;
-    char *vname;
-    IHandle_t *ds_linkH;
-};
+struct VnodeInfo vnodeInfo[nVNODECLASSES];
 
 
 struct VolumeSummary *volumeSummaryp;  /* Holds all the volumes in a part */
 int nVolumes;                  /* Number of volumes (read-write and read-only)
                                 * in volume summary */
 
-#ifdef AFS_NT40_ENV
-/* For NT, we can fork the per partition salvagers to gain the required
- * safety against Aborts. But there's too many complex data structures at
- * the per volume salvager layer to easilty copy the data across.
- * childJobNumber is resset from -1 to the job number if this is a
- * per partition child of the main salvager. This information is passed
- * out-of-band in the extra data area setup for the now unused parent/child
- * data transfer.
- */
-#define SALVAGER_MAGIC 0x00BBaaDD
-#define NOT_CHILD -1           /* job numbers start at 0 */
-/* If new options need to be passed to child, add them here. */
-typedef struct {
-    int cj_magic;
-    int cj_number;
-    char cj_part[32];
-} childJob_t;
-
-/* Child job this process is running. */
-childJob_t myjob = { SALVAGER_MAGIC, NOT_CHILD, "" };
-
-int nt_SalvagePartition(char *partName, int jobn);
-int nt_SetupPartitionSalvage(void *datap, int len);
-
-typedef struct {
-    struct InodeSummary *svgp_inodeSummaryp;
-    int svgp_count;
-} SVGParms_t;
-#define canfork 0
-#else
-#define canfork 1
-#endif
+char *tmpdir = NULL;
 
 
 
 /* Forward declarations */
 /*@printflike@*/ void Log(const char *format, ...);
 /*@printflike@*/ void Abort(const char *format, ...);
-void Exit(int code);
-int Fork(void);
-int Wait(char *prog);
-char *ToString(char *s);
-void AskOffline(VolumeId volumeId);
-void AskOnline(VolumeId volumeId, char *partition);
-void CheckLogFile(void);
-#ifndef AFS_NT40_ENV
-void TimeStampLogFile(void);
-#endif
-void ClearROInUseBit(struct VolumeSummary *summary);
-void CopyAndSalvage(register struct DirSummary *dir);
-int CopyInode(Device device, Inode inode1, Inode inode2, int rwvolume);
-void CopyOnWrite(register struct DirSummary *dir);
-void CountVolumeInodes(register struct ViceInodeInfo *ip, int maxInodes,
-                      register struct InodeSummary *summary);
-void DeleteExtraVolumeHeaderFile(register struct VolumeSummary *vsp);
-void DistilVnodeEssence(VolumeId vid, VnodeClass class, Inode ino,
-                       Unique * maxu);
-int GetInodeSummary(char *path, VolumeId singleVolumeNumber);
-void GetVolumeSummary(VolumeId singleVolumeNumber);
-void JudgeEntry(struct DirSummary *dir, char *name, VnodeId vnodeNumber,
-               Unique unique);
-void MaybeZapVolume(register struct InodeSummary *isp, char *message,
-                   int deleteMe, int check);
-void ObtainSalvageLock(void);
-void PrintInodeList(void);
-void PrintInodeSummary(void);
-void PrintVolumeSummary(void);
-int QuickCheck(register struct InodeSummary *isp, int nVols);
-void RemoveTheForce(char *path);
-void SalvageDir(char *name, VolumeId rwVid, struct VnodeInfo *dirVnodeInfo,
-               IHandle_t * alinkH, int i, struct DirSummary *rootdir,
-               int *rootdirfound);
-void SalvageFileSysParallel(struct DiskPartition *partP);
-void SalvageFileSys(struct DiskPartition *partP, VolumeId singleVolumeNumber);
-void SalvageFileSys1(struct DiskPartition *partP,
-                    VolumeId singleVolumeNumber);
-int SalvageHeader(register struct stuff *sp, struct InodeSummary *isp,
-                 int check, int *deleteMe);
-int SalvageIndex(Inode ino, VnodeClass class, int RW,
-                register struct ViceInodeInfo *ip, int nInodes,
-                struct VolumeSummary *volSummary, int check);
-int SalvageVnodes(register struct InodeSummary *rwIsp,
-                 register struct InodeSummary *thisIsp,
-                 register struct ViceInodeInfo *inodes, int check);
-int SalvageVolume(register struct InodeSummary *rwIsp, IHandle_t * alinkH);
-void DoSalvageVolumeGroup(register struct InodeSummary *isp, int nVols);
-#ifdef AFS_NT40_ENV
-void SalvageVolumeGroup(register struct InodeSummary *isp, int nVols);
-#else
-#define SalvageVolumeGroup DoSalvageVolumeGroup
-#endif
-int SalvageVolumeHeaderFile(register struct InodeSummary *isp,
-                           register struct ViceInodeInfo *inodes, int RW,
-                           int check, int *deleteMe);
-void showlog(void);
-int UseTheForceLuke(char *path);
-
 static int IsVnodeOrphaned(VnodeId vnode);
 
 /* Uniquifier stored in the Inode */
@@ -499,364 +325,25 @@ BadError(register int aerror)
     return 0;                  /* otherwise may be transient, e.g. EMFILE */
 }
 
-
-char *tmpdir = 0;
-static int
-handleit(struct cmd_syndesc *as)
-{
-    register struct cmd_item *ti;
-    char pname[100], *temp;
-    afs_int32 seenpart = 0, seenvol = 0, vid = 0, seenany = 0;
-    struct DiskPartition *partP;
-
-#ifdef AFS_SGI_VNODE_GLUE
-    if (afs_init_kernel_config(-1) < 0) {
-       printf
-           ("Can't determine NUMA configuration, not starting salvager.\n");
-       exit(1);
-    }
-#endif
-
-#ifdef FAST_RESTART
-    {
-       afs_int32 i;
-       for (i = 0; i < CMD_MAXPARMS; i++) {
-           if (as->parms[i].items) {
-               seenany = 1;
-               break;
-           }
-       }
-    }
-    if (!seenany) {
-       char *msg =
-           "Exiting immediately without salvage. Look into the FileLog to find volumes which really need to be salvaged!";
-
-       if (useSyslog)
-           Log(msg);
-       else
-           printf("%s\n", msg);
-
-       Exit(0);
-    }
-#endif /* FAST_RESTART */
-    if ((ti = as->parms[0].items)) {   /* -partition */
-       seenpart = 1;
-       strncpy(pname, ti->data, 100);
-    }
-    if ((ti = as->parms[1].items)) {   /* -volumeid */
-       if (!seenpart) {
-           printf
-               ("You must also specify '-partition' option with the '-volumeid' option\n");
-           exit(-1);
-       }
-       seenvol = 1;
-       vid = atoi(ti->data);
-    }
-    if (as->parms[2].items)    /* -debug */
-       debug = 1;
-    if (as->parms[3].items)    /* -nowrite */
-       Testing = 1;
-    if (as->parms[4].items)    /* -inodes */
-       ListInodeOption = 1;
-    if (as->parms[5].items)    /* -force */
-       ForceSalvage = 1;
-    if (as->parms[6].items)    /* -oktozap */
-       OKToZap = 1;
-    if (as->parms[7].items)    /* -rootinodes */
-       ShowRootFiles = 1;
-    if (as->parms[8].items)    /* -RebuildDirs */
-       RebuildDirs = 1;
-    if (as->parms[9].items)    /* -ForceReads */
-       forceR = 1;
-    if ((ti = as->parms[10].items)) {  /* -Parallel # */
-       temp = ti->data;
-       if (strncmp(temp, "all", 3) == 0) {
-           PartsPerDisk = 1;
-           temp += 3;
-       }
-       if (strlen(temp) != 0) {
-           Parallel = atoi(temp);
-           if (Parallel < 1)
-               Parallel = 1;
-           if (Parallel > MAXPARALLEL) {
-               printf("Setting parallel salvages to maximum of %d \n",
-                      MAXPARALLEL);
-               Parallel = MAXPARALLEL;
-           }
-       }
-    }
-    if ((ti = as->parms[11].items)) {  /* -tmpdir */
-       DIR *dirp;
-
-       tmpdir = ti->data;
-       dirp = opendir(tmpdir);
-       if (!dirp) {
-           printf
-               ("Can't open temporary placeholder dir %s; using current partition \n",
-                tmpdir);
-           tmpdir = NULL;
-       } else
-           closedir(dirp);
-    }
-    if ((ti = as->parms[12].items))    /* -showlog */
-       ShowLog = 1;
-    if ((ti = as->parms[13].items)) {  /* -log */
-       Testing = 1;
-       ShowSuid = 1;
-       Showmode = 1;
-    }
-    if ((ti = as->parms[14].items)) {  /* -showmounts */
-       Testing = 1;
-       Showmode = 1;
-       ShowMounts = 1;
-    }
-    if ((ti = as->parms[15].items)) {  /* -orphans */
-       if (Testing)
-           orphans = ORPH_IGNORE;
-       else if (strcmp(ti->data, "remove") == 0
-                || strcmp(ti->data, "r") == 0)
-           orphans = ORPH_REMOVE;
-       else if (strcmp(ti->data, "attach") == 0
-                || strcmp(ti->data, "a") == 0)
-           orphans = ORPH_ATTACH;
-    }
-#ifndef AFS_NT40_ENV           /* ignore options on NT */
-    if ((ti = as->parms[16].items)) {  /* -syslog */
-       useSyslog = 1;
-       ShowLog = 0;
-    }
-    if ((ti = as->parms[17].items)) {  /* -syslogfacility */
-       useSyslogFacility = atoi(ti->data);
-    }
-
-    if ((ti = as->parms[18].items)) {  /* -datelogs */
-       TimeStampLogFile();
-    }
-#endif
-
-#ifdef FAST_RESTART
-    if (ti = as->parms[19].items) {    /* -DontSalvage */
-       char *msg =
-           "Exiting immediately without salvage. Look into the FileLog to find volumes which really need to be salvaged!";
-
-       if (useSyslog)
-           Log(msg);
-       else
-           printf("%s\n", msg);
-       Exit(0);
-    }
-#endif /* FAST_RESTART */
-
-    /* Note:  if seemvol we initialize this as a standard volume utility:  this has the
-     * implication that the file server may be running; negotations have to be made with
-     * the file server in this case to take the read write volume and associated read-only
-     * volumes off line before salvaging */
-#ifdef AFS_NT40_ENV
-    if (seenvol) {
-       if (afs_winsockInit() < 0) {
-           ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0,
-                               AFSDIR_SALVAGER_FILE, 0);
-           Log("Failed to initailize winsock, exiting.\n");
-           Exit(1);
-       }
-    }
-#endif
-    VInitVolumePackage(seenvol ? volumeUtility : salvager, 5, 5,
-                      DONT_CONNECT_FS, 0);
-    DInit(10);
-#ifdef AFS_NT40_ENV
-    if (myjob.cj_number != NOT_CHILD) {
-       if (!seenpart) {
-           seenpart = 1;
-           (void)strcpy(pname, myjob.cj_part);
-       }
-    }
-#endif
-    if (seenpart == 0) {
-       for (partP = DiskPartitionList; partP; partP = partP->next) {
-           SalvageFileSysParallel(partP);
-       }
-       SalvageFileSysParallel(0);
-    } else {
-       partP = VGetPartition(pname, 0);
-       if (!partP) {
-           Log("salvage: Unknown or unmounted partition %s; salvage aborted\n", pname);
-           Exit(1);
-       }
-       if (!seenvol)
-           SalvageFileSys(partP, 0);
-       else {
-           /* Salvage individual volume */
-           if (vid <= 0) {
-               Log("salvage: invalid volume id specified; salvage aborted\n");
-               Exit(1);
-           }
-           SalvageFileSys(partP, vid);
-       }
-    }
-    return (0);
-}
-
-
-#ifndef AFS_NT40_ENV
-#include "AFS_component_version_number.c"
-#endif
 #define MAX_ARGS 128
 #ifdef AFS_NT40_ENV
 char *save_args[MAX_ARGS];
 int n_save_args = 0;
-pthread_t main_thread;
-#endif
-
-int
-main(int argc, char **argv)
-{
-    struct cmd_syndesc *ts;
-    int err = 0;
-    char commandLine[150];
-
-    int i;
-    extern char cml_version_number[];
-
-#ifdef AFS_AIX32_ENV
-    /*
-     * The following signal action for AIX is necessary so that in case of a 
-     * crash (i.e. core is generated) we can include the user's data section 
-     * in the core dump. Unfortunately, by default, only a partial core is
-     * generated which, in many cases, isn't too useful.
-     */
-    struct sigaction nsa;
-
-    sigemptyset(&nsa.sa_mask);
-    nsa.sa_handler = SIG_DFL;
-    nsa.sa_flags = SA_FULLDUMP;
-    sigaction(SIGABRT, &nsa, NULL);
-    sigaction(SIGSEGV, &nsa, NULL);
-#endif
-
-    /* Initialize directory paths */
-    if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
-#ifdef AFS_NT40_ENV
-       ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
-#endif
-       fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
-               argv[0]);
-       exit(2);
-    }
-#ifdef AFS_NT40_ENV
-    main_thread = pthread_self();
-    if (spawnDatap && spawnDataLen) {
-       /* This is a child per partition salvager. Don't setup log or
-        * try to lock the salvager lock.
-        */
-       if (nt_SetupPartitionSalvage(spawnDatap, spawnDataLen) < 0)
-           exit(3);
-    } else {
-#endif
-       for (commandLine[0] = '\0', i = 0; i < argc; i++) {
-           if (i > 0)
-               strcat(commandLine, " ");
-           strcat(commandLine, argv[i]);
-       }
-
-       /* All entries to the log will be appended.  Useful if there are
-        * multiple salvagers appending to the log.
-        */
-
-       CheckLogFile();
-#ifndef AFS_NT40_ENV
-#ifdef AFS_LINUX20_ENV
-       fcntl(fileno(logFile), F_SETFL, O_APPEND);      /* Isn't this redundant? */
-#else
-       fcntl(fileno(logFile), F_SETFL, FAPPEND);       /* Isn't this redundant? */
-#endif
-#endif
-       setlinebuf(logFile);
-
-#ifndef AFS_NT40_ENV
-       if (geteuid() != 0) {
-           printf("Salvager must be run as root.\n");
-           fflush(stdout);
-           Exit(0);
-       }
-#endif
-
-       /* bad for normal help flag processing, but can do nada */
-
-       fprintf(logFile, "%s\n", cml_version_number);
-       Log("STARTING AFS SALVAGER %s (%s)\n", SalvageVersion, commandLine);
-
-       /* Get and hold a lock for the duration of the salvage to make sure
-        * that no other salvage runs at the same time.  The routine
-        * VInitVolumePackage (called below) makes sure that a file server or
-        * other volume utilities don't interfere with the salvage.
-        */
-       ObtainSalvageLock();
-#ifdef AFS_NT40_ENV
-    }
+extern pthread_t main_thread;
+childJob_t myjob = { SALVAGER_MAGIC, NOT_CHILD, "" };
 #endif
 
-    ts = cmd_CreateSyntax("initcmd", handleit, 0, "initialize the program");
-    cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL,
-               "Name of partition to salvage");
-    cmd_AddParm(ts, "-volumeid", CMD_SINGLE, CMD_OPTIONAL,
-               "Volume Id to salvage");
-    cmd_AddParm(ts, "-debug", CMD_FLAG, CMD_OPTIONAL,
-               "Run in Debugging mode");
-    cmd_AddParm(ts, "-nowrite", CMD_FLAG, CMD_OPTIONAL,
-               "Run readonly/test mode");
-    cmd_AddParm(ts, "-inodes", CMD_FLAG, CMD_OPTIONAL,
-               "Just list affected afs inodes - debugging flag");
-    cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL, "Force full salvaging");
-    cmd_AddParm(ts, "-oktozap", CMD_FLAG, CMD_OPTIONAL,
-               "Give permission to destroy bogus inodes/volumes - debugging flag");
-    cmd_AddParm(ts, "-rootinodes", CMD_FLAG, CMD_OPTIONAL,
-               "Show inodes owned by root - debugging flag");
-    cmd_AddParm(ts, "-salvagedirs", CMD_FLAG, CMD_OPTIONAL,
-               "Force rebuild/salvage of all directories");
-    cmd_AddParm(ts, "-blockreads", CMD_FLAG, CMD_OPTIONAL,
-               "Read smaller blocks to handle IO/bad blocks");
-    cmd_AddParm(ts, "-parallel", CMD_SINGLE, CMD_OPTIONAL,
-               "# of max parallel partition salvaging");
-    cmd_AddParm(ts, "-tmpdir", CMD_SINGLE, CMD_OPTIONAL,
-               "Name of dir to place tmp files ");
-    cmd_AddParm(ts, "-showlog", CMD_FLAG, CMD_OPTIONAL,
-               "Show log file upon completion");
-    cmd_AddParm(ts, "-showsuid", CMD_FLAG, CMD_OPTIONAL,
-               "Report on suid/sgid files");
-    cmd_AddParm(ts, "-showmounts", CMD_FLAG, CMD_OPTIONAL,
-               "Report on mountpoints");
-    cmd_AddParm(ts, "-orphans", CMD_SINGLE, CMD_OPTIONAL,
-               "ignore | remove | attach");
-
-    /* note - syslog isn't avail on NT, but if we make it conditional, have
-     * to deal with screwy offsets for cmd params */
-    cmd_AddParm(ts, "-syslog", CMD_FLAG, CMD_OPTIONAL,
-               "Write salvage log to syslogs");
-    cmd_AddParm(ts, "-syslogfacility", CMD_SINGLE, CMD_OPTIONAL,
-               "Syslog facility number to use");
-    cmd_AddParm(ts, "-datelogs", CMD_FLAG, CMD_OPTIONAL,
-               "Include timestamp in logfile filename");
-
-#ifdef FAST_RESTART
-    cmd_AddParm(ts, "-DontSalvage", CMD_FLAG, CMD_OPTIONAL,
-               "Don't salvage. This my be set in BosConfig to let the fileserver restart immediately after a crash. Bad volumes will be taken offline");
-#endif /* FAST_RESTART */
-    err = cmd_Dispatch(argc, argv);
-    Exit(err);
-}
-
 /* Get the salvage lock if not already held. Hold until process exits. */
 void
 ObtainSalvageLock(void)
 {
-    int salvageLock;
+    FD_t salvageLock;
 
 #ifdef AFS_NT40_ENV
     salvageLock =
-       (int)CreateFile(AFSDIR_SERVER_SLVGLOCK_FILEPATH, 0, 0, NULL,
+       (FD_t)CreateFile(AFSDIR_SERVER_SLVGLOCK_FILEPATH, 0, 0, NULL,
                        OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
-    if (salvageLock == (int)INVALID_HANDLE_VALUE) {
+    if (salvageLock == INVALID_FD) {
        fprintf(stderr,
                "salvager:  There appears to be another salvager running!  Aborted.\n");
        Exit(1);
@@ -953,7 +440,7 @@ CheckIfBigFilesFS(char *mountPoint, char *devName)
 #define HDSTR "\\Device\\Harddisk"
 #define HDLEN  (sizeof(HDSTR)-1)       /* Length of "\Device\Harddisk" */
 int
-SameDisk(struct DiskPartition *p1, struct DiskPartition *p2)
+SameDisk(struct DiskPartition64 *p1, struct DiskPartition64 *p2)
 {
 #define RES_LEN 256
     char res[RES_LEN];
@@ -994,10 +481,10 @@ SameDisk(struct DiskPartition *p1, struct DiskPartition *p2)
  * PartsPerDisk are on the same disk.
  */
 void
-SalvageFileSysParallel(struct DiskPartition *partP)
+SalvageFileSysParallel(struct DiskPartition64 *partP)
 {
     struct job {
-       struct DiskPartition *partP;
+       struct DiskPartition64 *partP;
        int pid;                /* Pid for this job */
        int jobnumb;            /* Log file job number */
        struct job *nextjob;    /* Next partition on disk to salvage */
@@ -1185,7 +672,7 @@ SalvageFileSysParallel(struct DiskPartition *partP)
 
 
 void
-SalvageFileSys(struct DiskPartition *partP, VolumeId singleVolumeNumber)
+SalvageFileSys(struct DiskPartition64 *partP, VolumeId singleVolumeNumber)
 {
     if (!canfork || debug || Fork() == 0) {
        SalvageFileSys1(partP, singleVolumeNumber);
@@ -1217,7 +704,7 @@ get_DevName(char *pbuffer, char *wpath)
 }
 
 void
-SalvageFileSys1(struct DiskPartition *partP, VolumeId singleVolumeNumber)
+SalvageFileSys1(struct DiskPartition64 *partP, VolumeId singleVolumeNumber)
 {
     char *name, *tdir;
     char inodeListPath[256];
@@ -1249,10 +736,11 @@ SalvageFileSys1(struct DiskPartition *partP, VolumeId singleVolumeNumber)
        ForceSalvage = UseTheForceLuke(fileSysPath);
 
     if (singleVolumeNumber) {
-       if (!VConnectFS()) {
+       /* salvageserver already setup fssync conn for us */
+       if ((programType != salvageServer) && !VConnectFS()) {
            Abort("Couldn't connect to file server\n");
        }
-       AskOffline(singleVolumeNumber);
+       AskOffline(singleVolumeNumber, partP->name);
     } else {
        if (!Showmode)
            Log("SALVAGING FILE SYSTEM PARTITION %s (device=%s%s)\n",
@@ -1303,7 +791,7 @@ SalvageFileSys1(struct DiskPartition *partP, VolumeId singleVolumeNumber)
      * modified to actually do that so that the NT crt can be used there.
      */
     inodeFd =
-       _open_osfhandle((long)nt_open(inodeListPath, O_RDWR, 0), O_RDWR);
+       _open_osfhandle((intptr_t)nt_open(inodeListPath, O_RDWR, 0), O_RDWR);
     nt_unlink(inodeListPath);  /* NT's crt unlink won't if file is open. */
 #else
     inodeFd = afs_open(inodeListPath, O_RDONLY);
@@ -1386,13 +874,20 @@ SalvageFileSys1(struct DiskPartition *partP, VolumeId singleVolumeNumber)
 void
 DeleteExtraVolumeHeaderFile(register struct VolumeSummary *vsp)
 {
+    char path[64];
+    sprintf(path, "%s/%s", fileSysPath, vsp->fileName);
+
     if (!Showmode)
-       Log("The volume header file %s is not associated with any actual data (%sdeleted)\n", vsp->fileName, (Testing ? "would have been " : ""));
-    if (!Testing)
-       unlink(vsp->fileName);
+       Log("The volume header file %s is not associated with any actual data (%sdeleted)\n", path, (Testing ? "would have been " : ""));
+    if (!Testing) {
+       if (unlink(path)) {
+           Log("Unable to unlink %s (errno = %d)\n", path, errno);
+       }
+    }
     vsp->fileName = 0;
 }
 
+int
 CompareInodes(const void *_p1, const void *_p2)
 {
     register const struct ViceInodeInfo *p1 = _p1;
@@ -1493,9 +988,9 @@ void
 CountVolumeInodes(register struct ViceInodeInfo *ip, int maxInodes,
                  register struct InodeSummary *summary)
 {
-    int volume = ip->u.vnode.volumeId;
-    int rwvolume = volume;
-    register n, nSpecial;
+    VolumeId volume = ip->u.vnode.volumeId;
+    VolumeId rwvolume = volume;
+    register int n, nSpecial;
     register Unique maxunique;
     n = nSpecial = 0;
     maxunique = 0;
@@ -1520,7 +1015,7 @@ CountVolumeInodes(register struct ViceInodeInfo *ip, int maxInodes,
 }
 
 int
-OnlyOneVolume(struct ViceInodeInfo *inodeinfo, VolumeId singleVolumeNumber, void *rock)
+OnlyOneVolume(struct ViceInodeInfo *inodeinfo, afs_uint32 singleVolumeNumber, void *rock)
 {
     if (inodeinfo->u.vnode.vnodeNumber == INODESPECIAL)
        return (inodeinfo->u.special.parentId == singleVolumeNumber);
@@ -1710,14 +1205,15 @@ CompareVolumes(const void *_p1, const void *_p2)
 void
 GetVolumeSummary(VolumeId singleVolumeNumber)
 {
-    DIR *dirp;
+    DIR *dirp = NULL;
     afs_int32 nvols = 0;
     struct VolumeSummary *vsp, vs;
     struct VolumeDiskHeader diskHeader;
     struct dirent *dp;
 
     /* Get headers from volume directory */
-    if (chdir(fileSysPath) == -1 || (dirp = opendir(".")) == NULL)
+    dirp = opendir(fileSysPath);
+    if (dirp  == NULL)
        Abort("Can't read directory %s; not salvaged\n", fileSysPath);
     if (!singleVolumeNumber) {
        while ((dp = readdir(dirp))) {
@@ -1725,7 +1221,9 @@ GetVolumeSummary(VolumeId singleVolumeNumber)
            p = strrchr(dp->d_name, '.');
            if (p != NULL && strcmp(p, VHDREXT) == 0) {
                int fd;
-               if ((fd = afs_open(dp->d_name, O_RDONLY)) != -1
+               char name[64];
+               sprintf(name, "%s/%s", fileSysPath, dp->d_name);
+               if ((fd = afs_open(name, O_RDONLY)) != -1
                    && read(fd, (char *)&diskHeader, sizeof(diskHeader))
                    == sizeof(diskHeader)
                    && diskHeader.stamp.magic == VOLUMEHEADERMAGIC) {
@@ -1759,7 +1257,9 @@ GetVolumeSummary(VolumeId singleVolumeNumber)
        if (p != NULL && strcmp(p, VHDREXT) == 0) {
            int error = 0;
            int fd;
-           if ((fd = afs_open(dp->d_name, O_RDONLY)) == -1
+           char name[64];
+           sprintf(name, "%s/%s", fileSysPath, dp->d_name);
+           if ((fd = afs_open(name, O_RDONLY)) == -1
                || read(fd, &diskHeader, sizeof(diskHeader))
                != sizeof(diskHeader)
                || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
@@ -1769,31 +1269,52 @@ GetVolumeSummary(VolumeId singleVolumeNumber)
            if (error) {
                if (!singleVolumeNumber) {
                    if (!Showmode)
-                       Log("%s/%s is not a legitimate volume header file; %sdeleted\n", fileSysPathName, dp->d_name, (Testing ? "it would have been " : ""));
-                   if (!Testing)
-                       unlink(dp->d_name);
+                       Log("%s is not a legitimate volume header file; %sdeleted\n", name, (Testing ? "it would have been " : ""));
+                   if (!Testing) {
+                       if (unlink(name)) {
+                           Log("Unable to unlink %s (errno = %d)\n", name, errno);
+                       }
+                   }
                }
            } else {
                char nameShouldBe[64];
                DiskToVolumeHeader(&vsp->header, &diskHeader);
                if (singleVolumeNumber && vsp->header.id == singleVolumeNumber
                    && vsp->header.parent != singleVolumeNumber) {
-                   Log("%u is a read-only volume; not salvaged\n",
-                       singleVolumeNumber);
-                   Exit(1);
+                   if (programType == salvageServer) {
+#ifdef SALVSYNC_BUILD_CLIENT
+                       Log("fileserver requested salvage of clone %u; scheduling salvage of volume group %u...\n",
+                           vsp->header.id, vsp->header.parent);
+                       if (SALVSYNC_LinkVolume(vsp->header.parent,
+                                               vsp->header.id,
+                                               fileSysPartition->name,
+                                               NULL) != SYNC_OK) {
+                           Log("schedule request failed\n");
+                       }
+#endif
+                       Exit(SALSRV_EXIT_VOLGROUP_LINK);
+                   } else {
+                       Log("%u is a read-only volume; not salvaged\n",
+                           singleVolumeNumber);
+                       Exit(1);
+                   }
                }
                if (!singleVolumeNumber
                    || (vsp->header.id == singleVolumeNumber
                        || vsp->header.parent == singleVolumeNumber)) {
                    (void)afs_snprintf(nameShouldBe, sizeof nameShouldBe,
-                                      VFORMAT, vsp->header.id);
-                   if (singleVolumeNumber)
-                       AskOffline(vsp->header.id);
+                                      VFORMAT, afs_printable_uint32_lu(vsp->header.id));
+                   if (singleVolumeNumber 
+                       && vsp->header.id != singleVolumeNumber)
+                       AskOffline(vsp->header.id, fileSysPartition->name);
                    if (strcmp(nameShouldBe, dp->d_name)) {
                        if (!Showmode)
-                           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 " : ""));
-                       if (!Testing)
-                           unlink(dp->d_name);
+                           Log("Volume header file %s is incorrectly named; %sdeleted (it will be recreated later, if necessary)\n", name, (Testing ? "it would have been " : ""));
+                       if (!Testing) {
+                           if (unlink(name)) {
+                               Log("Unable to unlink %s (errno = %d)\n", name, errno);
+                           }
+                       }
                    } else {
                        vsp->fileName = ToString(dp->d_name);
                        nVolumes++;
@@ -1849,7 +1370,7 @@ CreateLinkTable(register struct InodeSummary *isp, Inode ino)
        Abort("Can't open link table for volume %u (error = %d)\n",
              isp->RWvolumeId, errno);
 
-    if (FDH_TRUNC(fdP, 0) < 0)
+    if (FDH_TRUNC(fdP, sizeof(version) + sizeof(short)) < 0)
        Abort("Can't truncate link table for volume %u (error = %d)\n",
              isp->RWvolumeId, errno);
 
@@ -1927,7 +1448,7 @@ DoSalvageVolumeGroup(register struct InodeSummary *isp, int nVols)
     int check;
     Inode ino;
     int dec_VGLinkH = 0;
-    int VGLinkH_p1;
+    int VGLinkH_p1 =0;
     FdHandle_t *fdP = NULL;
 
     VGLinkH_cnt = 0;
@@ -1971,7 +1492,23 @@ DoSalvageVolumeGroup(register struct InodeSummary *isp, int nVols)
        if (Testing) {
            IH_INIT(VGLinkH, fileSysDevice, -1, -1);
        } else {
+            int i, j;
+            struct ViceInodeInfo *ip;
            CreateLinkTable(isp, ino);
+           fdP = IH_OPEN(VGLinkH);
+            /* Sync fake 1 link counts to the link table, now that it exists */
+            if (fdP) {
+               for (i = 0; i < nVols; i++) {
+                       ip = allInodes + isp[i].index;
+                        for (j = isp[i].nSpecialInodes; j < isp[i].nInodes; j++) {
+#ifdef AFS_NT40_ENV
+                                nt_SetLinkCount(fdP, ip[j].inodeNumber, 1, 1);
+#else
+                                namei_SetLinkCount(fdP, ip[j].inodeNumber, 1, 1);
+#endif
+                   }
+               }
+           }
        }
     }
     if (fdP)
@@ -2116,7 +1653,7 @@ QuickCheck(register struct InodeSummary *isp, int nVols)
            && volHeader.stamp.magic == VOLUMEINFOMAGIC
            && volHeader.dontSalvage == DONT_SALVAGE
            && volHeader.needsSalvaged == 0 && volHeader.destroyMe == 0) {
-           if (volHeader.inUse == 1) {
+           if (volHeader.inUse != 0) {
                volHeader.inUse = 0;
                volHeader.inService = 1;
                if (!Testing) {
@@ -2218,23 +1755,26 @@ SalvageVolumeHeaderFile(register struct InodeSummary *isp,
     }
 
     if (isp->volSummary == NULL) {
-       char name[64];
-       (void)afs_snprintf(name, sizeof name, VFORMAT, isp->volumeId);
+       char path[64];
+       char headerName[64];
+       (void)afs_snprintf(headerName, sizeof headerName, VFORMAT, afs_printable_uint32_lu(isp->volumeId));
+       (void)afs_snprintf(path, sizeof path, "%s/%s", fileSysPath, headerName);
        if (check) {
            Log("No header file for volume %u\n", isp->volumeId);
            return -1;
        }
        if (!Showmode)
-           Log("No header file for volume %u; %screating %s/%s\n",
+           Log("No header file for volume %u; %screating %s\n",
                isp->volumeId, (Testing ? "it would have been " : ""),
-               fileSysPathName, name);
-       headerFd = afs_open(name, O_RDWR | O_CREAT | O_TRUNC, 0644);
+               path);
+       headerFd = afs_open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
        assert(headerFd != -1);
        isp->volSummary = (struct VolumeSummary *)
            malloc(sizeof(struct VolumeSummary));
-       isp->volSummary->fileName = ToString(name);
+       isp->volSummary->fileName = ToString(headerName);
     } else {
-       char name[64];
+       char path[64];
+       char headerName[64];
        /* hack: these two fields are obsolete... */
        isp->volSummary->header.volumeAcl = 0;
        isp->volSummary->header.volumeMountTable = 0;
@@ -2244,18 +1784,19 @@ SalvageVolumeHeaderFile(register struct InodeSummary *isp,
             sizeof(struct VolumeHeader))) {
            /* We often remove the name before calling us, so we make a fake one up */
            if (isp->volSummary->fileName) {
-               strcpy(name, isp->volSummary->fileName);
+               strcpy(headerName, isp->volSummary->fileName);
            } else {
-               (void)afs_snprintf(name, sizeof name, VFORMAT, isp->volumeId);
-               isp->volSummary->fileName = ToString(name);
+               (void)afs_snprintf(headerName, sizeof headerName, VFORMAT, afs_printable_uint32_lu(isp->volumeId));
+               isp->volSummary->fileName = ToString(headerName);
            }
+           (void)afs_snprintf(path, sizeof path, "%s/%s", fileSysPath, headerName);
 
-           Log("Header file %s is damaged or no longer valid%s\n", name,
+           Log("Header file %s is damaged or no longer valid%s\n", path,
                (check ? "" : "; repairing"));
            if (check)
                return -1;
 
-           headerFd = afs_open(name, O_RDWR | O_TRUNC, 0644);
+           headerFd = afs_open(path, O_RDWR | O_TRUNC, 0644);
            assert(headerFd != -1);
        }
     }
@@ -2554,7 +2095,7 @@ SalvageIndex(Inode ino, VnodeClass class, int RW,
                     * if no such match, take the first determined by our sort
                     * order */
                    register struct ViceInodeInfo *lip = ip;
-                   register lnInodes = nInodes;
+                   register int lnInodes = nInodes;
                    while (lnInodes
                           && lip->u.vnode.vnodeNumber == vnodeNumber) {
                        if (VNDISK_GET_INO(vnode) == lip->inodeNumber) {
@@ -2684,11 +2225,14 @@ SalvageIndex(Inode ino, VnodeClass class, int RW,
                        }
                        if (VNDISK_GET_INO(vnode)) {
                            if (!Showmode) {
-                               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)));
+                               time_t serverModifyTime = vnode->serverModifyTime;
+                               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(&serverModifyTime));
                            }
                        } else {
-                           if (!Showmode)
-                               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)));
+                           if (!Showmode) {
+                               time_t serverModifyTime = vnode->serverModifyTime;
+                               Log("Vnode %d (unique %u): bad directory vnode (no inode number listed); vnode deleted, vnode mod time=%s", vnodeNumber, vnode->uniquifier, ctime(&serverModifyTime));
+                           }
                        }
                        memset(vnode, 0, vcp->diskSize);
                        vnodeChanged = 1;
@@ -2744,7 +2288,7 @@ CopyOnWrite(register struct DirSummary *dir)
     struct VnodeDiskObject vnode;
     struct VnodeClassInfo *vcp = &VnodeClassInfo[vLarge];
     Inode oldinode, newinode;
-    int code;
+    afs_sfsize_t code;
 
     if (dir->copied || Testing)
        return;
@@ -2795,18 +2339,21 @@ CopyAndSalvage(register struct DirSummary *dir)
     struct VnodeClassInfo *vcp = &VnodeClassInfo[vLarge];
     Inode oldinode, newinode;
     DirHandle newdir;
-    register afs_int32 code;
+    FdHandle_t *fdP;
+    afs_int32 code;
+    afs_sfsize_t lcode;
     afs_int32 parentUnique = 1;
     struct VnodeEssence *vnodeEssence;
+    afs_fsize_t length;
 
     if (Testing)
        return;
     Log("Salvaging directory %u...\n", dir->vnodeNumber);
-    code =
+    lcode =
        IH_IREAD(vnodeInfo[vLarge].handle,
                 vnodeIndexOffset(vcp, dir->vnodeNumber), (char *)&vnode,
                 sizeof(vnode));
-    assert(code == sizeof(vnode));
+    assert(lcode == sizeof(vnode));
     oldinode = VNDISK_GET_INO(&vnode);
     /* Increment the version number by a whole lot to avoid problems with
      * clients that were promised new version numbers--but the file server
@@ -2837,8 +2384,10 @@ CopyAndSalvage(register struct DirSummary *dir)
     if (code) {
        /* didn't really build the new directory properly, let's just give up. */
        code = IH_DEC(dir->ds_linkH, newinode, dir->rwVid);
-       assert(code == 0);
        Log("Directory salvage returned code %d, continuing.\n", code);
+       if (code) {
+           Log("also failed to decrement link count on new inode");
+       }
        assert(1 == 2);
     }
     Log("Checking the results of the directory salvage...\n");
@@ -2850,12 +2399,14 @@ CopyAndSalvage(register struct DirSummary *dir)
     }
     vnode.cloned = 0;
     VNDISK_SET_INO(&vnode, newinode);
-    VNDISK_SET_LEN(&vnode, Length(&newdir));
-    code =
+    length = Length(&newdir);
+    VNDISK_SET_LEN(&vnode, length);
+    lcode =
        IH_IWRITE(vnodeInfo[vLarge].handle,
                  vnodeIndexOffset(vcp, dir->vnodeNumber), (char *)&vnode,
                  sizeof(vnode));
-    assert(code == sizeof(vnode));
+    assert(lcode == sizeof(vnode));
+#if 0
 #ifdef AFS_NT40_ENV
     nt_sync(fileSysDevice);
 #else
@@ -2863,15 +2414,23 @@ CopyAndSalvage(register struct DirSummary *dir)
                                 * an open FD on the file itself to fsync.
                                 */
 #endif
+#else
+    vnodeInfo[vLarge].handle->ih_synced = 1;
+#endif
+    /* make sure old directory file is really closed */
+    fdP = IH_OPEN(dir->dirHandle.dirh_handle);
+    FDH_REALLYCLOSE(fdP);
+    
     code = IH_DEC(dir->ds_linkH, oldinode, dir->rwVid);
     assert(code == 0);
     dir->dirHandle = newdir;
 }
 
-void
-JudgeEntry(struct DirSummary *dir, char *name, VnodeId vnodeNumber,
-          Unique unique)
+int
+JudgeEntry(void *dirVal, char *name, afs_int32 vnodeNumber,
+          afs_int32 unique)
 {
+    struct DirSummary *dir = (struct DirSummary *)dirVal;
     struct VnodeEssence *vnodeEssence;
     afs_int32 dirOrphaned, todelete;
 
@@ -2886,7 +2445,7 @@ JudgeEntry(struct DirSummary *dir, char *name, VnodeId vnodeNumber,
            CopyOnWrite(dir);
            assert(Delete(&dir->dirHandle, name) == 0);
        }
-       return;
+       return 0;
     }
 #ifdef AFS_AIX_ENV
 #ifndef AFS_NAMEI_ENV
@@ -2900,7 +2459,7 @@ JudgeEntry(struct DirSummary *dir, char *name, VnodeId vnodeNumber,
            CopyOnWrite(dir);
            assert(Delete(&dir->dirHandle, name) == 0);
        }
-       return;
+       return 0;
     }
 #endif
 #endif
@@ -2918,7 +2477,7 @@ JudgeEntry(struct DirSummary *dir, char *name, VnodeId vnodeNumber,
                CopyOnWrite(dir);
                assert(Delete(&dir->dirHandle, name) == 0);
            }
-           return;
+           return 0;
        }
     }
 
@@ -2933,7 +2492,7 @@ JudgeEntry(struct DirSummary *dir, char *name, VnodeId vnodeNumber,
             * entry. Otherwise, it will get created in the next 
             * salvage and deleted again here. So Just skip it.
             */
-           return;
+           return 0;
        }
 
        todelete = ((!vnodeEssence->unique || dirOrphaned) ? 1 : 0);
@@ -2942,7 +2501,7 @@ JudgeEntry(struct DirSummary *dir, char *name, VnodeId vnodeNumber,
            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")));
        }
        if (!Testing) {
-           ViceFid fid;
+           AFSFid fid;
            fid.Vnode = vnodeNumber;
            fid.Unique = vnodeEssence->unique;
            CopyOnWrite(dir);
@@ -2951,12 +2510,12 @@ JudgeEntry(struct DirSummary *dir, char *name, VnodeId vnodeNumber,
                assert(Create(&dir->dirHandle, name, &fid) == 0);
        }
        if (todelete)
-           return;             /* no need to continue */
+           return 0;           /* no need to continue */
     }
 
     if (strcmp(name, ".") == 0) {
        if (dir->vnodeNumber != vnodeNumber || (dir->unique != unique)) {
-           ViceFid fid;
+           AFSFid fid;
            if (!Showmode)
                Log("directory vnode %u.%u: bad '.' entry (was %u.%u); fixed\n", dir->vnodeNumber, dir->unique, vnodeNumber, unique);
            if (!Testing) {
@@ -2973,7 +2532,7 @@ JudgeEntry(struct DirSummary *dir, char *name, VnodeId vnodeNumber,
        }
        dir->haveDot = 1;
     } else if (strcmp(name, "..") == 0) {
-       ViceFid pa;
+       AFSFid pa;
        if (dir->parent) {
            struct VnodeEssence *dotdot;
            pa.Vnode = dir->parent;
@@ -3008,31 +2567,51 @@ JudgeEntry(struct DirSummary *dir, char *name, VnodeId vnodeNumber,
        }
        vnodeEssence->claimed = 0;      /* Not claimed: Orphaned */
        vnodeEssence->todelete = 1;     /* Will later delete vnode and decr inode */
-       return;
+       return 0;
     } else {
        if (ShowSuid && (vnodeEssence->modeBits & 06000))
            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);
-       if (ShowMounts && (vnodeEssence->type == vSymlink)
+       if (/* ShowMounts && */ (vnodeEssence->type == vSymlink)
            && !(vnodeEssence->modeBits & 0111)) {
            int code, size;
-           char buf[1024];
+           char buf[1025];
            IHandle_t *ihP;
            FdHandle_t *fdP;
 
            IH_INIT(ihP, fileSysDevice, dir->dirHandle.dirh_handle->ih_vid,
                    vnodeEssence->InodeNumber);
            fdP = IH_OPEN(ihP);
-           assert(fdP != NULL);
+           if (fdP == NULL) {
+               Log("ERROR %s could not open mount point vnode %u\n", dir->vname, vnodeNumber);
+               IH_RELEASE(ihP);
+               return 0;
+           }
            size = FDH_SIZE(fdP);
-           assert(size != -1);
-           memset(buf, 0, 1024);
+           if (size < 0) {
+               Log("ERROR %s mount point has invalid size %d, vnode %u\n", dir->vname, size, vnodeNumber);
+               FDH_REALLYCLOSE(fdP);
+               IH_RELEASE(ihP);
+               return 0;
+           }
+       
            if (size > 1024)
                size = 1024;
            code = FDH_READ(fdP, buf, size);
-           assert(code == size);
-           Log("In volume %u (%s) found mountpoint %s/%s to '%s'\n",
-               dir->dirHandle.dirh_handle->ih_vid, dir->vname,
-               dir->name ? dir->name : "??", name, buf);
+           if (code == size) {
+               buf[size] = '\0';
+               if ( (*buf != '#' && *buf != '%') || buf[strlen(buf)-1] != '.' ) {
+                   Log("Volume %u (%s) mount point %s/%s to '%s' invalid, %s to symbolic link\n",
+                       dir->dirHandle.dirh_handle->ih_vid, dir->vname, dir->name ? dir->name : "??", name, buf,
+                       Testing ? "would convert" : "converted");
+                   vnodeEssence->modeBits |= 0111;
+                   vnodeEssence->changed = 1;
+               } else if (ShowMounts) Log("In volume %u (%s) found mountpoint %s/%s to '%s'\n",
+                   dir->dirHandle.dirh_handle->ih_vid, dir->vname,
+                   dir->name ? dir->name : "??", name, buf);
+           } else {
+               Log("Volume %s cound not read mount point vnode %u size %d code %d\n",
+                   dir->vname, vnodeNumber, size, code);
+           }
            FDH_REALLYCLOSE(fdP);
            IH_RELEASE(ihP);
        }
@@ -3079,13 +2658,14 @@ JudgeEntry(struct DirSummary *dir, char *name, VnodeId vnodeNumber,
                    CopyOnWrite(dir);
                    assert(Delete(&dir->dirHandle, name) == 0);
                }
-               return;
+               return 0;
            }
        }
        /* This directory claims the vnode */
        vnodeEssence->claimed = 1;
     }
     vnodeEssence->count--;
+    return 0;
 }
 
 void
@@ -3095,7 +2675,7 @@ DistilVnodeEssence(VolumeId rwVId, VnodeClass class, Inode ino, Unique * maxu)
     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
     char buf[SIZEOF_LARGEDISKVNODE];
     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
-    int size;
+    afs_sfsize_t size;
     StreamHandle_t *file;
     int vnodeIndex;
     int nVnodes;
@@ -3148,8 +2728,16 @@ DistilVnodeEssence(VolumeId rwVId, VnodeClass class, Inode ino, Unique * maxu)
            vep->owner = vnode->owner;
            vep->group = vnode->group;
            if (vnode->type == vDirectory) {
-               assert(class == vLarge);
-               vip->inodes[vnodeIndex] = VNDISK_GET_INO(vnode);
+               if (class != vLarge) {
+                   VnodeId vnodeNumber = bitNumberToVnodeNumber(vnodeIndex, class);
+                   vip->nAllocatedVnodes--;
+                   memset(vnode, 0, sizeof(vnode));
+                   IH_IWRITE(vnodeInfo[vSmall].handle,
+                             vnodeIndexOffset(vcp, vnodeNumber),
+                             (char *)&vnode, sizeof(vnode));
+                   VolumeChanged = 1;
+               } else
+                   vip->inodes[vnodeIndex] = VNDISK_GET_INO(vnode);
            }
        }
     }
@@ -3302,8 +2890,8 @@ SalvageVolume(register struct InodeSummary *rwIsp, IHandle_t * alinkH)
     struct VnodeEssence *vep;
     afs_int32 v, pv;
     IHandle_t *h;
-    int nBytes;
-    ViceFid pa;
+    afs_sfsize_t nBytes;
+    AFSFid pa;
     VnodeId LFVnode, ThisVnode;
     Unique LFUnique, ThisUnique;
     char npath[128];
@@ -3326,6 +2914,11 @@ SalvageVolume(register struct InodeSummary *rwIsp, IHandle_t * alinkH)
        SalvageDir(volHeader.name, vid, dirVnodeInfo, alinkH, i, &rootdir,
                   &rootdirfound);
     }
+#ifdef AFS_NT40_ENV
+    nt_sync(fileSysDevice);
+#else
+    sync();                            /* This used to be done lower level, for every dir */
+#endif
     if (Showmode) {
        IH_RELEASE(h);
        return 0;
@@ -3382,7 +2975,7 @@ SalvageVolume(register struct InodeSummary *rwIsp, IHandle_t * alinkH)
                 * won't be visible there.
                 */
                if (class == vLarge) {
-                   ViceFid pa;
+                   AFSFid pa;
                    DirHandle dh;
 
                    /* Remove and recreate the ".." entry in this orphaned directory */
@@ -3503,6 +3096,8 @@ SalvageVolume(register struct InodeSummary *rwIsp, IHandle_t * alinkH)
                    if (!Showmode) {
                        Log("Vnode %u: link count incorrect (was %d, %s %d)\n", vnodeNumber, oldCount, (Testing ? "would have changed to" : "now"), vnode.linkCount);
                    }
+               } else {
+                   vnode.modeBits = vnp->modeBits;
                }
 
                vnode.dataVersion++;
@@ -3575,7 +3170,7 @@ void
 ClearROInUseBit(struct VolumeSummary *summary)
 {
     IHandle_t *h = summary->volumeInfoHandle;
-    int nBytes;
+    afs_sfsize_t nBytes;
 
     VolumeDiskData volHeader;
 
@@ -3587,7 +3182,7 @@ ClearROInUseBit(struct VolumeSummary *summary)
     volHeader.inService = 1;
     volHeader.dontSalvage = DONT_SALVAGE;
     if (!Testing) {
-       nBytes = IH_IREAD(h, 0, (char *)&volHeader, sizeof(volHeader));
+       nBytes = IH_IWRITE(h, 0, (char *)&volHeader, sizeof(volHeader));
        assert(nBytes == sizeof(volHeader));
     }
 }
@@ -3614,8 +3209,13 @@ MaybeZapVolume(register struct InodeSummary *isp, char *message, int deleteMe,
                if (!Showmode)
                    Log("it will be deleted instead.  It should be recloned.\n");
            }
-           if (!Testing)
-               unlink(isp->volSummary->fileName);
+           if (!Testing) {
+               char path[64];
+               sprintf(path, "%s/%s", fileSysPath, isp->volSummary->fileName);
+               if (unlink(path)) {
+                   Log("Unable to unlink %s (errno = %d)\n", path, errno);
+               }
+           }
        }
     } else if (!check) {
        Log("%s salvage was unsuccessful: read-write volume %u\n", message,
@@ -3626,19 +3226,126 @@ MaybeZapVolume(register struct InodeSummary *isp, char *message, int deleteMe,
 
 
 void
-AskOffline(VolumeId volumeId)
+AskOffline(VolumeId volumeId, char * partition)
 {
-    if (FSYNC_askfs(volumeId, NULL, FSYNC_OFF, FSYNC_SALVAGE) == FSYNC_DENIED) {
-       Log("AskOffline:  file server denied offline request; a general salvage is required.\n");
+    afs_int32 code, i;
+
+    for (i = 0; i < 3; i++) {
+       code = FSYNC_VolOp(volumeId, partition, FSYNC_VOL_OFF, FSYNC_SALVAGE, NULL);
+
+       if (code == SYNC_OK) {
+           break;
+       } else if (code == SYNC_DENIED) {
+#ifdef DEMAND_ATTACH_ENABLE
+           Log("AskOffline:  file server denied offline request; a general salvage may be required.\n");
+#else
+           Log("AskOffline:  file server denied offline request; a general salvage is required.\n");
+#endif
+           Abort("Salvage aborted\n");
+       } else if (code == SYNC_BAD_COMMAND) {
+           Log("AskOffline:  fssync protocol mismatch (bad command word '%d'); salvage aborting.\n",
+               FSYNC_VOL_OFF);
+#ifdef DEMAND_ATTACH_ENABLE
+           Log("AskOffline:  please make sure fileserver, volserver, salvageserver and salvager binaries are same version.\n");
+#else
+           Log("AskOffline:  please make sure fileserver, volserver and salvager binaries are same version.\n");
+#endif
+           Abort("Salvage aborted\n");
+       } else if (i < 2) {
+           /* try it again */
+           Log("AskOffline:  request for fileserver to take volume offline failed; trying again...\n");
+           FSYNC_clientFinis();
+           FSYNC_clientInit();
+       }
+    }
+    if (code != SYNC_OK) {
+       Log("AskOffline:  request for fileserver to take volume offline failed; salvage aborting.\n");
        Abort("Salvage aborted\n");
     }
+
+#ifdef AFS_DEMAND_ATTACH_FS
+    /* set inUse = programType in the volume header. We do this in case
+     * the fileserver restarts/crashes while we are salvaging.
+     * Otherwise, the fileserver could attach the volume again on
+     * startup while we are salvaging, which would be very bad, or
+     * schedule another salvage while we are salvaging, which would be
+     * annoying. */
+    if (!Testing) {
+       int fd;
+       IHandle_t *h;
+       char name[VMAXPATHLEN];
+       struct VolumeHeader header;
+       struct VolumeDiskHeader diskHeader;
+       struct VolumeDiskData volHeader;
+
+       afs_snprintf(name, sizeof(name), "%s/" VFORMAT, fileSysPathName,
+           afs_printable_uint32_lu(volumeId));
+
+       fd = afs_open(name, O_RDONLY);
+       if (fd < 0) {
+           return;
+       }
+       if (read(fd, &diskHeader, sizeof(diskHeader)) != sizeof(diskHeader) ||
+           diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
+
+           close(fd);
+           return;
+       }
+       close(fd);
+
+       DiskToVolumeHeader(&header, &diskHeader);
+
+       IH_INIT(h, fileSysDevice, header.parent, header.volumeInfo);
+       if (IH_IREAD(h, 0, (char*)&volHeader, sizeof(volHeader)) != sizeof(volHeader) ||
+           volHeader.stamp.magic != VOLUMEINFOMAGIC) {
+
+           IH_RELEASE(h);
+           return;
+       }
+
+       volHeader.inUse = programType;
+
+       /* If we can't re-write the header, bail out and error. We don't
+        * assert when reading the header, since it's possible the
+        * header isn't really there (when there's no data associated
+        * with the volume; we just delete the vol header file in that
+        * case). But if it's there enough that we can read it, but
+        * somehow we cannot write to it to signify we're salvaging it,
+        * we've got a big problem and we cannot continue. */
+       assert(IH_IWRITE(h, 0, (char*)&volHeader, sizeof(volHeader)) == sizeof(volHeader));
+
+       IH_RELEASE(h);
+    }
+#endif /* AFS_DEMAND_ATTACH_FS */
 }
 
 void
 AskOnline(VolumeId volumeId, char *partition)
 {
-    if (FSYNC_askfs(volumeId, partition, FSYNC_ON, 0) == FSYNC_DENIED) {
-       Log("AskOnline:  file server denied online request to volume %u partition %s\n", volumeId, partition);
+    afs_int32 code, i;
+
+    for (i = 0; i < 3; i++) {
+       code = FSYNC_VolOp(volumeId, partition, FSYNC_VOL_ON, FSYNC_WHATEVER, NULL);
+
+       if (code == SYNC_OK) {
+           break;
+       } else if (code == SYNC_DENIED) {
+           Log("AskOnline:  file server denied online request to volume %u partition %s; trying again...\n", volumeId, partition);
+       } else if (code == SYNC_BAD_COMMAND) {
+           Log("AskOnline:  fssync protocol mismatch (bad command word '%d')\n",
+               FSYNC_VOL_ON);
+#ifdef DEMAND_ATTACH_ENABLE
+           Log("AskOnline:  please make sure fileserver, volserver, salvageserver and salvager binaries are same version.\n");
+#else
+           Log("AskOnline:  please make sure fileserver, volserver and salvager binaries are same version.\n");
+#endif
+           break;
+       } else if (i < 2) {
+           /* try it again */
+           Log("AskOnline:  request for fileserver to take volume offline failed; trying again...\n");
+           FSYNC_clientFinis();
+           FSYNC_clientInit();
+       }
     }
 }
 
@@ -3675,7 +3382,7 @@ PrintInodeList(void)
     register struct ViceInodeInfo *ip;
     struct ViceInodeInfo *buf;
     struct afs_stat status;
-    register nInodes;
+    register int nInodes;
 
     assert(afs_fstat(inodeFd, &status) == 0);
     buf = (struct ViceInodeInfo *)malloc(status.st_size);
@@ -3724,16 +3431,38 @@ Fork(void)
 #else
     f = fork();
     assert(f >= 0);
+#ifdef AFS_DEMAND_ATTACH_FS
+    if ((f == 0) && (programType == salvageServer)) {
+       /* we are a salvageserver child */
+#ifdef FSSYNC_BUILD_CLIENT
+       VChildProcReconnectFS_r();
+#endif
+#ifdef SALVSYNC_BUILD_CLIENT
+       VReconnectSALV_r();
 #endif
+    }
+#endif /* AFS_DEMAND_ATTACH_FS */
+#endif /* !AFS_NT40_ENV */
     return f;
 }
 
 void
-Exit(code)
-     int code;
+Exit(int code)
 {
     if (ShowLog)
        showlog();
+
+#ifdef AFS_DEMAND_ATTACH_FS
+    if (programType == salvageServer) {
+#ifdef SALVSYNC_BUILD_CLIENT
+       VDisconnectSALV();
+#endif
+#ifdef FSSYNC_BUILD_CLIENT
+       VDisconnectFS();
+#endif
+    }
+#endif /* AFS_DEMAND_ATTACH_FS */
+
 #ifdef AFS_NT40_ENV
     if (main_thread != pthread_self())
        pthread_exit((void *)code);
@@ -3765,14 +3494,14 @@ TimeStamp(time_t clock, int precision)
     static char timestamp[20];
     lt = localtime(&clock);
     if (precision)
-       (void)strftime(timestamp, 20, "%m/%d/%Y %T", lt);
+       (void)strftime(timestamp, 20, "%m/%d/%Y %H:%M:%S", lt);
     else
        (void)strftime(timestamp, 20, "%m/%d/%Y %H:%M", lt);
     return timestamp;
 }
 
 void
-CheckLogFile(void)
+CheckLogFile(char * log_path)
 {
     char oldSlvgLog[AFSDIR_PATH_MAX];
 
@@ -3783,11 +3512,11 @@ CheckLogFile(void)
     }
 #endif
 
-    strcpy(oldSlvgLog, AFSDIR_SERVER_SLVGLOG_FILEPATH);
+    strcpy(oldSlvgLog, log_path);
     strcat(oldSlvgLog, ".old");
     if (!logFile) {
-       renamefile(AFSDIR_SERVER_SLVGLOG_FILEPATH, oldSlvgLog);
-       logFile = afs_fopen(AFSDIR_SERVER_SLVGLOG_FILEPATH, "a");
+       renamefile(log_path, oldSlvgLog);
+       logFile = afs_fopen(log_path, "a");
 
        if (!logFile) {         /* still nothing, use stdout */
            logFile = stdout;
@@ -3801,7 +3530,7 @@ CheckLogFile(void)
 
 #ifndef AFS_NT40_ENV
 void
-TimeStampLogFile(void)
+TimeStampLogFile(char * log_path)
 {
     char stampSlvgLog[AFSDIR_PATH_MAX];
     struct tm *lt;
@@ -3811,13 +3540,13 @@ TimeStampLogFile(void)
     lt = localtime(&now);
     (void)afs_snprintf(stampSlvgLog, sizeof stampSlvgLog,
                       "%s.%04d-%02d-%02d.%02d:%02d:%02d",
-                      AFSDIR_SERVER_SLVGLOG_FILEPATH, lt->tm_year + 1900,
+                      log_path, lt->tm_year + 1900,
                       lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min,
                       lt->tm_sec);
 
     /* try to link the logfile to a timestamped filename */
     /* if it fails, oh well, nothing we can do */
-    link(AFSDIR_SERVER_SLVGLOG_FILEPATH, stampSlvgLog);
+    link(log_path, stampSlvgLog);
 }
 #endif
 
@@ -3834,8 +3563,10 @@ showlog(void)
     }
 #endif
 
-    rewind(logFile);
-    fclose(logFile);
+    if (logFile) {
+       rewind(logFile);
+       fclose(logFile);
+    }
 
     logFile = afs_fopen(AFSDIR_SERVER_SLVGLOG_FILEPATH, "r");
 
@@ -3864,11 +3595,11 @@ Log(const char *format, ...)
        syslog(LOG_INFO, "%s", tmp);
     } else
 #endif
-    {
-       gettimeofday(&now, 0);
-       fprintf(logFile, "%s %s", TimeStamp(now.tv_sec, 1), tmp);
-       fflush(logFile);
-    }
+       if (logFile) {
+           gettimeofday(&now, 0);
+           fprintf(logFile, "%s %s", TimeStamp(now.tv_sec, 1), tmp);
+           fflush(logFile);
+       }
 }
 
 void
@@ -3885,12 +3616,12 @@ Abort(const char *format, ...)
        syslog(LOG_INFO, "%s", tmp);
     } else
 #endif
-    {
-       fprintf(logFile, "%s", tmp);
-       fflush(logFile);
-       if (ShowLog)
-           showlog();
-    }
+       if (logFile) {
+           fprintf(logFile, "%s", tmp);
+           fflush(logFile);
+           if (ShowLog)
+               showlog();
+       }
 
     if (debug)
        abort();
@@ -3905,16 +3636,18 @@ ToString(char *s)
     assert(p != NULL);
     strcpy(p, s);
     return p;
-
 }
 
 /* Remove the FORCESALVAGE file */
 void
 RemoveTheForce(char *path)
 {
+    char target[1024];
+    struct afs_stat force; /* so we can use afs_stat to find it */
+    strcpy(target,path);
+    strcat(target,"/FORCESALVAGE");
     if (!Testing && ForceSalvage) {
-       if (chdir(path) == 0)
-           unlink("FORCESALVAGE");
+        if (afs_stat(target,&force) == 0)  unlink(target);
     }
 }
 
@@ -3926,10 +3659,11 @@ int
 UseTheForceLuke(char *path)
 {
     struct afs_stat force;
+    char target[1024];
+    strcpy(target,path);
+    strcat(target,"/FORCESALVAGE");
 
-    assert(chdir(path) != -1);
-
-    return (afs_stat("FORCESALVAGE", &force) == 0);
+    return (afs_stat(target, &force) == 0);
 }
 #else
 /*
@@ -3937,7 +3671,7 @@ UseTheForceLuke(char *path)
  *
  * NOTE:
  *     The VRMIX fsck will not muck with the filesystem it is supposedly
- *     fixing and create a "FORCESAVAGE" file (by design).  Instead, we
+ *     fixing and create a "FORCESALVAGE" file (by design).  Instead, we
  *     muck directly with the root inode, which is within the normal
  *     domain of fsck.
  *     ListViceInodes() has a side effect of setting ForceSalvage if