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)
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 int aixlow_water = 8; /* default 8% */
150 struct DiskPartition *DiskPartitionList;
152 #ifdef AFS_SGI_XFS_IOPS_ENV
153 /* Verify that the on disk XFS inodes on the partition are large enough to
154 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
155 * too small to fit in the inode. Returns 0 if the attribute does fit in
158 #include <afs/xfsattrs.h>
159 static int VerifyXFSInodeSize(char *part, char *fstype)
162 int length = SIZEOF_XFS_ATTR_T;
167 if (strcmp("xfs", fstype))
170 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
171 if (((fd=open(part, O_RDONLY, 0)) != -1)
172 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
174 if (fsx.fsx_nextents) {
175 Log("Partition %s: XFS inodes too small, exiting.\n", part);
176 Log("Run xfs_size_check utility and remake partitions.\n");
184 (void) attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
191 static void VInitPartition_r(char *path, char *devname, Device dev)
193 struct DiskPartition *dp, *op;
194 dp = (struct DiskPartition *) malloc(sizeof (struct DiskPartition));
195 /* Add it to the end, to preserve order when we print statistics */
196 for (op = DiskPartitionList; op; op = op->next) {
203 DiskPartitionList = dp;
205 dp->name = (char *)malloc(strlen(path) + 1);
206 strncpy(dp->name, path, strlen(path) + 1);
207 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
208 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
209 dp->devName = (char *)malloc(2 * strlen(path) + 6);
210 strcpy(dp->devName, path);
211 strcat(dp->devName, "/");
212 strcat(dp->devName, "Lock");
213 mkdir(dp->devName, 0700);
214 strcat(dp->devName, path);
215 close(open(dp->devName, O_RDWR | O_CREAT, 0600));
216 dp->device = volutil_GetPartitionID(path);
218 dp->devName = (char *)malloc(strlen(devname) + 1);
219 strncpy(dp->devName, devname, strlen(devname) + 1);
224 dp->f_files = 1; /* just a default value */
225 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
226 if (programType == fileServer)
227 (void) namei_ViceREADME(VPartitionPath(dp));
229 VSetPartitionDiskUsage_r(dp);
232 static void VInitPartition(char *path, char *devname, Device dev)
235 VInitPartition_r(path, devname, dev);
240 /* VAttachPartitions() finds the vice partitions on this server. Calls
241 * VCheckPartition() to do some basic checks on the partition. If the partition
242 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
244 * Returns the number of errors returned by VCheckPartition. An error in
245 * VCheckPartition means that partition is a valid vice partition but the
246 * fileserver should not start because of the error found on that partition.
249 * No specific user space file system checks, since we don't know what
250 * is being used for vice partitions.
252 * Use partition name as devname.
254 int VCheckPartition(part, devname)
258 #ifdef AFS_LARGEFILE_ENV
259 struct stat64 status;
260 #else /* !AFS_LARGEFILE_ENV */
262 #endif /* !AFS_LARGEFILE_ENV */
263 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
264 char AFSIDatPath[MAXPATHLEN];
267 /* Only keep track of "/vicepx" partitions since it can get hairy
268 * when NFS mounts are involved.. */
269 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
272 #ifdef AFS_LARGEFILE_ENV
273 if (stat64(part, &status) < 0) {
274 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
277 #else /* !AFS_LARGEFILE_ENV */
278 if (stat(part, &status) < 0) {
279 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
282 #endif /* AFS_LARGEFILE_ENV */
284 #ifndef AFS_AIX32_ENV
285 if (programType == fileServer) {
286 char salvpath[MAXPATHLEN];
287 strcpy(salvpath, part);
288 strcat(salvpath, "/FORCESALVAGE");
289 #ifdef AFS_LARGEFILE_ENV
290 if (stat64(salvpath, &status) == 0) {
291 Log("VInitVnodes: Found %s; aborting\n", salvpath);
294 #else /* !AFS_LARGEFILE_ENV */
295 if (stat(salvpath, &status) == 0) {
296 Log("VInitVnodes: Found %s; aborting\n", salvpath);
299 #endif /* !AFS_LARGEFILE_ENV */
303 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
304 strcpy(AFSIDatPath, part);
305 strcat(AFSIDatPath, "/AFSIDat");
308 #ifdef AFS_LARGEFILE_ENV
309 stat64(AFSIDatPath, &status)
310 #else /* !AFS_LARGEFILE_ENV */
311 stat(AFSIDatPath, &status)
312 #endif /* !AFS_LARGEFILE_ENV */
317 dirp = opendir(part);
319 while (dp = readdir(dirp)) {
320 if (dp->d_name[0] == 'V') {
321 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);
328 #else /* AFS_NAMEI_ENV */
329 if (stat(AFSIDatPath, &status) == 0) {
330 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
333 #endif /* AFS_NAMEI_ENV */
336 #ifdef AFS_SGI_XFS_IOPS_ENV
337 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
341 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
342 if (status.st_ino != ROOTINO) {
343 Log("%s is not a mounted file system; ignored.\n", part);
348 VInitPartition(part, devname, status.st_dev);
353 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
354 * attached (return value 1), or only attached when it is a separately
355 * mounted partition (return value 0). For non-NAMEI environments, it
358 static int VIsAlwaysAttach(part)
366 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
369 strncpy(checkfile, part, 100);
370 strcat(checkfile, "/");
371 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
373 ret = stat(checkfile, &st);
374 return (ret < 0) ? 0 : 1;
375 #else /* AFS_NAMEI_ENV */
377 #endif /* AFS_NAMEI_ENV */
380 /* VAttachPartitions2() looks for and attaches /vicepX partitions
381 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
382 * used to attach /vicepX directories which aren't on dedicated
383 * partitions, in the NAMEI fileserver.
385 void VAttachPartitions2() {
392 while ((de = readdir(dirp))) {
394 strncat(pname, de->d_name, 20);
395 pname[sizeof(pname)-1] = '\0';
397 /* Only keep track of "/vicepx" partitions since automounter
399 if (VIsAlwaysAttach(pname))
400 VCheckPartition(pname, "");
403 #endif /* AFS_NAMEI_ENV */
405 #endif /* AFS_NT40_ENV */
408 int VAttachPartitions(void)
414 if (!(mntfile = fopen(MNTTAB, "r"))) {
415 Log("Can't open %s\n", MNTTAB);
419 while (!getmntent(mntfile, &mnt)) {
420 /* Ignore non ufs or non read/write partitions */
421 if ((strcmp(mnt.mnt_fstype, "ufs") !=0) ||
422 (strncmp(mnt.mnt_mntopts, "ro,ignore",9) ==0))
425 /* If we're going to always attach this partition, do it later. */
426 if (VIsAlwaysAttach(mnt.mnt_mountp))
429 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0 )
433 (void) fclose(mntfile);
435 /* Process the always-attach partitions, if any. */
436 VAttachPartitions2();
441 #endif /* AFS_SUN5_ENV */
442 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
443 int VAttachPartitions(void)
447 struct mntent *mntent;
449 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
450 Log("Problems in getting mount entries(setmntent)\n");
453 while (mntent = getmntent(mfd)) {
454 if (!hasmntopt(mntent, MNTOPT_RW)) continue;
456 /* If we're going to always attach this partition, do it later. */
457 if (VIsAlwaysAttach(mntent->mnt_dir))
460 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0 )
466 /* Process the always-attach partitions, if any. */
467 VAttachPartitions2();
474 * (This function was grabbed from df.c)
478 register struct vmount **vmountpp; /* place to tell where buffer is */
481 register struct vmount *vm;
484 /* set initial size of mntctl buffer to a MAGIC NUMBER */
487 /* try the operation until ok or a fatal error */
489 if ((vm = (struct vmount *)malloc(size)) == NULL) {
490 /* failed getting memory for mount status buf */
491 perror("FATAL ERROR: get_stat malloc failed\n");
496 * perform the QUERY mntctl - if it returns > 0, that is the
497 * number of vmount structures in the buffer. If it returns
498 * -1, an error occured. If it returned 0, then look in
499 * first word of buffer for needed size.
501 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t)vm)) > 0) {
502 /* OK, got it, now return */
506 } else if (nmounts == 0) {
507 /* the buffer wasn't big enough .... */
508 /* .... get required buffer size */
513 /* some other kind of error occurred */
520 int VAttachPartitions(void)
524 struct vmount *vmountp;
526 if ((nmounts = getmount(&vmountp)) <= 0) {
527 Log("Problems in getting # of mount entries(getmount)\n");
530 for (; nmounts; nmounts--,
531 vmountp = (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
532 char *part = vmt2dataptr(vmountp, VMT_STUB);
534 if (vmountp->vmt_flags & (MNT_READONLY|MNT_REMOVABLE|MNT_REMOTE))
535 continue; /* Ignore any "special" partitions */
538 #ifndef AFS_NAMEI_ENV
540 struct superblock fs;
541 /* The Log statements are non-sequiters in the SalvageLog and don't
542 * even appear in the VolserLog, so restrict them to the FileLog.
544 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT))<0) {
545 if (programType == fileServer)
546 Log("Can't read superblock for %s, ignoring it.\n", part);
549 if (IsBigFilesFileSystem(&fs)) {
550 if (programType == fileServer)
551 Log("%s is a big files filesystem, ignoring it.\n", part);
558 /* If we're going to always attach this partition, do it later. */
559 if (VIsAlwaysAttach(part))
562 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0 )
566 /* Process the always-attach partitions, if any. */
567 VAttachPartitions2();
572 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
573 int VAttachPartitions(void)
578 if (setfsent() < 0) {
579 Log("Error listing filesystems.\n");
583 while (fsent = getfsent()) {
584 if (strcmp(fsent->fs_type, "rw") != 0) continue;
586 /* If we're going to always attach this partition, do it later. */
587 if (VIsAlwaysAttach(fsent->fs_file))
590 if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0 )
595 /* Process the always-attach partitions, if any. */
596 VAttachPartitions2();
604 #include <sys/stat.h>
607 * validate names in vptab.
614 int VValidVPTEntry(struct vptab *vpe)
616 int len = strlen(vpe->vp_name);
619 if (len < VICE_PREFIX_SIZE+1 || len > VICE_PREFIX_SIZE + 2)
621 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
624 for (i=VICE_PREFIX_SIZE; i<len; i++) {
625 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
626 Log("Invalid partition name %s in registry, ignoring it.\n",
631 if (len == VICE_PREFIX_SIZE + 2) {
632 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE]-'a') * 26 +
633 (int)(vpe->vp_name[VICE_PREFIX_SIZE+1]-'a') ;
635 Log("Invalid partition name %s in registry, ignoring it.\n",
641 len = strlen(vpe->vp_dev);
642 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A' ||
643 vpe->vp_dev[0] > 'Z') {
644 Log("Invalid device name %s in registry, ignoring it.\n",
652 int VCheckPartition(char *partName)
659 /* partName is presumed to be of the form "X:" */
660 (void) sprintf(volRoot, "%c:\\", *partName);
662 if (!GetVolumeInformation(volRoot, /* volume root directory */
663 NULL, /* volume name buffer */
664 0, /* volume name size */
665 NULL, /* volume serial number */
666 &dwDummy, /* max component length */
667 &dwDummy, /* file system flags */
668 volFsType, /* file system name */
669 sizeof(volFsType))) {
670 err = GetLastError();
671 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n",
676 if (strcmp(volFsType, "NTFS")) {
677 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
685 int VAttachPartitions(void)
687 struct DiskPartition *partP, *prevP, *nextP;
688 struct vpt_iter iter;
691 if (vpt_Start(&iter)<0) {
692 Log("No partitions to attach.\n");
696 while (0==vpt_NextEntry(&iter, &entry)) {
697 if (!VValidVPTEntry(&entry)) {
701 /* This test for duplicates relies on the fact that the method
702 * of storing the partition names in the NT registry means the same
703 * partition name will never appear twice in the list.
705 for (partP = DiskPartitionList; partP; partP = partP->next) {
706 if (*partP->devName == *entry.vp_dev) {
707 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
708 partP->flags = PART_DUPLICATE;
709 break; /* Only one entry will ever be in this list. */
712 if (partP) continue; /* found a duplicate */
714 if (VCheckPartition(entry.vp_dev)<0)
716 /* This test allows for manually inserting the FORCESALVAGE flag
717 * and thereby invoking the salvager. scandisk obviously won't be
720 if (programType == fileServer) {
722 char salvpath[MAXPATHLEN];
723 strcpy(salvpath, entry.vp_dev);
724 strcat(salvpath, "\\FORCESALVAGE");
725 if (stat(salvpath, &status) == 0) {
726 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
730 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
734 /* Run through partition list and clear out the dupes. */
735 prevP = nextP = NULL;
736 for (partP = DiskPartitionList; partP; partP = nextP) {
738 if (partP->flags == PART_DUPLICATE) {
740 prevP->next = partP->next;
742 DiskPartitionList = partP->next;
753 #ifdef AFS_LINUX22_ENV
754 int VAttachPartitions(void)
758 struct mntent *mntent;
760 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
761 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
762 Log("Problems in getting mount entries(setmntent)\n");
766 while ((mntent = getmntent(mfd))) {
767 /* If we're going to always attach this partition, do it later. */
768 if (VIsAlwaysAttach(mntent->mnt_dir))
771 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0 )
776 /* Process the always-attach partitions, if any. */
777 VAttachPartitions2();
781 #endif /* AFS_LINUX22_ENV */
783 /* This routine is to be called whenever the actual name of the partition
784 * is required. The canonical name is still in part->name.
786 char * VPartitionPath(struct DiskPartition *part)
789 return part->devName;
795 /* get partition structure, abortp tells us if we should abort on failure */
796 struct DiskPartition *VGetPartition_r(char *name, int abortp)
798 register struct DiskPartition *dp;
799 for (dp = DiskPartitionList; dp; dp = dp->next) {
800 if (strcmp(dp->name, name) == 0)
808 struct DiskPartition *VGetPartition(char *name, int abortp)
810 struct DiskPartition *retVal;
812 retVal = VGetPartition_r(name, abortp);
818 void VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
820 ULARGE_INTEGER free_user, total, free_total;
821 int ufree, tot, tfree;
823 if (!GetDiskFreeSpaceEx(VPartitionPath(dp), &free_user, &total,
825 printf("Failed to get disk space info for %s, error = %d\n",
826 dp->name, GetLastError());
830 /* Convert to 1K units. */
831 ufree = (int) Int64ShraMod32(free_user.QuadPart, 10);
832 tot = (int) Int64ShraMod32(total.QuadPart, 10);
833 tfree = (int) Int64ShraMod32(free_total.QuadPart, 10);
835 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
836 dp->totalUsable = tot;
841 void VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
844 int fd, totalblks, free, used, availblks, bsize, code;
847 struct statvfs statbuf;
849 struct statfs statbuf;
852 if (dp->flags & PART_DONTUPDATE)
854 /* Note: we don't bother syncing because it's only an estimate, update
855 is syncing every 30 seconds anyway, we only have to keep the disk
856 approximately 10% from full--you just can't get the stuff in from
857 the net fast enough to worry */
859 code = statvfs(dp->name, &statbuf);
861 code = statfs(dp->name, &statbuf);
864 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n", dp->name, errno);
867 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
868 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
871 totalblks = statbuf.f_blocks;
872 free = statbuf.f_bfree;
873 reserved = free - statbuf.f_bavail;
875 bsize = statbuf.f_frsize;
877 bsize = statbuf.f_bsize;
879 availblks = totalblks - reserved;
880 dp->f_files = statbuf.f_files; /* max # of files in partition */
882 /* Now free and totalblks are in fragment units, but we want them in
886 free *= (bsize/1024);
887 totalblks *= (bsize / 1024);
888 availblks *= (bsize / 1024 );
889 reserved *= (bsize / 1024 );
892 free /= (1024/bsize);
893 totalblks /= (1024/bsize);
894 availblks /= (1024/bsize);
895 reserved /= (1024/bsize);
897 /* now compute remaining figures */
898 used = totalblks - free;
900 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
901 dp->totalUsable = availblks;
902 dp->free = availblks - used; /* this is exactly f_bavail */
904 #endif /* AFS_NT40_ENV */
906 void VSetPartitionDiskUsage(register struct DiskPartition *dp)
909 VSetPartitionDiskUsage_r(dp);
913 void VResetDiskUsage_r(void)
915 struct DiskPartition *dp;
916 for (dp = DiskPartitionList; dp; dp = dp->next) {
917 VSetPartitionDiskUsage_r(dp);
918 #ifndef AFS_PTHREAD_ENV
920 #endif /* !AFS_PTHREAD_ENV */
924 void VResetDiskUsage(void)
931 void VAdjustDiskUsage_r(Error *ec, Volume *vp, afs_size_t blocks, afs_size_t checkBlocks)
934 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
935 for less than BlocksSpare would skip the error-checking path, and we
936 could grow existing files forever, not just for another BlocksSpare
940 afs_int32 rem, minavail;
942 if ((rem = vp->partition->free - checkBlocks) <
943 (minavail = (vp->partition->totalUsable * aixlow_water) / 100))
945 if (vp->partition->free - checkBlocks < 0)
948 else if (V_maxquota(vp) && V_diskused(vp) + checkBlocks > V_maxquota(vp))
951 vp->partition->free -= blocks;
952 V_diskused(vp) += blocks;
955 void VAdjustDiskUsage(Error *ec, Volume *vp, afs_size_t blocks, afs_size_t checkBlocks)
958 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
962 int VDiskUsage_r(Volume *vp, afs_size_t blocks)
966 afs_int32 rem, minavail;
968 if ((rem = vp->partition->free - blocks) <
969 (minavail = (vp->partition->totalUsable * aixlow_water) / 100))
971 if (vp->partition->free - blocks < 0)
975 vp->partition->free -= blocks;
979 int VDiskUsage(Volume *vp, afs_size_t blocks)
983 retVal = VDiskUsage_r(vp, blocks);
988 void VPrintDiskStats_r(void)
990 struct DiskPartition *dp;
991 for (dp = DiskPartitionList; dp; dp = dp->next) {
992 Log("Partition %s: %d available 1K blocks (minfree=%d), ",
993 dp->name, dp->totalUsable, dp->minFree);
995 Log("overallocated by %d blocks\n", -dp->free);
997 Log("%d free blocks\n", dp->free);
1001 void VPrintDiskStats(void)
1004 VPrintDiskStats_r();
1009 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1010 #define LOCKFILE "LOCKFILE"
1011 void VLockPartition_r(char *name)
1013 struct DiskPartition *dp = VGetPartition_r(name, 0);
1017 if (dp->lock_fd == -1) {
1020 (void) sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1021 dp->lock_fd = (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,
1033 void VUnlockPartition_r(char *name)
1035 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1038 if (!dp) return; /* no partition, will fail later */
1039 memset((char*)&lap, 0, sizeof(lap));
1041 UnlockFileEx((HANDLE)dp->lock_fd, 0, 1, 0, &lap);
1042 CloseHandle((HANDLE)dp->lock_fd);
1045 #else /* AFS_NT40_ENV */
1047 #if defined(AFS_HPUX_ENV)
1048 #define BITS_PER_CHAR (8)
1049 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1051 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1052 #endif /* defined(AFS_HPUX_ENV) */
1054 void VLockPartition_r(char *name)
1056 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1057 char *partitionName;
1059 struct timeval pausing;
1060 #if defined(AFS_HPUX_ENV)
1062 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1063 unsigned int *globalMask;
1064 int globalMaskIndex;
1065 #endif /* defined(AFS_HPUX_ENV) */
1066 #ifdef AFS_NAMEI_ENV
1067 #ifdef AFS_AIX42_ENV
1068 char LockFileName[MAXPATHLEN + 1];
1070 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1071 partitionName = (char *) &LockFileName;
1075 if (!dp) return; /* no partition, will fail later */
1076 if (dp->lock_fd != -1) return;
1078 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1079 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1080 partitionName = dp->devName;
1084 partitionName = dp->name;
1088 for (retries=25; retries; retries--) {
1089 dp->lock_fd = open(partitionName, code);
1090 if (dp->lock_fd != -1) break;
1091 if (errno == ENOENT)
1094 pausing.tv_usec = 500000;
1095 select(0, NULL, NULL, NULL, &pausing);
1097 assert(retries != 0);
1099 #if defined (AFS_HPUX_ENV)
1101 assert(getprivgrp(privGrpList) == 0);
1104 * In general, it will difficult and time-consuming ,if not impossible,
1105 * to try to find the privgroup to which this process belongs that has the
1106 * smallest membership, to minimise the security hole. So, we use the privgrp
1107 * to which everybody belongs.
1109 /* first, we have to find the global mask */
1110 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1111 globalMaskIndex++) {
1112 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1113 globalMask = &(privGrpList[globalMaskIndex].
1114 priv_mask[LOCKRDONLY_OFFSET]);
1119 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1120 /* allow everybody to set a lock on a read-only file descriptor */
1121 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1122 assert(setprivgrp(PRIV_GLOBAL,
1123 privGrpList[globalMaskIndex].priv_mask) == 0);
1125 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1127 /* remove the privilege granted to everybody to lock a read-only fd */
1128 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1129 assert(setprivgrp(PRIV_GLOBAL,
1130 privGrpList[globalMaskIndex].priv_mask) == 0);
1133 /* in this case, we should be able to do this with impunity, anyway */
1134 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1137 assert (lockfRtn != -1);
1139 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1140 assert (lockf(dp->lock_fd, F_LOCK, 0) != -1);
1142 assert (flock(dp->lock_fd, LOCK_EX) == 0);
1143 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1147 void VUnlockPartition_r(char *name)
1149 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1150 if (!dp) return; /* no partition, will fail later */
1155 #endif /* AFS_NT40_ENV */
1157 void VLockPartition(char *name)
1160 VLockPartition_r(name);
1164 void VUnlockPartition(char *name)
1167 VUnlockPartition_r(name);