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 dp->name = (char *)malloc(strlen(path) + 1);
194 strncpy(dp->name, path, strlen(path) + 1);
195 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
196 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
197 dp->devName = (char *)malloc(2 * strlen(path) + 6);
198 strcpy(dp->devName, path);
199 strcat(dp->devName, "/");
200 strcat(dp->devName, "Lock");
201 mkdir(dp->devName, 0700);
202 strcat(dp->devName, path);
203 close(open(dp->devName, O_RDWR | O_CREAT, 0600));
204 dp->device = volutil_GetPartitionID(path);
206 dp->devName = (char *)malloc(strlen(devname) + 1);
207 strncpy(dp->devName, devname, strlen(devname) + 1);
212 dp->f_files = 1; /* just a default value */
213 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
214 if (programType == fileServer)
215 (void) namei_ViceREADME(VPartitionPath(dp));
217 VSetPartitionDiskUsage_r(dp);
220 static void VInitPartition(char *path, char *devname, Device dev)
223 VInitPartition_r(path, devname, dev);
228 /* VAttachPartitions() finds the vice partitions on this server. Calls
229 * VCheckPartition() to do some basic checks on the partition. If the partition
230 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
232 * Returns the number of errors returned by VCheckPartition. An error in
233 * VCheckPartition means that partition is a valid vice partition but the
234 * fileserver should not start because of the error found on that partition.
237 * No specific user space file system checks, since we don't know what
238 * is being used for vice partitions.
240 * Use partition name as devname.
242 int VCheckPartition(part, devname)
247 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
248 char AFSIDatPath[MAXPATHLEN];
251 /* Only keep track of "/vicepx" partitions since it can get hairy
252 * when NFS mounts are involved.. */
253 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
256 if (stat(part, &status) < 0) {
257 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
261 #ifndef AFS_AIX32_ENV
262 if (programType == fileServer) {
263 char salvpath[MAXPATHLEN];
264 strcpy(salvpath, part);
265 strcat(salvpath, "/FORCESALVAGE");
266 if (stat(salvpath, &status) == 0) {
267 Log("VInitVnodes: Found %s; aborting\n", salvpath);
273 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
274 strcpy(AFSIDatPath, part);
275 strcat(AFSIDatPath, "/AFSIDat");
277 if (stat(AFSIDatPath, &status) < 0) {
281 dirp = opendir(part);
283 while (dp = readdir(dirp)) {
284 if (dp->d_name[0] == 'V') {
285 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);
292 #else /* AFS_NAMEI_ENV */
293 if (stat(AFSIDatPath, &status) == 0) {
294 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
297 #endif /* AFS_NAMEI_ENV */
300 #ifdef AFS_SGI_XFS_IOPS_ENV
301 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
305 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
306 if (status.st_ino != ROOTINO) {
307 Log("%s is not a mounted file system; ignored.\n", part);
312 VInitPartition(part, devname, status.st_dev);
317 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
318 * attached (return value 1), or only attached when it is a separately
319 * mounted partition (return value 0). For non-NAMEI environments, it
322 static int VIsAlwaysAttach(part)
330 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
333 strncpy(checkfile, part, 100);
334 strcat(checkfile, "/");
335 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
337 ret = stat(checkfile, &st);
338 return (ret < 0) ? 0 : 1;
339 #else /* AFS_NAMEI_ENV */
341 #endif /* AFS_NAMEI_ENV */
344 /* VAttachPartitions2() looks for and attaches /vicepX partitions
345 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
346 * used to attach /vicepX directories which aren't on dedicated
347 * partitions, in the NAMEI fileserver.
349 void VAttachPartitions2() {
356 while (de = readdir(dirp)) {
358 strncat(pname, de->d_name, 20);
359 pname[sizeof(pname)-1] = '\0';
361 /* Only keep track of "/vicepx" partitions since automounter
363 if (VIsAlwaysAttach(pname))
364 VCheckPartition(pname, "");
367 #endif /* AFS_NAMEI_ENV */
369 #endif /* AFS_NT40_ENV */
372 int VAttachPartitions(void)
378 if (!(mntfile = fopen(MNTTAB, "r"))) {
379 Log("Can't open %s\n", MNTTAB);
383 while (!getmntent(mntfile, &mnt)) {
384 /* Ignore non ufs or non read/write partitions */
385 if ((strcmp(mnt.mnt_fstype, "ufs") !=0) ||
386 (strncmp(mnt.mnt_mntopts, "ro,ignore",9) ==0))
389 /* If we're going to always attach this partition, do it later. */
390 if (VIsAlwaysAttach(mnt.mnt_mountp))
393 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0 )
397 (void) fclose(mntfile);
399 /* Process the always-attach partitions, if any. */
400 VAttachPartitions2();
405 #endif /* AFS_SUN5_ENV */
406 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
407 int VAttachPartitions(void)
411 struct mntent *mntent;
413 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
414 Log("Problems in getting mount entries(setmntent)\n");
417 while (mntent = getmntent(mfd)) {
418 if (!hasmntopt(mntent, MNTOPT_RW)) continue;
420 /* If we're going to always attach this partition, do it later. */
421 if (VIsAlwaysAttach(mntent->mnt_dir))
424 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0 )
430 /* Process the always-attach partitions, if any. */
431 VAttachPartitions2();
438 * (This function was grabbed from df.c)
442 register struct vmount **vmountpp; /* place to tell where buffer is */
445 register struct vmount *vm;
448 /* set initial size of mntctl buffer to a MAGIC NUMBER */
451 /* try the operation until ok or a fatal error */
453 if ((vm = (struct vmount *)malloc(size)) == NULL) {
454 /* failed getting memory for mount status buf */
455 perror("FATAL ERROR: get_stat malloc failed\n");
460 * perform the QUERY mntctl - if it returns > 0, that is the
461 * number of vmount structures in the buffer. If it returns
462 * -1, an error occured. If it returned 0, then look in
463 * first word of buffer for needed size.
465 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t)vm)) > 0) {
466 /* OK, got it, now return */
470 } else if (nmounts == 0) {
471 /* the buffer wasn't big enough .... */
472 /* .... get required buffer size */
477 /* some other kind of error occurred */
484 int VAttachPartitions(void)
488 struct vmount *vmountp;
490 if ((nmounts = getmount(&vmountp)) <= 0) {
491 Log("Problems in getting # of mount entries(getmount)\n");
494 for (; nmounts; nmounts--,
495 vmountp = (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
496 char *part = vmt2dataptr(vmountp, VMT_STUB);
498 if (vmountp->vmt_flags & (MNT_READONLY|MNT_REMOVABLE|MNT_REMOTE))
499 continue; /* Ignore any "special" partitions */
502 #ifndef AFS_NAMEI_ENV
504 struct superblock fs;
505 /* The Log statements are non-sequiters in the SalvageLog and don't
506 * even appear in the VolserLog, so restrict them to the FileLog.
508 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT))<0) {
509 if (programType == fileServer)
510 Log("Can't read superblock for %s, ignoring it.\n", part);
513 if (IsBigFilesFileSystem(&fs)) {
514 if (programType == fileServer)
515 Log("%s is a big files filesystem, ignoring it.\n", part);
522 /* If we're going to always attach this partition, do it later. */
523 if (VIsAlwaysAttach(part))
526 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0 )
530 /* Process the always-attach partitions, if any. */
531 VAttachPartitions2();
536 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
537 int VAttachPartitions(void)
542 if (setfsent() < 0) {
543 Log("Error listing filesystems.\n");
547 while (fsent = getfsent()) {
548 if (strcmp(fsent->fs_type, "rw") != 0) continue;
550 /* If we're going to always attach this partition, do it later. */
551 if (VIsAlwaysAttach(fsent->fs_file))
554 if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0 )
559 /* Process the always-attach partitions, if any. */
560 VAttachPartitions2();
568 #include <sys/stat.h>
571 * validate names in vptab.
578 int VValidVPTEntry(struct vptab *vpe)
580 int len = strlen(vpe->vp_name);
583 if (len < VICE_PREFIX_SIZE+1 || len > VICE_PREFIX_SIZE + 2)
585 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
588 for (i=VICE_PREFIX_SIZE; i<len; i++) {
589 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
590 Log("Invalid partition name %s in registry, ignoring it.\n",
595 if (len == VICE_PREFIX_SIZE + 2) {
596 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE]-'a') * 26 +
597 (int)(vpe->vp_name[VICE_PREFIX_SIZE+1]-'a') ;
599 Log("Invalid partition name %s in registry, ignoring it.\n",
605 len = strlen(vpe->vp_dev);
606 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A' ||
607 vpe->vp_dev[0] > 'Z') {
608 Log("Invalid device name %s in registry, ignoring it.\n",
616 int VCheckPartition(char *partName)
623 /* partName is presumed to be of the form "X:" */
624 (void) sprintf(volRoot, "%c:\\", *partName);
626 if (!GetVolumeInformation(volRoot, /* volume root directory */
627 NULL, /* volume name buffer */
628 0, /* volume name size */
629 NULL, /* volume serial number */
630 &dwDummy, /* max component length */
631 &dwDummy, /* file system flags */
632 volFsType, /* file system name */
633 sizeof(volFsType))) {
634 err = GetLastError();
635 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n",
640 if (strcmp(volFsType, "NTFS")) {
641 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
649 int VAttachPartitions(void)
651 struct DiskPartition *partP, *prevP, *nextP;
652 struct vpt_iter iter;
655 if (vpt_Start(&iter)<0) {
656 Log("No partitions to attach.\n");
660 while (0==vpt_NextEntry(&iter, &entry)) {
661 if (!VValidVPTEntry(&entry)) {
665 /* This test for duplicates relies on the fact that the method
666 * of storing the partition names in the NT registry means the same
667 * partition name will never appear twice in the list.
669 for (partP = DiskPartitionList; partP; partP = partP->next) {
670 if (*partP->devName == *entry.vp_dev) {
671 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
672 partP->flags = PART_DUPLICATE;
673 break; /* Only one entry will ever be in this list. */
676 if (partP) continue; /* found a duplicate */
678 if (VCheckPartition(entry.vp_dev)<0)
680 /* This test allows for manually inserting the FORCESALVAGE flag
681 * and thereby invoking the salvager. scandisk obviously won't be
684 if (programType == fileServer) {
686 char salvpath[MAXPATHLEN];
687 strcpy(salvpath, entry.vp_dev);
688 strcat(salvpath, "\\FORCESALVAGE");
689 if (stat(salvpath, &status) == 0) {
690 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
694 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
698 /* Run through partition list and clear out the dupes. */
699 prevP = nextP = NULL;
700 for (partP = DiskPartitionList; partP; partP = nextP) {
702 if (partP->flags == PART_DUPLICATE) {
704 prevP->next = partP->next;
706 DiskPartitionList = partP->next;
717 #ifdef AFS_LINUX22_ENV
718 int VAttachPartitions(void)
722 struct mntent *mntent;
724 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
725 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
726 Log("Problems in getting mount entries(setmntent)\n");
730 while (mntent = getmntent(mfd)) {
731 /* If we're going to always attach this partition, do it later. */
732 if (VIsAlwaysAttach(mntent->mnt_dir))
735 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0 )
740 /* Process the always-attach partitions, if any. */
741 VAttachPartitions2();
745 #endif /* AFS_LINUX22_ENV */
747 /* This routine is to be called whenever the actual name of the partition
748 * is required. The canonical name is still in part->name.
750 char * VPartitionPath(struct DiskPartition *part)
753 return part->devName;
759 /* get partition structure, abortp tells us if we should abort on failure */
760 struct DiskPartition *VGetPartition_r(char *name, int abortp)
762 register struct DiskPartition *dp;
763 for (dp = DiskPartitionList; dp; dp = dp->next) {
764 if (strcmp(dp->name, name) == 0)
772 struct DiskPartition *VGetPartition(char *name, int abortp)
774 struct DiskPartition *retVal;
776 retVal = VGetPartition_r(name, abortp);
782 void VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
784 ULARGE_INTEGER free_user, total, free_total;
785 int ufree, tot, tfree;
787 if (!GetDiskFreeSpaceEx(VPartitionPath(dp), &free_user, &total,
789 printf("Failed to get disk space info for %s, error = %d\n",
790 dp->name, GetLastError());
794 /* Convert to 1K units. */
795 ufree = (int) Int64ShraMod32(free_user.QuadPart, 10);
796 tot = (int) Int64ShraMod32(total.QuadPart, 10);
797 tfree = (int) Int64ShraMod32(free_total.QuadPart, 10);
799 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
800 dp->totalUsable = tot;
805 void VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
808 int fd, totalblks, free, used, availblks, bsize, code;
811 struct statvfs statbuf;
813 struct statfs statbuf;
816 if (dp->flags & PART_DONTUPDATE)
818 /* Note: we don't bother syncing because it's only an estimate, update
819 is syncing every 30 seconds anyway, we only have to keep the disk
820 approximately 10% from full--you just can't get the stuff in from
821 the net fast enough to worry */
823 code = statvfs(dp->name, &statbuf);
825 code = statfs(dp->name, &statbuf);
828 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n", dp->name, errno);
831 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
832 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
835 totalblks = statbuf.f_blocks;
836 free = statbuf.f_bfree;
837 reserved = free - statbuf.f_bavail;
839 bsize = statbuf.f_frsize;
841 bsize = statbuf.f_bsize;
843 availblks = totalblks - reserved;
844 dp->f_files = statbuf.f_files; /* max # of files in partition */
846 /* Now free and totalblks are in fragment units, but we want them in
850 free *= (bsize/1024);
851 totalblks *= (bsize / 1024);
852 availblks *= (bsize / 1024 );
853 reserved *= (bsize / 1024 );
856 free /= (1024/bsize);
857 totalblks /= (1024/bsize);
858 availblks /= (1024/bsize);
859 reserved /= (1024/bsize);
861 /* now compute remaining figures */
862 used = totalblks - free;
864 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
865 dp->totalUsable = availblks;
866 dp->free = availblks - used; /* this is exactly f_bavail */
868 #endif /* AFS_NT40_ENV */
870 void VSetPartitionDiskUsage(register struct DiskPartition *dp)
873 VSetPartitionDiskUsage_r(dp);
877 void VResetDiskUsage_r(void)
879 struct DiskPartition *dp;
880 for (dp = DiskPartitionList; dp; dp = dp->next) {
881 VSetPartitionDiskUsage_r(dp);
882 #ifndef AFS_PTHREAD_ENV
884 #endif /* !AFS_PTHREAD_ENV */
888 void VResetDiskUsage(void)
895 void VAdjustDiskUsage_r(Error *ec, Volume *vp, afs_int32 blocks, afs_int32 checkBlocks)
898 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
899 for less than BlocksSpare would skip the error-checking path, and we
900 could grow existing files forever, not just for another BlocksSpare
904 afs_int32 rem, minavail;
906 if ((rem = vp->partition->free - checkBlocks) <
907 (minavail = (vp->partition->totalUsable * aixlow_water) / 100))
909 if (vp->partition->free - checkBlocks < 0)
912 else if (V_maxquota(vp) && V_diskused(vp) + checkBlocks > V_maxquota(vp))
915 vp->partition->free -= blocks;
916 V_diskused(vp) += blocks;
919 void VAdjustDiskUsage(Error *ec, Volume *vp, afs_int32 blocks, afs_int32 checkBlocks)
922 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
926 int VDiskUsage_r(Volume *vp, afs_int32 blocks)
930 afs_int32 rem, minavail;
932 if ((rem = vp->partition->free - blocks) <
933 (minavail = (vp->partition->totalUsable * aixlow_water) / 100))
935 if (vp->partition->free - blocks < 0)
939 vp->partition->free -= blocks;
943 int VDiskUsage(Volume *vp, afs_int32 blocks)
947 retVal = VDiskUsage_r(vp, blocks);
952 void VPrintDiskStats_r(void)
954 struct DiskPartition *dp;
955 for (dp = DiskPartitionList; dp; dp = dp->next) {
956 Log("Partition %s: %d available 1K blocks (minfree=%d), ",
957 dp->name, dp->totalUsable, dp->minFree);
959 Log("overallocated by %d blocks\n", -dp->free);
961 Log("%d free blocks\n", dp->free);
965 void VPrintDiskStats(void)
973 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
974 #define LOCKFILE "LOCKFILE"
975 void VLockPartition_r(char *name)
977 struct DiskPartition *dp = VGetPartition_r(name, 0);
981 if (dp->lock_fd == -1) {
984 (void) sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
985 dp->lock_fd = (int)CreateFile(path, GENERIC_WRITE,
986 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
987 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
988 assert (dp->lock_fd != (int)INVALID_HANDLE_VALUE);
990 memset((char*)&lap, 0, sizeof(lap));
991 rc = LockFileEx((HANDLE)dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK,
997 void VUnlockPartition_r(char *name)
999 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1002 if (!dp) return; /* no partition, will fail later */
1003 memset((char*)&lap, 0, sizeof(lap));
1005 UnlockFileEx((HANDLE)dp->lock_fd, 0, 1, 0, &lap);
1006 CloseHandle((HANDLE)dp->lock_fd);
1009 #else /* AFS_NT40_ENV */
1011 #if defined(AFS_HPUX_ENV)
1012 #define BITS_PER_CHAR (8)
1013 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1015 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1016 #endif /* defined(AFS_HPUX_ENV) */
1018 void VLockPartition_r(char *name)
1020 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1021 char *partitionName;
1023 struct timeval pausing;
1024 #if defined(AFS_HPUX_ENV)
1026 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1027 unsigned int *globalMask;
1028 int globalMaskIndex;
1029 #endif /* defined(AFS_HPUX_ENV) */
1030 #ifdef AFS_NAMEI_ENV
1031 #ifdef AFS_AIX42_ENV
1032 char LockFileName[MAXPATHLEN + 1];
1034 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1035 partitionName = (char *) &LockFileName;
1039 if (!dp) return; /* no partition, will fail later */
1040 if (dp->lock_fd != -1) return;
1042 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1043 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1044 partitionName = dp->devName;
1048 partitionName = dp->name;
1052 for (retries=25; retries; retries--) {
1053 dp->lock_fd = open(partitionName, code);
1054 if (dp->lock_fd != -1) break;
1055 if (errno == ENOENT)
1058 pausing.tv_usec = 500000;
1059 select(0, NULL, NULL, NULL, &pausing);
1061 assert(retries != 0);
1063 #if defined (AFS_HPUX_ENV)
1065 assert(getprivgrp(privGrpList) == 0);
1068 * In general, it will difficult and time-consuming ,if not impossible,
1069 * to try to find the privgroup to which this process belongs that has the
1070 * smallest membership, to minimise the security hole. So, we use the privgrp
1071 * to which everybody belongs.
1073 /* first, we have to find the global mask */
1074 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1075 globalMaskIndex++) {
1076 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1077 globalMask = &(privGrpList[globalMaskIndex].
1078 priv_mask[LOCKRDONLY_OFFSET]);
1083 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1084 /* allow everybody to set a lock on a read-only file descriptor */
1085 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1086 assert(setprivgrp(PRIV_GLOBAL,
1087 privGrpList[globalMaskIndex].priv_mask) == 0);
1089 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1091 /* remove the privilege granted to everybody to lock a read-only fd */
1092 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1093 assert(setprivgrp(PRIV_GLOBAL,
1094 privGrpList[globalMaskIndex].priv_mask) == 0);
1097 /* in this case, we should be able to do this with impunity, anyway */
1098 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1101 assert (lockfRtn != -1);
1103 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1104 assert (lockf(dp->lock_fd, F_LOCK, 0) != -1);
1106 assert (flock(dp->lock_fd, LOCK_EX) == 0);
1107 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1111 void VUnlockPartition_r(char *name)
1113 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1114 if (!dp) return; /* no partition, will fail later */
1119 #endif /* AFS_NT40_ENV */
1121 void VLockPartition(char *name)
1124 VLockPartition_r(name);
1128 void VUnlockPartition(char *name)
1131 VUnlockPartition_r(name);