dafs-accurately-track-inuse-20080317
[openafs.git] / src / vol / vol-salvage.c
index 1a3a9bd..7161272 100644 (file)
@@ -89,6 +89,13 @@ Vnodes with 0 inode pointers in RW volumes are now deleted.
 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>
@@ -99,12 +106,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)
@@ -221,7 +222,7 @@ static char *TimeStamp(time_t clock, int precision);
 
 
 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 */
@@ -234,11 +235,18 @@ 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 */
@@ -261,7 +269,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 */
@@ -284,7 +292,7 @@ struct VolumeSummary *volumeSummaryp;       /* Holds all the volumes in a part */
 int nVolumes;                  /* Number of volumes (read-write and read-only)
                                 * in volume summary */
 
-extern char * tmpdir = 0;
+char *tmpdir = NULL;
 
 
 
@@ -316,15 +324,14 @@ BadError(register int aerror)
     return 0;                  /* otherwise may be transient, e.g. EMFILE */
 }
 
-
 #define MAX_ARGS 128
 #ifdef AFS_NT40_ENV
 char *save_args[MAX_ARGS];
 int n_save_args = 0;
-pthread_t main_thread;
+extern pthread_t main_thread;
+childJob_t myjob = { SALVAGER_MAGIC, NOT_CHILD, "" };
 #endif
 
-
 /* Get the salvage lock if not already held. Hold until process exits. */
 void
 ObtainSalvageLock(void)
@@ -432,7 +439,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];
@@ -473,10 +480,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 */
@@ -664,7 +671,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);
@@ -696,7 +703,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];
@@ -1000,7 +1007,7 @@ CountVolumeInodes(register struct ViceInodeInfo *ip, int maxInodes,
 }
 
 int
-OnlyOneVolume(struct ViceInodeInfo *inodeinfo, VolumeId singleVolumeNumber, void *rock)
+OnlyOneVolume(struct ViceInodeInfo *inodeinfo, int singleVolumeNumber, void *rock)
 {
     if (inodeinfo->u.vnode.vnodeNumber == INODESPECIAL)
        return (inodeinfo->u.special.parentId == singleVolumeNumber);
@@ -1258,9 +1265,23 @@ GetVolumeSummary(VolumeId singleVolumeNumber)
                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
@@ -1329,7 +1350,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);
 
@@ -1451,7 +1472,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)
@@ -1596,7 +1633,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) {
@@ -2168,9 +2205,10 @@ SalvageIndex(Inode ino, VnodeClass class, int RW,
                                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)
+                           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;
@@ -2226,7 +2264,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;
@@ -2277,18 +2315,19 @@ CopyAndSalvage(register struct DirSummary *dir)
     struct VnodeClassInfo *vcp = &VnodeClassInfo[vLarge];
     Inode oldinode, newinode;
     DirHandle newdir;
-    register afs_int32 code;
+    afs_int32 code;
+    afs_sfsize_t lcode;
     afs_int32 parentUnique = 1;
     struct VnodeEssence *vnodeEssence;
 
     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
@@ -2319,8 +2358,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");
@@ -2333,11 +2374,12 @@ CopyAndSalvage(register struct DirSummary *dir)
     vnode.cloned = 0;
     VNDISK_SET_INO(&vnode, newinode);
     VNDISK_SET_LEN(&vnode, Length(&newdir));
-    code =
+    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
@@ -2345,6 +2387,9 @@ CopyAndSalvage(register struct DirSummary *dir)
                                 * an open FD on the file itself to fsync.
                                 */
 #endif
+#else
+    vnodeInfo[vLarge].handle->ih_synced = 1;
+#endif
     code = IH_DEC(dir->ds_linkH, oldinode, dir->rwVid);
     assert(code == 0);
     dir->dirHandle = newdir;
@@ -2577,7 +2622,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;
@@ -2784,7 +2829,7 @@ SalvageVolume(register struct InodeSummary *rwIsp, IHandle_t * alinkH)
     struct VnodeEssence *vep;
     afs_int32 v, pv;
     IHandle_t *h;
-    int nBytes;
+    afs_sfsize_t nBytes;
     ViceFid pa;
     VnodeId LFVnode, ThisVnode;
     Unique LFUnique, ThisUnique;
@@ -2808,6 +2853,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;
@@ -3057,7 +3107,7 @@ void
 ClearROInUseBit(struct VolumeSummary *summary)
 {
     IHandle_t *h = summary->volumeInfoHandle;
-    int nBytes;
+    afs_sfsize_t nBytes;
 
     VolumeDiskData volHeader;
 
@@ -3069,7 +3119,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));
     }
 }
@@ -3258,7 +3308,18 @@ 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;
 }
 
@@ -3268,6 +3329,18 @@ Exit(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);
@@ -3398,11 +3471,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
@@ -3419,12 +3492,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();