#include <sys/file.h>
#include <unistd.h>
#endif
+#include <dirent.h>
#include <sys/stat.h>
#ifdef AFS_PTHREAD_ENV
#include <assert.h>
volume header ON: this means that the volumes will not be attached by the
file server and WILL BE DESTROYED the next time a system salvage is performed */
+#ifdef FSSYNC_BUILD_CLIENT
static void
RemoveInodes(Device dev, VolumeId vid)
{
return (VAttachVolumeByName_r(ec, partname, headerName, V_SECRETLY));
}
+#endif /* FSSYNC_BUILD_CLIENT */
void
return code;
}
+#ifdef FSSYNC_BUILD_CLIENT
/**
* write an existing volume disk header.
*
int fd;
char path[MAXPATHLEN];
+#ifdef AFS_DEMAND_ATTACH_FS
+ /* prevent racing with VGC scanners reading the vol header while we are
+ * writing it */
+ code = VPartHeaderLock(dp, READ_LOCK);
+ if (code) {
+ return EIO;
+ }
+#endif /* AFS_DEMAND_ATTACH_FS */
+
flags |= O_RDWR;
(void)afs_snprintf(path, sizeof(path),
}
}
+#ifdef AFS_DEMAND_ATTACH_FS
+ VPartHeaderUnlock(dp, READ_LOCK);
+#endif /* AFS_DEMAND_ATTACH_FS */
+
return code;
}
{
afs_int32 code;
+#ifdef AFS_DEMAND_ATTACH_FS
+ VolumeDiskHeader_t oldhdr;
+ int delvgc = 0, addvgc = 0;
+ SYNC_response res;
+
+ /* first, see if anything with the volume IDs have changed; if so, we
+ * need to update the VGC */
+
+ code = VReadVolumeDiskHeader(hdr->id, dp, &oldhdr);
+ if (code == 0 && (oldhdr.id != hdr->id || oldhdr.parent != hdr->parent)) {
+ /* the vol id or parent vol id changed; need to delete the VGC entry
+ * for the old vol id/parent, and add the new one */
+ delvgc = 1;
+ addvgc = 1;
+
+ } else if (code) {
+ /* couldn't get the old header info; add the new header info to the
+ * VGC in case it hasn't been added yet */
+ addvgc = 1;
+ }
+
+#endif /* AFS_DEMAND_ATTACH_FS */
+
code = _VWriteVolumeDiskHeader(hdr, dp, 0);
if (code) {
goto done;
}
+#ifdef AFS_DEMAND_ATTACH_FS
+ if (delvgc) {
+ memset(&res, 0, sizeof(res));
+ code = FSYNC_VGCDel(dp->name, oldhdr.parent, oldhdr.id, FSYNC_WHATEVER, &res);
+
+ /* unknown vol id is okay; it just further suggests the old header
+ * data was bogus, which is fine since we're trying to fix it */
+ if (code && res.hdr.reason != FSYNC_UNKNOWN_VOLID) {
+ Log("VWriteVolumeDiskHeader: FSYNC_VGCDel(%s, %lu, %lu) "
+ "failed with code %ld reason %ld\n", dp->name,
+ afs_printable_uint32_lu(oldhdr.parent),
+ afs_printable_uint32_lu(oldhdr.id),
+ afs_printable_int32_ld(code),
+ afs_printable_int32_ld(res.hdr.reason));
+ }
+
+ }
+ if (addvgc) {
+ memset(&res, 0, sizeof(res));
+ code = FSYNC_VGCAdd(dp->name, hdr->parent, hdr->id, FSYNC_WHATEVER, &res);
+ if (code) {
+ Log("VWriteVolumeDiskHeader: FSYNC_VGCAdd(%s, %lu, %lu) "
+ "failed with code %ld reason %ld\n", dp->name,
+ afs_printable_uint32_lu(hdr->parent),
+ afs_printable_uint32_lu(hdr->id),
+ afs_printable_int32_ld(code),
+ afs_printable_int32_ld(res.hdr.reason));
+ }
+ }
+
+#endif /* AFS_DEMAND_ATTACH_FS */
+
done:
return code;
}
struct DiskPartition64 * dp)
{
afs_int32 code = 0;
+#ifdef AFS_DEMAND_ATTACH_FS
+ SYNC_response res;
+#endif /* AFS_DEMAND_ATTACH_FS */
code = _VWriteVolumeDiskHeader(hdr, dp, O_CREAT | O_EXCL);
if (code) {
goto done;
}
+#ifdef AFS_DEMAND_ATTACH_FS
+ memset(&res, 0, sizeof(res));
+ code = FSYNC_VGCAdd(dp->name, hdr->parent, hdr->id, FSYNC_WHATEVER, &res);
+ if (code) {
+ Log("VCreateVolumeDiskHeader: FSYNC_VGCAdd(%s, %lu, %lu) failed "
+ "with code %ld reason %ld\n", dp->name,
+ afs_printable_uint32_lu(hdr->parent),
+ afs_printable_uint32_lu(hdr->id),
+ afs_printable_int32_ld(code),
+ afs_printable_int32_ld(res.hdr.reason));
+ }
+#endif /* AFS_DEMAND_ATTACH_FS */
+
done:
return code;
}
{
afs_int32 code = 0;
char path[MAXPATHLEN];
+#ifdef AFS_DEMAND_ATTACH_FS
+ SYNC_response res;
+#endif /* AFS_DEMAND_ATTACH_FS */
(void)afs_snprintf(path, sizeof(path),
"%s/" VFORMAT,
goto done;
}
+#ifdef AFS_DEMAND_ATTACH_FS
+ 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 "
+ "with code %ld reason %ld\n", dp->name,
+ afs_printable_uint32_lu(parent),
+ afs_printable_uint32_lu(volid),
+ afs_printable_int32_ld(code),
+ afs_printable_int32_ld(res.hdr.reason));
+ }
+#endif /* AFS_DEMAND_ATTACH_FS */
+
done:
return code;
}
+#endif /* FSSYNC_BUILD_CLIENT */
+
+/**
+ * handle a single vol header as part of VWalkVolumeHeaders.
+ *
+ * @param[in] dp disk partition
+ * @param[in] volfunc function to call when a vol header is successfully read
+ * @param[in] name full path name to the .vol header
+ * @param[out] hdr header data read in from the .vol header
+ * @param[in] locked 1 if the partition headers are locked, 0 otherwise
+ * @param[in] rock the rock to pass to volfunc
+ *
+ * @return operation status
+ * @retval 0 success
+ * @retval -1 fatal error, stop scanning
+ * @retval 1 failed to read header
+ * @retval 2 volfunc callback indicated error after header read
+ */
+static int
+_VHandleVolumeHeader(struct DiskPartition64 *dp, VWalkVolFunc volfunc,
+ const char *name, struct VolumeDiskHeader *hdr,
+ int locked, void *rock)
+{
+ int error = 0;
+ int fd;
+
+ if ((fd = afs_open(name, O_RDONLY)) == -1
+ || read(fd, hdr, sizeof(*hdr))
+ != sizeof(*hdr)
+ || hdr->stamp.magic != VOLUMEHEADERMAGIC) {
+ error = 1;
+ }
+
+ if (fd >= 0) {
+ close(fd);
+ }
+
+#ifdef AFSFS_DEMAND_ATTACH_FS
+ if (locked) {
+ VPartHeaderUnlock(dp);
+ }
+#endif /* AFS_DEMAND_ATTACH_FS */
+
+ if (!error && volfunc) {
+ /* the volume header seems fine; call the caller-supplied
+ * 'we-found-a-volume-header' function */
+ int last = 1;
+
+#ifdef AFS_DEMAND_ATTACH_FS
+ if (!locked) {
+ last = 0;
+ }
+#endif /* AFS_DEMAND_ATTACH_FS */
+
+ error = (*volfunc) (dp, name, hdr, last, rock);
+ if (error < 0) {
+ return -1;
+ }
+ if (error) {
+ error = 2;
+ }
+ }
+
+#ifdef AFS_DEMAND_ATTACH_FS
+ if (error && !locked) {
+ int code;
+ /* retry reading the volume header under the partition
+ * header lock, just to be safe and ensure we're not
+ * racing something rewriting the vol header */
+ code = VPartHeaderLock(dp, WRITE_LOCK);
+ if (code) {
+ Log("Error acquiring partition write lock when "
+ "looking at header %s\n", name);
+ return -1;
+ }
+
+ return _VHandleVolumeHeader(dp, volfunc, name, hdr, 1, rock);
+ }
+#endif /* AFS_DEMAND_ATTACH_FS */
+
+ return error;
+}
+
+/**
+ * walk through the list of volume headers on a partition.
+ *
+ * This function looks through all of the .vol headers on a partition, reads in
+ * each header, and calls the supplied volfunc function on each one. If the
+ * header cannot be read (or volfunc returns a positive error code), DAFS will
+ * VPartHeaderExLock() and retry. If that fails, or if we are non-DAFS, errfunc
+ * will be called (which typically will unlink the problem volume header).
+ *
+ * If volfunc returns a negative error code, walking the partition will stop
+ * and we will return an error immediately.
+ *
+ * @param[in] dp partition to walk
+ * @param[in] partpath the path opendir()
+ * @param[in] volfunc the function to call when a header is encountered, or
+ * NULL to just skip over valid headers
+ * @param[in] errfunc the function to call when a problematic header is
+ * encountered, or NULL to just skip over bad headers
+ * @param[in] rock rock for volfunc and errfunc
+ *
+ * @see VWalkVolFunc
+ * @see VWalkErrFunc
+ *
+ * @return operation status
+ * @retval 0 success
+ * @retval negative fatal error, walk did not finish
+ */
+int
+VWalkVolumeHeaders(struct DiskPartition64 *dp, const char *partpath,
+ VWalkVolFunc volfunc, VWalkErrFunc errfunc, void *rock)
+{
+ DIR *dirp = NULL;
+ struct dirent *dentry = NULL;
+ int code = 0;
+ struct VolumeDiskHeader diskHeader;
+
+ dirp = opendir(partpath);
+ if (!dirp) {
+ Log("VWalkVolumeHeaders: cannot open directory %s\n", partpath);
+ code = -1;
+ goto done;
+ }
+
+ while ((dentry = readdir(dirp))) {
+ char *p = dentry->d_name;
+ p = strrchr(dentry->d_name, '.');
+ if (p != NULL && strcmp(p, VHDREXT) == 0) {
+ char name[VMAXPATHLEN];
+
+ sprintf(name, "%s/%s", partpath, dentry->d_name);
+
+ code = _VHandleVolumeHeader(dp, volfunc, name, &diskHeader, -1, rock);
+ if (code < 0) {
+ /* fatal error, stop walking */
+ goto done;
+ }
+ if (code && errfunc) {
+ /* error with header; call the caller-supplied vol error
+ * function */
+
+ struct VolumeDiskHeader *hdr = &diskHeader;
+ if (code == 1) {
+ /* we failed to read the header at all, so don't pass in
+ * the header ptr */
+ hdr = NULL;
+ }
+ (*errfunc) (dp, name, hdr, rock);
+ }
+ code = 0;
+ }
+ }
+ done:
+ if (dirp) {
+ closedir(dirp);
+ dirp = NULL;
+ }
+
+ return code;
+}
#ifdef AFS_DEMAND_ATTACH_FS
VLockFileInit(struct VLockFile *lf, const char *path)
{
memset(lf, 0, sizeof(*lf));
- assert(pthread_mutex_init(&lf->mutex, NULL) == 0);
lf->path = strdup(path);
+ lf->fd = INVALID_FD;
+ assert(pthread_mutex_init(&lf->mutex, NULL) == 0);
}
# ifdef AFS_NT40_ENV
if (!LockFileEx(handle, flags, 0, 1, 0, &lap)) {
if (GetLastError() == ERROR_LOCK_VIOLATION) {
- CloseHandle(handle);
return EBUSY;
}
- CloseHandle(handle);
return EIO;
}
if (fcntl(fd, cmd, &sf)) {
if (nonblock && (errno == EACCES || errno == EAGAIN)) {
/* We asked for a nonblocking lock, and it was already locked */
- close(fd);
return EBUSY;
}
Log("_VLockFd: fcntl failed with error %d when trying to lock "
"fd %d (locktype=%d)\n", errno, fd, locktype);
- close(fd);
return EIO;
}
_VUnlockFd(lf->fd, offset);
}
- assert(pthread_mutex_lock(&lf->mutex) == 0);
+ assert(pthread_mutex_unlock(&lf->mutex) == 0);
}
/**