vol: Fix VCreateVolume special inode cleanup
[openafs.git] / src / vol / vutil.c
index 860be81..80bbf97 100644 (file)
 
 #include <roken.h>
 
-#include <stdio.h>
-#include <sys/types.h>
-#include <errno.h>
-#ifdef AFS_NT40_ENV
-#include <time.h>
-#include <fcntl.h>
-#else
-#include <sys/time.h>
+#ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
-#include <unistd.h>
 #endif
-#include <dirent.h>
-#include <sys/stat.h>
-#include <afs/afs_assert.h>
+
+#ifdef HAVE_SYS_LOCKF_H
+#include <sys/lockf.h>
+#endif
 
 #include <rx/xdr.h>
 #include <afs/afsint.h>
@@ -45,7 +38,6 @@
 #include <afs/afsutil.h>
 #ifdef AFS_NT40_ENV
 #include "ntops.h"
-#include <io.h>
 #endif
 #include "vnode.h"
 #include "volume.h"
 #include "vol_prototypes.h"
 #include "common.h"
 
-#ifdef AFS_AIX_ENV
-#include <sys/lockf.h>
-#endif
-#if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV) || defined(AFS_LINUX20_ENV)
-#include <string.h>
-#else
-#include <strings.h>
+#ifndef AFS_NT40_ENV
+# ifdef O_LARGEFILE
+#  define AFS_SETLKW   F_SETLKW64
+#  define AFS_SETLK    F_SETLK64
+#  define afs_st_flock flock64
+# else
+#  define AFS_SETLKW   F_SETLKW
+#  define AFS_SETLK    F_SETLK
+#  define afs_st_flock flock
+# endif
 #endif
 
 /* Note:  the volume creation functions herein leave the destroyMe flag in the
@@ -72,7 +67,8 @@
 
 #ifdef FSSYNC_BUILD_CLIENT
 static void
-RemoveInodes(struct afs_inode_info *stuff, Device dev, VolumeId vid)
+RemoveInodes(struct afs_inode_info *stuff, Device dev, VolumeId parent,
+             VolumeId vid)
 {
     int i;
     IHandle_t *handle;
@@ -80,11 +76,16 @@ RemoveInodes(struct afs_inode_info *stuff, Device dev, VolumeId vid)
     /* This relies on the fact that IDEC only needs the device and NT only
      * needs the dev and vid to decrement volume special files.
      */
-    IH_INIT(handle, dev, vid, -1);
+    IH_INIT(handle, dev, parent, -1);
     for (i = 0; i < MAXINODETYPE; i++) {
        Inode inode = *stuff[i].inode;
-       if (VALID_INO(inode))
-           IH_DEC(handle, inode, vid);
+       if (VALID_INO(inode)) {
+           if (stuff[i].inodeType == VI_LINKTABLE) {
+               IH_DEC(handle, inode, parent);
+           } else {
+               IH_DEC(handle, inode, vid);
+           }
+       }
     }
     IH_RELEASE(handle);
 }
@@ -112,7 +113,7 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
     struct VolumeDiskHeader diskHeader;
     IHandle_t *handle;
     FdHandle_t *fdP;
-    Inode nearInode = 0;
+    Inode nearInode AFS_UNUSED = 0;
     char *part, *name;
     struct stat st;
     struct VolumeHeader tempHeader;
@@ -182,9 +183,10 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
     vol.stamp.magic = VOLUMEINFOMAGIC;
     vol.stamp.version = VOLUMEINFOVERSION;
     vol.destroyMe = DESTROY_ME;
-    (void)afs_snprintf(headerName, sizeof headerName, VFORMAT, afs_printable_uint32_lu(vol.id));
-    (void)afs_snprintf(volumePath, sizeof volumePath, "%s" OS_DIRSEP "%s",
-                      VPartitionPath(partition), headerName);
+    snprintf(headerName, sizeof headerName, VFORMAT,
+            afs_printable_uint32_lu(vol.id));
+    snprintf(volumePath, sizeof volumePath, "%s" OS_DIRSEP "%s",
+            VPartitionPath(partition), headerName);
     rc = stat(volumePath, &st);
     if (rc == 0 || errno != ENOENT) {
        if (rc == 0) {
@@ -241,7 +243,7 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
          bad:
            if (handle)
                IH_RELEASE(handle);
-           RemoveInodes(stuff, device, vol.id);
+           RemoveInodes(stuff, device, vol.parentId, vol.id);
            if (!*ec) {
                *ec = VNOVOL;
            }
@@ -410,9 +412,8 @@ VReadVolumeDiskHeader(VolumeId volid,
     int fd;
     char path[MAXPATHLEN];
 
-    (void)afs_snprintf(path, sizeof(path),
-                      "%s" OS_DIRSEP VFORMAT,
-                      VPartitionPath(dp), afs_printable_uint32_lu(volid));
+    snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
+            VPartitionPath(dp), afs_printable_uint32_lu(volid));
     fd = open(path, O_RDONLY);
     if (fd < 0) {
        Log("VReadVolumeDiskHeader: Couldn't open header for volume %lu (errno %d).\n",
@@ -466,9 +467,8 @@ _VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
 
     flags |= O_RDWR;
 
-    (void)afs_snprintf(path, sizeof(path),
-                      "%s" OS_DIRSEP VFORMAT,
-                      VPartitionPath(dp), afs_printable_uint32_lu(hdr->id));
+    snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
+            VPartitionPath(dp), afs_printable_uint32_lu(hdr->id));
     fd = open(path, flags, 0644);
     if (fd < 0) {
        code = errno;
@@ -646,9 +646,8 @@ VDestroyVolumeDiskHeader(struct DiskPartition64 * dp,
     SYNC_response res;
 #endif /* AFS_DEMAND_ATTACH_FS */
 
-    (void)afs_snprintf(path, sizeof(path),
-                       "%s" OS_DIRSEP VFORMAT,
-                       VPartitionPath(dp), afs_printable_uint32_lu(volid));
+    snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
+             VPartitionPath(dp), afs_printable_uint32_lu(volid));
     code = unlink(path);
     if (code) {
        Log("VDestroyVolumeDiskHeader: Couldn't unlink disk header, error = %d\n", errno);
@@ -912,20 +911,20 @@ _VLockFd(FD_t handle, afs_uint32 offset, int locktype, int nonblock)
 }
 
 static_inline void
-_VUnlockFd(struct VLockFile *lf, afs_uint32 offset)
+_VUnlockFd(FD_t handle, afs_uint32 offset)
 {
     OVERLAPPED lap;
 
     memset(&lap, 0, sizeof(lap));
     lap.Offset = offset;
 
-    UnlockFileEx(lf->fd, 0, 1, 0, &lap);
+    UnlockFileEx(handle, 0, 1, 0, &lap);
 }
 
 static_inline void
-_VCloseFd(struct VLockFile *lf)
+_VCloseFd(FD_t handle)
 {
-    CloseHandle(lf->fd);
+    CloseHandle(handle);
 }
 
 #else /* !AFS_NT40_ENV */
@@ -938,7 +937,7 @@ _VCloseFd(struct VLockFile *lf)
  * @return file descriptor
  *  @retval INVALID_FD failure opening file
  */
-static_inline int
+static_inline FD_t
 _VOpenPath(const char *path)
 {
     int fd;
@@ -965,17 +964,17 @@ _VOpenPath(const char *path)
  *  @retval EIO   error acquiring file lock
  */
 static_inline int
-_VLockFd(int fd, afs_uint32 offset, int locktype, int nonblock)
+_VLockFd(FD_t fd, afs_uint32 offset, int locktype, int nonblock)
 {
     int l_type = F_WRLCK;
-    int cmd = F_SETLKW;
-    struct flock sf;
+    int cmd = AFS_SETLKW;
+    struct afs_st_flock sf;
 
     if (locktype == READ_LOCK) {
        l_type = F_RDLCK;
     }
     if (nonblock) {
-       cmd = F_SETLK;
+       cmd = AFS_SETLK;
     }
 
     sf.l_start = offset;
@@ -1015,7 +1014,7 @@ _VLockFd(int fd, afs_uint32 offset, int locktype, int nonblock)
  * @param[in] fd file descriptor to close
  */
 static_inline void
-_VCloseFd(int fd)
+_VCloseFd(FD_t fd)
 {
     if (close(fd)) {
        Log("_VCloseFd: error %d closing fd %d\n",
@@ -1030,16 +1029,16 @@ _VCloseFd(int fd)
  * @param[in] offset offset to unlock
  */
 static_inline void
-_VUnlockFd(int fd, afs_uint32 offset)
+_VUnlockFd(FD_t fd, afs_uint32 offset)
 {
-    struct flock sf;
+    struct afs_st_flock sf;
 
     sf.l_start = offset;
     sf.l_len = 1;
     sf.l_type = F_UNLCK;
     sf.l_whence = SEEK_SET;
 
-    if (fcntl(fd, F_SETLK, &sf)) {
+    if (fcntl(fd, AFS_SETLK, &sf)) {
        Log("_VUnlockFd: fcntl failed with error %d when trying to unlock "
            "fd %d\n", errno, fd);
     }