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 */
68 #include <sys/lockf.h>
74 #include <checklist.h>
76 #if defined(AFS_SUN_ENV)
84 #include <sys/mnttab.h>
85 #include <sys/mntent.h>
87 #ifdef AFS_LINUX22_ENV
89 #include <sys/statfs.h>
96 #endif /* AFS_SGI_ENV */
97 #endif /* AFS_NT40_ENV */
98 #if defined(AFS_SGI_ENV)
99 #include <sys/errno.h>
100 #include <sys/stat.h>
102 #include <sys/file.h>
109 #ifdef HAVE_STRINGS_H
116 #include <afs/afsint.h>
118 #include <afs/errors.h>
121 #include <afs/afssyscalls.h>
127 #include "namei_ops.h"
129 #endif /* AFS_NT40_ENV */
130 #endif /* AFS_NAMEI_ENV */
133 #include "partition.h"
134 #ifdef AFS_PTHREAD_ENV
136 #else /* AFS_PTHREAD_ENV */
137 #include <afs/assert.h>
138 #endif /* AFS_PTHREAD_ENV */
140 #if defined(AFS_HPUX_ENV)
141 #include <sys/types.h>
142 #include <sys/privgrp.h>
143 #endif /* defined(AFS_HPUX_ENV) */
146 #include <jfs/filsys.h>
149 /*@printflike@*/ extern void Log(const char *format, ...);
151 int aixlow_water = 8; /* default 8% */
152 struct DiskPartition *DiskPartitionList;
154 #ifdef AFS_SGI_XFS_IOPS_ENV
155 /* Verify that the on disk XFS inodes on the partition are large enough to
156 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
157 * too small to fit in the inode. Returns 0 if the attribute does fit in
160 #include <afs/xfsattrs.h>
161 static int VerifyXFSInodeSize(char *part, char *fstype)
164 int length = SIZEOF_XFS_ATTR_T;
169 if (strcmp("xfs", fstype))
172 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
173 if (((fd=open(part, O_RDONLY, 0)) != -1)
174 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
176 if (fsx.fsx_nextents) {
177 Log("Partition %s: XFS inodes too small, exiting.\n", part);
178 Log("Run xfs_size_check utility and remake partitions.\n");
186 (void) attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
193 static void VInitPartition_r(char *path, char *devname, Device dev)
195 struct DiskPartition *dp, *op;
196 dp = (struct DiskPartition *) malloc(sizeof (struct DiskPartition));
197 /* Add it to the end, to preserve order when we print statistics */
198 for (op = DiskPartitionList; op; op = op->next) {
205 DiskPartitionList = dp;
207 dp->name = (char *)malloc(strlen(path) + 1);
208 strncpy(dp->name, path, strlen(path) + 1);
209 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
210 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
211 dp->devName = (char *)malloc(2 * strlen(path) + 6);
212 strcpy(dp->devName, path);
213 strcat(dp->devName, "/");
214 strcat(dp->devName, "Lock");
215 mkdir(dp->devName, 0700);
216 strcat(dp->devName, path);
217 close(open(dp->devName, O_RDWR | O_CREAT, 0600));
218 dp->device = volutil_GetPartitionID(path);
220 dp->devName = (char *)malloc(strlen(devname) + 1);
221 strncpy(dp->devName, devname, strlen(devname) + 1);
226 dp->f_files = 1; /* just a default value */
227 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
228 if (programType == fileServer)
229 (void) namei_ViceREADME(VPartitionPath(dp));
231 VSetPartitionDiskUsage_r(dp);
234 static void VInitPartition(char *path, char *devname, Device dev)
237 VInitPartition_r(path, devname, dev);
242 /* VAttachPartitions() finds the vice partitions on this server. Calls
243 * VCheckPartition() to do some basic checks on the partition. If the partition
244 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
246 * Returns the number of errors returned by VCheckPartition. An error in
247 * VCheckPartition means that partition is a valid vice partition but the
248 * fileserver should not start because of the error found on that partition.
251 * No specific user space file system checks, since we don't know what
252 * is being used for vice partitions.
254 * Use partition name as devname.
256 int VCheckPartition(char *part, char *devname)
259 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
260 char AFSIDatPath[MAXPATHLEN];
263 /* Only keep track of "/vicepx" partitions since it can get hairy
264 * when NFS mounts are involved.. */
265 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
268 if (stat(part, &status) < 0) {
269 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
273 #ifndef AFS_AIX32_ENV
274 if (programType == fileServer) {
275 char salvpath[MAXPATHLEN];
276 strcpy(salvpath, part);
277 strcat(salvpath, "/FORCESALVAGE");
278 if (stat(salvpath, &status) == 0) {
279 Log("VInitVnodes: Found %s; aborting\n", salvpath);
285 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
286 strcpy(AFSIDatPath, part);
287 strcat(AFSIDatPath, "/AFSIDat");
289 if (stat(AFSIDatPath, &status) < 0) {
293 dirp = opendir(part);
295 while (dp = readdir(dirp)) {
296 if (dp->d_name[0] == 'V') {
297 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);
304 #else /* AFS_NAMEI_ENV */
305 if (stat(AFSIDatPath, &status) == 0) {
306 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
309 #endif /* AFS_NAMEI_ENV */
312 #ifdef AFS_SGI_XFS_IOPS_ENV
313 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
317 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
318 if (status.st_ino != ROOTINO) {
319 Log("%s is not a mounted file system; ignored.\n", part);
324 VInitPartition(part, devname, status.st_dev);
329 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
330 * attached (return value 1), or only attached when it is a separately
331 * mounted partition (return value 0). For non-NAMEI environments, it
334 static int VIsAlwaysAttach(char *part)
341 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
344 strncpy(checkfile, part, 100);
345 strcat(checkfile, "/");
346 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
348 ret = stat(checkfile, &st);
349 return (ret < 0) ? 0 : 1;
350 #else /* AFS_NAMEI_ENV */
352 #endif /* AFS_NAMEI_ENV */
355 /* VAttachPartitions2() looks for and attaches /vicepX partitions
356 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
357 * used to attach /vicepX directories which aren't on dedicated
358 * partitions, in the NAMEI fileserver.
360 void VAttachPartitions2() {
367 while ((de = readdir(dirp))) {
369 strncat(pname, de->d_name, 20);
370 pname[sizeof(pname)-1] = '\0';
372 /* Only keep track of "/vicepx" partitions since automounter
374 if (VIsAlwaysAttach(pname))
375 VCheckPartition(pname, "");
378 #endif /* AFS_NAMEI_ENV */
380 #endif /* AFS_NT40_ENV */
383 int VAttachPartitions(void)
389 if (!(mntfile = fopen(MNTTAB, "r"))) {
390 Log("Can't open %s\n", MNTTAB);
394 while (!getmntent(mntfile, &mnt)) {
395 /* Ignore non ufs or non read/write partitions */
396 if ((strcmp(mnt.mnt_fstype, "ufs") !=0) ||
397 (strncmp(mnt.mnt_mntopts, "ro,ignore",9) ==0))
400 /* If we're going to always attach this partition, do it later. */
401 if (VIsAlwaysAttach(mnt.mnt_mountp))
404 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0 )
408 (void) fclose(mntfile);
410 /* Process the always-attach partitions, if any. */
411 VAttachPartitions2();
416 #endif /* AFS_SUN5_ENV */
417 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
418 int VAttachPartitions(void)
422 struct mntent *mntent;
424 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
425 Log("Problems in getting mount entries(setmntent)\n");
428 while (mntent = getmntent(mfd)) {
429 if (!hasmntopt(mntent, MNTOPT_RW)) continue;
431 /* If we're going to always attach this partition, do it later. */
432 if (VIsAlwaysAttach(mntent->mnt_dir))
435 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0 )
441 /* Process the always-attach partitions, if any. */
442 VAttachPartitions2();
449 * (This function was grabbed from df.c)
452 getmount(register struct vmount **vmountpp)
455 register struct vmount *vm;
458 /* set initial size of mntctl buffer to a MAGIC NUMBER */
461 /* try the operation until ok or a fatal error */
463 if ((vm = (struct vmount *)malloc(size)) == NULL) {
464 /* failed getting memory for mount status buf */
465 perror("FATAL ERROR: get_stat malloc failed\n");
470 * perform the QUERY mntctl - if it returns > 0, that is the
471 * number of vmount structures in the buffer. If it returns
472 * -1, an error occured. If it returned 0, then look in
473 * first word of buffer for needed size.
475 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t)vm)) > 0) {
476 /* OK, got it, now return */
480 } else if (nmounts == 0) {
481 /* the buffer wasn't big enough .... */
482 /* .... get required buffer size */
487 /* some other kind of error occurred */
494 int VAttachPartitions(void)
498 struct vmount *vmountp;
500 if ((nmounts = getmount(&vmountp)) <= 0) {
501 Log("Problems in getting # of mount entries(getmount)\n");
504 for (; nmounts; nmounts--,
505 vmountp = (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
506 char *part = vmt2dataptr(vmountp, VMT_STUB);
508 if (vmountp->vmt_flags & (MNT_READONLY|MNT_REMOVABLE|MNT_REMOTE))
509 continue; /* Ignore any "special" partitions */
512 #ifndef AFS_NAMEI_ENV
514 struct superblock fs;
515 /* The Log statements are non-sequiters in the SalvageLog and don't
516 * even appear in the VolserLog, so restrict them to the FileLog.
518 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT))<0) {
519 if (programType == fileServer)
520 Log("Can't read superblock for %s, ignoring it.\n", part);
523 if (IsBigFilesFileSystem(&fs)) {
524 if (programType == fileServer)
525 Log("%s is a big files filesystem, ignoring it.\n", part);
532 /* If we're going to always attach this partition, do it later. */
533 if (VIsAlwaysAttach(part))
536 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0 )
540 /* Process the always-attach partitions, if any. */
541 VAttachPartitions2();
546 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
547 int VAttachPartitions(void)
552 if (setfsent() < 0) {
553 Log("Error listing filesystems.\n");
557 while (fsent = getfsent()) {
558 if (strcmp(fsent->fs_type, "rw") != 0) continue;
560 /* If we're going to always attach this partition, do it later. */
561 if (VIsAlwaysAttach(fsent->fs_file))
564 if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0 )
569 /* Process the always-attach partitions, if any. */
570 VAttachPartitions2();
578 #include <sys/stat.h>
581 * validate names in vptab.
588 int VValidVPTEntry(struct vptab *vpe)
590 int len = strlen(vpe->vp_name);
593 if (len < VICE_PREFIX_SIZE+1 || len > VICE_PREFIX_SIZE + 2)
595 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
598 for (i=VICE_PREFIX_SIZE; i<len; i++) {
599 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
600 Log("Invalid partition name %s in registry, ignoring it.\n",
605 if (len == VICE_PREFIX_SIZE + 2) {
606 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE]-'a') * 26 +
607 (int)(vpe->vp_name[VICE_PREFIX_SIZE+1]-'a') ;
609 Log("Invalid partition name %s in registry, ignoring it.\n",
615 len = strlen(vpe->vp_dev);
616 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A' ||
617 vpe->vp_dev[0] > 'Z') {
618 Log("Invalid device name %s in registry, ignoring it.\n",
626 int VCheckPartition(char *partName)
633 /* partName is presumed to be of the form "X:" */
634 (void) sprintf(volRoot, "%c:\\", *partName);
636 if (!GetVolumeInformation(volRoot, /* volume root directory */
637 NULL, /* volume name buffer */
638 0, /* volume name size */
639 NULL, /* volume serial number */
640 &dwDummy, /* max component length */
641 &dwDummy, /* file system flags */
642 volFsType, /* file system name */
643 sizeof(volFsType))) {
644 err = GetLastError();
645 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n",
650 if (strcmp(volFsType, "NTFS")) {
651 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
659 int VAttachPartitions(void)
661 struct DiskPartition *partP, *prevP, *nextP;
662 struct vpt_iter iter;
665 if (vpt_Start(&iter)<0) {
666 Log("No partitions to attach.\n");
670 while (0==vpt_NextEntry(&iter, &entry)) {
671 if (!VValidVPTEntry(&entry)) {
675 /* This test for duplicates relies on the fact that the method
676 * of storing the partition names in the NT registry means the same
677 * partition name will never appear twice in the list.
679 for (partP = DiskPartitionList; partP; partP = partP->next) {
680 if (*partP->devName == *entry.vp_dev) {
681 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
682 partP->flags = PART_DUPLICATE;
683 break; /* Only one entry will ever be in this list. */
686 if (partP) continue; /* found a duplicate */
688 if (VCheckPartition(entry.vp_dev)<0)
690 /* This test allows for manually inserting the FORCESALVAGE flag
691 * and thereby invoking the salvager. scandisk obviously won't be
694 if (programType == fileServer) {
696 char salvpath[MAXPATHLEN];
697 strcpy(salvpath, entry.vp_dev);
698 strcat(salvpath, "\\FORCESALVAGE");
699 if (stat(salvpath, &status) == 0) {
700 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
704 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
708 /* Run through partition list and clear out the dupes. */
709 prevP = nextP = NULL;
710 for (partP = DiskPartitionList; partP; partP = nextP) {
712 if (partP->flags == PART_DUPLICATE) {
714 prevP->next = partP->next;
716 DiskPartitionList = partP->next;
727 #ifdef AFS_LINUX22_ENV
728 int VAttachPartitions(void)
732 struct mntent *mntent;
734 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
735 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
736 Log("Problems in getting mount entries(setmntent)\n");
740 while ((mntent = getmntent(mfd))) {
741 /* If we're going to always attach this partition, do it later. */
742 if (VIsAlwaysAttach(mntent->mnt_dir))
745 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0 )
750 /* Process the always-attach partitions, if any. */
751 VAttachPartitions2();
755 #endif /* AFS_LINUX22_ENV */
757 /* This routine is to be called whenever the actual name of the partition
758 * is required. The canonical name is still in part->name.
760 char * VPartitionPath(struct DiskPartition *part)
763 return part->devName;
769 /* get partition structure, abortp tells us if we should abort on failure */
770 struct DiskPartition *VGetPartition_r(char *name, int abortp)
772 register struct DiskPartition *dp;
773 for (dp = DiskPartitionList; dp; dp = dp->next) {
774 if (strcmp(dp->name, name) == 0)
782 struct DiskPartition *VGetPartition(char *name, int abortp)
784 struct DiskPartition *retVal;
786 retVal = VGetPartition_r(name, abortp);
792 void VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
794 ULARGE_INTEGER free_user, total, free_total;
795 int ufree, tot, tfree;
797 if (!GetDiskFreeSpaceEx(VPartitionPath(dp), &free_user, &total,
799 printf("Failed to get disk space info for %s, error = %d\n",
800 dp->name, GetLastError());
804 /* Convert to 1K units. */
805 ufree = (int) Int64ShraMod32(free_user.QuadPart, 10);
806 tot = (int) Int64ShraMod32(total.QuadPart, 10);
807 tfree = (int) Int64ShraMod32(free_total.QuadPart, 10);
809 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
810 dp->totalUsable = tot;
815 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);