vol: fix vutil format-truncation nit
[openafs.git] / src / vol / vutil.c
index 5f0e0b6..d5b8d8d 100644 (file)
@@ -99,7 +99,7 @@ RemoveInodes(struct afs_inode_info *stuff, Device dev, VolumeId parent,
 }
 
 Volume *
-VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
+VCreateVolume(Error * ec, char *partname, VolumeId volumeId, VolumeId parentId)
 {                              /* Should be the same as volumeId if there is
                                 * no parent */
     Volume *retVal;
@@ -110,12 +110,12 @@ VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
 }
 
 Volume *
-VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
+VCreateVolume_r(Error * ec, char *partname, VolumeId volumeId, VolumeId parentId)
 {                              /* Should be the same as volumeId if there is
                                 * no parent */
     VolumeDiskData vol;
     int i, rc;
-    char headerName[VMAXPATHLEN], volumePath[VMAXPATHLEN];
+    char headerName[VMAXPATHLEN], volumePath[VMAXPATHLEN + 1];
     Device device;
     struct DiskPartition64 *partition;
     struct VolumeDiskHeader diskHeader;
@@ -158,9 +158,13 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
     if (*ec == VNOVOL || !strcmp(partition->name, part)) {
        /* this case is ok */
     } else {
-       /* return EXDEV if it's a clone to an alternate partition
+       /* return EXDEV if it's a clone or read-only to an alternate partition
         * otherwise assume it's a move */
        if (vol.parentId != vol.id) {
+           Log("VCreateVolume: volume %" AFS_VOLID_FMT " for parent %" AFS_VOLID_FMT
+               " found on %s; unable to create volume on %s.\n",
+               afs_printable_VolumeId_lu(vol.id),
+               afs_printable_VolumeId_lu(vol.parentId), part, partition->name);
            *ec = EXDEV;
            return NULL;
        }
@@ -192,7 +196,7 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
     vol.stamp.version = VOLUMEINFOVERSION;
     vol.destroyMe = DESTROY_ME;
     snprintf(headerName, sizeof headerName, VFORMAT,
-            afs_printable_uint32_lu(vol.id));
+            afs_printable_VolumeId_lu(vol.id));
     snprintf(volumePath, sizeof volumePath, "%s" OS_DIRSEP "%s",
             VPartitionPath(partition), headerName);
     rc = stat(volumePath, &st);
@@ -303,8 +307,8 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
     rc = VCreateVolumeDiskHeader(&diskHeader, partition);
     if (rc) {
        Log("VCreateVolume: Error %d trying to write volume header for "
-           "volume %u on partition %s; volume not created\n", rc,
-           vol.id, VPartitionPath(partition));
+           "volume %" AFS_VOLID_FMT " on partition %s; volume not created\n", rc,
+           afs_printable_VolumeId_lu(vol.id), VPartitionPath(partition));
        if (rc == EEXIST) {
            *ec = VVOLEXISTS;
        }
@@ -449,16 +453,16 @@ VReadVolumeDiskHeader(VolumeId volid,
     char path[MAXPATHLEN];
 
     snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
-            VPartitionPath(dp), afs_printable_uint32_lu(volid));
+            VPartitionPath(dp), afs_printable_VolumeId_lu(volid));
     fd = open(path, O_RDONLY);
     if (fd < 0) {
-       Log("VReadVolumeDiskHeader: Couldn't open header for volume %lu (errno %d).\n",
-           afs_printable_uint32_lu(volid), errno);
+       Log("VReadVolumeDiskHeader: Couldn't open header for volume %" AFS_VOLID_FMT " (errno %d).\n",
+           afs_printable_VolumeId_lu(volid), errno);
        code = -1;
 
     } else if (hdr && read(fd, hdr, sizeof(*hdr)) != sizeof(*hdr)) {
-       Log("VReadVolumeDiskHeader: Couldn't read header for volume %lu.\n",
-           afs_printable_uint32_lu(volid));
+       Log("VReadVolumeDiskHeader: Couldn't read header for volume %" AFS_VOLID_FMT ".\n",
+           afs_printable_VolumeId_lu(volid));
        code = EIO;
     }
 
@@ -504,7 +508,7 @@ _VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
     flags |= O_RDWR;
 
     snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
-            VPartitionPath(dp), afs_printable_uint32_lu(hdr->id));
+            VPartitionPath(dp), afs_printable_VolumeId_lu(hdr->id));
     fd = open(path, flags, 0644);
     if (fd < 0) {
        code = errno;
@@ -666,9 +670,6 @@ VCreateVolumeDiskHeader(VolumeDiskHeader_t * hdr,
  * @return operation status
  *    @retval 0 success
  *
- * @note if parent is 0, the parent volume ID will be looked up from the
- * fileserver
- *
  * @note for non-DAFS, parent is currently ignored
  */
 afs_int32
@@ -683,7 +684,7 @@ VDestroyVolumeDiskHeader(struct DiskPartition64 * dp,
 #endif /* AFS_DEMAND_ATTACH_FS */
 
     snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
-             VPartitionPath(dp), afs_printable_uint32_lu(volid));
+             VPartitionPath(dp), afs_printable_VolumeId_lu(volid));
     code = unlink(path);
     if (code) {
        Log("VDestroyVolumeDiskHeader: Couldn't unlink disk header, error = %d\n", errno);
@@ -691,28 +692,14 @@ VDestroyVolumeDiskHeader(struct DiskPartition64 * dp,
     }
 
 #ifdef AFS_DEMAND_ATTACH_FS
+    /* Remove the volume entry from the fileserver's volume group cache, if found. */
     memset(&res, 0, sizeof(res));
-    if (!parent) {
-       FSSYNC_VGQry_response_t q_res;
-
-       code = FSYNC_VGCQuery(dp->name, volid, &q_res, &res);
-       if (code) {
-           Log("VDestroyVolumeDiskHeader: FSYNC_VGCQuery(%s, %lu) failed "
-               "with code %ld, reason %ld\n", dp->name,
-               afs_printable_uint32_lu(volid), afs_printable_int32_ld(code),
-               afs_printable_int32_ld(res.hdr.reason));
-           goto done;
-       }
-
-       parent = q_res.rw;
-
-    }
     code = FSYNC_VGCDel(dp->name, parent, volid, FSYNC_WHATEVER, &res);
     if (code) {
-       Log("VDestroyVolumeDiskHeader: FSYNC_VGCDel(%s, %lu, %lu) failed "
+       Log("VDestroyVolumeDiskHeader: FSYNC_VGCDel(%s, %" AFS_VOLID_FMT ", %" AFS_VOLID_FMT ") failed "
            "with code %ld reason %ld\n", dp->name,
-           afs_printable_uint32_lu(parent),
-           afs_printable_uint32_lu(volid),
+           afs_printable_VolumeId_lu(parent),
+           afs_printable_VolumeId_lu(volid),
            afs_printable_int32_ld(code),
            afs_printable_int32_ld(res.hdr.reason));
     }
@@ -853,7 +840,7 @@ VWalkVolumeHeaders(struct DiskPartition64 *dp, const char *partpath,
        if (p != NULL && strcmp(p, VHDREXT) == 0) {
            char name[VMAXPATHLEN];
 
-           sprintf(name, "%s" OS_DIRSEP "%s", partpath, dentry->d_name);
+           snprintf(name, VMAXPATHLEN, "%s" OS_DIRSEP "%s", partpath, dentry->d_name);
 
            code = _VHandleVolumeHeader(dp, volfunc, name, &diskHeader, -1, rock);
            if (code < 0) {
@@ -1006,6 +993,8 @@ _VLockFd(FD_t fd, afs_uint32 offset, int locktype, int nonblock)
     int cmd = AFS_SETLKW;
     struct afs_st_flock sf;
 
+    opr_Assert(fd >= 0);
+
     if (locktype == READ_LOCK) {
        l_type = F_RDLCK;
     }
@@ -1142,6 +1131,7 @@ VLockFileLock(struct VLockFile *lf, afs_uint32 offset, int locktype, int nonbloc
     opr_mutex_enter(&lf->mutex);
 
     if (lf->fd == INVALID_FD) {
+       opr_Assert(lf->refcount == 0);
        lf->fd = _VOpenPath(lf->path);
        if (lf->fd == INVALID_FD) {
            opr_mutex_exit(&lf->mutex);
@@ -1151,12 +1141,15 @@ VLockFileLock(struct VLockFile *lf, afs_uint32 offset, int locktype, int nonbloc
 
     lf->refcount++;
 
+    opr_Assert(lf->refcount > 0);
+
     opr_mutex_exit(&lf->mutex);
 
     code = _VLockFd(lf->fd, offset, locktype, nonblock);
 
     if (code) {
        opr_mutex_enter(&lf->mutex);
+       opr_Assert(lf->refcount > 0);
        if (--lf->refcount < 1) {
            _VCloseFd(lf->fd);
            lf->fd = INVALID_FD;
@@ -1173,6 +1166,7 @@ VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset)
     opr_mutex_enter(&lf->mutex);
 
     opr_Assert(lf->fd != INVALID_FD);
+    opr_Assert(lf->refcount > 0);
 
     if (--lf->refcount < 1) {
        _VCloseFd(lf->fd);