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>
23 #include <sys/types.h>
35 #include <afs/afs_assert.h>
38 #include <afs/afsint.h>
40 #include <afs/errors.h>
43 #include <afs/afssyscalls.h>
45 #include <afs/afsutil.h>
52 #include "volume_inline.h"
53 #include "partition.h"
54 #include "viceinode.h"
56 #include "volinodes.h"
57 #include "vol_prototypes.h"
61 #include <sys/lockf.h>
63 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV) || defined(AFS_LINUX20_ENV)
70 #define afs_open open64
71 #else /* !O_LARGEFILE */
73 #endif /* !O_LARGEFILE */
76 /* Note: the volume creation functions herein leave the destroyMe flag in the
77 volume header ON: this means that the volumes will not be attached by the
78 file server and WILL BE DESTROYED the next time a system salvage is performed */
80 #ifdef FSSYNC_BUILD_CLIENT
82 RemoveInodes(struct afs_inode_info *stuff, Device dev, VolumeId vid)
87 /* This relies on the fact that IDEC only needs the device and NT only
88 * needs the dev and vid to decrement volume special files.
90 IH_INIT(handle, dev, vid, -1);
91 for (i = 0; i < MAXINODETYPE; i++) {
92 Inode inode = *stuff[i].inode;
94 IH_DEC(handle, inode, vid);
100 VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
101 { /* Should be the same as volumeId if there is
105 retVal = VCreateVolume_r(ec, partname, volumeId, parentId);
111 VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
112 { /* Should be the same as volumeId if there is
116 char headerName[VMAXPATHLEN], volumePath[VMAXPATHLEN];
118 struct DiskPartition64 *partition;
119 struct VolumeDiskHeader diskHeader;
125 struct VolumeHeader tempHeader;
126 struct afs_inode_info stuff[MAXINODETYPE];
128 # ifdef AFS_DEMAND_ATTACH_FS
130 # endif /* AFS_DEMAND_ATTACH_FS */
132 init_inode_info(&tempHeader, stuff);
135 memset(&vol, 0, sizeof(vol));
137 vol.parentId = parentId;
138 vol.copyDate = time(0); /* The only date which really means when this
139 * @i(instance) of this volume was created.
140 * Creation date does not mean this */
142 /* Initialize handle for error case below. */
145 /* Verify that the parition is valid before writing to it. */
146 if (!(partition = VGetPartition_r(partname, 0))) {
147 Log("VCreateVolume: partition %s is not in service.\n", partname);
151 #if defined(NEARINODE_HINT)
152 nearInodeHash(volumeId, nearInode);
153 nearInode %= partition->f_files;
155 VGetVolumePath(ec, vol.id, &part, &name);
156 if (*ec == VNOVOL || !strcmp(partition->name, part)) {
157 /* this case is ok */
159 /* return EXDEV if it's a clone to an alternate partition
160 * otherwise assume it's a move */
161 if (vol.parentId != vol.id) {
168 # ifdef AFS_DEMAND_ATTACH_FS
169 /* volume doesn't exist yet, but we must lock it to try to prevent something
170 * else from reading it when we're e.g. half way through creating it (or
171 * something tries to create the same volume at the same time) */
172 locktype = VVolLockType(V_VOLUPD, 1);
173 rc = VLockVolumeByIdNB(volumeId, partition, locktype);
175 Log("VCreateVolume: vol %lu already locked by someone else\n",
176 afs_printable_uint32_lu(volumeId));
180 # else /* AFS_DEMAND_ATTACH_FS */
181 VLockPartition_r(partname);
182 # endif /* !AFS_DEMAND_ATTACH_FS */
184 memset(&tempHeader, 0, sizeof(tempHeader));
185 tempHeader.stamp.magic = VOLUMEHEADERMAGIC;
186 tempHeader.stamp.version = VOLUMEHEADERVERSION;
187 tempHeader.id = vol.id;
188 tempHeader.parent = vol.parentId;
189 vol.stamp.magic = VOLUMEINFOMAGIC;
190 vol.stamp.version = VOLUMEINFOVERSION;
191 vol.destroyMe = DESTROY_ME;
192 (void)afs_snprintf(headerName, sizeof headerName, VFORMAT, afs_printable_uint32_lu(vol.id));
193 (void)afs_snprintf(volumePath, sizeof volumePath, "%s" OS_DIRSEP "%s",
194 VPartitionPath(partition), headerName);
195 rc = stat(volumePath, &st);
196 if (rc == 0 || errno != ENOENT) {
198 Log("VCreateVolume: Header file %s already exists!\n",
202 Log("VCreateVolume: Error %d trying to stat header file %s\n",
208 device = partition->device;
210 for (i = 0; i < MAXINODETYPE; i++) {
211 struct afs_inode_info *p = &stuff[i];
216 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
217 (p->inodeType == VI_LINKTABLE) ? vol.parentId : vol.id,
218 INODESPECIAL, p->inodeType, vol.parentId);
219 if (!(VALID_INO(*(p->inode)))) {
220 if (errno == EEXIST && (p->inodeType == VI_LINKTABLE)) {
221 /* Increment the reference count instead. */
225 *(p->inode) = namei_MakeSpecIno(vol.parentId, VI_LINKTABLE);
226 IH_INIT(lh, device, parentId, *(p->inode));
232 code = IH_INC(lh, *(p->inode), parentId);
233 FDH_REALLYCLOSE(fdP);
242 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
243 vol.id, INODESPECIAL, p->inodeType, vol.parentId);
246 if (!VALID_INO(*(p->inode))) {
247 Log("VCreateVolume: Problem creating %s file associated with volume header %s\n", p->description, volumePath);
251 RemoveInodes(stuff, device, vol.id);
255 VDestroyVolumeDiskHeader(partition, volumeId, parentId);
257 # ifdef AFS_DEMAND_ATTACH_FS
259 VUnlockVolumeById(volumeId, partition);
261 # endif /* AFS_DEMAND_ATTACH_FS */
264 IH_INIT(handle, device, vol.parentId, *(p->inode));
265 fdP = IH_OPEN(handle);
267 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
268 PrintInode(stmp, *(p->inode)), errno);
271 if (FDH_PWRITE(fdP, (char *)&p->stamp, sizeof(p->stamp), 0) !=
273 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
274 PrintInode(stmp, *(p->inode)), errno);
275 FDH_REALLYCLOSE(fdP);
278 FDH_REALLYCLOSE(fdP);
280 nearInode = *(p->inode);
283 IH_INIT(handle, device, vol.parentId, tempHeader.volumeInfo);
284 fdP = IH_OPEN(handle);
286 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
287 PrintInode(stmp, tempHeader.volumeInfo), errno);
290 if (FDH_PWRITE(fdP, (char *)&vol, sizeof(vol), 0) != sizeof(vol)) {
291 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
292 PrintInode(stmp, tempHeader.volumeInfo), errno);
293 FDH_REALLYCLOSE(fdP);
299 VolumeHeaderToDisk(&diskHeader, &tempHeader);
300 rc = VCreateVolumeDiskHeader(&diskHeader, partition);
302 Log("VCreateVolume: Error %d trying to write volume header for "
303 "volume %u on partition %s; volume not created\n", rc,
304 vol.id, VPartitionPath(partition));
311 # ifdef AFS_DEMAND_ATTACH_FS
313 VUnlockVolumeById(volumeId, partition);
315 # endif /* AFS_DEMAND_ATTACH_FS */
316 return (VAttachVolumeByName_r(ec, partname, headerName, V_SECRETLY));
318 #endif /* FSSYNC_BUILD_CLIENT */
322 AssignVolumeName(VolumeDiskData * vol, char *name, char *ext)
325 AssignVolumeName_r(vol, name, ext);
330 AssignVolumeName_r(VolumeDiskData * vol, char *name, char *ext)
333 strncpy(vol->name, name, VNAMESIZE - 1);
334 vol->name[VNAMESIZE - 1] = '\0';
335 dot = strrchr(vol->name, '.');
336 if (dot && (strcmp(dot, ".backup") == 0 || strcmp(dot, ".readonly") == 0))
339 strncat(vol->name, ext, VNAMESIZE - 1 - strlen(vol->name));
343 CopyVolumeHeader_r(VolumeDiskData * from, VolumeDiskData * to)
345 /* The id and parentId fields are not copied; these are inviolate--the to volume
346 * is assumed to have already been created. The id's cannot be changed once
347 * creation has taken place, since they are embedded in the various inodes associated
348 * with the volume. The copydate is also inviolate--it always reflects the time
349 * this volume was created (compare with the creation date--the creation date of
350 * a backup volume is the creation date of the original parent, because the backup
351 * is used to backup the parent volume). */
355 parent = to->parentId;
356 copydate = to->copyDate;
357 memcpy(to, from, sizeof(*from));
359 to->parentId = parent;
360 to->copyDate = copydate;
361 to->destroyMe = DESTROY_ME; /* Caller must always clear this!!! */
362 to->stamp.magic = VOLUMEINFOMAGIC;
363 to->stamp.version = VOLUMEINFOVERSION;
368 CopyVolumeHeader(VolumeDiskData * from, VolumeDiskData * to)
373 code = CopyVolumeHeader_r(from, to);
379 ClearVolumeStats(VolumeDiskData * vol)
382 ClearVolumeStats_r(vol);
387 ClearVolumeStats_r(VolumeDiskData * vol)
389 memset(vol->weekUse, 0, sizeof(vol->weekUse));
395 * read an existing volume disk header.
397 * @param[in] volid volume id
398 * @param[in] dp disk partition object
399 * @param[out] hdr volume disk header or NULL
401 * @note if hdr is NULL, this is essentially an existence test for the vol
404 * @return operation status
406 * @retval -1 volume header doesn't exist
407 * @retval EIO failed to read volume header
412 VReadVolumeDiskHeader(VolumeId volid,
413 struct DiskPartition64 * dp,
414 VolumeDiskHeader_t * hdr)
418 char path[MAXPATHLEN];
420 (void)afs_snprintf(path, sizeof(path),
421 "%s" OS_DIRSEP VFORMAT,
422 VPartitionPath(dp), afs_printable_uint32_lu(volid));
423 fd = open(path, O_RDONLY);
425 Log("VReadVolumeDiskHeader: Couldn't open header for volume %lu (errno %d).\n",
426 afs_printable_uint32_lu(volid), errno);
429 } else if (hdr && read(fd, hdr, sizeof(*hdr)) != sizeof(*hdr)) {
430 Log("VReadVolumeDiskHeader: Couldn't read header for volume %lu.\n",
431 afs_printable_uint32_lu(volid));
441 #ifdef FSSYNC_BUILD_CLIENT
443 * write an existing volume disk header.
445 * @param[in] hdr volume disk header
446 * @param[in] dp disk partition object
447 * @param[in] cr assert if O_CREAT | O_EXCL should be passed to open()
449 * @return operation status
451 * @retval -1 volume header doesn't exist
452 * @retval EIO failed to write volume header
457 _VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
458 struct DiskPartition64 * dp,
463 char path[MAXPATHLEN];
465 #ifdef AFS_DEMAND_ATTACH_FS
466 /* prevent racing with VGC scanners reading the vol header while we are
468 code = VPartHeaderLock(dp, READ_LOCK);
472 #endif /* AFS_DEMAND_ATTACH_FS */
476 (void)afs_snprintf(path, sizeof(path),
477 "%s" OS_DIRSEP VFORMAT,
478 VPartitionPath(dp), afs_printable_uint32_lu(hdr->id));
479 fd = open(path, flags, 0644);
482 Log("_VWriteVolumeDiskHeader: Couldn't open header for volume %lu, "
483 "error = %d\n", afs_printable_uint32_lu(hdr->id), errno);
484 } else if (write(fd, hdr, sizeof(*hdr)) != sizeof(*hdr)) {
485 Log("_VWriteVolumeDiskHeader: Couldn't write header for volume %lu, "
486 "error = %d\n", afs_printable_uint32_lu(hdr->id), errno);
491 if (close(fd) != 0) {
492 Log("_VWriteVolumeDiskHeader: Error closing header for volume "
493 "%lu, errno %d\n", afs_printable_uint32_lu(hdr->id), errno);
497 #ifdef AFS_DEMAND_ATTACH_FS
498 VPartHeaderUnlock(dp, READ_LOCK);
499 #endif /* AFS_DEMAND_ATTACH_FS */
505 * write an existing volume disk header.
507 * @param[in] hdr volume disk header
508 * @param[in] dp disk partition object
510 * @return operation status
512 * @retval ENOENT volume header doesn't exist
513 * @retval EIO failed to write volume header
516 VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
517 struct DiskPartition64 * dp)
521 #ifdef AFS_DEMAND_ATTACH_FS
522 VolumeDiskHeader_t oldhdr;
523 int delvgc = 0, addvgc = 0;
526 /* first, see if anything with the volume IDs have changed; if so, we
527 * need to update the VGC */
529 code = VReadVolumeDiskHeader(hdr->id, dp, &oldhdr);
530 if (code == 0 && (oldhdr.id != hdr->id || oldhdr.parent != hdr->parent)) {
531 /* the vol id or parent vol id changed; need to delete the VGC entry
532 * for the old vol id/parent, and add the new one */
537 /* couldn't get the old header info; add the new header info to the
538 * VGC in case it hasn't been added yet */
542 #endif /* AFS_DEMAND_ATTACH_FS */
544 code = _VWriteVolumeDiskHeader(hdr, dp, 0);
549 #ifdef AFS_DEMAND_ATTACH_FS
551 memset(&res, 0, sizeof(res));
552 code = FSYNC_VGCDel(dp->name, oldhdr.parent, oldhdr.id, FSYNC_WHATEVER, &res);
554 /* unknown vol id is okay; it just further suggests the old header
555 * data was bogus, which is fine since we're trying to fix it */
556 if (code && res.hdr.reason != FSYNC_UNKNOWN_VOLID) {
557 Log("VWriteVolumeDiskHeader: FSYNC_VGCDel(%s, %lu, %lu) "
558 "failed with code %ld reason %ld\n", dp->name,
559 afs_printable_uint32_lu(oldhdr.parent),
560 afs_printable_uint32_lu(oldhdr.id),
561 afs_printable_int32_ld(code),
562 afs_printable_int32_ld(res.hdr.reason));
567 memset(&res, 0, sizeof(res));
568 code = FSYNC_VGCAdd(dp->name, hdr->parent, hdr->id, FSYNC_WHATEVER, &res);
570 Log("VWriteVolumeDiskHeader: FSYNC_VGCAdd(%s, %lu, %lu) "
571 "failed with code %ld reason %ld\n", dp->name,
572 afs_printable_uint32_lu(hdr->parent),
573 afs_printable_uint32_lu(hdr->id),
574 afs_printable_int32_ld(code),
575 afs_printable_int32_ld(res.hdr.reason));
579 #endif /* AFS_DEMAND_ATTACH_FS */
586 * create and write a volume disk header to disk.
588 * @param[in] hdr volume disk header
589 * @param[in] dp disk partition object
591 * @return operation status
593 * @retval EEXIST volume header already exists
594 * @retval EIO failed to write volume header
599 VCreateVolumeDiskHeader(VolumeDiskHeader_t * hdr,
600 struct DiskPartition64 * dp)
603 #ifdef AFS_DEMAND_ATTACH_FS
605 #endif /* AFS_DEMAND_ATTACH_FS */
607 code = _VWriteVolumeDiskHeader(hdr, dp, O_CREAT | O_EXCL);
612 #ifdef AFS_DEMAND_ATTACH_FS
613 memset(&res, 0, sizeof(res));
614 code = FSYNC_VGCAdd(dp->name, hdr->parent, hdr->id, FSYNC_WHATEVER, &res);
616 Log("VCreateVolumeDiskHeader: FSYNC_VGCAdd(%s, %lu, %lu) failed "
617 "with code %ld reason %ld\n", dp->name,
618 afs_printable_uint32_lu(hdr->parent),
619 afs_printable_uint32_lu(hdr->id),
620 afs_printable_int32_ld(code),
621 afs_printable_int32_ld(res.hdr.reason));
623 #endif /* AFS_DEMAND_ATTACH_FS */
631 * destroy a volume disk header.
633 * @param[in] dp disk partition object
634 * @param[in] volid volume id
635 * @param[in] parent parent's volume id, 0 if unknown
637 * @return operation status
640 * @note if parent is 0, the parent volume ID will be looked up from the
643 * @note for non-DAFS, parent is currently ignored
646 VDestroyVolumeDiskHeader(struct DiskPartition64 * dp,
651 char path[MAXPATHLEN];
652 #ifdef AFS_DEMAND_ATTACH_FS
654 #endif /* AFS_DEMAND_ATTACH_FS */
656 (void)afs_snprintf(path, sizeof(path),
657 "%s" OS_DIRSEP VFORMAT,
658 VPartitionPath(dp), afs_printable_uint32_lu(volid));
661 Log("VDestroyVolumeDiskHeader: Couldn't unlink disk header, error = %d\n", errno);
665 #ifdef AFS_DEMAND_ATTACH_FS
666 memset(&res, 0, sizeof(res));
668 FSSYNC_VGQry_response_t q_res;
670 code = FSYNC_VGCQuery(dp->name, volid, &q_res, &res);
672 Log("VDestroyVolumeDiskHeader: FSYNC_VGCQuery(%s, %lu) failed "
673 "with code %ld, reason %ld\n", dp->name,
674 afs_printable_uint32_lu(volid), afs_printable_int32_ld(code),
675 afs_printable_int32_ld(res.hdr.reason));
682 code = FSYNC_VGCDel(dp->name, parent, volid, FSYNC_WHATEVER, &res);
684 Log("VDestroyVolumeDiskHeader: FSYNC_VGCDel(%s, %lu, %lu) failed "
685 "with code %ld reason %ld\n", dp->name,
686 afs_printable_uint32_lu(parent),
687 afs_printable_uint32_lu(volid),
688 afs_printable_int32_ld(code),
689 afs_printable_int32_ld(res.hdr.reason));
691 #endif /* AFS_DEMAND_ATTACH_FS */
696 #endif /* FSSYNC_BUILD_CLIENT */
699 * handle a single vol header as part of VWalkVolumeHeaders.
701 * @param[in] dp disk partition
702 * @param[in] volfunc function to call when a vol header is successfully read
703 * @param[in] name full path name to the .vol header
704 * @param[out] hdr header data read in from the .vol header
705 * @param[in] locked 1 if the partition headers are locked, 0 otherwise
706 * @param[in] rock the rock to pass to volfunc
708 * @return operation status
710 * @retval -1 fatal error, stop scanning
711 * @retval 1 failed to read header
712 * @retval 2 volfunc callback indicated error after header read
715 _VHandleVolumeHeader(struct DiskPartition64 *dp, VWalkVolFunc volfunc,
716 const char *name, struct VolumeDiskHeader *hdr,
717 int locked, void *rock)
722 if ((fd = afs_open(name, O_RDONLY)) == -1
723 || read(fd, hdr, sizeof(*hdr))
725 || hdr->stamp.magic != VOLUMEHEADERMAGIC) {
733 #ifdef AFSFS_DEMAND_ATTACH_FS
735 VPartHeaderUnlock(dp);
737 #endif /* AFS_DEMAND_ATTACH_FS */
739 if (!error && volfunc) {
740 /* the volume header seems fine; call the caller-supplied
741 * 'we-found-a-volume-header' function */
744 #ifdef AFS_DEMAND_ATTACH_FS
748 #endif /* AFS_DEMAND_ATTACH_FS */
750 error = (*volfunc) (dp, name, hdr, last, rock);
759 #ifdef AFS_DEMAND_ATTACH_FS
760 if (error && !locked) {
762 /* retry reading the volume header under the partition
763 * header lock, just to be safe and ensure we're not
764 * racing something rewriting the vol header */
765 code = VPartHeaderLock(dp, WRITE_LOCK);
767 Log("Error acquiring partition write lock when "
768 "looking at header %s\n", name);
772 return _VHandleVolumeHeader(dp, volfunc, name, hdr, 1, rock);
774 #endif /* AFS_DEMAND_ATTACH_FS */
780 * walk through the list of volume headers on a partition.
782 * This function looks through all of the .vol headers on a partition, reads in
783 * each header, and calls the supplied volfunc function on each one. If the
784 * header cannot be read (or volfunc returns a positive error code), DAFS will
785 * VPartHeaderExLock() and retry. If that fails, or if we are non-DAFS, errfunc
786 * will be called (which typically will unlink the problem volume header).
788 * If volfunc returns a negative error code, walking the partition will stop
789 * and we will return an error immediately.
791 * @param[in] dp partition to walk
792 * @param[in] partpath the path opendir()
793 * @param[in] volfunc the function to call when a header is encountered, or
794 * NULL to just skip over valid headers
795 * @param[in] errfunc the function to call when a problematic header is
796 * encountered, or NULL to just skip over bad headers
797 * @param[in] rock rock for volfunc and errfunc
802 * @return operation status
804 * @retval negative fatal error, walk did not finish
807 VWalkVolumeHeaders(struct DiskPartition64 *dp, const char *partpath,
808 VWalkVolFunc volfunc, VWalkErrFunc errfunc, void *rock)
811 struct dirent *dentry = NULL;
813 struct VolumeDiskHeader diskHeader;
815 dirp = opendir(partpath);
817 Log("VWalkVolumeHeaders: cannot open directory %s\n", partpath);
822 while ((dentry = readdir(dirp))) {
823 char *p = dentry->d_name;
824 p = strrchr(dentry->d_name, '.');
825 if (p != NULL && strcmp(p, VHDREXT) == 0) {
826 char name[VMAXPATHLEN];
828 sprintf(name, "%s" OS_DIRSEP "%s", partpath, dentry->d_name);
830 code = _VHandleVolumeHeader(dp, volfunc, name, &diskHeader, -1, rock);
832 /* fatal error, stop walking */
835 if (code && errfunc) {
836 /* error with header; call the caller-supplied vol error
839 struct VolumeDiskHeader *hdr = &diskHeader;
841 /* we failed to read the header at all, so don't pass in
845 (*errfunc) (dp, name, hdr, rock);
860 * initialize a struct VLockFile.
862 * @param[in] lf struct VLockFile to initialize
863 * @param[in] path Full path to the file to use for locks. The string contents
867 VLockFileInit(struct VLockFile *lf, const char *path)
869 memset(lf, 0, sizeof(*lf));
870 lf->path = strdup(path);
872 MUTEX_INIT(&lf->mutex, "vlockfile", MUTEX_DEFAULT, 0);
877 _VOpenPath(const char *path)
881 handle = CreateFile(path,
882 GENERIC_READ | GENERIC_WRITE,
883 FILE_SHARE_READ | FILE_SHARE_WRITE,
886 FILE_ATTRIBUTE_HIDDEN,
888 if (handle == INVALID_HANDLE_VALUE) {
896 _VLockFd(FD_t handle, afs_uint32 offset, int locktype, int nonblock)
901 if (locktype == WRITE_LOCK) {
902 flags |= LOCKFILE_EXCLUSIVE_LOCK;
905 flags |= LOCKFILE_FAIL_IMMEDIATELY;
908 memset(&lap, 0, sizeof(lap));
911 if (!LockFileEx(handle, flags, 0, 1, 0, &lap)) {
912 if (GetLastError() == ERROR_LOCK_VIOLATION) {
922 _VUnlockFd(struct VLockFile *lf, afs_uint32 offset)
926 memset(&lap, 0, sizeof(lap));
929 UnlockFileEx(lf->fd, 0, 1, 0, &lap);
933 _VCloseFd(struct VLockFile *lf)
938 #else /* !AFS_NT40_ENV */
941 * open a file on the local filesystem suitable for locking
943 * @param[in] path abs path of the file to open
945 * @return file descriptor
946 * @retval INVALID_FD failure opening file
949 _VOpenPath(const char *path)
953 fd = open(path, O_RDWR | O_CREAT, 0660);
961 * lock an offset in a file descriptor.
963 * @param[in] fd file descriptor to lock
964 * @param[in] offset offset in file to lock
965 * @param[in] locktype READ_LOCK or WRITE_LOCK
966 * @param[in] nonblock 1 to fail immediately, 0 to wait to acquire lock
968 * @return operation status
970 * @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
972 * @retval EIO error acquiring file lock
975 _VLockFd(int fd, afs_uint32 offset, int locktype, int nonblock)
977 int l_type = F_WRLCK;
981 if (locktype == READ_LOCK) {
991 sf.l_whence = SEEK_SET;
993 if (fcntl(fd, cmd, &sf)) {
994 if (nonblock && (errno == EACCES || errno == EAGAIN)) {
995 /* We asked for a nonblocking lock, and it was already locked */
997 if (fcntl(fd, F_GETLK, &sf) != 0 || sf.l_pid == 0) {
998 Log("_VLockFd: fcntl failed with error %d when trying to "
999 "query the conflicting lock for fd %d (locktype=%d, "
1000 "offset=%lu)\n", errno, fd, locktype,
1001 afs_printable_uint32_lu(offset));
1003 Log("_VLockFd: conflicting lock held on fd %d, offset %lu by "
1004 "pid %ld (locktype=%d)\n", fd,
1005 afs_printable_uint32_lu(offset), (long int)sf.l_pid,
1010 Log("_VLockFd: fcntl failed with error %d when trying to lock "
1011 "fd %d (locktype=%d, offset=%lu)\n", errno, fd, locktype,
1012 afs_printable_uint32_lu(offset));
1020 * close a file descriptor used for file locking.
1022 * @param[in] fd file descriptor to close
1028 Log("_VCloseFd: error %d closing fd %d\n",
1034 * unlock a file offset in a file descriptor.
1036 * @param[in] fd file descriptor to unlock
1037 * @param[in] offset offset to unlock
1040 _VUnlockFd(int fd, afs_uint32 offset)
1044 sf.l_start = offset;
1046 sf.l_type = F_UNLCK;
1047 sf.l_whence = SEEK_SET;
1049 if (fcntl(fd, F_SETLK, &sf)) {
1050 Log("_VUnlockFd: fcntl failed with error %d when trying to unlock "
1051 "fd %d\n", errno, fd);
1054 #endif /* !AFS_NT40_ENV */
1057 * reinitialize a struct VLockFile.
1059 * Use this to close the lock file (unlocking any locks in it), and effectively
1060 * restore lf to the state it was in when it was initialized. This is the same
1061 * as unlocking all of the locks on the file, without having to remember what
1062 * all of the locks were. Do not unlock previously held locks after calling
1065 * @param[in] lf struct VLockFile to reinit
1067 * @pre nobody is waiting for a lock on this lockfile or otherwise using
1068 * this lockfile at all
1071 VLockFileReinit(struct VLockFile *lf)
1073 MUTEX_ENTER(&lf->mutex);
1075 if (lf->fd != INVALID_FD) {
1077 lf->fd = INVALID_FD;
1082 MUTEX_EXIT(&lf->mutex);
1086 * lock a file on disk for the process.
1088 * @param[in] lf the struct VLockFile representing the file to lock
1089 * @param[in] offset the offset in the file to lock
1090 * @param[in] locktype READ_LOCK or WRITE_LOCK
1091 * @param[in] nonblock 0 to wait for conflicting locks to clear before
1092 * obtaining the lock; 1 to fail immediately if a
1093 * conflicting lock is held by someone else
1095 * @return operation status
1097 * @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
1099 * @retval EIO error acquiring file lock
1103 * @note do not try to lock/unlock the same offset in the same file from
1104 * different threads; use VGetDiskLock to protect threads from each other in
1105 * addition to other processes
1108 VLockFileLock(struct VLockFile *lf, afs_uint32 offset, int locktype, int nonblock)
1112 osi_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1114 MUTEX_ENTER(&lf->mutex);
1116 if (lf->fd == INVALID_FD) {
1117 lf->fd = _VOpenPath(lf->path);
1118 if (lf->fd == INVALID_FD) {
1119 MUTEX_EXIT(&lf->mutex);
1126 MUTEX_EXIT(&lf->mutex);
1128 code = _VLockFd(lf->fd, offset, locktype, nonblock);
1131 MUTEX_ENTER(&lf->mutex);
1132 if (--lf->refcount < 1) {
1134 lf->fd = INVALID_FD;
1136 MUTEX_EXIT(&lf->mutex);
1143 VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset)
1145 MUTEX_ENTER(&lf->mutex);
1147 osi_Assert(lf->fd != INVALID_FD);
1149 if (--lf->refcount < 1) {
1151 lf->fd = INVALID_FD;
1153 _VUnlockFd(lf->fd, offset);
1156 MUTEX_EXIT(&lf->mutex);
1159 #ifdef AFS_DEMAND_ATTACH_FS
1162 * initialize a struct VDiskLock.
1164 * @param[in] dl struct VDiskLock to initialize
1165 * @param[in] lf the struct VLockFile to associate with this disk lock
1168 VDiskLockInit(struct VDiskLock *dl, struct VLockFile *lf, afs_uint32 offset)
1171 memset(dl, 0, sizeof(*dl));
1172 Lock_Init(&dl->rwlock);
1173 MUTEX_INIT(&dl->mutex, "disklock", MUTEX_DEFAULT, 0);
1174 CV_INIT(&dl->cv, "disklock cv", CV_DEFAULT, 0);
1176 dl->offset = offset;
1180 * acquire a lock on a file on local disk.
1182 * @param[in] dl the VDiskLock structure corresponding to the file on disk
1183 * @param[in] locktype READ_LOCK if you want a read lock, or WRITE_LOCK if
1184 * you want a write lock
1185 * @param[in] nonblock 0 to wait for conflicting locks to clear before
1186 * obtaining the lock; 1 to fail immediately if a
1187 * conflicting lock is held by someone else
1189 * @return operation status
1191 * @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
1193 * @retval EIO error acquiring file lock
1197 * @note while normal fcntl-y locks on Unix systems generally only work per-
1198 * process, this interface also deals with locks between threads in the
1199 * process in addition to different processes acquiring the lock
1202 VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
1205 osi_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1208 if (locktype == READ_LOCK) {
1209 ObtainReadLockNoBlock(&dl->rwlock, code);
1211 ObtainWriteLockNoBlock(&dl->rwlock, code);
1218 } else if (locktype == READ_LOCK) {
1219 ObtainReadLock(&dl->rwlock);
1221 ObtainWriteLock(&dl->rwlock);
1224 MUTEX_ENTER(&dl->mutex);
1226 if ((dl->flags & VDISKLOCK_ACQUIRING)) {
1227 /* Some other thread is waiting to acquire an fs lock. If nonblock=1,
1228 * we can return immediately, since we know we'll need to wait to
1229 * acquire. Otherwise, wait for the other thread to finish acquiring
1234 while ((dl->flags & VDISKLOCK_ACQUIRING)) {
1235 CV_WAIT(&dl->cv, &dl->mutex);
1240 if (code == 0 && !(dl->flags & VDISKLOCK_ACQUIRED)) {
1241 /* no other thread holds the lock on the actual file; so grab one */
1243 /* first try, don't block on the lock to see if we can get it without
1245 code = VLockFileLock(dl->lockfile, dl->offset, locktype, 1);
1247 if (code == EBUSY && !nonblock) {
1249 /* mark that we are waiting on the fs lock */
1250 dl->flags |= VDISKLOCK_ACQUIRING;
1252 MUTEX_EXIT(&dl->mutex);
1253 code = VLockFileLock(dl->lockfile, dl->offset, locktype, nonblock);
1254 MUTEX_ENTER(&dl->mutex);
1256 dl->flags &= ~VDISKLOCK_ACQUIRING;
1259 dl->flags |= VDISKLOCK_ACQUIRED;
1262 CV_BROADCAST(&dl->cv);
1267 if (locktype == READ_LOCK) {
1268 ReleaseReadLock(&dl->rwlock);
1270 ReleaseWriteLock(&dl->rwlock);
1273 /* successfully got the lock, so inc the number of unlocks we need
1274 * to do before we can unlock the actual file */
1278 MUTEX_EXIT(&dl->mutex);
1284 * release a lock on a file on local disk.
1286 * @param[in] dl the struct VDiskLock to release
1287 * @param[in] locktype READ_LOCK if you are unlocking a read lock, or
1288 * WRITE_LOCK if you are unlocking a write lock
1290 * @return operation status
1294 VReleaseDiskLock(struct VDiskLock *dl, int locktype)
1296 osi_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1298 MUTEX_ENTER(&dl->mutex);
1299 osi_Assert(dl->lockers > 0);
1301 if (--dl->lockers < 1) {
1302 /* no threads are holding this lock anymore, so we can release the
1303 * actual disk lock */
1304 VLockFileUnlock(dl->lockfile, dl->offset);
1305 dl->flags &= ~VDISKLOCK_ACQUIRED;
1308 MUTEX_EXIT(&dl->mutex);
1310 if (locktype == READ_LOCK) {
1311 ReleaseReadLock(&dl->rwlock);
1313 ReleaseWriteLock(&dl->rwlock);
1317 #endif /* AFS_DEMAND_ATTACH_FS */