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>
106 #ifdef HAVE_STRINGS_H
113 #include <afs/afsint.h>
115 #include <afs/errors.h>
118 #include <afs/afssyscalls.h>
124 #include "namei_ops.h"
126 #endif /* AFS_NT40_ENV */
127 #endif /* AFS_NAMEI_ENV */
130 #include "partition.h"
131 #ifdef AFS_PTHREAD_ENV
133 #else /* AFS_PTHREAD_ENV */
134 #include <afs/assert.h>
135 #endif /* AFS_PTHREAD_ENV */
137 #if defined(AFS_HPUX_ENV)
138 #include <sys/types.h>
139 #include <sys/privgrp.h>
140 #endif /* defined(AFS_HPUX_ENV) */
143 #include <jfs/filsys.h>
146 int aixlow_water = 8; /* default 8% */
147 struct DiskPartition *DiskPartitionList;
149 #ifdef AFS_SGI_XFS_IOPS_ENV
150 /* Verify that the on disk XFS inodes on the partition are large enough to
151 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
152 * too small to fit in the inode. Returns 0 if the attribute does fit in
155 #include <afs/xfsattrs.h>
156 static int VerifyXFSInodeSize(char *part, char *fstype)
159 int length = SIZEOF_XFS_ATTR_T;
164 if (strcmp("xfs", fstype))
167 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
168 if (((fd=open(part, O_RDONLY, 0)) != -1)
169 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
171 if (fsx.fsx_nextents) {
172 Log("Partition %s: XFS inodes too small, exiting.\n", part);
173 Log("Run xfs_size_check utility and remake partitions.\n");
181 (void) attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
188 static void VInitPartition_r(char *path, char *devname, Device dev)
190 struct DiskPartition *dp, *op;
191 dp = (struct DiskPartition *) malloc(sizeof (struct DiskPartition));
192 /* Add it to the end, to preserve order when we print statistics */
193 for (op = DiskPartitionList; op; op = op->next) {
200 DiskPartitionList = dp;
202 dp->name = (char *)malloc(strlen(path) + 1);
203 strncpy(dp->name, path, strlen(path) + 1);
204 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
205 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
206 dp->devName = (char *)malloc(2 * strlen(path) + 6);
207 strcpy(dp->devName, path);
208 strcat(dp->devName, "/");
209 strcat(dp->devName, "Lock");
210 mkdir(dp->devName, 0700);
211 strcat(dp->devName, path);
212 close(open(dp->devName, O_RDWR | O_CREAT, 0600));
213 dp->device = volutil_GetPartitionID(path);
215 dp->devName = (char *)malloc(strlen(devname) + 1);
216 strncpy(dp->devName, devname, strlen(devname) + 1);
221 dp->f_files = 1; /* just a default value */
222 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
223 if (programType == fileServer)
224 (void) namei_ViceREADME(VPartitionPath(dp));
226 VSetPartitionDiskUsage_r(dp);
229 static void VInitPartition(char *path, char *devname, Device dev)
232 VInitPartition_r(path, devname, dev);
237 /* VAttachPartitions() finds the vice partitions on this server. Calls
238 * VCheckPartition() to do some basic checks on the partition. If the partition
239 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
241 * Returns the number of errors returned by VCheckPartition. An error in
242 * VCheckPartition means that partition is a valid vice partition but the
243 * fileserver should not start because of the error found on that partition.
246 * No specific user space file system checks, since we don't know what
247 * is being used for vice partitions.
249 * Use partition name as devname.
251 int VCheckPartition(part, devname)
256 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
257 char AFSIDatPath[MAXPATHLEN];
260 /* Only keep track of "/vicepx" partitions since it can get hairy
261 * when NFS mounts are involved.. */
262 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
265 if (stat(part, &status) < 0) {
266 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
270 #ifndef AFS_AIX32_ENV
271 if (programType == fileServer) {
272 char salvpath[MAXPATHLEN];
273 strcpy(salvpath, part);
274 strcat(salvpath, "/FORCESALVAGE");
275 if (stat(salvpath, &status) == 0) {
276 Log("VInitVnodes: Found %s; aborting\n", salvpath);
282 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
283 strcpy(AFSIDatPath, part);
284 strcat(AFSIDatPath, "/AFSIDat");
286 if (stat(AFSIDatPath, &status) < 0) {
290 dirp = opendir(part);
292 while (dp = readdir(dirp)) {
293 if (dp->d_name[0] == 'V') {
294 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);
301 #else /* AFS_NAMEI_ENV */
302 if (stat(AFSIDatPath, &status) == 0) {
303 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
306 #endif /* AFS_NAMEI_ENV */
309 #ifdef AFS_SGI_XFS_IOPS_ENV
310 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
314 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
315 if (status.st_ino != ROOTINO) {
316 Log("%s is not a mounted file system; ignored.\n", part);
321 VInitPartition(part, devname, status.st_dev);
326 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
327 * attached (return value 1), or only attached when it is a separately
328 * mounted partition (return value 0). For non-NAMEI environments, it
331 static int VIsAlwaysAttach(part)
339 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
342 strncpy(checkfile, part, 100);
343 strcat(checkfile, "/");
344 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
346 ret = stat(checkfile, &st);
347 return (ret < 0) ? 0 : 1;
348 #else /* AFS_NAMEI_ENV */
350 #endif /* AFS_NAMEI_ENV */
353 /* VAttachPartitions2() looks for and attaches /vicepX partitions
354 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
355 * used to attach /vicepX directories which aren't on dedicated
356 * partitions, in the NAMEI fileserver.
358 void VAttachPartitions2() {
365 while ((de = readdir(dirp))) {
367 strncat(pname, de->d_name, 20);
368 pname[sizeof(pname)-1] = '\0';
370 /* Only keep track of "/vicepx" partitions since automounter
372 if (VIsAlwaysAttach(pname))
373 VCheckPartition(pname, "");
376 #endif /* AFS_NAMEI_ENV */
378 #endif /* AFS_NT40_ENV */
381 int VAttachPartitions(void)
387 if (!(mntfile = fopen(MNTTAB, "r"))) {
388 Log("Can't open %s\n", MNTTAB);
392 while (!getmntent(mntfile, &mnt)) {
393 /* Ignore non ufs or non read/write partitions */
394 if ((strcmp(mnt.mnt_fstype, "ufs") !=0) ||
395 (strncmp(mnt.mnt_mntopts, "ro,ignore",9) ==0))
398 /* If we're going to always attach this partition, do it later. */
399 if (VIsAlwaysAttach(mnt.mnt_mountp))
402 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0 )
406 (void) fclose(mntfile);
408 /* Process the always-attach partitions, if any. */
409 VAttachPartitions2();
414 #endif /* AFS_SUN5_ENV */
415 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
416 int VAttachPartitions(void)
420 struct mntent *mntent;
422 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
423 Log("Problems in getting mount entries(setmntent)\n");
426 while (mntent = getmntent(mfd)) {
427 if (!hasmntopt(mntent, MNTOPT_RW)) continue;
429 /* If we're going to always attach this partition, do it later. */
430 if (VIsAlwaysAttach(mntent->mnt_dir))
433 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0 )
439 /* Process the always-attach partitions, if any. */
440 VAttachPartitions2();
447 * (This function was grabbed from df.c)
451 register struct vmount **vmountpp; /* place to tell where buffer is */
454 register struct vmount *vm;
457 /* set initial size of mntctl buffer to a MAGIC NUMBER */
460 /* try the operation until ok or a fatal error */
462 if ((vm = (struct vmount *)malloc(size)) == NULL) {
463 /* failed getting memory for mount status buf */
464 perror("FATAL ERROR: get_stat malloc failed\n");
469 * perform the QUERY mntctl - if it returns > 0, that is the
470 * number of vmount structures in the buffer. If it returns
471 * -1, an error occured. If it returned 0, then look in
472 * first word of buffer for needed size.
474 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t)vm)) > 0) {
475 /* OK, got it, now return */
479 } else if (nmounts == 0) {
480 /* the buffer wasn't big enough .... */
481 /* .... get required buffer size */
486 /* some other kind of error occurred */
493 int VAttachPartitions(void)
497 struct vmount *vmountp;
499 if ((nmounts = getmount(&vmountp)) <= 0) {
500 Log("Problems in getting # of mount entries(getmount)\n");
503 for (; nmounts; nmounts--,
504 vmountp = (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
505 char *part = vmt2dataptr(vmountp, VMT_STUB);
507 if (vmountp->vmt_flags & (MNT_READONLY|MNT_REMOVABLE|MNT_REMOTE))
508 continue; /* Ignore any "special" partitions */
511 #ifndef AFS_NAMEI_ENV
513 struct superblock fs;
514 /* The Log statements are non-sequiters in the SalvageLog and don't
515 * even appear in the VolserLog, so restrict them to the FileLog.
517 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT))<0) {
518 if (programType == fileServer)
519 Log("Can't read superblock for %s, ignoring it.\n", part);
522 if (IsBigFilesFileSystem(&fs)) {
523 if (programType == fileServer)
524 Log("%s is a big files filesystem, ignoring it.\n", part);
531 /* If we're going to always attach this partition, do it later. */
532 if (VIsAlwaysAttach(part))
535 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0 )
539 /* Process the always-attach partitions, if any. */
540 VAttachPartitions2();
545 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
546 int VAttachPartitions(void)
551 if (setfsent() < 0) {
552 Log("Error listing filesystems.\n");
556 while (fsent = getfsent()) {
557 if (strcmp(fsent->fs_type, "rw") != 0) continue;
559 /* If we're going to always attach this partition, do it later. */
560 if (VIsAlwaysAttach(fsent->fs_file))
563 if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0 )
568 /* Process the always-attach partitions, if any. */
569 VAttachPartitions2();
577 #include <sys/stat.h>
580 * validate names in vptab.
587 int VValidVPTEntry(struct vptab *vpe)
589 int len = strlen(vpe->vp_name);
592 if (len < VICE_PREFIX_SIZE+1 || len > VICE_PREFIX_SIZE + 2)
594 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
597 for (i=VICE_PREFIX_SIZE; i<len; i++) {
598 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
599 Log("Invalid partition name %s in registry, ignoring it.\n",
604 if (len == VICE_PREFIX_SIZE + 2) {
605 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE]-'a') * 26 +
606 (int)(vpe->vp_name[VICE_PREFIX_SIZE+1]-'a') ;
608 Log("Invalid partition name %s in registry, ignoring it.\n",
614 len = strlen(vpe->vp_dev);
615 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A' ||
616 vpe->vp_dev[0] > 'Z') {
617 Log("Invalid device name %s in registry, ignoring it.\n",
625 int VCheckPartition(char *partName)
632 /* partName is presumed to be of the form "X:" */
633 (void) sprintf(volRoot, "%c:\\", *partName);
635 if (!GetVolumeInformation(volRoot, /* volume root directory */
636 NULL, /* volume name buffer */
637 0, /* volume name size */
638 NULL, /* volume serial number */
639 &dwDummy, /* max component length */
640 &dwDummy, /* file system flags */
641 volFsType, /* file system name */
642 sizeof(volFsType))) {
643 err = GetLastError();
644 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n",
649 if (strcmp(volFsType, "NTFS")) {
650 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
658 int VAttachPartitions(void)
660 struct DiskPartition *partP, *prevP, *nextP;
661 struct vpt_iter iter;
664 if (vpt_Start(&iter)<0) {
665 Log("No partitions to attach.\n");
669 while (0==vpt_NextEntry(&iter, &entry)) {
670 if (!VValidVPTEntry(&entry)) {
674 /* This test for duplicates relies on the fact that the method
675 * of storing the partition names in the NT registry means the same
676 * partition name will never appear twice in the list.
678 for (partP = DiskPartitionList; partP; partP = partP->next) {
679 if (*partP->devName == *entry.vp_dev) {
680 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
681 partP->flags = PART_DUPLICATE;
682 break; /* Only one entry will ever be in this list. */
685 if (partP) continue; /* found a duplicate */
687 if (VCheckPartition(entry.vp_dev)<0)
689 /* This test allows for manually inserting the FORCESALVAGE flag
690 * and thereby invoking the salvager. scandisk obviously won't be
693 if (programType == fileServer) {
695 char salvpath[MAXPATHLEN];
696 strcpy(salvpath, entry.vp_dev);
697 strcat(salvpath, "\\FORCESALVAGE");
698 if (stat(salvpath, &status) == 0) {
699 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
703 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
707 /* Run through partition list and clear out the dupes. */
708 prevP = nextP = NULL;
709 for (partP = DiskPartitionList; partP; partP = nextP) {
711 if (partP->flags == PART_DUPLICATE) {
713 prevP->next = partP->next;
715 DiskPartitionList = partP->next;
726 #ifdef AFS_LINUX22_ENV
727 int VAttachPartitions(void)
731 struct mntent *mntent;
733 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
734 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
735 Log("Problems in getting mount entries(setmntent)\n");
739 while ((mntent = getmntent(mfd))) {
740 /* If we're going to always attach this partition, do it later. */
741 if (VIsAlwaysAttach(mntent->mnt_dir))
744 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0 )
749 /* Process the always-attach partitions, if any. */
750 VAttachPartitions2();
754 #endif /* AFS_LINUX22_ENV */
756 /* This routine is to be called whenever the actual name of the partition
757 * is required. The canonical name is still in part->name.
759 char * VPartitionPath(struct DiskPartition *part)
762 return part->devName;
768 /* get partition structure, abortp tells us if we should abort on failure */
769 struct DiskPartition *VGetPartition_r(char *name, int abortp)
771 register struct DiskPartition *dp;
772 for (dp = DiskPartitionList; dp; dp = dp->next) {
773 if (strcmp(dp->name, name) == 0)
781 struct DiskPartition *VGetPartition(char *name, int abortp)
783 struct DiskPartition *retVal;
785 retVal = VGetPartition_r(name, abortp);
791 void VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
793 ULARGE_INTEGER free_user, total, free_total;
794 int ufree, tot, tfree;
796 if (!GetDiskFreeSpaceEx(VPartitionPath(dp), &free_user, &total,
798 printf("Failed to get disk space info for %s, error = %d\n",
799 dp->name, GetLastError());
803 /* Convert to 1K units. */
804 ufree = (int) Int64ShraMod32(free_user.QuadPart, 10);
805 tot = (int) Int64ShraMod32(total.QuadPart, 10);
806 tfree = (int) Int64ShraMod32(free_total.QuadPart, 10);
808 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
809 dp->totalUsable = tot;
814 void VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
817 int fd, totalblks, free, used, availblks, bsize, code;
820 struct statvfs statbuf;
822 struct statfs statbuf;
825 if (dp->flags & PART_DONTUPDATE)
827 /* Note: we don't bother syncing because it's only an estimate, update
828 is syncing every 30 seconds anyway, we only have to keep the disk
829 approximately 10% from full--you just can't get the stuff in from
830 the net fast enough to worry */
832 code = statvfs(dp->name, &statbuf);
834 code = statfs(dp->name, &statbuf);
837 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n", dp->name, errno);
840 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
841 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
844 totalblks = statbuf.f_blocks;
845 free = statbuf.f_bfree;
846 reserved = free - statbuf.f_bavail;
848 bsize = statbuf.f_frsize;
850 bsize = statbuf.f_bsize;
852 availblks = totalblks - reserved;
853 dp->f_files = statbuf.f_files; /* max # of files in partition */
855 /* Now free and totalblks are in fragment units, but we want them in
859 free *= (bsize/1024);
860 totalblks *= (bsize / 1024);
861 availblks *= (bsize / 1024 );
862 reserved *= (bsize / 1024 );
865 free /= (1024/bsize);
866 totalblks /= (1024/bsize);
867 availblks /= (1024/bsize);
868 reserved /= (1024/bsize);
870 /* now compute remaining figures */
871 used = totalblks - free;
873 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
874 dp->totalUsable = availblks;
875 dp->free = availblks - used; /* this is exactly f_bavail */
877 #endif /* AFS_NT40_ENV */
879 void VSetPartitionDiskUsage(register struct DiskPartition *dp)
882 VSetPartitionDiskUsage_r(dp);
886 void VResetDiskUsage_r(void)
888 struct DiskPartition *dp;
889 for (dp = DiskPartitionList; dp; dp = dp->next) {
890 VSetPartitionDiskUsage_r(dp);
891 #ifndef AFS_PTHREAD_ENV
893 #endif /* !AFS_PTHREAD_ENV */
897 void VResetDiskUsage(void)
904 void VAdjustDiskUsage_r(Error *ec, Volume *vp, afs_int32 blocks, afs_int32 checkBlocks)
907 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
908 for less than BlocksSpare would skip the error-checking path, and we
909 could grow existing files forever, not just for another BlocksSpare
913 afs_int32 rem, minavail;
915 if ((rem = vp->partition->free - checkBlocks) <
916 (minavail = (vp->partition->totalUsable * aixlow_water) / 100))
918 if (vp->partition->free - checkBlocks < 0)
921 else if (V_maxquota(vp) && V_diskused(vp) + checkBlocks > V_maxquota(vp))
924 vp->partition->free -= blocks;
925 V_diskused(vp) += blocks;
928 void VAdjustDiskUsage(Error *ec, Volume *vp, afs_int32 blocks, afs_int32 checkBlocks)
931 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
935 int VDiskUsage_r(Volume *vp, afs_int32 blocks)
939 afs_int32 rem, minavail;
941 if ((rem = vp->partition->free - blocks) <
942 (minavail = (vp->partition->totalUsable * aixlow_water) / 100))
944 if (vp->partition->free - blocks < 0)
948 vp->partition->free -= blocks;
952 int VDiskUsage(Volume *vp, afs_int32 blocks)
956 retVal = VDiskUsage_r(vp, blocks);
961 void VPrintDiskStats_r(void)
963 struct DiskPartition *dp;
964 for (dp = DiskPartitionList; dp; dp = dp->next) {
965 Log("Partition %s: %d available 1K blocks (minfree=%d), ",
966 dp->name, dp->totalUsable, dp->minFree);
968 Log("overallocated by %d blocks\n", -dp->free);
970 Log("%d free blocks\n", dp->free);
974 void VPrintDiskStats(void)
982 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
983 #define LOCKFILE "LOCKFILE"
984 void VLockPartition_r(char *name)
986 struct DiskPartition *dp = VGetPartition_r(name, 0);
990 if (dp->lock_fd == -1) {
993 (void) sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
994 dp->lock_fd = (int)CreateFile(path, GENERIC_WRITE,
995 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
996 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
997 assert (dp->lock_fd != (int)INVALID_HANDLE_VALUE);
999 memset((char*)&lap, 0, sizeof(lap));
1000 rc = LockFileEx((HANDLE)dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK,
1006 void VUnlockPartition_r(char *name)
1008 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1011 if (!dp) return; /* no partition, will fail later */
1012 memset((char*)&lap, 0, sizeof(lap));
1014 UnlockFileEx((HANDLE)dp->lock_fd, 0, 1, 0, &lap);
1015 CloseHandle((HANDLE)dp->lock_fd);
1018 #else /* AFS_NT40_ENV */
1020 #if defined(AFS_HPUX_ENV)
1021 #define BITS_PER_CHAR (8)
1022 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1024 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1025 #endif /* defined(AFS_HPUX_ENV) */
1027 void VLockPartition_r(char *name)
1029 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1030 char *partitionName;
1032 struct timeval pausing;
1033 #if defined(AFS_HPUX_ENV)
1035 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1036 unsigned int *globalMask;
1037 int globalMaskIndex;
1038 #endif /* defined(AFS_HPUX_ENV) */
1039 #ifdef AFS_NAMEI_ENV
1040 #ifdef AFS_AIX42_ENV
1041 char LockFileName[MAXPATHLEN + 1];
1043 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1044 partitionName = (char *) &LockFileName;
1048 if (!dp) return; /* no partition, will fail later */
1049 if (dp->lock_fd != -1) return;
1051 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1052 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1053 partitionName = dp->devName;
1057 partitionName = dp->name;
1061 for (retries=25; retries; retries--) {
1062 dp->lock_fd = open(partitionName, code);
1063 if (dp->lock_fd != -1) break;
1064 if (errno == ENOENT)
1067 pausing.tv_usec = 500000;
1068 select(0, NULL, NULL, NULL, &pausing);
1070 assert(retries != 0);
1072 #if defined (AFS_HPUX_ENV)
1074 assert(getprivgrp(privGrpList) == 0);
1077 * In general, it will difficult and time-consuming ,if not impossible,
1078 * to try to find the privgroup to which this process belongs that has the
1079 * smallest membership, to minimise the security hole. So, we use the privgrp
1080 * to which everybody belongs.
1082 /* first, we have to find the global mask */
1083 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1084 globalMaskIndex++) {
1085 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1086 globalMask = &(privGrpList[globalMaskIndex].
1087 priv_mask[LOCKRDONLY_OFFSET]);
1092 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1093 /* allow everybody to set a lock on a read-only file descriptor */
1094 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1095 assert(setprivgrp(PRIV_GLOBAL,
1096 privGrpList[globalMaskIndex].priv_mask) == 0);
1098 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1100 /* remove the privilege granted to everybody to lock a read-only fd */
1101 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1102 assert(setprivgrp(PRIV_GLOBAL,
1103 privGrpList[globalMaskIndex].priv_mask) == 0);
1106 /* in this case, we should be able to do this with impunity, anyway */
1107 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1110 assert (lockfRtn != -1);
1112 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1113 assert (lockf(dp->lock_fd, F_LOCK, 0) != -1);
1115 assert (flock(dp->lock_fd, LOCK_EX) == 0);
1116 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1120 void VUnlockPartition_r(char *name)
1122 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1123 if (!dp) return; /* no partition, will fail later */
1128 #endif /* AFS_NT40_ENV */
1130 void VLockPartition(char *name)
1133 VLockPartition_r(name);
1137 void VUnlockPartition(char *name)
1140 VUnlockPartition_r(name);