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
14 Institution: The Information Technology Center, Carnegie-Mellon University
18 #include <afsconfig.h>
19 #include <afs/param.h>
29 #include <sys/param.h>
30 #include <sys/types.h>
33 #include <sys/statvfs.h>
34 #endif /* AFS_HAVE_STATVFS */
35 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
36 #include <sys/mount.h>
39 #if !defined(AFS_SGI_ENV)
41 #include <sys/mount.h>
43 #else /* AFS_OSF_ENV */
44 #ifdef AFS_VFSINCL_ENV
47 #include <sys/fs/ufs_fs.h>
49 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
50 #include <ufs/ufs/dinode.h>
51 #include <ufs/ffs/fs.h>
56 #else /* AFS_VFSINCL_ENV */
57 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
60 #endif /* AFS_VFSINCL_ENV */
61 #endif /* AFS_OSF_ENV */
62 #include <sys/errno.h>
68 #include <sys/lockf.h>
74 #include <checklist.h>
76 #if defined(AFS_SUN_ENV)
81 #include <sys/mnttab.h>
82 #include <sys/mntent.h>
84 #ifdef AFS_LINUX22_ENV
86 #include <sys/statfs.h>
93 #endif /* AFS_SGI_ENV */
94 #endif /* AFS_NT40_ENV */
95 #if defined(AFS_SGI_ENV)
96 #include <sys/errno.h>
104 #include <afs/afsint.h>
106 #include <afs/errors.h>
109 #include <afs/afssyscalls.h>
115 #include "namei_ops.h"
117 #endif /* AFS_NT40_ENV */
118 #endif /* AFS_NAMEI_ENV */
121 #include "partition.h"
122 #ifdef AFS_PTHREAD_ENV
124 #else /* AFS_PTHREAD_ENV */
125 #include <afs/assert.h>
126 #endif /* AFS_PTHREAD_ENV */
128 #if defined(AFS_HPUX_ENV)
129 #include <sys/types.h>
130 #include <sys/privgrp.h>
131 #endif /* defined(AFS_HPUX_ENV) */
134 #include <jfs/filsys.h>
137 int aixlow_water = 8; /* default 8% */
138 struct DiskPartition *DiskPartitionList;
140 #ifdef AFS_SGI_XFS_IOPS_ENV
141 /* Verify that the on disk XFS inodes on the partition are large enough to
142 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
143 * too small to fit in the inode. Returns 0 if the attribute does fit in
146 #include <afs/xfsattrs.h>
147 static int VerifyXFSInodeSize(char *part, char *fstype)
150 int length = SIZEOF_XFS_ATTR_T;
155 if (strcmp("xfs", fstype))
158 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
159 if (((fd=open(part, O_RDONLY, 0)) != -1)
160 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
162 if (fsx.fsx_nextents) {
163 Log("Partition %s: XFS inodes too small, exiting.\n", part);
164 Log("Run xfs_size_check utility and remake partitions.\n");
172 (void) attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
179 static void VInitPartition_r(char *path, char *devname, Device dev)
181 struct DiskPartition *dp, *op;
182 dp = (struct DiskPartition *) malloc(sizeof (struct DiskPartition));
183 /* Add it to the end, to preserve order when we print statistics */
184 for (op = DiskPartitionList; op; op = op->next) {
191 DiskPartitionList = dp;
193 strcpy(dp->name, path);
194 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
195 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
196 strcpy(dp->devName, path);
197 strcat(dp->devName, "/");
198 strcat(dp->devName, "Lock");
199 mkdir(dp->devName, 0700);
200 strcat(dp->devName, path);
201 close(open(dp->devName, O_RDWR | O_CREAT, 0600));
202 dp->device = volutil_GetPartitionID(path);
204 strcpy(dp->devName, devname);
209 dp->f_files = 1; /* just a default value */
210 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
211 if (programType == fileServer)
212 (void) namei_ViceREADME(VPartitionPath(dp));
214 VSetPartitionDiskUsage_r(dp);
217 static void VInitPartition(char *path, char *devname, Device dev)
220 VInitPartition_r(path, devname, dev);
225 /* VAttachPartitions() finds the vice partitions on this server. Calls
226 * VCheckPartition() to do some basic checks on the partition. If the partition
227 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
229 * Returns the number of errors returned by VCheckPartition. An error in
230 * VCheckPartition means that partition is a valid vice partition but the
231 * fileserver should not start because of the error found on that partition.
234 * No specific user space file system checks, since we don't know what
235 * is being used for vice partitions.
237 * Use partition name as devname.
239 int VCheckPartition(part, devname)
244 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
245 char AFSIDatPath[MAXPATHLEN];
248 /* Only keep track of "/vicepx" partitions since it can get hairy
249 * when NFS mounts are involved.. */
250 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
253 if (stat(part, &status) < 0) {
254 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
258 #ifndef AFS_AIX32_ENV
259 if (programType == fileServer) {
260 char salvpath[MAXPATHLEN];
261 strcpy(salvpath, part);
262 strcat(salvpath, "/FORCESALVAGE");
263 if (stat(salvpath, &status) == 0) {
264 Log("VInitVnodes: Found %s; aborting\n", salvpath);
270 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
271 strcpy(AFSIDatPath, part);
272 strcat(AFSIDatPath, "/AFSIDat");
274 if (stat(AFSIDatPath, &status) < 0) {
278 dirp = opendir(part);
280 while (dp = readdir(dirp)) {
281 if (dp->d_name[0] == 'V') {
282 Log("This program is compiled with AFS_NAMEI_ENV, but partition %s seems to contain volumes which don't use the namei-interface; aborting\n", part);
289 #else /* AFS_NAMEI_ENV */
290 if (stat(AFSIDatPath, &status) == 0) {
291 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
294 #endif /* AFS_NAMEI_ENV */
297 #ifdef AFS_SGI_XFS_IOPS_ENV
298 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
302 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
303 if (status.st_ino != ROOTINO) {
304 Log("%s is not a mounted file system; ignored.\n", part);
309 VInitPartition(part, devname, status.st_dev);
314 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
315 * attached (return value 1), or only attached when it is a separately
316 * mounted partition (return value 0). For non-NAMEI environments, it
319 static int VIsAlwaysAttach(part)
327 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
330 strncpy(checkfile, part, 100);
331 strcat(checkfile, "/");
332 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
334 ret = stat(checkfile, &st);
335 return (ret < 0) ? 0 : 1;
336 #else /* AFS_NAMEI_ENV */
338 #endif /* AFS_NAMEI_ENV */
341 /* VAttachPartitions2() looks for and attaches /vicepX partitions
342 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
343 * used to attach /vicepX directories which aren't on dedicated
344 * partitions, in the NAMEI fileserver.
346 void VAttachPartitions2() {
353 while (de = readdir(dirp)) {
355 strncat(pname, de->d_name, 20);
356 pname[sizeof(pname)-1] = '\0';
358 /* Only keep track of "/vicepx" partitions since automounter
360 if (VIsAlwaysAttach(pname))
361 VCheckPartition(pname, "");
364 #endif /* AFS_NAMEI_ENV */
366 #endif /* AFS_NT40_ENV */
369 int VAttachPartitions(void)
375 if (!(mntfile = fopen(MNTTAB, "r"))) {
376 Log("Can't open %s\n", MNTTAB);
380 while (!getmntent(mntfile, &mnt)) {
381 /* Ignore non ufs or non read/write partitions */
382 if ((strcmp(mnt.mnt_fstype, "ufs") !=0) ||
383 (strncmp(mnt.mnt_mntopts, "ro,ignore",9) ==0))
386 /* If we're going to always attach this partition, do it later. */
387 if (VIsAlwaysAttach(mnt.mnt_mountp))
390 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0 )
394 (void) fclose(mntfile);
396 /* Process the always-attach partitions, if any. */
397 VAttachPartitions2();
402 #endif /* AFS_SUN5_ENV */
403 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
404 int VAttachPartitions(void)
408 struct mntent *mntent;
410 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
411 Log("Problems in getting mount entries(setmntent)\n");
414 while (mntent = getmntent(mfd)) {
415 if (!hasmntopt(mntent, MNTOPT_RW)) continue;
417 /* If we're going to always attach this partition, do it later. */
418 if (VIsAlwaysAttach(mntent->mnt_dir))
421 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0 )
427 /* Process the always-attach partitions, if any. */
428 VAttachPartitions2();
435 * (This function was grabbed from df.c)
439 register struct vmount **vmountpp; /* place to tell where buffer is */
442 register struct vmount *vm;
445 /* set initial size of mntctl buffer to a MAGIC NUMBER */
448 /* try the operation until ok or a fatal error */
450 if ((vm = (struct vmount *)malloc(size)) == NULL) {
451 /* failed getting memory for mount status buf */
452 perror("FATAL ERROR: get_stat malloc failed\n");
457 * perform the QUERY mntctl - if it returns > 0, that is the
458 * number of vmount structures in the buffer. If it returns
459 * -1, an error occured. If it returned 0, then look in
460 * first word of buffer for needed size.
462 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t)vm)) > 0) {
463 /* OK, got it, now return */
467 } else if (nmounts == 0) {
468 /* the buffer wasn't big enough .... */
469 /* .... get required buffer size */
474 /* some other kind of error occurred */
481 int VAttachPartitions(void)
485 struct vmount *vmountp;
487 if ((nmounts = getmount(&vmountp)) <= 0) {
488 Log("Problems in getting # of mount entries(getmount)\n");
491 for (; nmounts; nmounts--,
492 vmountp = (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
493 char *part = vmt2dataptr(vmountp, VMT_STUB);
495 if (vmountp->vmt_flags & (MNT_READONLY|MNT_REMOVABLE|MNT_REMOTE))
496 continue; /* Ignore any "special" partitions */
499 #ifndef AFS_NAMEI_ENV
501 struct superblock fs;
502 /* The Log statements are non-sequiters in the SalvageLog and don't
503 * even appear in the VolserLog, so restrict them to the FileLog.
505 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT))<0) {
506 if (programType == fileServer)
507 Log("Can't read superblock for %s, ignoring it.\n", part);
510 if (IsBigFilesFileSystem(&fs)) {
511 if (programType == fileServer)
512 Log("%s is a big files filesystem, ignoring it.\n", part);
519 /* If we're going to always attach this partition, do it later. */
520 if (VIsAlwaysAttach(part))
523 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0 )
527 /* Process the always-attach partitions, if any. */
528 VAttachPartitions2();
533 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
534 int VAttachPartitions(void)
539 if (setfsent() < 0) {
540 Log("Error listing filesystems.\n");
544 while (fsent = getfsent()) {
545 if (strcmp(fsent->fs_type, "rw") != 0) continue;
547 /* If we're going to always attach this partition, do it later. */
548 if (VIsAlwaysAttach(fsent->fs_file))
551 if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0 )
556 /* Process the always-attach partitions, if any. */
557 VAttachPartitions2();
565 #include <sys/stat.h>
568 * validate names in vptab.
575 int VValidVPTEntry(struct vptab *vpe)
577 int len = strlen(vpe->vp_name);
580 if (len < VICE_PREFIX_SIZE+1 || len > VICE_PREFIX_SIZE + 2)
582 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
585 for (i=VICE_PREFIX_SIZE; i<len; i++) {
586 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
587 Log("Invalid partition name %s in registry, ignoring it.\n",
592 if (len == VICE_PREFIX_SIZE + 2) {
593 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE]-'a') * 26 +
594 (int)(vpe->vp_name[VICE_PREFIX_SIZE+1]-'a') ;
596 Log("Invalid partition name %s in registry, ignoring it.\n",
602 len = strlen(vpe->vp_dev);
603 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A' ||
604 vpe->vp_dev[0] > 'Z') {
605 Log("Invalid device name %s in registry, ignoring it.\n",
613 int VCheckPartition(char *partName)
620 /* partName is presumed to be of the form "X:" */
621 (void) sprintf(volRoot, "%c:\\", *partName);
623 if (!GetVolumeInformation(volRoot, /* volume root directory */
624 NULL, /* volume name buffer */
625 0, /* volume name size */
626 NULL, /* volume serial number */
627 &dwDummy, /* max component length */
628 &dwDummy, /* file system flags */
629 volFsType, /* file system name */
630 sizeof(volFsType))) {
631 err = GetLastError();
632 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n",
637 if (strcmp(volFsType, "NTFS")) {
638 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
646 int VAttachPartitions(void)
648 struct DiskPartition *partP, *prevP, *nextP;
649 struct vpt_iter iter;
652 if (vpt_Start(&iter)<0) {
653 Log("No partitions to attach.\n");
657 while (0==vpt_NextEntry(&iter, &entry)) {
658 if (!VValidVPTEntry(&entry)) {
662 /* This test for duplicates relies on the fact that the method
663 * of storing the partition names in the NT registry means the same
664 * partition name will never appear twice in the list.
666 for (partP = DiskPartitionList; partP; partP = partP->next) {
667 if (*partP->devName == *entry.vp_dev) {
668 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
669 partP->flags = PART_DUPLICATE;
670 break; /* Only one entry will ever be in this list. */
673 if (partP) continue; /* found a duplicate */
675 if (VCheckPartition(entry.vp_dev)<0)
677 /* This test allows for manually inserting the FORCESALVAGE flag
678 * and thereby invoking the salvager. scandisk obviously won't be
681 if (programType == fileServer) {
683 char salvpath[MAXPATHLEN];
684 strcpy(salvpath, entry.vp_dev);
685 strcat(salvpath, "\\FORCESALVAGE");
686 if (stat(salvpath, &status) == 0) {
687 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
691 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
695 /* Run through partition list and clear out the dupes. */
696 prevP = nextP = NULL;
697 for (partP = DiskPartitionList; partP; partP = nextP) {
699 if (partP->flags == PART_DUPLICATE) {
701 prevP->next = partP->next;
703 DiskPartitionList = partP->next;
714 #ifdef AFS_LINUX22_ENV
715 int VAttachPartitions(void)
719 struct mntent *mntent;
721 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
722 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
723 Log("Problems in getting mount entries(setmntent)\n");
727 while (mntent = getmntent(mfd)) {
728 /* If we're going to always attach this partition, do it later. */
729 if (VIsAlwaysAttach(mntent->mnt_dir))
732 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0 )
737 /* Process the always-attach partitions, if any. */
738 VAttachPartitions2();
742 #endif /* AFS_LINUX22_ENV */
744 /* This routine is to be called whenever the actual name of the partition
745 * is required. The canonical name is still in part->name.
747 char * VPartitionPath(struct DiskPartition *part)
750 return part->devName;
756 /* get partition structure, abortp tells us if we should abort on failure */
757 struct DiskPartition *VGetPartition_r(char *name, int abortp)
759 register struct DiskPartition *dp;
760 for (dp = DiskPartitionList; dp; dp = dp->next) {
761 if (strcmp(dp->name, name) == 0)
769 struct DiskPartition *VGetPartition(char *name, int abortp)
771 struct DiskPartition *retVal;
773 retVal = VGetPartition_r(name, abortp);
779 void VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
781 ULARGE_INTEGER free_user, total, free_total;
782 int ufree, tot, tfree;
784 if (!GetDiskFreeSpaceEx(VPartitionPath(dp), &free_user, &total,
786 printf("Failed to get disk space info for %s, error = %d\n",
787 dp->name, GetLastError());
791 /* Convert to 1K units. */
792 ufree = (int) Int64ShraMod32(free_user.QuadPart, 10);
793 tot = (int) Int64ShraMod32(total.QuadPart, 10);
794 tfree = (int) Int64ShraMod32(free_total.QuadPart, 10);
796 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
797 dp->totalUsable = tot;
802 void VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
805 int fd, totalblks, free, used, availblks, bsize, code;
808 struct statvfs statbuf;
810 struct statfs statbuf;
813 if (dp->flags & PART_DONTUPDATE)
815 /* Note: we don't bother syncing because it's only an estimate, update
816 is syncing every 30 seconds anyway, we only have to keep the disk
817 approximately 10% from full--you just can't get the stuff in from
818 the net fast enough to worry */
820 code = statvfs(dp->name, &statbuf);
822 code = statfs(dp->name, &statbuf);
825 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n", dp->name, errno);
828 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
829 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
832 totalblks = statbuf.f_blocks;
833 free = statbuf.f_bfree;
834 reserved = free - statbuf.f_bavail;
836 bsize = statbuf.f_frsize;
838 bsize = statbuf.f_bsize;
840 availblks = totalblks - reserved;
841 dp->f_files = statbuf.f_files; /* max # of files in partition */
843 /* Now free and totalblks are in fragment units, but we want them in
847 free *= (bsize/1024);
848 totalblks *= (bsize / 1024);
849 availblks *= (bsize / 1024 );
850 reserved *= (bsize / 1024 );
853 free /= (1024/bsize);
854 totalblks /= (1024/bsize);
855 availblks /= (1024/bsize);
856 reserved /= (1024/bsize);
858 /* now compute remaining figures */
859 used = totalblks - free;
861 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
862 dp->totalUsable = availblks;
863 dp->free = availblks - used; /* this is exactly f_bavail */
865 #endif /* AFS_NT40_ENV */
867 void VSetPartitionDiskUsage(register struct DiskPartition *dp)
870 VSetPartitionDiskUsage_r(dp);
874 void VResetDiskUsage_r(void)
876 struct DiskPartition *dp;
877 for (dp = DiskPartitionList; dp; dp = dp->next) {
878 VSetPartitionDiskUsage_r(dp);
879 #ifndef AFS_PTHREAD_ENV
881 #endif /* !AFS_PTHREAD_ENV */
885 void VResetDiskUsage(void)
892 void VAdjustDiskUsage_r(Error *ec, Volume *vp, afs_int32 blocks, afs_int32 checkBlocks)
895 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
896 for less than BlocksSpare would skip the error-checking path, and we
897 could grow existing files forever, not just for another BlocksSpare
901 afs_int32 rem, minavail;
903 if ((rem = vp->partition->free - checkBlocks) <
904 (minavail = (vp->partition->totalUsable * aixlow_water) / 100))
906 if (vp->partition->free - checkBlocks < 0)
909 else if (V_maxquota(vp) && V_diskused(vp) + checkBlocks > V_maxquota(vp))
912 vp->partition->free -= blocks;
913 V_diskused(vp) += blocks;
916 void VAdjustDiskUsage(Error *ec, Volume *vp, afs_int32 blocks, afs_int32 checkBlocks)
919 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
923 int VDiskUsage_r(Volume *vp, afs_int32 blocks)
927 afs_int32 rem, minavail;
929 if ((rem = vp->partition->free - blocks) <
930 (minavail = (vp->partition->totalUsable * aixlow_water) / 100))
932 if (vp->partition->free - blocks < 0)
936 vp->partition->free -= blocks;
940 int VDiskUsage(Volume *vp, afs_int32 blocks)
944 retVal = VDiskUsage_r(vp, blocks);
949 void VPrintDiskStats_r(void)
951 struct DiskPartition *dp;
952 for (dp = DiskPartitionList; dp; dp = dp->next) {
953 Log("Partition %s: %d available 1K blocks (minfree=%d), ",
954 dp->name, dp->totalUsable, dp->minFree);
956 Log("overallocated by %d blocks\n", -dp->free);
958 Log("%d free blocks\n", dp->free);
962 void VPrintDiskStats(void)
970 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
971 #define LOCKFILE "LOCKFILE"
972 void VLockPartition_r(char *name)
974 struct DiskPartition *dp = VGetPartition_r(name, 0);
978 if (dp->lock_fd == -1) {
981 (void) sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
982 dp->lock_fd = (int)CreateFile(path, GENERIC_WRITE,
983 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
984 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
985 assert (dp->lock_fd != (int)INVALID_HANDLE_VALUE);
987 memset((char*)&lap, 0, sizeof(lap));
988 rc = LockFileEx((HANDLE)dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK,
994 void VUnlockPartition_r(char *name)
996 register struct DiskPartition *dp = VGetPartition_r(name, 0);
999 if (!dp) return; /* no partition, will fail later */
1000 memset((char*)&lap, 0, sizeof(lap));
1002 UnlockFileEx((HANDLE)dp->lock_fd, 0, 1, 0, &lap);
1003 CloseHandle((HANDLE)dp->lock_fd);
1006 #else /* AFS_NT40_ENV */
1008 #if defined(AFS_HPUX_ENV)
1009 #define BITS_PER_CHAR (8)
1010 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1012 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1013 #endif /* defined(AFS_HPUX_ENV) */
1015 void VLockPartition_r(char *name)
1017 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1018 char *partitionName;
1020 struct timeval pausing;
1021 #if defined(AFS_HPUX_ENV)
1023 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1024 unsigned int *globalMask;
1025 int globalMaskIndex;
1026 #endif /* defined(AFS_HPUX_ENV) */
1027 #ifdef AFS_NAMEI_ENV
1028 #ifdef AFS_AIX42_ENV
1029 char LockFileName[MAXPATHLEN + 1];
1031 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1032 partitionName = (char *) &LockFileName;
1036 if (!dp) return; /* no partition, will fail later */
1037 if (dp->lock_fd != -1) return;
1039 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1040 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1041 partitionName = dp->devName;
1045 partitionName = dp->name;
1049 for (retries=25; retries; retries--) {
1050 dp->lock_fd = open(partitionName, code);
1051 if (dp->lock_fd != -1) break;
1052 if (errno == ENOENT)
1055 pausing.tv_usec = 500000;
1056 select(0, NULL, NULL, NULL, &pausing);
1058 assert(retries != 0);
1060 #if defined (AFS_HPUX_ENV)
1062 assert(getprivgrp(privGrpList) == 0);
1065 * In general, it will difficult and time-consuming ,if not impossible,
1066 * to try to find the privgroup to which this process belongs that has the
1067 * smallest membership, to minimise the security hole. So, we use the privgrp
1068 * to which everybody belongs.
1070 /* first, we have to find the global mask */
1071 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1072 globalMaskIndex++) {
1073 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1074 globalMask = &(privGrpList[globalMaskIndex].
1075 priv_mask[LOCKRDONLY_OFFSET]);
1080 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1081 /* allow everybody to set a lock on a read-only file descriptor */
1082 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1083 assert(setprivgrp(PRIV_GLOBAL,
1084 privGrpList[globalMaskIndex].priv_mask) == 0);
1086 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1088 /* remove the privilege granted to everybody to lock a read-only fd */
1089 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1090 assert(setprivgrp(PRIV_GLOBAL,
1091 privGrpList[globalMaskIndex].priv_mask) == 0);
1094 /* in this case, we should be able to do this with impunity, anyway */
1095 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1098 assert (lockfRtn != -1);
1100 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1101 assert (lockf(dp->lock_fd, F_LOCK, 0) != -1);
1103 assert (flock(dp->lock_fd, LOCK_EX) == 0);
1104 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1108 void VUnlockPartition_r(char *name)
1110 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1111 if (!dp) return; /* no partition, will fail later */
1116 #endif /* AFS_NT40_ENV */
1118 void VLockPartition(char *name)
1121 VLockPartition_r(name);
1125 void VUnlockPartition(char *name)
1128 VUnlockPartition_r(name);