2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
13 Institution: The Information Technology Center, Carnegie-Mellon University
17 #include <afsconfig.h>
18 #include <afs/param.h>
22 #include <sys/types.h>
34 #ifdef AFS_PTHREAD_ENV
36 #else /* AFS_PTHREAD_ENV */
37 #include <afs/assert.h>
38 #endif /* AFS_PTHREAD_ENV */
41 #include <afs/afsint.h>
43 #include <afs/errors.h>
46 #include <afs/afssyscalls.h>
48 #include <afs/afsutil.h>
55 #include "volume_inline.h"
56 #include "partition.h"
57 #include "viceinode.h"
59 #include "volinodes.h"
60 #include "vol_prototypes.h"
64 #include <sys/lockf.h>
66 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV) || defined(AFS_LINUX20_ENV)
73 #define afs_open open64
74 #else /* !O_LARGEFILE */
76 #endif /* !O_LARGEFILE */
79 /* Note: the volume creation functions herein leave the destroyMe flag in the
80 volume header ON: this means that the volumes will not be attached by the
81 file server and WILL BE DESTROYED the next time a system salvage is performed */
83 #ifdef FSSYNC_BUILD_CLIENT
85 RemoveInodes(struct afs_inode_info *stuff, Device dev, VolumeId vid)
90 /* This relies on the fact that IDEC only needs the device and NT only
91 * needs the dev and vid to decrement volume special files.
93 IH_INIT(handle, dev, vid, -1);
94 for (i = 0; i < MAXINODETYPE; i++) {
95 Inode inode = *stuff[i].inode;
97 IH_DEC(handle, inode, vid);
103 VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
104 { /* Should be the same as volumeId if there is
108 retVal = VCreateVolume_r(ec, partname, volumeId, parentId);
114 VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
115 { /* Should be the same as volumeId if there is
119 char headerName[VMAXPATHLEN], volumePath[VMAXPATHLEN];
121 struct DiskPartition64 *partition;
122 struct VolumeDiskHeader diskHeader;
128 struct VolumeHeader tempHeader;
129 struct afs_inode_info stuff[MAXINODETYPE];
130 # ifdef AFS_DEMAND_ATTACH_FS
132 # endif /* AFS_DEMAND_ATTACH_FS */
134 init_inode_info(&tempHeader, stuff);
137 memset(&vol, 0, sizeof(vol));
139 vol.parentId = parentId;
140 vol.copyDate = time(0); /* The only date which really means when this
141 * @i(instance) of this volume was created.
142 * Creation date does not mean this */
144 /* Initialize handle for error case below. */
147 /* Verify that the parition is valid before writing to it. */
148 if (!(partition = VGetPartition_r(partname, 0))) {
149 Log("VCreateVolume: partition %s is not in service.\n", partname);
153 #if defined(NEARINODE_HINT)
154 nearInodeHash(volumeId, nearInode);
155 nearInode %= partition->f_files;
157 VGetVolumePath(ec, vol.id, &part, &name);
158 if (*ec == VNOVOL || !strcmp(partition->name, part)) {
159 /* this case is ok */
161 /* return EXDEV if it's a clone to an alternate partition
162 * otherwise assume it's a move */
163 if (vol.parentId != vol.id) {
170 # ifdef AFS_DEMAND_ATTACH_FS
171 /* volume doesn't exist yet, but we must lock it to try to prevent something
172 * else from reading it when we're e.g. half way through creating it (or
173 * something tries to create the same volume at the same time) */
174 locktype = VVolLockType(V_VOLUPD, 1);
175 rc = VLockVolumeByIdNB(volumeId, partition, locktype);
177 Log("VCreateVolume: vol %lu already locked by someone else\n",
178 afs_printable_uint32_lu(volumeId));
182 # else /* AFS_DEMAND_ATTACH_FS */
183 VLockPartition_r(partname);
184 # endif /* !AFS_DEMAND_ATTACH_FS */
186 memset(&tempHeader, 0, sizeof(tempHeader));
187 tempHeader.stamp.magic = VOLUMEHEADERMAGIC;
188 tempHeader.stamp.version = VOLUMEHEADERVERSION;
189 tempHeader.id = vol.id;
190 tempHeader.parent = vol.parentId;
191 vol.stamp.magic = VOLUMEINFOMAGIC;
192 vol.stamp.version = VOLUMEINFOVERSION;
193 vol.destroyMe = DESTROY_ME;
194 (void)afs_snprintf(headerName, sizeof headerName, VFORMAT, afs_printable_uint32_lu(vol.id));
195 (void)afs_snprintf(volumePath, sizeof volumePath, "%s/%s",
196 VPartitionPath(partition), headerName);
197 rc = stat(volumePath, &st);
198 if (rc == 0 || errno != ENOENT) {
200 Log("VCreateVolume: Header file %s already exists!\n",
204 Log("VCreateVolume: Error %d trying to stat header file %s\n",
210 device = partition->device;
212 for (i = 0; i < MAXINODETYPE; i++) {
213 struct afs_inode_info *p = &stuff[i];
218 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
219 (p->inodeType == VI_LINKTABLE) ? vol.parentId : vol.id,
220 INODESPECIAL, p->inodeType, vol.parentId);
221 if (!(VALID_INO(*(p->inode)))) {
222 if (errno == EEXIST && (p->inodeType == VI_LINKTABLE)) {
223 /* Increment the reference count instead. */
228 *(p->inode) = nt_MakeSpecIno(VI_LINKTABLE);
230 *(p->inode) = namei_MakeSpecIno(vol.parentId, VI_LINKTABLE);
232 IH_INIT(lh, device, parentId, *(p->inode));
238 code = IH_INC(lh, *(p->inode), parentId);
239 FDH_REALLYCLOSE(fdP);
248 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
249 vol.id, INODESPECIAL, p->inodeType, vol.parentId);
252 if (!VALID_INO(*(p->inode))) {
253 Log("VCreateVolume: Problem creating %s file associated with volume header %s\n", p->description, volumePath);
257 RemoveInodes(stuff, device, vol.id);
261 VDestroyVolumeDiskHeader(partition, volumeId, parentId);
263 # ifdef AFS_DEMAND_ATTACH_FS
265 VUnlockVolumeById(volumeId, partition);
267 # endif /* AFS_DEMAND_ATTACH_FS */
270 IH_INIT(handle, device, vol.parentId, *(p->inode));
271 fdP = IH_OPEN(handle);
273 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
274 PrintInode(NULL, *(p->inode)), errno);
277 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
278 Log("VCreateVolume: Problem lseek inode %s (err=%d)\n",
279 PrintInode(NULL, *(p->inode)), errno);
280 FDH_REALLYCLOSE(fdP);
283 if (FDH_WRITE(fdP, (char *)&p->stamp, sizeof(p->stamp)) !=
285 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
286 PrintInode(NULL, *(p->inode)), errno);
287 FDH_REALLYCLOSE(fdP);
290 FDH_REALLYCLOSE(fdP);
292 nearInode = *(p->inode);
295 IH_INIT(handle, device, vol.parentId, tempHeader.volumeInfo);
296 fdP = IH_OPEN(handle);
298 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
299 PrintInode(NULL, tempHeader.volumeInfo), errno);
302 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
303 Log("VCreateVolume: Problem lseek inode %s (err=%d)\n",
304 PrintInode(NULL, tempHeader.volumeInfo), errno);
305 FDH_REALLYCLOSE(fdP);
308 if (FDH_WRITE(fdP, (char *)&vol, sizeof(vol)) != sizeof(vol)) {
309 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
310 PrintInode(NULL, tempHeader.volumeInfo), errno);
311 FDH_REALLYCLOSE(fdP);
317 VolumeHeaderToDisk(&diskHeader, &tempHeader);
318 rc = VCreateVolumeDiskHeader(&diskHeader, partition);
320 Log("VCreateVolume: Error %d trying to write volume header for "
321 "volume %u on partition %s; volume not created\n", rc,
322 vol.id, VPartitionPath(partition));
329 # ifdef AFS_DEMAND_ATTACH_FS
331 VUnlockVolumeById(volumeId, partition);
333 # endif /* AFS_DEMAND_ATTACH_FS */
334 return (VAttachVolumeByName_r(ec, partname, headerName, V_SECRETLY));
336 #endif /* FSSYNC_BUILD_CLIENT */
340 AssignVolumeName(VolumeDiskData * vol, char *name, char *ext)
343 AssignVolumeName_r(vol, name, ext);
348 AssignVolumeName_r(VolumeDiskData * vol, char *name, char *ext)
351 strncpy(vol->name, name, VNAMESIZE - 1);
352 vol->name[VNAMESIZE - 1] = '\0';
353 dot = strrchr(vol->name, '.');
354 if (dot && (strcmp(dot, ".backup") == 0 || strcmp(dot, ".readonly") == 0))
357 strncat(vol->name, ext, VNAMESIZE - 1 - strlen(vol->name));
361 CopyVolumeHeader_r(VolumeDiskData * from, VolumeDiskData * to)
363 /* The id and parentId fields are not copied; these are inviolate--the to volume
364 * is assumed to have already been created. The id's cannot be changed once
365 * creation has taken place, since they are embedded in the various inodes associated
366 * with the volume. The copydate is also inviolate--it always reflects the time
367 * this volume was created (compare with the creation date--the creation date of
368 * a backup volume is the creation date of the original parent, because the backup
369 * is used to backup the parent volume). */
373 parent = to->parentId;
374 copydate = to->copyDate;
375 memcpy(to, from, sizeof(*from));
377 to->parentId = parent;
378 to->copyDate = copydate;
379 to->destroyMe = DESTROY_ME; /* Caller must always clear this!!! */
380 to->stamp.magic = VOLUMEINFOMAGIC;
381 to->stamp.version = VOLUMEINFOVERSION;
386 CopyVolumeHeader(VolumeDiskData * from, VolumeDiskData * to)
391 code = CopyVolumeHeader_r(from, to);
397 ClearVolumeStats(VolumeDiskData * vol)
400 ClearVolumeStats_r(vol);
405 ClearVolumeStats_r(VolumeDiskData * vol)
407 memset(vol->weekUse, 0, sizeof(vol->weekUse));
413 * read an existing volume disk header.
415 * @param[in] volid volume id
416 * @param[in] dp disk partition object
417 * @param[out] hdr volume disk header or NULL
419 * @note if hdr is NULL, this is essentially an existence test for the vol
422 * @return operation status
424 * @retval -1 volume header doesn't exist
425 * @retval EIO failed to read volume header
430 VReadVolumeDiskHeader(VolumeId volid,
431 struct DiskPartition64 * dp,
432 VolumeDiskHeader_t * hdr)
436 char path[MAXPATHLEN];
438 (void)afs_snprintf(path, sizeof(path),
440 VPartitionPath(dp), afs_printable_uint32_lu(volid));
441 fd = open(path, O_RDONLY);
443 Log("VReadVolumeDiskHeader: Couldn't open header for volume %lu (errno %d).\n",
444 afs_printable_uint32_lu(volid), errno);
447 } else if (hdr && read(fd, hdr, sizeof(*hdr)) != sizeof(*hdr)) {
448 Log("VReadVolumeDiskHeader: Couldn't read header for volume %lu.\n",
449 afs_printable_uint32_lu(volid));
459 #ifdef FSSYNC_BUILD_CLIENT
461 * write an existing volume disk header.
463 * @param[in] hdr volume disk header
464 * @param[in] dp disk partition object
465 * @param[in] cr assert if O_CREAT | O_EXCL should be passed to open()
467 * @return operation status
469 * @retval -1 volume header doesn't exist
470 * @retval EIO failed to write volume header
475 _VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
476 struct DiskPartition64 * dp,
481 char path[MAXPATHLEN];
483 #ifdef AFS_DEMAND_ATTACH_FS
484 /* prevent racing with VGC scanners reading the vol header while we are
486 code = VPartHeaderLock(dp, READ_LOCK);
490 #endif /* AFS_DEMAND_ATTACH_FS */
494 (void)afs_snprintf(path, sizeof(path),
496 VPartitionPath(dp), afs_printable_uint32_lu(hdr->id));
497 fd = open(path, flags, 0644);
500 Log("_VWriteVolumeDiskHeader: Couldn't open header for volume %lu, "
501 "error = %d\n", afs_printable_uint32_lu(hdr->id), errno);
502 } else if (write(fd, hdr, sizeof(*hdr)) != sizeof(*hdr)) {
503 Log("_VWriteVolumeDiskHeader: Couldn't write header for volume %lu, "
504 "error = %d\n", afs_printable_uint32_lu(hdr->id), errno);
509 if (close(fd) != 0) {
510 Log("_VWriteVolumeDiskHeader: Error closing header for volume "
511 "%lu, errno %d\n", afs_printable_uint32_lu(hdr->id), errno);
515 #ifdef AFS_DEMAND_ATTACH_FS
516 VPartHeaderUnlock(dp, READ_LOCK);
517 #endif /* AFS_DEMAND_ATTACH_FS */
523 * write an existing volume disk header.
525 * @param[in] hdr volume disk header
526 * @param[in] dp disk partition object
528 * @return operation status
530 * @retval ENOENT volume header doesn't exist
531 * @retval EIO failed to write volume header
534 VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
535 struct DiskPartition64 * dp)
539 #ifdef AFS_DEMAND_ATTACH_FS
540 VolumeDiskHeader_t oldhdr;
541 int delvgc = 0, addvgc = 0;
544 /* first, see if anything with the volume IDs have changed; if so, we
545 * need to update the VGC */
547 code = VReadVolumeDiskHeader(hdr->id, dp, &oldhdr);
548 if (code == 0 && (oldhdr.id != hdr->id || oldhdr.parent != hdr->parent)) {
549 /* the vol id or parent vol id changed; need to delete the VGC entry
550 * for the old vol id/parent, and add the new one */
555 /* couldn't get the old header info; add the new header info to the
556 * VGC in case it hasn't been added yet */
560 #endif /* AFS_DEMAND_ATTACH_FS */
562 code = _VWriteVolumeDiskHeader(hdr, dp, 0);
567 #ifdef AFS_DEMAND_ATTACH_FS
569 memset(&res, 0, sizeof(res));
570 code = FSYNC_VGCDel(dp->name, oldhdr.parent, oldhdr.id, FSYNC_WHATEVER, &res);
572 /* unknown vol id is okay; it just further suggests the old header
573 * data was bogus, which is fine since we're trying to fix it */
574 if (code && res.hdr.reason != FSYNC_UNKNOWN_VOLID) {
575 Log("VWriteVolumeDiskHeader: FSYNC_VGCDel(%s, %lu, %lu) "
576 "failed with code %ld reason %ld\n", dp->name,
577 afs_printable_uint32_lu(oldhdr.parent),
578 afs_printable_uint32_lu(oldhdr.id),
579 afs_printable_int32_ld(code),
580 afs_printable_int32_ld(res.hdr.reason));
585 memset(&res, 0, sizeof(res));
586 code = FSYNC_VGCAdd(dp->name, hdr->parent, hdr->id, FSYNC_WHATEVER, &res);
588 Log("VWriteVolumeDiskHeader: FSYNC_VGCAdd(%s, %lu, %lu) "
589 "failed with code %ld reason %ld\n", dp->name,
590 afs_printable_uint32_lu(hdr->parent),
591 afs_printable_uint32_lu(hdr->id),
592 afs_printable_int32_ld(code),
593 afs_printable_int32_ld(res.hdr.reason));
597 #endif /* AFS_DEMAND_ATTACH_FS */
604 * create and write a volume disk header to disk.
606 * @param[in] hdr volume disk header
607 * @param[in] dp disk partition object
609 * @return operation status
611 * @retval EEXIST volume header already exists
612 * @retval EIO failed to write volume header
617 VCreateVolumeDiskHeader(VolumeDiskHeader_t * hdr,
618 struct DiskPartition64 * dp)
621 #ifdef AFS_DEMAND_ATTACH_FS
623 #endif /* AFS_DEMAND_ATTACH_FS */
625 code = _VWriteVolumeDiskHeader(hdr, dp, O_CREAT | O_EXCL);
630 #ifdef AFS_DEMAND_ATTACH_FS
631 memset(&res, 0, sizeof(res));
632 code = FSYNC_VGCAdd(dp->name, hdr->parent, hdr->id, FSYNC_WHATEVER, &res);
634 Log("VCreateVolumeDiskHeader: FSYNC_VGCAdd(%s, %lu, %lu) failed "
635 "with code %ld reason %ld\n", dp->name,
636 afs_printable_uint32_lu(hdr->parent),
637 afs_printable_uint32_lu(hdr->id),
638 afs_printable_int32_ld(code),
639 afs_printable_int32_ld(res.hdr.reason));
641 #endif /* AFS_DEMAND_ATTACH_FS */
649 * destroy a volume disk header.
651 * @param[in] dp disk partition object
652 * @param[in] volid volume id
653 * @param[in] parent parent's volume id, 0 if unknown
655 * @return operation status
658 * @note if parent is 0, the parent volume ID will be looked up from the
661 * @note for non-DAFS, parent is currently ignored
664 VDestroyVolumeDiskHeader(struct DiskPartition64 * dp,
669 char path[MAXPATHLEN];
670 #ifdef AFS_DEMAND_ATTACH_FS
672 #endif /* AFS_DEMAND_ATTACH_FS */
674 (void)afs_snprintf(path, sizeof(path),
676 VPartitionPath(dp), afs_printable_uint32_lu(volid));
679 Log("VDestroyVolumeDiskHeader: Couldn't unlink disk header, error = %d\n", errno);
683 #ifdef AFS_DEMAND_ATTACH_FS
684 memset(&res, 0, sizeof(res));
686 FSSYNC_VGQry_response_t q_res;
688 code = FSYNC_VGCQuery(dp->name, volid, &q_res, &res);
690 Log("VDestroyVolumeDiskHeader: FSYNC_VGCQuery(%s, %lu) failed "
691 "with code %ld, reason %ld\n", dp->name,
692 afs_printable_uint32_lu(volid), afs_printable_int32_ld(code),
693 afs_printable_int32_ld(res.hdr.reason));
700 code = FSYNC_VGCDel(dp->name, parent, volid, FSYNC_WHATEVER, &res);
702 Log("VDestroyVolumeDiskHeader: FSYNC_VGCDel(%s, %lu, %lu) failed "
703 "with code %ld reason %ld\n", dp->name,
704 afs_printable_uint32_lu(parent),
705 afs_printable_uint32_lu(volid),
706 afs_printable_int32_ld(code),
707 afs_printable_int32_ld(res.hdr.reason));
709 #endif /* AFS_DEMAND_ATTACH_FS */
714 #endif /* FSSYNC_BUILD_CLIENT */
717 * handle a single vol header as part of VWalkVolumeHeaders.
719 * @param[in] dp disk partition
720 * @param[in] volfunc function to call when a vol header is successfully read
721 * @param[in] name full path name to the .vol header
722 * @param[out] hdr header data read in from the .vol header
723 * @param[in] locked 1 if the partition headers are locked, 0 otherwise
724 * @param[in] rock the rock to pass to volfunc
726 * @return operation status
728 * @retval -1 fatal error, stop scanning
729 * @retval 1 failed to read header
730 * @retval 2 volfunc callback indicated error after header read
733 _VHandleVolumeHeader(struct DiskPartition64 *dp, VWalkVolFunc volfunc,
734 const char *name, struct VolumeDiskHeader *hdr,
735 int locked, void *rock)
740 if ((fd = afs_open(name, O_RDONLY)) == -1
741 || read(fd, hdr, sizeof(*hdr))
743 || hdr->stamp.magic != VOLUMEHEADERMAGIC) {
751 #ifdef AFSFS_DEMAND_ATTACH_FS
753 VPartHeaderUnlock(dp);
755 #endif /* AFS_DEMAND_ATTACH_FS */
757 if (!error && volfunc) {
758 /* the volume header seems fine; call the caller-supplied
759 * 'we-found-a-volume-header' function */
762 #ifdef AFS_DEMAND_ATTACH_FS
766 #endif /* AFS_DEMAND_ATTACH_FS */
768 error = (*volfunc) (dp, name, hdr, last, rock);
777 #ifdef AFS_DEMAND_ATTACH_FS
778 if (error && !locked) {
780 /* retry reading the volume header under the partition
781 * header lock, just to be safe and ensure we're not
782 * racing something rewriting the vol header */
783 code = VPartHeaderLock(dp, WRITE_LOCK);
785 Log("Error acquiring partition write lock when "
786 "looking at header %s\n", name);
790 return _VHandleVolumeHeader(dp, volfunc, name, hdr, 1, rock);
792 #endif /* AFS_DEMAND_ATTACH_FS */
798 * walk through the list of volume headers on a partition.
800 * This function looks through all of the .vol headers on a partition, reads in
801 * each header, and calls the supplied volfunc function on each one. If the
802 * header cannot be read (or volfunc returns a positive error code), DAFS will
803 * VPartHeaderExLock() and retry. If that fails, or if we are non-DAFS, errfunc
804 * will be called (which typically will unlink the problem volume header).
806 * If volfunc returns a negative error code, walking the partition will stop
807 * and we will return an error immediately.
809 * @param[in] dp partition to walk
810 * @param[in] partpath the path opendir()
811 * @param[in] volfunc the function to call when a header is encountered, or
812 * NULL to just skip over valid headers
813 * @param[in] errfunc the function to call when a problematic header is
814 * encountered, or NULL to just skip over bad headers
815 * @param[in] rock rock for volfunc and errfunc
820 * @return operation status
822 * @retval negative fatal error, walk did not finish
825 VWalkVolumeHeaders(struct DiskPartition64 *dp, const char *partpath,
826 VWalkVolFunc volfunc, VWalkErrFunc errfunc, void *rock)
829 struct dirent *dentry = NULL;
831 struct VolumeDiskHeader diskHeader;
833 dirp = opendir(partpath);
835 Log("VWalkVolumeHeaders: cannot open directory %s\n", partpath);
840 while ((dentry = readdir(dirp))) {
841 char *p = dentry->d_name;
842 p = strrchr(dentry->d_name, '.');
843 if (p != NULL && strcmp(p, VHDREXT) == 0) {
844 char name[VMAXPATHLEN];
846 sprintf(name, "%s/%s", partpath, dentry->d_name);
848 code = _VHandleVolumeHeader(dp, volfunc, name, &diskHeader, -1, rock);
850 /* fatal error, stop walking */
853 if (code && errfunc) {
854 /* error with header; call the caller-supplied vol error
857 struct VolumeDiskHeader *hdr = &diskHeader;
859 /* we failed to read the header at all, so don't pass in
863 (*errfunc) (dp, name, hdr, rock);
877 #ifdef AFS_PTHREAD_ENV
878 # define AFS_LF_LOCK(lf) assert(pthread_mutex_lock(&((lf)->mutex)) == 0)
879 # define AFS_LF_UNLOCK(lf) assert(pthread_mutex_unlock(&((lf)->mutex)) == 0)
881 # define AFS_LF_LOCK(lf)
882 # define AFS_LF_UNLOCK(lf)
883 #endif /* AFS_PTHREAD_ENV */
886 * initialize a struct VLockFile.
888 * @param[in] lf struct VLockFile to initialize
889 * @param[in] path Full path to the file to use for locks. The string contents
893 VLockFileInit(struct VLockFile *lf, const char *path)
895 memset(lf, 0, sizeof(*lf));
896 lf->path = strdup(path);
898 #ifdef AFS_PTHREAD_ENV
899 assert(pthread_mutex_init(&lf->mutex, NULL) == 0);
900 #endif /* AFS_PTHREAD_ENV */
905 _VOpenPath(const char *path)
909 handle = CreateFile(path,
910 GENERIC_READ | GENERIC_WRITE,
911 FILE_SHARE_READ | FILE_SHARE_WRITE,
914 FILE_ATTRIBUTE_HIDDEN,
916 if (handle == INVALID_HANDLE_VALUE) {
924 _VLockFd(FD_t handle, afs_uint32 offset, int locktype, int nonblock)
929 if (locktype == WRITE_LOCK) {
930 flags |= LOCKFILE_EXCLUSIVE_LOCK;
933 flags |= LOCKFILE_FAIL_IMMEDIATELY;
936 memset(&lap, 0, sizeof(lap));
939 if (!LockFileEx(handle, flags, 0, 1, 0, &lap)) {
940 if (GetLastError() == ERROR_LOCK_VIOLATION) {
950 _VUnlockFd(struct VLockFile *lf, afs_uint32 offset)
954 memset(&lap, 0, sizeof(lap));
957 UnlockFileEx(lf->fd, 0, 1, 0, &lap);
961 _VCloseFd(struct VLockFile *lf)
966 #else /* !AFS_NT40_ENV */
969 * open a file on the local filesystem suitable for locking
971 * @param[in] path abs path of the file to open
973 * @return file descriptor
974 * @retval INVALID_FD failure opening file
977 _VOpenPath(const char *path)
981 fd = open(path, O_RDWR | O_CREAT, 0660);
989 * lock an offset in a file descriptor.
991 * @param[in] fd file descriptor to lock
992 * @param[in] offset offset in file to lock
993 * @param[in] locktype READ_LOCK or WRITE_LOCK
994 * @param[in] nonblock 1 to fail immediately, 0 to wait to acquire lock
996 * @return operation status
998 * @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
1000 * @retval EIO error acquiring file lock
1003 _VLockFd(int fd, afs_uint32 offset, int locktype, int nonblock)
1005 int l_type = F_WRLCK;
1009 if (locktype == READ_LOCK) {
1016 sf.l_start = offset;
1019 sf.l_whence = SEEK_SET;
1021 if (fcntl(fd, cmd, &sf)) {
1022 if (nonblock && (errno == EACCES || errno == EAGAIN)) {
1023 /* We asked for a nonblocking lock, and it was already locked */
1025 if (fcntl(fd, F_GETLK, &sf) != 0 || sf.l_pid == 0) {
1026 Log("_VLockFd: fcntl failed with error %d when trying to "
1027 "query the conflicting lock for fd %d (locktype=%d, "
1028 "offset=%lu)\n", errno, fd, locktype,
1029 afs_printable_uint32_lu(offset));
1031 Log("_VLockFd: conflicting lock held on fd %d, offset %lu by "
1032 "pid %ld (locktype=%d)\n", fd,
1033 afs_printable_uint32_lu(offset), (long int)sf.l_pid,
1038 Log("_VLockFd: fcntl failed with error %d when trying to lock "
1039 "fd %d (locktype=%d, offset=%lu)\n", errno, fd, locktype,
1040 afs_printable_uint32_lu(offset));
1048 * close a file descriptor used for file locking.
1050 * @param[in] fd file descriptor to close
1056 Log("_VCloseFd: error %d closing fd %d\n",
1062 * unlock a file offset in a file descriptor.
1064 * @param[in] fd file descriptor to unlock
1065 * @param[in] offset offset to unlock
1068 _VUnlockFd(int fd, afs_uint32 offset)
1072 sf.l_start = offset;
1074 sf.l_type = F_UNLCK;
1075 sf.l_whence = SEEK_SET;
1077 if (fcntl(fd, F_SETLK, &sf)) {
1078 Log("_VUnlockFd: fcntl failed with error %d when trying to unlock "
1079 "fd %d\n", errno, fd);
1082 #endif /* !AFS_NT40_ENV */
1085 * reinitialize a struct VLockFile.
1087 * Use this to close the lock file (unlocking any locks in it), and effectively
1088 * restore lf to the state it was in when it was initialized. This is the same
1089 * as unlocking all of the locks on the file, without having to remember what
1090 * all of the locks were. Do not unlock previously held locks after calling
1093 * @param[in] lf struct VLockFile to reinit
1095 * @pre nobody is waiting for a lock on this lockfile or otherwise using
1096 * this lockfile at all
1099 VLockFileReinit(struct VLockFile *lf)
1101 #ifdef AFS_PTHREAD_ENV
1102 assert(pthread_mutex_lock(&lf->mutex) == 0);
1103 #endif /* AFS_PTHREAD_ENV */
1105 if (lf->fd != INVALID_FD) {
1107 lf->fd = INVALID_FD;
1112 #ifdef AFS_PTHREAD_ENV
1113 assert(pthread_mutex_unlock(&lf->mutex) == 0);
1114 #endif /* AFS_PTHREAD_ENV */
1118 * lock a file on disk for the process.
1120 * @param[in] lf the struct VLockFile representing the file to lock
1121 * @param[in] offset the offset in the file to lock
1122 * @param[in] locktype READ_LOCK or WRITE_LOCK
1123 * @param[in] nonblock 0 to wait for conflicting locks to clear before
1124 * obtaining the lock; 1 to fail immediately if a
1125 * conflicting lock is held by someone else
1127 * @return operation status
1129 * @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
1131 * @retval EIO error acquiring file lock
1135 * @note do not try to lock/unlock the same offset in the same file from
1136 * different threads; use VGetDiskLock to protect threads from each other in
1137 * addition to other processes
1140 VLockFileLock(struct VLockFile *lf, afs_uint32 offset, int locktype, int nonblock)
1144 assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1148 if (lf->fd == INVALID_FD) {
1149 lf->fd = _VOpenPath(lf->path);
1150 if (lf->fd == INVALID_FD) {
1160 code = _VLockFd(lf->fd, offset, locktype, nonblock);
1164 if (--lf->refcount < 1) {
1166 lf->fd = INVALID_FD;
1175 VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset)
1179 assert(lf->fd != INVALID_FD);
1181 if (--lf->refcount < 1) {
1183 lf->fd = INVALID_FD;
1185 _VUnlockFd(lf->fd, offset);
1191 #ifdef AFS_DEMAND_ATTACH_FS
1194 * initialize a struct VDiskLock.
1196 * @param[in] dl struct VDiskLock to initialize
1197 * @param[in] lf the struct VLockFile to associate with this disk lock
1200 VDiskLockInit(struct VDiskLock *dl, struct VLockFile *lf, afs_uint32 offset)
1203 memset(dl, 0, sizeof(*dl));
1204 Lock_Init(&dl->rwlock);
1205 assert(pthread_mutex_init(&dl->mutex, NULL) == 0);
1206 assert(pthread_cond_init(&dl->cv, NULL) == 0);
1208 dl->offset = offset;
1212 * acquire a lock on a file on local disk.
1214 * @param[in] dl the VDiskLock structure corresponding to the file on disk
1215 * @param[in] locktype READ_LOCK if you want a read lock, or WRITE_LOCK if
1216 * you want a write lock
1217 * @param[in] nonblock 0 to wait for conflicting locks to clear before
1218 * obtaining the lock; 1 to fail immediately if a
1219 * conflicting lock is held by someone else
1221 * @return operation status
1223 * @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
1225 * @retval EIO error acquiring file lock
1229 * @note while normal fcntl-y locks on Unix systems generally only work per-
1230 * process, this interface also deals with locks between threads in the
1231 * process in addition to different processes acquiring the lock
1234 VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
1237 assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1240 if (locktype == READ_LOCK) {
1241 ObtainReadLockNoBlock(&dl->rwlock, code);
1243 ObtainWriteLockNoBlock(&dl->rwlock, code);
1250 } else if (locktype == READ_LOCK) {
1251 ObtainReadLock(&dl->rwlock);
1253 ObtainWriteLock(&dl->rwlock);
1256 assert(pthread_mutex_lock(&dl->mutex) == 0);
1258 if ((dl->flags & VDISKLOCK_ACQUIRING)) {
1259 /* Some other thread is waiting to acquire an fs lock. If nonblock=1,
1260 * we can return immediately, since we know we'll need to wait to
1261 * acquire. Otherwise, wait for the other thread to finish acquiring
1266 while ((dl->flags & VDISKLOCK_ACQUIRING)) {
1267 assert(pthread_cond_wait(&dl->cv, &dl->mutex) == 0);
1272 if (code == 0 && !(dl->flags & VDISKLOCK_ACQUIRED)) {
1273 /* no other thread holds the lock on the actual file; so grab one */
1275 /* first try, don't block on the lock to see if we can get it without
1277 code = VLockFileLock(dl->lockfile, dl->offset, locktype, 1);
1279 if (code == EBUSY && !nonblock) {
1281 /* mark that we are waiting on the fs lock */
1282 dl->flags |= VDISKLOCK_ACQUIRING;
1284 assert(pthread_mutex_unlock(&dl->mutex) == 0);
1285 code = VLockFileLock(dl->lockfile, dl->offset, locktype, nonblock);
1286 assert(pthread_mutex_lock(&dl->mutex) == 0);
1288 dl->flags &= ~VDISKLOCK_ACQUIRING;
1291 dl->flags |= VDISKLOCK_ACQUIRED;
1294 assert(pthread_cond_broadcast(&dl->cv) == 0);
1299 if (locktype == READ_LOCK) {
1300 ReleaseReadLock(&dl->rwlock);
1302 ReleaseWriteLock(&dl->rwlock);
1305 /* successfully got the lock, so inc the number of unlocks we need
1306 * to do before we can unlock the actual file */
1310 assert(pthread_mutex_unlock(&dl->mutex) == 0);
1316 * release a lock on a file on local disk.
1318 * @param[in] dl the struct VDiskLock to release
1319 * @param[in] locktype READ_LOCK if you are unlocking a read lock, or
1320 * WRITE_LOCK if you are unlocking a write lock
1322 * @return operation status
1326 VReleaseDiskLock(struct VDiskLock *dl, int locktype)
1328 assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1330 assert(pthread_mutex_lock(&dl->mutex) == 0);
1331 assert(dl->lockers > 0);
1333 if (--dl->lockers < 1) {
1334 /* no threads are holding this lock anymore, so we can release the
1335 * actual disk lock */
1336 VLockFileUnlock(dl->lockfile, dl->offset);
1337 dl->flags &= ~VDISKLOCK_ACQUIRED;
1340 assert(pthread_mutex_unlock(&dl->mutex) == 0);
1342 if (locktype == READ_LOCK) {
1343 ReleaseReadLock(&dl->rwlock);
1345 ReleaseWriteLock(&dl->rwlock);
1349 #endif /* AFS_DEMAND_ATTACH_FS */