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 "partition.h"
56 #include "viceinode.h"
58 #include "volinodes.h"
59 #include "vol_prototypes.h"
62 #include <sys/lockf.h>
64 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV) || defined(AFS_LINUX20_ENV)
71 #define afs_open open64
72 #else /* !O_LARGEFILE */
74 #endif /* !O_LARGEFILE */
76 /*@printflike@*/ extern void Log(const char *format, ...);
78 #define nFILES (sizeof (stuff)/sizeof(struct stuff))
80 /* Note: the volume creation functions herein leave the destroyMe flag in the
81 volume header ON: this means that the volumes will not be attached by the
82 file server and WILL BE DESTROYED the next time a system salvage is performed */
84 #ifdef FSSYNC_BUILD_CLIENT
86 RemoveInodes(Device dev, VolumeId vid)
91 /* This relies on the fact that IDEC only needs the device and NT only
92 * needs the dev and vid to decrement volume special files.
94 IH_INIT(handle, dev, vid, -1);
95 for (i = 0; i < nFILES; i++) {
96 Inode inode = *stuff[i].inode;
98 IH_DEC(handle, inode, vid);
104 VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
105 { /* Should be the same as volumeId if there is
109 retVal = VCreateVolume_r(ec, partname, volumeId, parentId);
115 VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
116 { /* Should be the same as volumeId if there is
120 char headerName[VMAXPATHLEN], volumePath[VMAXPATHLEN];
122 struct DiskPartition64 *partition;
123 struct VolumeDiskHeader diskHeader;
131 memset(&vol, 0, sizeof(vol));
133 vol.parentId = parentId;
134 vol.copyDate = time(0); /* The only date which really means when this
135 * @i(instance) of this volume was created.
136 * Creation date does not mean this */
138 /* Initialize handle for error case below. */
141 /* Verify that the parition is valid before writing to it. */
142 if (!(partition = VGetPartition_r(partname, 0))) {
143 Log("VCreateVolume: partition %s is not in service.\n", partname);
147 #if defined(NEARINODE_HINT)
148 nearInodeHash(volumeId, nearInode);
149 nearInode %= partition->f_files;
151 VGetVolumePath(ec, vol.id, &part, &name);
152 if (*ec == VNOVOL || !strcmp(partition->name, part)) {
153 /* this case is ok */
155 /* return EXDEV if it's a clone to an alternate partition
156 * otherwise assume it's a move */
157 if (vol.parentId != vol.id) {
163 VLockPartition_r(partname);
164 memset(&tempHeader, 0, sizeof(tempHeader));
165 tempHeader.stamp.magic = VOLUMEHEADERMAGIC;
166 tempHeader.stamp.version = VOLUMEHEADERVERSION;
167 tempHeader.id = vol.id;
168 tempHeader.parent = vol.parentId;
169 vol.stamp.magic = VOLUMEINFOMAGIC;
170 vol.stamp.version = VOLUMEINFOVERSION;
171 vol.destroyMe = DESTROY_ME;
172 (void)afs_snprintf(headerName, sizeof headerName, VFORMAT, afs_printable_uint32_lu(vol.id));
173 (void)afs_snprintf(volumePath, sizeof volumePath, "%s/%s",
174 VPartitionPath(partition), headerName);
175 rc = stat(volumePath, &st);
176 if (rc == 0 || errno != ENOENT) {
178 Log("VCreateVolume: Header file %s already exists!\n",
182 Log("VCreateVolume: Error %d trying to stat header file %s\n",
188 device = partition->device;
190 for (i = 0; i < nFILES; i++) {
191 register struct stuff *p = &stuff[i];
196 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
197 (p->inodeType == VI_LINKTABLE) ? vol.parentId : vol.id,
198 INODESPECIAL, p->inodeType, vol.parentId);
199 if (!(VALID_INO(*(p->inode)))) {
200 if (errno == EEXIST) {
201 /* Increment the reference count instead. */
206 *(p->inode) = nt_MakeSpecIno(VI_LINKTABLE);
208 *(p->inode) = namei_MakeSpecIno(vol.parentId, VI_LINKTABLE);
210 IH_INIT(lh, device, parentId, *(p->inode));
216 code = IH_INC(lh, *(p->inode), parentId);
217 FDH_REALLYCLOSE(fdP);
226 IH_CREATE(NULL, device, VPartitionPath(partition), nearInode,
227 vol.id, INODESPECIAL, p->inodeType, vol.parentId);
230 if (!VALID_INO(*(p->inode))) {
231 Log("VCreateVolume: Problem creating %s file associated with volume header %s\n", p->description, volumePath);
235 RemoveInodes(device, vol.id);
239 VDestroyVolumeDiskHeader(partition, volumeId, parentId);
242 IH_INIT(handle, device, vol.parentId, *(p->inode));
243 fdP = IH_OPEN(handle);
245 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
246 PrintInode(NULL, *(p->inode)), errno);
249 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
250 Log("VCreateVolume: Problem lseek inode %s (err=%d)\n",
251 PrintInode(NULL, *(p->inode)), errno);
252 FDH_REALLYCLOSE(fdP);
255 if (FDH_WRITE(fdP, (char *)&p->stamp, sizeof(p->stamp)) !=
257 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
258 PrintInode(NULL, *(p->inode)), errno);
259 FDH_REALLYCLOSE(fdP);
262 FDH_REALLYCLOSE(fdP);
264 nearInode = *(p->inode);
267 IH_INIT(handle, device, vol.parentId, tempHeader.volumeInfo);
268 fdP = IH_OPEN(handle);
270 Log("VCreateVolume: Problem iopen inode %s (err=%d)\n",
271 PrintInode(NULL, tempHeader.volumeInfo), errno);
274 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
275 Log("VCreateVolume: Problem lseek inode %s (err=%d)\n",
276 PrintInode(NULL, tempHeader.volumeInfo), errno);
277 FDH_REALLYCLOSE(fdP);
280 if (FDH_WRITE(fdP, (char *)&vol, sizeof(vol)) != sizeof(vol)) {
281 Log("VCreateVolume: Problem writing to inode %s (err=%d)\n",
282 PrintInode(NULL, tempHeader.volumeInfo), errno);
283 FDH_REALLYCLOSE(fdP);
289 VolumeHeaderToDisk(&diskHeader, &tempHeader);
290 rc = VCreateVolumeDiskHeader(&diskHeader, partition);
292 Log("VCreateVolume: Error %d trying to write volume header for "
293 "volume %u on partition %s; volume not created\n", rc,
294 vol.id, VPartitionPath(partition));
301 return (VAttachVolumeByName_r(ec, partname, headerName, V_SECRETLY));
303 #endif /* FSSYNC_BUILD_CLIENT */
307 AssignVolumeName(register VolumeDiskData * vol, char *name, char *ext)
310 AssignVolumeName_r(vol, name, ext);
315 AssignVolumeName_r(register VolumeDiskData * vol, char *name, char *ext)
318 strncpy(vol->name, name, VNAMESIZE - 1);
319 vol->name[VNAMESIZE - 1] = '\0';
320 dot = strrchr(vol->name, '.');
321 if (dot && (strcmp(dot, ".backup") == 0 || strcmp(dot, ".readonly") == 0))
324 strncat(vol->name, ext, VNAMESIZE - 1 - strlen(vol->name));
328 CopyVolumeHeader_r(VolumeDiskData * from, VolumeDiskData * to)
330 /* The id and parentId fields are not copied; these are inviolate--the to volume
331 * is assumed to have already been created. The id's cannot be changed once
332 * creation has taken place, since they are embedded in the various inodes associated
333 * with the volume. The copydate is also inviolate--it always reflects the time
334 * this volume was created (compare with the creation date--the creation date of
335 * a backup volume is the creation date of the original parent, because the backup
336 * is used to backup the parent volume). */
340 parent = to->parentId;
341 copydate = to->copyDate;
342 memcpy(to, from, sizeof(*from));
344 to->parentId = parent;
345 to->copyDate = copydate;
346 to->destroyMe = DESTROY_ME; /* Caller must always clear this!!! */
347 to->stamp.magic = VOLUMEINFOMAGIC;
348 to->stamp.version = VOLUMEINFOVERSION;
353 CopyVolumeHeader(VolumeDiskData * from, VolumeDiskData * to)
358 code = CopyVolumeHeader_r(from, to);
364 ClearVolumeStats(register VolumeDiskData * vol)
367 ClearVolumeStats_r(vol);
372 ClearVolumeStats_r(register VolumeDiskData * vol)
374 memset(vol->weekUse, 0, sizeof(vol->weekUse));
380 * read an existing volume disk header.
382 * @param[in] volid volume id
383 * @param[in] dp disk partition object
384 * @param[out] hdr volume disk header
386 * @return operation status
388 * @retval -1 volume header doesn't exist
389 * @retval EIO failed to read volume header
394 VReadVolumeDiskHeader(VolumeId volid,
395 struct DiskPartition64 * dp,
396 VolumeDiskHeader_t * hdr)
400 char path[MAXPATHLEN];
402 (void)afs_snprintf(path, sizeof(path),
404 VPartitionPath(dp), afs_printable_uint32_lu(volid));
405 fd = open(path, O_RDONLY);
407 Log("VReadVolumeDiskHeader: Couldn't open header for volume %lu.\n",
408 afs_printable_uint32_lu(volid));
410 } else if (read(fd, hdr, sizeof(*hdr)) != sizeof(*hdr)) {
411 Log("VReadVolumeDiskHeader: Couldn't read header for volume %lu.\n",
412 afs_printable_uint32_lu(volid));
422 #ifdef FSSYNC_BUILD_CLIENT
424 * write an existing volume disk header.
426 * @param[in] hdr volume disk header
427 * @param[in] dp disk partition object
428 * @param[in] cr assert if O_CREAT | O_EXCL should be passed to open()
430 * @return operation status
432 * @retval -1 volume header doesn't exist
433 * @retval EIO failed to write volume header
438 _VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
439 struct DiskPartition64 * dp,
444 char path[MAXPATHLEN];
446 #ifdef AFS_DEMAND_ATTACH_FS
447 /* prevent racing with VGC scanners reading the vol header while we are
449 code = VPartHeaderLock(dp, READ_LOCK);
453 #endif /* AFS_DEMAND_ATTACH_FS */
457 (void)afs_snprintf(path, sizeof(path),
459 VPartitionPath(dp), afs_printable_uint32_lu(hdr->id));
460 fd = open(path, flags, 0644);
463 Log("_VWriteVolumeDiskHeader: Couldn't open header for volume %lu, "
464 "error = %d\n", afs_printable_uint32_lu(hdr->id), errno);
465 } else if (write(fd, hdr, sizeof(*hdr)) != sizeof(*hdr)) {
466 Log("_VWriteVolumeDiskHeader: Couldn't write header for volume %lu, "
467 "error = %d\n", afs_printable_uint32_lu(hdr->id), errno);
472 if (close(fd) != 0) {
473 Log("_VWriteVolumeDiskHeader: Error closing header for volume "
474 "%lu, errno %d\n", afs_printable_uint32_lu(hdr->id), errno);
478 #ifdef AFS_DEMAND_ATTACH_FS
479 VPartHeaderUnlock(dp, READ_LOCK);
480 #endif /* AFS_DEMAND_ATTACH_FS */
486 * write an existing volume disk header.
488 * @param[in] hdr volume disk header
489 * @param[in] dp disk partition object
491 * @return operation status
493 * @retval ENOENT volume header doesn't exist
494 * @retval EIO failed to write volume header
497 VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
498 struct DiskPartition64 * dp)
502 #ifdef AFS_DEMAND_ATTACH_FS
503 VolumeDiskHeader_t oldhdr;
504 int delvgc = 0, addvgc = 0;
507 /* first, see if anything with the volume IDs have changed; if so, we
508 * need to update the VGC */
510 code = VReadVolumeDiskHeader(hdr->id, dp, &oldhdr);
511 if (code == 0 && (oldhdr.id != hdr->id || oldhdr.parent != hdr->parent)) {
512 /* the vol id or parent vol id changed; need to delete the VGC entry
513 * for the old vol id/parent, and add the new one */
518 /* couldn't get the old header info; add the new header info to the
519 * VGC in case it hasn't been added yet */
523 #endif /* AFS_DEMAND_ATTACH_FS */
525 code = _VWriteVolumeDiskHeader(hdr, dp, 0);
530 #ifdef AFS_DEMAND_ATTACH_FS
532 memset(&res, 0, sizeof(res));
533 code = FSYNC_VGCDel(dp->name, oldhdr.parent, oldhdr.id, FSYNC_WHATEVER, &res);
535 /* unknown vol id is okay; it just further suggests the old header
536 * data was bogus, which is fine since we're trying to fix it */
537 if (code && res.hdr.reason != FSYNC_UNKNOWN_VOLID) {
538 Log("VWriteVolumeDiskHeader: FSYNC_VGCDel(%s, %lu, %lu) "
539 "failed with code %ld reason %ld\n", dp->name,
540 afs_printable_uint32_lu(oldhdr.parent),
541 afs_printable_uint32_lu(oldhdr.id),
542 afs_printable_int32_ld(code),
543 afs_printable_int32_ld(res.hdr.reason));
548 memset(&res, 0, sizeof(res));
549 code = FSYNC_VGCAdd(dp->name, hdr->parent, hdr->id, FSYNC_WHATEVER, &res);
551 Log("VWriteVolumeDiskHeader: FSYNC_VGCAdd(%s, %lu, %lu) "
552 "failed with code %ld reason %ld\n", dp->name,
553 afs_printable_uint32_lu(hdr->parent),
554 afs_printable_uint32_lu(hdr->id),
555 afs_printable_int32_ld(code),
556 afs_printable_int32_ld(res.hdr.reason));
560 #endif /* AFS_DEMAND_ATTACH_FS */
567 * create and write a volume disk header to disk.
569 * @param[in] hdr volume disk header
570 * @param[in] dp disk partition object
572 * @return operation status
574 * @retval EEXIST volume header already exists
575 * @retval EIO failed to write volume header
580 VCreateVolumeDiskHeader(VolumeDiskHeader_t * hdr,
581 struct DiskPartition64 * dp)
584 #ifdef AFS_DEMAND_ATTACH_FS
586 #endif /* AFS_DEMAND_ATTACH_FS */
588 code = _VWriteVolumeDiskHeader(hdr, dp, O_CREAT | O_EXCL);
593 #ifdef AFS_DEMAND_ATTACH_FS
594 memset(&res, 0, sizeof(res));
595 code = FSYNC_VGCAdd(dp->name, hdr->parent, hdr->id, FSYNC_WHATEVER, &res);
597 Log("VCreateVolumeDiskHeader: FSYNC_VGCAdd(%s, %lu, %lu) failed "
598 "with code %ld reason %ld\n", dp->name,
599 afs_printable_uint32_lu(hdr->parent),
600 afs_printable_uint32_lu(hdr->id),
601 afs_printable_int32_ld(code),
602 afs_printable_int32_ld(res.hdr.reason));
604 #endif /* AFS_DEMAND_ATTACH_FS */
612 * destroy a volume disk header.
614 * @param[in] dp disk partition object
615 * @param[in] volid volume id
616 * @param[in] parent parent's volume id, 0 if unknown
618 * @return operation status
621 * @note if parent is 0, the parent volume ID will be looked up from the
624 * @note for non-DAFS, parent is currently ignored
627 VDestroyVolumeDiskHeader(struct DiskPartition64 * dp,
632 char path[MAXPATHLEN];
633 #ifdef AFS_DEMAND_ATTACH_FS
635 #endif /* AFS_DEMAND_ATTACH_FS */
637 (void)afs_snprintf(path, sizeof(path),
639 VPartitionPath(dp), afs_printable_uint32_lu(volid));
642 Log("VDestroyVolumeDiskHeader: Couldn't unlink disk header, error = %d\n", errno);
646 #ifdef AFS_DEMAND_ATTACH_FS
647 memset(&res, 0, sizeof(res));
649 FSSYNC_VGQry_response_t q_res;
651 code = FSYNC_VGCQuery(dp->name, volid, &q_res, &res);
653 Log("VDestroyVolumeDiskHeader: FSYNC_VGCQuery(%s, %lu) failed "
654 "with code %ld, reason %ld\n", dp->name,
655 afs_printable_uint32_lu(volid), afs_printable_int32_ld(code),
656 afs_printable_int32_ld(res.hdr.reason));
663 code = FSYNC_VGCDel(dp->name, parent, volid, FSYNC_WHATEVER, &res);
665 Log("VDestroyVolumeDiskHeader: FSYNC_VGCDel(%s, %lu, %lu) failed "
666 "with code %ld reason %ld\n", dp->name,
667 afs_printable_uint32_lu(parent),
668 afs_printable_uint32_lu(volid),
669 afs_printable_int32_ld(code),
670 afs_printable_int32_ld(res.hdr.reason));
672 #endif /* AFS_DEMAND_ATTACH_FS */
677 #endif /* FSSYNC_BUILD_CLIENT */
680 * handle a single vol header as part of VWalkVolumeHeaders.
682 * @param[in] dp disk partition
683 * @param[in] volfunc function to call when a vol header is successfully read
684 * @param[in] name full path name to the .vol header
685 * @param[out] hdr header data read in from the .vol header
686 * @param[in] locked 1 if the partition headers are locked, 0 otherwise
687 * @param[in] rock the rock to pass to volfunc
689 * @return operation status
691 * @retval -1 fatal error, stop scanning
692 * @retval 1 failed to read header
693 * @retval 2 volfunc callback indicated error after header read
696 _VHandleVolumeHeader(struct DiskPartition64 *dp, VWalkVolFunc volfunc,
697 const char *name, struct VolumeDiskHeader *hdr,
698 int locked, void *rock)
703 if ((fd = afs_open(name, O_RDONLY)) == -1
704 || read(fd, hdr, sizeof(*hdr))
706 || hdr->stamp.magic != VOLUMEHEADERMAGIC) {
714 #ifdef AFSFS_DEMAND_ATTACH_FS
716 VPartHeaderUnlock(dp);
718 #endif /* AFS_DEMAND_ATTACH_FS */
720 if (!error && volfunc) {
721 /* the volume header seems fine; call the caller-supplied
722 * 'we-found-a-volume-header' function */
725 #ifdef AFS_DEMAND_ATTACH_FS
729 #endif /* AFS_DEMAND_ATTACH_FS */
731 error = (*volfunc) (dp, name, hdr, last, rock);
740 #ifdef AFS_DEMAND_ATTACH_FS
741 if (error && !locked) {
743 /* retry reading the volume header under the partition
744 * header lock, just to be safe and ensure we're not
745 * racing something rewriting the vol header */
746 code = VPartHeaderLock(dp, WRITE_LOCK);
748 Log("Error acquiring partition write lock when "
749 "looking at header %s\n", name);
753 return _VHandleVolumeHeader(dp, volfunc, name, hdr, 1, rock);
755 #endif /* AFS_DEMAND_ATTACH_FS */
761 * walk through the list of volume headers on a partition.
763 * This function looks through all of the .vol headers on a partition, reads in
764 * each header, and calls the supplied volfunc function on each one. If the
765 * header cannot be read (or volfunc returns a positive error code), DAFS will
766 * VPartHeaderExLock() and retry. If that fails, or if we are non-DAFS, errfunc
767 * will be called (which typically will unlink the problem volume header).
769 * If volfunc returns a negative error code, walking the partition will stop
770 * and we will return an error immediately.
772 * @param[in] dp partition to walk
773 * @param[in] partpath the path opendir()
774 * @param[in] volfunc the function to call when a header is encountered, or
775 * NULL to just skip over valid headers
776 * @param[in] errfunc the function to call when a problematic header is
777 * encountered, or NULL to just skip over bad headers
778 * @param[in] rock rock for volfunc and errfunc
783 * @return operation status
785 * @retval negative fatal error, walk did not finish
788 VWalkVolumeHeaders(struct DiskPartition64 *dp, const char *partpath,
789 VWalkVolFunc volfunc, VWalkErrFunc errfunc, void *rock)
792 struct dirent *dentry = NULL;
794 struct VolumeDiskHeader diskHeader;
796 dirp = opendir(partpath);
798 Log("VWalkVolumeHeaders: cannot open directory %s\n", partpath);
803 while ((dentry = readdir(dirp))) {
804 char *p = dentry->d_name;
805 p = strrchr(dentry->d_name, '.');
806 if (p != NULL && strcmp(p, VHDREXT) == 0) {
807 char name[VMAXPATHLEN];
809 sprintf(name, "%s/%s", partpath, dentry->d_name);
811 code = _VHandleVolumeHeader(dp, volfunc, name, &diskHeader, -1, rock);
813 /* fatal error, stop walking */
816 if (code && errfunc) {
817 /* error with header; call the caller-supplied vol error
820 struct VolumeDiskHeader *hdr = &diskHeader;
822 /* we failed to read the header at all, so don't pass in
826 (*errfunc) (dp, name, hdr, rock);
840 #ifdef AFS_PTHREAD_ENV
841 # define AFS_LF_LOCK(lf) assert(pthread_mutex_lock(&((lf)->mutex)) == 0)
842 # define AFS_LF_UNLOCK(lf) assert(pthread_mutex_unlock(&((lf)->mutex)) == 0)
844 # define AFS_LF_LOCK(lf)
845 # define AFS_LF_UNLOCK(lf)
846 #endif /* AFS_PTHREAD_ENV */
849 * initialize a struct VLockFile.
851 * @param[in] lf struct VLockFile to initialize
852 * @param[in] path Full path to the file to use for locks. The string contents
856 VLockFileInit(struct VLockFile *lf, const char *path)
858 memset(lf, 0, sizeof(*lf));
859 lf->path = strdup(path);
861 #ifdef AFS_PTHREAD_ENV
862 assert(pthread_mutex_init(&lf->mutex, NULL) == 0);
863 #endif /* AFS_PTHREAD_ENV */
868 _VOpenPath(const char *path)
872 handle = CreateFile(path,
873 GENERIC_READ | GENERIC_WRITE,
874 FILE_SHARE_READ | FILE_SHARE_WRITE,
877 FILE_ATTRIBUTE_HIDDEN,
879 if (handle == INVALID_HANDLE_VALUE) {
887 _VLockFd(FD_t handle, afs_uint32 offset, int locktype, int nonblock)
892 if (locktype == WRITE_LOCK) {
893 flags |= LOCKFILE_EXCLUSIVE_LOCK;
896 flags |= LOCKFILE_FAIL_IMMEDIATELY;
899 memset(&lap, 0, sizeof(lap));
902 if (!LockFileEx(handle, flags, 0, 1, 0, &lap)) {
903 if (GetLastError() == ERROR_LOCK_VIOLATION) {
913 _VUnlockFd(struct VLockFile *lf, afs_uint32 offset)
917 memset(&lap, 0, sizeof(lap));
920 UnlockFileEx(lf->fd, 0, 1, 0, &lap);
924 _VCloseFd(struct VLockFile *lf)
929 #else /* !AFS_NT40_ENV */
932 * open a file on the local filesystem suitable for locking
934 * @param[in] path abs path of the file to open
936 * @return file descriptor
937 * @retval INVALID_FD failure opening file
940 _VOpenPath(const char *path)
944 fd = open(path, O_RDWR | O_CREAT, 0660);
952 * lock an offset in a file descriptor.
954 * @param[in] fd file descriptor to lock
955 * @param[in] offset offset in file to lock
956 * @param[in] locktype READ_LOCK or WRITE_LOCK
957 * @param[in] nonblock 1 to fail immediately, 0 to wait to acquire lock
959 * @return operation status
961 * @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
963 * @retval EIO error acquiring file lock
966 _VLockFd(int fd, afs_uint32 offset, int locktype, int nonblock)
968 int l_type = F_WRLCK;
972 if (locktype == READ_LOCK) {
982 sf.l_whence = SEEK_SET;
984 if (fcntl(fd, cmd, &sf)) {
985 if (nonblock && (errno == EACCES || errno == EAGAIN)) {
986 /* We asked for a nonblocking lock, and it was already locked */
989 Log("_VLockFd: fcntl failed with error %d when trying to lock "
990 "fd %d (locktype=%d)\n", errno, fd, locktype);
998 * close a file descriptor used for file locking.
1000 * @param[in] fd file descriptor to close
1006 Log("_VCloseFd: error %d closing fd %d\n",
1012 * unlock a file offset in a file descriptor.
1014 * @param[in] fd file descriptor to unlock
1015 * @param[in] offset offset to unlock
1018 _VUnlockFd(int fd, afs_uint32 offset)
1022 sf.l_start = offset;
1024 sf.l_type = F_UNLCK;
1025 sf.l_whence = SEEK_SET;
1027 if (fcntl(fd, F_SETLK, &sf)) {
1028 Log("_VUnlockFd: fcntl failed with error %d when trying to unlock "
1029 "fd %d\n", errno, fd);
1032 #endif /* !AFS_NT40_ENV */
1035 * reinitialize a struct VLockFile.
1037 * Use this to close the lock file (unlocking any locks in it), and effectively
1038 * restore lf to the state it was in when it was initialized. This is the same
1039 * as unlocking all of the locks on the file, without having to remember what
1040 * all of the locks were. Do not unlock previously held locks after calling
1043 * @param[in] lf struct VLockFile to reinit
1045 * @pre nobody is waiting for a lock on this lockfile or otherwise using
1046 * this lockfile at all
1049 VLockFileReinit(struct VLockFile *lf)
1051 #ifdef AFS_PTHREAD_ENV
1052 assert(pthread_mutex_lock(&lf->mutex) == 0);
1053 #endif /* AFS_PTHREAD_ENV */
1055 if (lf->fd != INVALID_FD) {
1057 lf->fd = INVALID_FD;
1062 #ifdef AFS_PTHREAD_ENV
1063 assert(pthread_mutex_unlock(&lf->mutex) == 0);
1064 #endif /* AFS_PTHREAD_ENV */
1068 * lock a file on disk for the process.
1070 * @param[in] lf the struct VLockFile representing the file to lock
1071 * @param[in] offset the offset in the file to lock
1072 * @param[in] locktype READ_LOCK or WRITE_LOCK
1073 * @param[in] nonblock 0 to wait for conflicting locks to clear before
1074 * obtaining the lock; 1 to fail immediately if a
1075 * conflicting lock is held by someone else
1077 * @return operation status
1079 * @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
1081 * @retval EIO error acquiring file lock
1085 * @note do not try to lock/unlock the same offset in the same file from
1086 * different threads; use VGetDiskLock to protect threads from each other in
1087 * addition to other processes
1090 VLockFileLock(struct VLockFile *lf, afs_uint32 offset, int locktype, int nonblock)
1094 assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1098 if (lf->fd == INVALID_FD) {
1099 lf->fd = _VOpenPath(lf->path);
1100 if (lf->fd == INVALID_FD) {
1110 code = _VLockFd(lf->fd, offset, locktype, nonblock);
1114 if (--lf->refcount < 1) {
1116 lf->fd = INVALID_FD;
1125 VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset)
1129 assert(lf->fd != INVALID_FD);
1131 if (--lf->refcount < 1) {
1133 lf->fd = INVALID_FD;
1135 _VUnlockFd(lf->fd, offset);
1141 #ifdef AFS_DEMAND_ATTACH_FS
1144 * initialize a struct VDiskLock.
1146 * @param[in] dl struct VDiskLock to initialize
1147 * @param[in] lf the struct VLockFile to associate with this disk lock
1150 VDiskLockInit(struct VDiskLock *dl, struct VLockFile *lf, afs_uint32 offset)
1153 memset(dl, 0, sizeof(*dl));
1154 Lock_Init(&dl->rwlock);
1155 assert(pthread_mutex_init(&dl->mutex, NULL) == 0);
1156 assert(pthread_cond_init(&dl->cv, NULL) == 0);
1158 dl->offset = offset;
1162 * acquire a lock on a file on local disk.
1164 * @param[in] dl the VDiskLock structure corresponding to the file on disk
1165 * @param[in] locktype READ_LOCK if you want a read lock, or WRITE_LOCK if
1166 * you want a write lock
1167 * @param[in] nonblock 0 to wait for conflicting locks to clear before
1168 * obtaining the lock; 1 to fail immediately if a
1169 * conflicting lock is held by someone else
1171 * @return operation status
1173 * @retval EBUSY someone else is holding a conflicting lock and nonblock=1 was
1175 * @retval EIO error acquiring file lock
1179 * @note while normal fcntl-y locks on Unix systems generally only work per-
1180 * process, this interface also deals with locks between threads in the
1181 * process in addition to different processes acquiring the lock
1184 VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
1187 assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1190 if (locktype == READ_LOCK) {
1191 ObtainReadLockNoBlock(&dl->rwlock, code);
1193 ObtainWriteLockNoBlock(&dl->rwlock, code);
1200 } else if (locktype == READ_LOCK) {
1201 ObtainReadLock(&dl->rwlock);
1203 ObtainWriteLock(&dl->rwlock);
1206 assert(pthread_mutex_lock(&dl->mutex) == 0);
1208 if ((dl->flags & VDISKLOCK_ACQUIRING)) {
1209 /* Some other thread is waiting to acquire an fs lock. If nonblock=1,
1210 * we can return immediately, since we know we'll need to wait to
1211 * acquire. Otherwise, wait for the other thread to finish acquiring
1216 while ((dl->flags & VDISKLOCK_ACQUIRING)) {
1217 assert(pthread_cond_wait(&dl->cv, &dl->mutex) == 0);
1222 if (code == 0 && !(dl->flags & VDISKLOCK_ACQUIRED)) {
1223 /* no other thread holds the lock on the actual file; so grab one */
1225 /* first try, don't block on the lock to see if we can get it without
1227 code = VLockFileLock(dl->lockfile, dl->offset, locktype, 1);
1229 if (code == EBUSY && !nonblock) {
1231 /* mark that we are waiting on the fs lock */
1232 dl->flags |= VDISKLOCK_ACQUIRING;
1234 assert(pthread_mutex_unlock(&dl->mutex) == 0);
1235 code = VLockFileLock(dl->lockfile, dl->offset, locktype, nonblock);
1236 assert(pthread_mutex_lock(&dl->mutex) == 0);
1238 dl->flags &= ~VDISKLOCK_ACQUIRING;
1241 dl->flags |= VDISKLOCK_ACQUIRED;
1244 assert(pthread_cond_broadcast(&dl->cv) == 0);
1249 if (locktype == READ_LOCK) {
1250 ReleaseReadLock(&dl->rwlock);
1252 ReleaseWriteLock(&dl->rwlock);
1255 /* successfully got the lock, so inc the number of unlocks we need
1256 * to do before we can unlock the actual file */
1260 assert(pthread_mutex_unlock(&dl->mutex) == 0);
1266 * release a lock on a file on local disk.
1268 * @param[in] dl the struct VDiskLock to release
1269 * @param[in] locktype READ_LOCK if you are unlocking a read lock, or
1270 * WRITE_LOCK if you are unlocking a write lock
1272 * @return operation status
1276 VReleaseDiskLock(struct VDiskLock *dl, int locktype)
1278 assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
1280 assert(pthread_mutex_lock(&dl->mutex) == 0);
1281 assert(dl->lockers > 0);
1283 if (--dl->lockers < 1) {
1284 /* no threads are holding this lock anymore, so we can release the
1285 * actual disk lock */
1286 VLockFileUnlock(dl->lockfile, dl->offset);
1287 dl->flags &= ~VDISKLOCK_ACQUIRED;
1290 assert(pthread_mutex_unlock(&dl->mutex) == 0);
1292 if (locktype == READ_LOCK) {
1293 ReleaseReadLock(&dl->rwlock);
1295 ReleaseWriteLock(&dl->rwlock);
1299 #endif /* AFS_DEMAND_ATTACH_FS */