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>
30 #include <sys/param.h>
31 #include <sys/types.h>
34 #include <sys/statvfs.h>
35 #endif /* AFS_HAVE_STATVFS */
36 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
37 #include <sys/mount.h>
40 #if !defined(AFS_SGI_ENV)
42 #include <sys/mount.h>
44 #else /* AFS_OSF_ENV */
45 #ifdef AFS_VFSINCL_ENV
48 #include <sys/fs/ufs_fs.h>
50 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
51 #include <ufs/ufs/dinode.h>
52 #include <ufs/ffs/fs.h>
57 #else /* AFS_VFSINCL_ENV */
58 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
61 #endif /* AFS_VFSINCL_ENV */
62 #endif /* AFS_OSF_ENV */
69 #include <sys/lockf.h>
75 #include <checklist.h>
77 #if defined(AFS_SUN_ENV)
85 #include <sys/mnttab.h>
86 #include <sys/mntent.h>
88 #ifdef AFS_LINUX22_ENV
90 #include <sys/statfs.h>
97 #endif /* AFS_SGI_ENV */
98 #endif /* AFS_NT40_ENV */
99 #if defined(AFS_SGI_ENV)
100 #include <sys/errno.h>
101 #include <sys/stat.h>
103 #include <sys/file.h>
110 #ifdef HAVE_STRINGS_H
117 #include <afs/afsint.h>
119 #include <afs/errors.h>
122 #include <afs/afssyscalls.h>
128 #include "namei_ops.h"
130 #endif /* AFS_NT40_ENV */
131 #endif /* AFS_NAMEI_ENV */
134 #include "partition.h"
135 #ifdef AFS_PTHREAD_ENV
137 #else /* AFS_PTHREAD_ENV */
138 #include <afs/assert.h>
139 #endif /* AFS_PTHREAD_ENV */
141 #if defined(AFS_HPUX_ENV)
142 #include <sys/types.h>
143 #include <sys/privgrp.h>
144 #endif /* defined(AFS_HPUX_ENV) */
147 #include <jfs/filsys.h>
150 /*@printflike@*/ extern void Log(const char *format, ...);
152 int aixlow_water = 8; /* default 8% */
153 struct DiskPartition *DiskPartitionList;
155 #ifdef AFS_SGI_XFS_IOPS_ENV
156 /* Verify that the on disk XFS inodes on the partition are large enough to
157 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
158 * too small to fit in the inode. Returns 0 if the attribute does fit in
161 #include <afs/xfsattrs.h>
163 VerifyXFSInodeSize(char *part, char *fstype)
166 int length = SIZEOF_XFS_ATTR_T;
171 if (strcmp("xfs", fstype))
174 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
175 if (((fd = open(part, O_RDONLY, 0)) != -1)
176 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
178 if (fsx.fsx_nextents) {
179 Log("Partition %s: XFS inodes too small, exiting.\n", part);
180 Log("Run xfs_size_check utility and remake partitions.\n");
187 (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
195 VInitPartition_r(char *path, char *devname, Device dev)
197 struct DiskPartition *dp, *op;
198 dp = (struct DiskPartition *)malloc(sizeof(struct DiskPartition));
199 /* Add it to the end, to preserve order when we print statistics */
200 for (op = DiskPartitionList; op; op = op->next) {
207 DiskPartitionList = dp;
209 dp->name = (char *)malloc(strlen(path) + 1);
210 strncpy(dp->name, path, strlen(path) + 1);
211 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
212 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
213 dp->devName = (char *)malloc(2 * strlen(path) + 6);
214 strcpy(dp->devName, path);
215 strcat(dp->devName, "/");
216 strcat(dp->devName, "Lock");
217 mkdir(dp->devName, 0700);
218 strcat(dp->devName, path);
219 close(open(dp->devName, O_RDWR | O_CREAT, 0600));
220 dp->device = volutil_GetPartitionID(path);
222 dp->devName = (char *)malloc(strlen(devname) + 1);
223 strncpy(dp->devName, devname, strlen(devname) + 1);
228 dp->f_files = 1; /* just a default value */
229 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
230 if (programType == fileServer)
231 (void)namei_ViceREADME(VPartitionPath(dp));
233 VSetPartitionDiskUsage_r(dp);
237 VInitPartition(char *path, char *devname, Device dev)
239 VOL_LOCK VInitPartition_r(path, devname, dev);
243 /* VAttachPartitions() finds the vice partitions on this server. Calls
244 * VCheckPartition() to do some basic checks on the partition. If the partition
245 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
247 * Returns the number of errors returned by VCheckPartition. An error in
248 * VCheckPartition means that partition is a valid vice partition but the
249 * fileserver should not start because of the error found on that partition.
252 * No specific user space file system checks, since we don't know what
253 * is being used for vice partitions.
255 * Use partition name as devname.
258 VCheckPartition(char *part, char *devname)
261 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
262 char AFSIDatPath[MAXPATHLEN];
265 /* Only keep track of "/vicepx" partitions since it can get hairy
266 * when NFS mounts are involved.. */
267 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
270 if (stat(part, &status) < 0) {
271 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
274 #ifndef AFS_AIX32_ENV
275 if (programType == fileServer) {
276 char salvpath[MAXPATHLEN];
277 strcpy(salvpath, part);
278 strcat(salvpath, "/FORCESALVAGE");
279 if (stat(salvpath, &status) == 0) {
280 Log("VInitVnodes: Found %s; aborting\n", salvpath);
286 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
287 strcpy(AFSIDatPath, part);
288 strcat(AFSIDatPath, "/AFSIDat");
290 if (stat(AFSIDatPath, &status) < 0) {
294 dirp = opendir(part);
296 while (dp = readdir(dirp)) {
297 if (dp->d_name[0] == 'V') {
298 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);
305 #else /* AFS_NAMEI_ENV */
306 if (stat(AFSIDatPath, &status) == 0) {
307 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
310 #endif /* AFS_NAMEI_ENV */
313 #ifdef AFS_SGI_XFS_IOPS_ENV
314 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
318 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
319 if (status.st_ino != ROOTINO) {
320 Log("%s is not a mounted file system; ignored.\n", part);
325 VInitPartition(part, devname, status.st_dev);
330 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
331 * attached (return value 1), or only attached when it is a separately
332 * mounted partition (return value 0). For non-NAMEI environments, it
336 VIsAlwaysAttach(char *part)
343 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
346 strncpy(checkfile, part, 100);
347 strcat(checkfile, "/");
348 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
350 ret = stat(checkfile, &st);
351 return (ret < 0) ? 0 : 1;
352 #else /* AFS_NAMEI_ENV */
354 #endif /* AFS_NAMEI_ENV */
357 /* VAttachPartitions2() looks for and attaches /vicepX partitions
358 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
359 * used to attach /vicepX directories which aren't on dedicated
360 * partitions, in the NAMEI fileserver.
371 while ((de = readdir(dirp))) {
373 strncat(pname, de->d_name, 20);
374 pname[sizeof(pname) - 1] = '\0';
376 /* Only keep track of "/vicepx" partitions since automounter
378 if (VIsAlwaysAttach(pname))
379 VCheckPartition(pname, "");
382 #endif /* AFS_NAMEI_ENV */
384 #endif /* AFS_NT40_ENV */
388 VAttachPartitions(void)
394 if (!(mntfile = fopen(MNTTAB, "r"))) {
395 Log("Can't open %s\n", MNTTAB);
399 while (!getmntent(mntfile, &mnt)) {
400 /* Ignore non ufs or non read/write partitions */
401 if ((strcmp(mnt.mnt_fstype, "ufs") != 0)
402 || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
405 /* If we're going to always attach this partition, do it later. */
406 if (VIsAlwaysAttach(mnt.mnt_mountp))
409 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0)
413 (void)fclose(mntfile);
415 /* Process the always-attach partitions, if any. */
416 VAttachPartitions2();
421 #endif /* AFS_SUN5_ENV */
422 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
424 VAttachPartitions(void)
428 struct mntent *mntent;
430 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
431 Log("Problems in getting mount entries(setmntent)\n");
434 while (mntent = getmntent(mfd)) {
435 if (!hasmntopt(mntent, MNTOPT_RW))
438 /* If we're going to always attach this partition, do it later. */
439 if (VIsAlwaysAttach(mntent->mnt_dir))
442 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
448 /* Process the always-attach partitions, if any. */
449 VAttachPartitions2();
456 * (This function was grabbed from df.c)
459 getmount(register struct vmount **vmountpp)
462 register struct vmount *vm;
465 /* set initial size of mntctl buffer to a MAGIC NUMBER */
468 /* try the operation until ok or a fatal error */
470 if ((vm = (struct vmount *)malloc(size)) == NULL) {
471 /* failed getting memory for mount status buf */
472 perror("FATAL ERROR: get_stat malloc failed\n");
477 * perform the QUERY mntctl - if it returns > 0, that is the
478 * number of vmount structures in the buffer. If it returns
479 * -1, an error occured. If it returned 0, then look in
480 * first word of buffer for needed size.
482 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
483 /* OK, got it, now return */
487 } else if (nmounts == 0) {
488 /* the buffer wasn't big enough .... */
489 /* .... get required buffer size */
494 /* some other kind of error occurred */
502 VAttachPartitions(void)
506 struct vmount *vmountp;
508 if ((nmounts = getmount(&vmountp)) <= 0) {
509 Log("Problems in getting # of mount entries(getmount)\n");
514 (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
515 char *part = vmt2dataptr(vmountp, VMT_STUB);
517 if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
518 continue; /* Ignore any "special" partitions */
521 #ifndef AFS_NAMEI_ENV
523 struct superblock fs;
524 /* The Log statements are non-sequiters in the SalvageLog and don't
525 * even appear in the VolserLog, so restrict them to the FileLog.
527 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
528 if (programType == fileServer)
529 Log("Can't read superblock for %s, ignoring it.\n", part);
532 if (IsBigFilesFileSystem(&fs)) {
533 if (programType == fileServer)
534 Log("%s is a big files filesystem, ignoring it.\n", part);
541 /* If we're going to always attach this partition, do it later. */
542 if (VIsAlwaysAttach(part))
545 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0)
549 /* Process the always-attach partitions, if any. */
550 VAttachPartitions2();
555 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
557 VAttachPartitions(void)
562 if (setfsent() < 0) {
563 Log("Error listing filesystems.\n");
567 while (fsent = getfsent()) {
568 if (strcmp(fsent->fs_type, "rw") != 0)
571 /* If we're going to always attach this partition, do it later. */
572 if (VIsAlwaysAttach(fsent->fs_file))
575 if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0)
580 /* Process the always-attach partitions, if any. */
581 VAttachPartitions2();
589 #include <sys/stat.h>
592 * validate names in vptab.
600 VValidVPTEntry(struct vptab *vpe)
602 int len = strlen(vpe->vp_name);
605 if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
607 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
610 for (i = VICE_PREFIX_SIZE; i < len; i++) {
611 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
612 Log("Invalid partition name %s in registry, ignoring it.\n",
617 if (len == VICE_PREFIX_SIZE + 2) {
618 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
619 (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
621 Log("Invalid partition name %s in registry, ignoring it.\n",
627 len = strlen(vpe->vp_dev);
628 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
629 || vpe->vp_dev[0] > 'Z') {
630 Log("Invalid device name %s in registry, ignoring it.\n",
639 VCheckPartition(char *partName)
646 /* partName is presumed to be of the form "X:" */
647 (void)sprintf(volRoot, "%c:\\", *partName);
649 if (!GetVolumeInformation(volRoot, /* volume root directory */
650 NULL, /* volume name buffer */
651 0, /* volume name size */
652 NULL, /* volume serial number */
653 &dwDummy, /* max component length */
654 &dwDummy, /* file system flags */
655 volFsType, /* file system name */
656 sizeof(volFsType))) {
657 err = GetLastError();
658 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
662 if (strcmp(volFsType, "NTFS")) {
663 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
672 VAttachPartitions(void)
674 struct DiskPartition *partP, *prevP, *nextP;
675 struct vpt_iter iter;
678 if (vpt_Start(&iter) < 0) {
679 Log("No partitions to attach.\n");
683 while (0 == vpt_NextEntry(&iter, &entry)) {
684 if (!VValidVPTEntry(&entry)) {
688 /* This test for duplicates relies on the fact that the method
689 * of storing the partition names in the NT registry means the same
690 * partition name will never appear twice in the list.
692 for (partP = DiskPartitionList; partP; partP = partP->next) {
693 if (*partP->devName == *entry.vp_dev) {
694 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
695 partP->flags = PART_DUPLICATE;
696 break; /* Only one entry will ever be in this list. */
700 continue; /* found a duplicate */
702 if (VCheckPartition(entry.vp_dev) < 0)
704 /* This test allows for manually inserting the FORCESALVAGE flag
705 * and thereby invoking the salvager. scandisk obviously won't be
708 if (programType == fileServer) {
710 char salvpath[MAXPATHLEN];
711 strcpy(salvpath, entry.vp_dev);
712 strcat(salvpath, "\\FORCESALVAGE");
713 if (stat(salvpath, &status) == 0) {
714 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
718 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
722 /* Run through partition list and clear out the dupes. */
723 prevP = nextP = NULL;
724 for (partP = DiskPartitionList; partP; partP = nextP) {
726 if (partP->flags == PART_DUPLICATE) {
728 prevP->next = partP->next;
730 DiskPartitionList = partP->next;
740 #ifdef AFS_LINUX22_ENV
742 VAttachPartitions(void)
746 struct mntent *mntent;
748 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
749 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
750 Log("Problems in getting mount entries(setmntent)\n");
754 while ((mntent = getmntent(mfd))) {
755 /* If we're going to always attach this partition, do it later. */
756 if (VIsAlwaysAttach(mntent->mnt_dir))
759 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
764 /* Process the always-attach partitions, if any. */
765 VAttachPartitions2();
769 #endif /* AFS_LINUX22_ENV */
771 /* This routine is to be called whenever the actual name of the partition
772 * is required. The canonical name is still in part->name.
775 VPartitionPath(struct DiskPartition *part)
778 return part->devName;
784 /* get partition structure, abortp tells us if we should abort on failure */
785 struct DiskPartition *
786 VGetPartition_r(char *name, int abortp)
788 register struct DiskPartition *dp;
789 for (dp = DiskPartitionList; dp; dp = dp->next) {
790 if (strcmp(dp->name, name) == 0)
798 struct DiskPartition *
799 VGetPartition(char *name, int abortp)
801 struct DiskPartition *retVal;
802 VOL_LOCK retVal = VGetPartition_r(name, abortp);
803 VOL_UNLOCK return retVal;
808 VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
810 ULARGE_INTEGER free_user, total, free_total;
811 int ufree, tot, tfree;
813 if (!GetDiskFreeSpaceEx
814 (VPartitionPath(dp), &free_user, &total, &free_total)) {
815 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
820 /* Convert to 1K units. */
821 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
822 tot = (int)Int64ShraMod32(total.QuadPart, 10);
823 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
825 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
826 dp->totalUsable = tot;
832 VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
834 int fd, totalblks, free, used, availblks, bsize, code;
837 struct statvfs statbuf;
839 struct statfs statbuf;
842 if (dp->flags & PART_DONTUPDATE)
844 /* Note: we don't bother syncing because it's only an estimate, update
845 * is syncing every 30 seconds anyway, we only have to keep the disk
846 * approximately 10% from full--you just can't get the stuff in from
847 * the net fast enough to worry */
849 code = statvfs(dp->name, &statbuf);
851 code = statfs(dp->name, &statbuf);
854 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
858 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
859 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
862 totalblks = statbuf.f_blocks;
863 free = statbuf.f_bfree;
864 reserved = free - statbuf.f_bavail;
866 bsize = statbuf.f_frsize;
868 bsize = statbuf.f_bsize;
870 availblks = totalblks - reserved;
871 dp->f_files = statbuf.f_files; /* max # of files in partition */
873 /* Now free and totalblks are in fragment units, but we want them in
877 free *= (bsize / 1024);
878 totalblks *= (bsize / 1024);
879 availblks *= (bsize / 1024);
880 reserved *= (bsize / 1024);
882 free /= (1024 / bsize);
883 totalblks /= (1024 / bsize);
884 availblks /= (1024 / bsize);
885 reserved /= (1024 / bsize);
887 /* now compute remaining figures */
888 used = totalblks - free;
890 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
891 dp->totalUsable = availblks;
892 dp->free = availblks - used; /* this is exactly f_bavail */
894 #endif /* AFS_NT40_ENV */
897 VSetPartitionDiskUsage(register struct DiskPartition *dp)
899 VOL_LOCK VSetPartitionDiskUsage_r(dp);
903 VResetDiskUsage_r(void)
905 struct DiskPartition *dp;
906 for (dp = DiskPartitionList; dp; dp = dp->next) {
907 VSetPartitionDiskUsage_r(dp);
908 #ifndef AFS_PTHREAD_ENV
910 #endif /* !AFS_PTHREAD_ENV */
915 VResetDiskUsage(void)
917 VOL_LOCK VResetDiskUsage_r();
921 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_int32 blocks,
922 afs_int32 checkBlocks)
925 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
926 * for less than BlocksSpare would skip the error-checking path, and we
927 * could grow existing files forever, not just for another BlocksSpare
931 afs_int32 rem, minavail;
933 if ((rem = vp->partition->free - checkBlocks) < (minavail =
939 if (vp->partition->free - checkBlocks < 0)
942 else if (V_maxquota(vp)
943 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
946 vp->partition->free -= blocks;
947 V_diskused(vp) += blocks;
951 VAdjustDiskUsage(Error * ec, Volume * vp, afs_int32 blocks,
952 afs_int32 checkBlocks)
954 VOL_LOCK VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
958 VDiskUsage_r(Volume * vp, afs_int32 blocks)
962 afs_int32 rem, minavail;
964 if ((rem = vp->partition->free - blocks) < (minavail =
967 aixlow_water) / 100))
969 if (vp->partition->free - blocks < 0)
973 vp->partition->free -= blocks;
978 VDiskUsage(Volume * vp, afs_int32 blocks)
981 VOL_LOCK retVal = VDiskUsage_r(vp, blocks);
982 VOL_UNLOCK return retVal;
986 VPrintDiskStats_r(void)
988 struct DiskPartition *dp;
989 for (dp = DiskPartitionList; dp; dp = dp->next) {
990 Log("Partition %s: %d available 1K blocks (minfree=%d), ", dp->name,
991 dp->totalUsable, dp->minFree);
993 Log("overallocated by %d blocks\n", -dp->free);
995 Log("%d free blocks\n", dp->free);
1000 VPrintDiskStats(void)
1002 VOL_LOCK VPrintDiskStats_r();
1006 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1007 #define LOCKFILE "LOCKFILE"
1009 VLockPartition_r(char *name)
1011 struct DiskPartition *dp = VGetPartition_r(name, 0);
1016 if (dp->lock_fd == -1) {
1019 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1021 (int)CreateFile(path, GENERIC_WRITE,
1022 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1023 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1024 assert(dp->lock_fd != (int)INVALID_HANDLE_VALUE);
1026 memset((char *)&lap, 0, sizeof(lap));
1027 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1034 VUnlockPartition_r(char *name)
1036 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1040 return; /* no partition, will fail later */
1041 memset((char *)&lap, 0, sizeof(lap));
1043 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1044 CloseHandle((HANDLE) dp->lock_fd);
1047 #else /* AFS_NT40_ENV */
1049 #if defined(AFS_HPUX_ENV)
1050 #define BITS_PER_CHAR (8)
1051 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1053 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1054 #endif /* defined(AFS_HPUX_ENV) */
1057 VLockPartition_r(char *name)
1059 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1060 char *partitionName;
1062 struct timeval pausing;
1063 #if defined(AFS_HPUX_ENV)
1065 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1066 unsigned int *globalMask;
1067 int globalMaskIndex;
1068 #endif /* defined(AFS_HPUX_ENV) */
1069 #ifdef AFS_NAMEI_ENV
1070 #ifdef AFS_AIX42_ENV
1071 char LockFileName[MAXPATHLEN + 1];
1073 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1074 partitionName = (char *)&LockFileName;
1079 return; /* no partition, will fail later */
1080 if (dp->lock_fd != -1)
1083 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1084 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1085 partitionName = dp->devName;
1089 partitionName = dp->name;
1093 for (retries = 25; retries; retries--) {
1094 dp->lock_fd = open(partitionName, code);
1095 if (dp->lock_fd != -1)
1097 if (errno == ENOENT)
1100 pausing.tv_usec = 500000;
1101 select(0, NULL, NULL, NULL, &pausing);
1103 assert(retries != 0);
1105 #if defined (AFS_HPUX_ENV)
1107 assert(getprivgrp(privGrpList) == 0);
1110 * In general, it will difficult and time-consuming ,if not impossible,
1111 * to try to find the privgroup to which this process belongs that has the
1112 * smallest membership, to minimise the security hole. So, we use the privgrp
1113 * to which everybody belongs.
1115 /* first, we have to find the global mask */
1116 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1117 globalMaskIndex++) {
1118 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1120 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1125 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1126 /* allow everybody to set a lock on a read-only file descriptor */
1127 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1128 assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1131 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1133 /* remove the privilege granted to everybody to lock a read-only fd */
1134 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1135 assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1138 /* in this case, we should be able to do this with impunity, anyway */
1139 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1142 assert(lockfRtn != -1);
1144 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1145 assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1147 assert(flock(dp->lock_fd, LOCK_EX) == 0);
1148 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1153 VUnlockPartition_r(char *name)
1155 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1157 return; /* no partition, will fail later */
1162 #endif /* AFS_NT40_ENV */
1165 VLockPartition(char *name)
1167 VOL_LOCK VLockPartition_r(name);
1171 VUnlockPartition(char *name)
1173 VOL_LOCK VUnlockPartition_r(name);