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 #ifdef HAVE_SYS_FILE_H
26 #ifdef HAVE_SYS_LOCKF_H
27 #include <sys/lockf.h>
30 #include <afs/afs_assert.h>
33 #include <afs/afsint.h>
35 #include <afs/errors.h>
38 #include <afs/afssyscalls.h>
40 #include <afs/afsutil.h>
46 #include "volume_inline.h"
47 #include "partition.h"
48 #include "viceinode.h"
50 #include "volinodes.h"
51 #include "vol_prototypes.h"
56 # define AFS_SETLKW F_SETLKW64
57 # define AFS_SETLK F_SETLK64
58 # define afs_st_flock flock64
60 # define AFS_SETLKW F_SETLKW
61 # define AFS_SETLK F_SETLK
62 # define afs_st_flock flock
66 /* Note: the volume creation functions herein leave the destroyMe flag in the
67 volume header ON: this means that the volumes will not be attached by the
68 file server and WILL BE DESTROYED the next time a system salvage is performed */
70 #ifdef FSSYNC_BUILD_CLIENT
72 RemoveInodes(struct afs_inode_info *stuff, Device dev, VolumeId vid)
77 /* This relies on the fact that IDEC only needs the device and NT only
78 * needs the dev and vid to decrement volume special files.
80 IH_INIT(handle, dev, vid, -1);
81 for (i = 0; i < MAXINODETYPE; i++) {
82 Inode inode = *stuff[i].inode;
84 IH_DEC(handle, inode, vid);
90 VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
91 { /* Should be the same as volumeId if there is
95 retVal = VCreateVolume_r(ec, partname, volumeId, parentId);
101 VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
102 { /* Should be the same as volumeId if there is
106 char headerName[VMAXPATHLEN], volumePath[VMAXPATHLEN];
108 struct DiskPartition64 *partition;
109 struct VolumeDiskHeader diskHeader;
112 Inode nearInode AFS_UNUSED = 0;
115 struct VolumeHeader tempHeader;
116 struct afs_inode_info stuff[MAXINODETYPE];
118 # ifdef AFS_DEMAND_ATTACH_FS
120 # endif /* AFS_DEMAND_ATTACH_FS */
122 init_inode_info(&tempHeader, stuff);
125 memset(&vol, 0, sizeof(vol));
127 vol.parentId = parentId;
128 vol.copyDate = time(0); /* The only date which really means when this
129 * @i(instance) of this volume was created.
130 * Creation date does not mean this */
132 /* Initialize handle for error case below. */
135 /* Verify that the parition is valid before writing to it. */
136 if (!(partition = VGetPartition_r(partname, 0))) {
137 Log("VCreateVolume: partition %s is not in service.\n", partname);
141 #if defined(NEARINODE_HINT)
142 nearInodeHash(volumeId, nearInode);
143 nearInode %= partition->f_files;
145 VGetVolumePath(ec, vol.id, &part, &name);
146 if (*ec == VNOVOL || !strcmp(partition->name, part)) {
147 /* this case is ok */
149 /* return EXDEV if it's a clone to an alternate partition
150 * otherwise assume it's a move */
151 if (vol.parentId != vol.id) {
158 # ifdef AFS_DEMAND_ATTACH_FS
159 /* volume doesn't exist yet, but we must lock it to try to prevent something
160 * else from reading it when we're e.g. half way through creating it (or
161 * something tries to create the same volume at the same time) */
162 locktype = VVolLockType(V_VOLUPD, 1);
163 rc = VLockVolumeByIdNB(volumeId, partition, locktype);
165 Log("VCreateVolume: vol %lu already locked by someone else\n",
166 afs_printable_uint32_lu(volumeId));
170 # else /* AFS_DEMAND_ATTACH_FS */
171 VLockPartition_r(partname);
172 # endif /* !AFS_DEMAND_ATTACH_FS */
174 memset(&tempHeader, 0, sizeof(tempHeader));
175 tempHeader.stamp.magic = VOLUMEHEADERMAGIC;
176 tempHeader.stamp.version = VOLUMEHEADERVERSION;
177 tempHeader.id = vol.id;
178 tempHeader.parent = vol.parentId;
179 vol.stamp.magic = VOLUMEINFOMAGIC;
180 vol.stamp.version = VOLUMEINFOVERSION;
181 vol.destroyMe = DESTROY_ME;
182 snprintf(headerName, sizeof headerName, VFORMAT,
183 afs_printable_uint32_lu(vol.id));
184 snprintf(volumePath, sizeof volumePath, "%s" OS_DIRSEP "%s",
185 VPartitionPath(partition), headerName);
186 rc = stat(volumePath, &st);
187 if (rc == 0 || errno != ENOENT) {
189 Log("VCreateVolume: Header file %s already exists!\n",
193 Log("VCreateVolume: Error %d trying to stat header file %s\n",
199 device = partition->device;
201 for (i = 0; i < MAXINODETYPE; i++) {
202 struct afs_inode_info *p = &stuff[i];
207 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
208 (p->inodeType == VI_LINKTABLE) ? vol.parentId : vol.id,
209 INODESPECIAL, p->inodeType, vol.parentId);
210 if (!(VALID_INO(*(p->inode)))) {
211 if (errno == EEXIST && (p->inodeType == VI_LINKTABLE)) {
212 /* Increment the reference count instead. */
216 *(p->inode) = namei_MakeSpecIno(vol.parentId, VI_LINKTABLE);
217 IH_INIT(lh, device, parentId, *(p->inode));
223 code = IH_INC(lh, *(p->inode), parentId);
224 FDH_REALLYCLOSE(fdP);
233 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
234 vol.id, INODESPECIAL, p->inodeType, vol.parentId);
237 if (!VALID_INO(*(p->inode))) {
238 Log("VCreateVolume: Problem creating %s file associated with volume header %s\n", p->description, volumePath);
242 RemoveInodes(stuff, device, vol.id);
246 VDestroyVolumeDiskHeader(partition, volumeId, parentId);
248 # ifdef AFS_DEMAND_ATTACH_FS
250 VUnlockVolumeById(volumeId, partition);
252 # endif /* AFS_DEMAND_ATTACH_FS */
255 IH_INIT(handle, device, vol.parentId, *(p->inode));
256 fdP = IH_OPEN(handle);
258 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
259 PrintInode(stmp, *(p->inode)), errno);
262 if (FDH_PWRITE(fdP, (char *)&p->stamp, sizeof(p->stamp), 0) !=
264 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
265 PrintInode(stmp, *(p->inode)), errno);
266 FDH_REALLYCLOSE(fdP);
269 FDH_REALLYCLOSE(fdP);
271 nearInode = *(p->inode);
274 IH_INIT(handle, device, vol.parentId, tempHeader.volumeInfo);
275 fdP = IH_OPEN(handle);
277 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
278 PrintInode(stmp, tempHeader.volumeInfo), errno);
281 if (FDH_PWRITE(fdP, (char *)&vol, sizeof(vol), 0) != sizeof(vol)) {
282 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
283 PrintInode(stmp, tempHeader.volumeInfo), errno);
284 FDH_REALLYCLOSE(fdP);
290 VolumeHeaderToDisk(&diskHeader, &tempHeader);
291 rc = VCreateVolumeDiskHeader(&diskHeader, partition);
293 Log("VCreateVolume: Error %d trying to write volume header for "
294 "volume %u on partition %s; volume not created\n", rc,
295 vol.id, VPartitionPath(partition));
302 # ifdef AFS_DEMAND_ATTACH_FS
304 VUnlockVolumeById(volumeId, partition);
306 # endif /* AFS_DEMAND_ATTACH_FS */
307 return (VAttachVolumeByName_r(ec, partname, headerName, V_SECRETLY));
309 #endif /* FSSYNC_BUILD_CLIENT */
313 AssignVolumeName(VolumeDiskData * vol, char *name, char *ext)
316 AssignVolumeName_r(vol, name, ext);
321 AssignVolumeName_r(VolumeDiskData * vol, char *name, char *ext)
324 strncpy(vol->name, name, VNAMESIZE - 1);
325 vol->name[VNAMESIZE - 1] = '\0';
326 dot = strrchr(vol->name, '.');
327 if (dot && (strcmp(dot, ".backup") == 0 || strcmp(dot, ".readonly") == 0))
330 strncat(vol->name, ext, VNAMESIZE - 1 - strlen(vol->name));
334 CopyVolumeHeader_r(VolumeDiskData * from, VolumeDiskData * to)
336 /* The id and parentId fields are not copied; these are inviolate--the to volume
337 * is assumed to have already been created. The id's cannot be changed once
338 * creation has taken place, since they are embedded in the various inodes associated
339 * with the volume. The copydate is also inviolate--it always reflects the time
340 * this volume was created (compare with the creation date--the creation date of
341 * a backup volume is the creation date of the original parent, because the backup
342 * is used to backup the parent volume). */
346 parent = to->parentId;
347 copydate = to->copyDate;
348 memcpy(to, from, sizeof(*from));
350 to->parentId = parent;
351 to->copyDate = copydate;
352 to->destroyMe = DESTROY_ME; /* Caller must always clear this!!! */
353 to->stamp.magic = VOLUMEINFOMAGIC;
354 to->stamp.version = VOLUMEINFOVERSION;
359 CopyVolumeHeader(VolumeDiskData * from, VolumeDiskData * to)
364 code = CopyVolumeHeader_r(from, to);
370 ClearVolumeStats(VolumeDiskData * vol)
373 ClearVolumeStats_r(vol);
378 ClearVolumeStats_r(VolumeDiskData * vol)
380 memset(vol->weekUse, 0, sizeof(vol->weekUse));
386 * read an existing volume disk header.
388 * @param[in] volid volume id
389 * @param[in] dp disk partition object
390 * @param[out] hdr volume disk header or NULL
392 * @note if hdr is NULL, this is essentially an existence test for the vol
395 * @return operation status
397 * @retval -1 volume header doesn't exist
398 * @retval EIO failed to read volume header
403 VReadVolumeDiskHeader(VolumeId volid,
404 struct DiskPartition64 * dp,
405 VolumeDiskHeader_t * hdr)
409 char path[MAXPATHLEN];
411 snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
412 VPartitionPath(dp), afs_printable_uint32_lu(volid));
413 fd = open(path, O_RDONLY);
415 Log("VReadVolumeDiskHeader: Couldn't open header for volume %lu (errno %d).\n",
416 afs_printable_uint32_lu(volid), errno);
419 } else if (hdr && read(fd, hdr, sizeof(*hdr)) != sizeof(*hdr)) {
420 Log("VReadVolumeDiskHeader: Couldn't read header for volume %lu.\n",
421 afs_printable_uint32_lu(volid));
431 #ifdef FSSYNC_BUILD_CLIENT
433 * write an existing volume disk header.
435 * @param[in] hdr volume disk header
436 * @param[in] dp disk partition object
437 * @param[in] cr assert if O_CREAT | O_EXCL should be passed to open()
439 * @return operation status
441 * @retval -1 volume header doesn't exist
442 * @retval EIO failed to write volume header
447 _VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
448 struct DiskPartition64 * dp,
453 char path[MAXPATHLEN];
455 #ifdef AFS_DEMAND_ATTACH_FS
456 /* prevent racing with VGC scanners reading the vol header while we are
458 code = VPartHeaderLock(dp, READ_LOCK);
462 #endif /* AFS_DEMAND_ATTACH_FS */
466 snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
467 VPartitionPath(dp), afs_printable_uint32_lu(hdr->id));
468 fd = open(path, flags, 0644);
471 Log("_VWriteVolumeDiskHeader: Couldn't open header for volume %lu, "
472 "error = %d\n", afs_printable_uint32_lu(hdr->id), errno);
473 } else if (write(fd, hdr, sizeof(*hdr)) != sizeof(*hdr)) {
474 Log("_VWriteVolumeDiskHeader: Couldn't write header for volume %lu, "
475 "error = %d\n", afs_printable_uint32_lu(hdr->id), errno);
480 if (close(fd) != 0) {
481 Log("_VWriteVolumeDiskHeader: Error closing header for volume "
482 "%lu, errno %d\n", afs_printable_uint32_lu(hdr->id), errno);
486 #ifdef AFS_DEMAND_ATTACH_FS
487 VPartHeaderUnlock(dp, READ_LOCK);
488 #endif /* AFS_DEMAND_ATTACH_FS */
494 * write an existing volume disk header.
496 * @param[in] hdr volume disk header
497 * @param[in] dp disk partition object
499 * @return operation status
501 * @retval ENOENT volume header doesn't exist
502 * @retval EIO failed to write volume header
505 VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
506 struct DiskPartition64 * dp)
510 #ifdef AFS_DEMAND_ATTACH_FS
511 VolumeDiskHeader_t oldhdr;
512 int delvgc = 0, addvgc = 0;
515 /* first, see if anything with the volume IDs have changed; if so, we
516 * need to update the VGC */
518 code = VReadVolumeDiskHeader(hdr->id, dp, &oldhdr);
519 if (code == 0 && (oldhdr.id != hdr->id || oldhdr.parent != hdr->parent)) {
520 /* the vol id or parent vol id changed; need to delete the VGC entry
521 * for the old vol id/parent, and add the new one */
526 /* couldn't get the old header info; add the new header info to the
527 * VGC in case it hasn't been added yet */
531 #endif /* AFS_DEMAND_ATTACH_FS */
533 code = _VWriteVolumeDiskHeader(hdr, dp, 0);
538 #ifdef AFS_DEMAND_ATTACH_FS
540 memset(&res, 0, sizeof(res));
541 code = FSYNC_VGCDel(dp->name, oldhdr.parent, oldhdr.id, FSYNC_WHATEVER, &res);
543 /* unknown vol id is okay; it just further suggests the old header
544 * data was bogus, which is fine since we're trying to fix it */
545 if (code && res.hdr.reason != FSYNC_UNKNOWN_VOLID) {
546 Log("VWriteVolumeDiskHeader: FSYNC_VGCDel(%s, %lu, %lu) "
547 "failed with code %ld reason %ld\n", dp->name,
548 afs_printable_uint32_lu(oldhdr.parent),
549 afs_printable_uint32_lu(oldhdr.id),
550 afs_printable_int32_ld(code),
551 afs_printable_int32_ld(res.hdr.reason));
556 memset(&res, 0, sizeof(res));
557 code = FSYNC_VGCAdd(dp->name, hdr->parent, hdr->id, FSYNC_WHATEVER, &res);
559 Log("VWriteVolumeDiskHeader: FSYNC_VGCAdd(%s, %lu, %lu) "
560 "failed with code %ld reason %ld\n", dp->name,
561 afs_printable_uint32_lu(hdr->parent),
562 afs_printable_uint32_lu(hdr->id),
563 afs_printable_int32_ld(code),
564 afs_printable_int32_ld(res.hdr.reason));
568 #endif /* AFS_DEMAND_ATTACH_FS */
575 * create and write a volume disk header to disk.
577 * @param[in] hdr volume disk header
578 * @param[in] dp disk partition object
580 * @return operation status
582 * @retval EEXIST volume header already exists
583 * @retval EIO failed to write volume header
588 VCreateVolumeDiskHeader(VolumeDiskHeader_t * hdr,
589 struct DiskPartition64 * dp)
592 #ifdef AFS_DEMAND_ATTACH_FS
594 #endif /* AFS_DEMAND_ATTACH_FS */
596 code = _VWriteVolumeDiskHeader(hdr, dp, O_CREAT | O_EXCL);
601 #ifdef AFS_DEMAND_ATTACH_FS
602 memset(&res, 0, sizeof(res));
603 code = FSYNC_VGCAdd(dp->name, hdr->parent, hdr->id, FSYNC_WHATEVER, &res);
605 Log("VCreateVolumeDiskHeader: FSYNC_VGCAdd(%s, %lu, %lu) failed "
606 "with code %ld reason %ld\n", dp->name,
607 afs_printable_uint32_lu(hdr->parent),
608 afs_printable_uint32_lu(hdr->id),
609 afs_printable_int32_ld(code),
610 afs_printable_int32_ld(res.hdr.reason));
612 #endif /* AFS_DEMAND_ATTACH_FS */
620 * destroy a volume disk header.
622 * @param[in] dp disk partition object
623 * @param[in] volid volume id
624 * @param[in] parent parent's volume id, 0 if unknown
626 * @return operation status
629 * @note if parent is 0, the parent volume ID will be looked up from the
632 * @note for non-DAFS, parent is currently ignored
635 VDestroyVolumeDiskHeader(struct DiskPartition64 * dp,
640 char path[MAXPATHLEN];
641 #ifdef AFS_DEMAND_ATTACH_FS
643 #endif /* AFS_DEMAND_ATTACH_FS */
645 snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
646 VPartitionPath(dp), afs_printable_uint32_lu(volid));
649 Log("VDestroyVolumeDiskHeader: Couldn't unlink disk header, error = %d\n", errno);
653 #ifdef AFS_DEMAND_ATTACH_FS
654 memset(&res, 0, sizeof(res));
656 FSSYNC_VGQry_response_t q_res;
658 code = FSYNC_VGCQuery(dp->name, volid, &q_res, &res);
660 Log("VDestroyVolumeDiskHeader: FSYNC_VGCQuery(%s, %lu) failed "
661 "with code %ld, reason %ld\n", dp->name,
662 afs_printable_uint32_lu(volid), afs_printable_int32_ld(code),
663 afs_printable_int32_ld(res.hdr.reason));
670 code = FSYNC_VGCDel(dp->name, parent, volid, FSYNC_WHATEVER, &res);
672 Log("VDestroyVolumeDiskHeader: FSYNC_VGCDel(%s, %lu, %lu) failed "
673 "with code %ld reason %ld\n", dp->name,
674 afs_printable_uint32_lu(parent),
675 afs_printable_uint32_lu(volid),
676 afs_printable_int32_ld(code),
677 afs_printable_int32_ld(res.hdr.reason));
679 #endif /* AFS_DEMAND_ATTACH_FS */
684 #endif /* FSSYNC_BUILD_CLIENT */
687 * handle a single vol header as part of VWalkVolumeHeaders.
689 * @param[in] dp disk partition
690 * @param[in] volfunc function to call when a vol header is successfully read
691 * @param[in] name full path name to the .vol header
692 * @param[out] hdr header data read in from the .vol header
693 * @param[in] locked 1 if the partition headers are locked, 0 otherwise
694 * @param[in] rock the rock to pass to volfunc
696 * @return operation status
698 * @retval -1 fatal error, stop scanning
699 * @retval 1 failed to read header
700 * @retval 2 volfunc callback indicated error after header read
703 _VHandleVolumeHeader(struct DiskPartition64 *dp, VWalkVolFunc volfunc,
704 const char *name, struct VolumeDiskHeader *hdr,
705 int locked, void *rock)
710 if ((fd = OS_OPEN(name, O_RDONLY, 0)) == INVALID_FD
711 || OS_READ(fd, hdr, sizeof(*hdr))
713 || hdr->stamp.magic != VOLUMEHEADERMAGIC) {
717 if (fd != INVALID_FD) {
721 #ifdef AFSFS_DEMAND_ATTACH_FS
723 VPartHeaderUnlock(dp);
725 #endif /* AFS_DEMAND_ATTACH_FS */
727 if (!error && volfunc) {
728 /* the volume header seems fine; call the caller-supplied
729 * 'we-found-a-volume-header' function */
732 #ifdef AFS_DEMAND_ATTACH_FS
736 #endif /* AFS_DEMAND_ATTACH_FS */
738 error = (*volfunc) (dp, name, hdr, last, rock);
747 #ifdef AFS_DEMAND_ATTACH_FS
748 if (error && !locked) {
750 /* retry reading the volume header under the partition
751 * header lock, just to be safe and ensure we're not
752 * racing something rewriting the vol header */
753 code = VPartHeaderLock(dp, WRITE_LOCK);
755 Log("Error acquiring partition write lock when "
756 "looking at header %s\n", name);
760 return _VHandleVolumeHeader(dp, volfunc, name, hdr, 1, rock);
762 #endif /* AFS_DEMAND_ATTACH_FS */
768 * walk through the list of volume headers on a partition.
770 * This function looks through all of the .vol headers on a partition, reads in
771 * each header, and calls the supplied volfunc function on each one. If the
772 * header cannot be read (or volfunc returns a positive error code), DAFS will
773 * VPartHeaderExLock() and retry. If that fails, or if we are non-DAFS, errfunc
774 * will be called (which typically will unlink the problem volume header).
776 * If volfunc returns a negative error code, walking the partition will stop
777 * and we will return an error immediately.
779 * @param[in] dp partition to walk
780 * @param[in] partpath the path opendir()
781 * @param[in] volfunc the function to call when a header is encountered, or
782 * NULL to just skip over valid headers
783 * @param[in] errfunc the function to call when a problematic header is
784 * encountered, or NULL to just skip over bad headers
785 * @param[in] rock rock for volfunc and errfunc
790 * @return operation status
792 * @retval negative fatal error, walk did not finish
795 VWalkVolumeHeaders(struct DiskPartition64 *dp, const char *partpath,
796 VWalkVolFunc volfunc, VWalkErrFunc errfunc, void *rock)
799 struct dirent *dentry = NULL;
801 struct VolumeDiskHeader diskHeader;
803 dirp = opendir(partpath);
805 Log("VWalkVolumeHeaders: cannot open directory %s\n", partpath);
810 while ((dentry = readdir(dirp))) {
811 char *p = dentry->d_name;
812 p = strrchr(dentry->d_name, '.');
813 if (p != NULL && strcmp(p, VHDREXT) == 0) {
814 char name[VMAXPATHLEN];
816 sprintf(name, "%s" OS_DIRSEP "%s", partpath, dentry->d_name);
818 code = _VHandleVolumeHeader(dp, volfunc, name, &diskHeader, -1, rock);
820 /* fatal error, stop walking */
823 if (code && errfunc) {
824 /* error with header; call the caller-supplied vol error
827 struct VolumeDiskHeader *hdr = &diskHeader;
829 /* we failed to read the header at all, so don't pass in
833 (*errfunc) (dp, name, hdr, rock);
848 * initialize a struct VLockFile.
850 * @param[in] lf struct VLockFile to initialize
851 * @param[in] path Full path to the file to use for locks. The string contents
855 VLockFileInit(struct VLockFile *lf, const char *path)
857 memset(lf, 0, sizeof(*lf));
858 lf->path = strdup(path);
860 MUTEX_INIT(&lf->mutex, "vlockfile", MUTEX_DEFAULT, 0);
865 _VOpenPath(const char *path)
869 handle = CreateFile(path,
870 GENERIC_READ | GENERIC_WRITE,
871 FILE_SHARE_READ | FILE_SHARE_WRITE,
874 FILE_ATTRIBUTE_HIDDEN,
876 if (handle == INVALID_HANDLE_VALUE) {
884 _VLockFd(FD_t handle, afs_uint32 offset, int locktype, int nonblock)
889 if (locktype == WRITE_LOCK) {
890 flags |= LOCKFILE_EXCLUSIVE_LOCK;
893 flags |= LOCKFILE_FAIL_IMMEDIATELY;
896 memset(&lap, 0, sizeof(lap));
899 if (!LockFileEx(handle, flags, 0, 1, 0, &lap)) {
900 if (GetLastError() == ERROR_LOCK_VIOLATION) {
910 _VUnlockFd(FD_t handle, afs_uint32 offset)
914 memset(&lap, 0, sizeof(lap));
917 UnlockFileEx(handle, 0, 1, 0, &lap);
921 _VCloseFd(FD_t handle)
926 #else /* !AFS_NT40_ENV */
929 * open a file on the local filesystem suitable for locking
931 * @param[in] path abs path of the file to open
933 * @return file descriptor
934 * @retval INVALID_FD failure opening file
937 _VOpenPath(const char *path)
941 fd = open(path, O_RDWR | O_CREAT, 0660);
949 * lock an offset in a file descriptor.
951 * @param[in] fd file descriptor to lock
952 * @param[in] offset offset in file to lock
953 * @param[in] locktype READ_LOCK or WRITE_LOCK
954 * @param[in] nonblock 1 to fail immediately, 0 to wait to acquire lock
956 * @return operation status
958 * @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
960 * @retval EIO error acquiring file lock
963 _VLockFd(FD_t fd, afs_uint32 offset, int locktype, int nonblock)
965 int l_type = F_WRLCK;
966 int cmd = AFS_SETLKW;
967 struct afs_st_flock sf;
969 if (locktype == READ_LOCK) {
979 sf.l_whence = SEEK_SET;
981 if (fcntl(fd, cmd, &sf)) {
982 if (nonblock && (errno == EACCES || errno == EAGAIN)) {
983 /* We asked for a nonblocking lock, and it was already locked */
985 if (fcntl(fd, F_GETLK, &sf) != 0 || sf.l_pid == 0) {
986 Log("_VLockFd: fcntl failed with error %d when trying to "
987 "query the conflicting lock for fd %d (locktype=%d, "
988 "offset=%lu)\n", errno, fd, locktype,
989 afs_printable_uint32_lu(offset));
991 Log("_VLockFd: conflicting lock held on fd %d, offset %lu by "
992 "pid %ld (locktype=%d)\n", fd,
993 afs_printable_uint32_lu(offset), (long int)sf.l_pid,
998 Log("_VLockFd: fcntl failed with error %d when trying to lock "
999 "fd %d (locktype=%d, offset=%lu)\n", errno, fd, locktype,
1000 afs_printable_uint32_lu(offset));
1008 * close a file descriptor used for file locking.
1010 * @param[in] fd file descriptor to close
1016 Log("_VCloseFd: error %d closing fd %d\n",
1022 * unlock a file offset in a file descriptor.
1024 * @param[in] fd file descriptor to unlock
1025 * @param[in] offset offset to unlock
1028 _VUnlockFd(FD_t fd, afs_uint32 offset)
1030 struct afs_st_flock sf;
1032 sf.l_start = offset;
1034 sf.l_type = F_UNLCK;
1035 sf.l_whence = SEEK_SET;
1037 if (fcntl(fd, AFS_SETLK, &sf)) {
1038 Log("_VUnlockFd: fcntl failed with error %d when trying to unlock "
1039 "fd %d\n", errno, fd);
1042 #endif /* !AFS_NT40_ENV */
1045 * reinitialize a struct VLockFile.
1047 * Use this to close the lock file (unlocking any locks in it), and effectively
1048 * restore lf to the state it was in when it was initialized. This is the same
1049 * as unlocking all of the locks on the file, without having to remember what
1050 * all of the locks were. Do not unlock previously held locks after calling
1053 * @param[in] lf struct VLockFile to reinit
1055 * @pre nobody is waiting for a lock on this lockfile or otherwise using
1056 * this lockfile at all
1059 VLockFileReinit(struct VLockFile *lf)
1061 MUTEX_ENTER(&lf->mutex);
1063 if (lf->fd != INVALID_FD) {
1065 lf->fd = INVALID_FD;
1070 MUTEX_EXIT(&lf->mutex);
1074 * lock a file on disk for the process.
1076 * @param[in] lf the struct VLockFile representing the file to lock
1077 * @param[in] offset the offset in the file to lock
1078 * @param[in] locktype READ_LOCK or WRITE_LOCK
1079 * @param[in] nonblock 0 to wait for conflicting locks to clear before
1080 * obtaining the lock; 1 to fail immediately if a
1081 * conflicting lock is held by someone else
1083 * @return operation status
1085 * @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
1087 * @retval EIO error acquiring file lock
1091 * @note do not try to lock/unlock the same offset in the same file from
1092 * different threads; use VGetDiskLock to protect threads from each other in
1093 * addition to other processes
1096 VLockFileLock(struct VLockFile *lf, afs_uint32 offset, int locktype, int nonblock)
1100 osi_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1102 MUTEX_ENTER(&lf->mutex);
1104 if (lf->fd == INVALID_FD) {
1105 lf->fd = _VOpenPath(lf->path);
1106 if (lf->fd == INVALID_FD) {
1107 MUTEX_EXIT(&lf->mutex);
1114 MUTEX_EXIT(&lf->mutex);
1116 code = _VLockFd(lf->fd, offset, locktype, nonblock);
1119 MUTEX_ENTER(&lf->mutex);
1120 if (--lf->refcount < 1) {
1122 lf->fd = INVALID_FD;
1124 MUTEX_EXIT(&lf->mutex);
1131 VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset)
1133 MUTEX_ENTER(&lf->mutex);
1135 osi_Assert(lf->fd != INVALID_FD);
1137 if (--lf->refcount < 1) {
1139 lf->fd = INVALID_FD;
1141 _VUnlockFd(lf->fd, offset);
1144 MUTEX_EXIT(&lf->mutex);
1147 #ifdef AFS_DEMAND_ATTACH_FS
1150 * initialize a struct VDiskLock.
1152 * @param[in] dl struct VDiskLock to initialize
1153 * @param[in] lf the struct VLockFile to associate with this disk lock
1156 VDiskLockInit(struct VDiskLock *dl, struct VLockFile *lf, afs_uint32 offset)
1159 memset(dl, 0, sizeof(*dl));
1160 Lock_Init(&dl->rwlock);
1161 MUTEX_INIT(&dl->mutex, "disklock", MUTEX_DEFAULT, 0);
1162 CV_INIT(&dl->cv, "disklock cv", CV_DEFAULT, 0);
1164 dl->offset = offset;
1168 * acquire a lock on a file on local disk.
1170 * @param[in] dl the VDiskLock structure corresponding to the file on disk
1171 * @param[in] locktype READ_LOCK if you want a read lock, or WRITE_LOCK if
1172 * you want a write lock
1173 * @param[in] nonblock 0 to wait for conflicting locks to clear before
1174 * obtaining the lock; 1 to fail immediately if a
1175 * conflicting lock is held by someone else
1177 * @return operation status
1179 * @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
1181 * @retval EIO error acquiring file lock
1185 * @note while normal fcntl-y locks on Unix systems generally only work per-
1186 * process, this interface also deals with locks between threads in the
1187 * process in addition to different processes acquiring the lock
1190 VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
1193 osi_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1196 if (locktype == READ_LOCK) {
1197 ObtainReadLockNoBlock(&dl->rwlock, code);
1199 ObtainWriteLockNoBlock(&dl->rwlock, code);
1206 } else if (locktype == READ_LOCK) {
1207 ObtainReadLock(&dl->rwlock);
1209 ObtainWriteLock(&dl->rwlock);
1212 MUTEX_ENTER(&dl->mutex);
1214 if ((dl->flags & VDISKLOCK_ACQUIRING)) {
1215 /* Some other thread is waiting to acquire an fs lock. If nonblock=1,
1216 * we can return immediately, since we know we'll need to wait to
1217 * acquire. Otherwise, wait for the other thread to finish acquiring
1222 while ((dl->flags & VDISKLOCK_ACQUIRING)) {
1223 CV_WAIT(&dl->cv, &dl->mutex);
1228 if (code == 0 && !(dl->flags & VDISKLOCK_ACQUIRED)) {
1229 /* no other thread holds the lock on the actual file; so grab one */
1231 /* first try, don't block on the lock to see if we can get it without
1233 code = VLockFileLock(dl->lockfile, dl->offset, locktype, 1);
1235 if (code == EBUSY && !nonblock) {
1237 /* mark that we are waiting on the fs lock */
1238 dl->flags |= VDISKLOCK_ACQUIRING;
1240 MUTEX_EXIT(&dl->mutex);
1241 code = VLockFileLock(dl->lockfile, dl->offset, locktype, nonblock);
1242 MUTEX_ENTER(&dl->mutex);
1244 dl->flags &= ~VDISKLOCK_ACQUIRING;
1247 dl->flags |= VDISKLOCK_ACQUIRED;
1250 CV_BROADCAST(&dl->cv);
1255 if (locktype == READ_LOCK) {
1256 ReleaseReadLock(&dl->rwlock);
1258 ReleaseWriteLock(&dl->rwlock);
1261 /* successfully got the lock, so inc the number of unlocks we need
1262 * to do before we can unlock the actual file */
1266 MUTEX_EXIT(&dl->mutex);
1272 * release a lock on a file on local disk.
1274 * @param[in] dl the struct VDiskLock to release
1275 * @param[in] locktype READ_LOCK if you are unlocking a read lock, or
1276 * WRITE_LOCK if you are unlocking a write lock
1278 * @return operation status
1282 VReleaseDiskLock(struct VDiskLock *dl, int locktype)
1284 osi_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1286 MUTEX_ENTER(&dl->mutex);
1287 osi_Assert(dl->lockers > 0);
1289 if (--dl->lockers < 1) {
1290 /* no threads are holding this lock anymore, so we can release the
1291 * actual disk lock */
1292 VLockFileUnlock(dl->lockfile, dl->offset);
1293 dl->flags &= ~VDISKLOCK_ACQUIRED;
1296 MUTEX_EXIT(&dl->mutex);
1298 if (locktype == READ_LOCK) {
1299 ReleaseReadLock(&dl->rwlock);
1301 ReleaseWriteLock(&dl->rwlock);
1305 #endif /* AFS_DEMAND_ATTACH_FS */