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
9 * Portions Copyright (c) 2003 Apple Computer, Inc.
10 * Portions Copyright (c) 2006 Sine Nomine Associates
17 Institution: The Information Technology Center, Carnegie-Mellon University
21 #include <afsconfig.h>
22 #include <afs/param.h>
33 #include <sys/param.h>
34 #include <sys/types.h>
36 #if AFS_HAVE_STATVFS || AFS_HAVE_STATVFS64
37 #include <sys/statvfs.h>
38 #endif /* AFS_HAVE_STATVFS */
39 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
40 #include <sys/mount.h>
43 #if !defined(AFS_SGI_ENV)
45 #include <sys/mount.h>
47 #else /* AFS_OSF_ENV */
48 #ifdef AFS_VFSINCL_ENV
51 #include <sys/fs/ufs_fs.h>
53 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
54 #include <ufs/ufs/dinode.h>
55 #include <ufs/ffs/fs.h>
60 #else /* AFS_VFSINCL_ENV */
61 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
64 #endif /* AFS_VFSINCL_ENV */
65 #endif /* AFS_OSF_ENV */
72 #include <sys/lockf.h>
78 #include <checklist.h>
80 #if defined(AFS_SUN_ENV)
88 #include <sys/mnttab.h>
89 #include <sys/mntent.h>
91 #ifdef AFS_LINUX22_ENV
93 #include <sys/statfs.h>
100 #endif /* AFS_SGI_ENV */
101 #endif /* AFS_NT40_ENV */
102 #if defined(AFS_SGI_ENV)
103 #include <sys/errno.h>
104 #include <sys/stat.h>
106 #include <sys/file.h>
113 #ifdef HAVE_STRINGS_H
119 #include <afs/afsint.h>
121 #include <afs/errors.h>
124 #include <afs/afssyscalls.h>
130 #include "namei_ops.h"
132 #endif /* AFS_NT40_ENV */
133 #endif /* AFS_NAMEI_ENV */
136 #include "partition.h"
137 #ifdef AFS_PTHREAD_ENV
139 #else /* AFS_PTHREAD_ENV */
140 #include <afs/assert.h>
141 #endif /* AFS_PTHREAD_ENV */
143 #if defined(AFS_HPUX_ENV)
144 #include <sys/types.h>
145 #include <sys/privgrp.h>
146 #endif /* defined(AFS_HPUX_ENV) */
149 #include <jfs/filsys.h>
154 #define afs_stat stat64
155 #define afs_open open64
156 #define afs_fopen fopen64
158 #if AFS_HAVE_STATVFS64
159 # define afs_statvfs statvfs64
161 # if AFS_HAVE_STATFS64
162 # define afs_statfs statfs64
164 # if AFS_HAVE_STATVFS
165 # define afs_statvfs statvfs
167 # define afs_statfs statfs
168 # endif /* !AFS_HAVE_STATVFS */
169 # endif /* !AFS_HAVE_STATFS64 */
170 #endif /* !AFS_HAVE_STATVFS64 */
171 #endif /* !AFS_NT40_ENV */
173 #else /* !O_LARGEFILE */
175 #define afs_stat stat
176 #define afs_open open
177 #define afs_fopen fopen
180 #define afs_statvfs statvfs
181 #else /* !AFS_HAVE_STATVFS */
182 #define afs_statfs statfs
183 #endif /* !AFS_HAVE_STATVFS */
184 #endif /* !AFS_NT40_ENV */
186 #endif /* !O_LARGEFILE */
188 /*@printflike@*/ extern void Log(const char *format, ...);
190 int aixlow_water = 8; /* default 8% */
191 struct DiskPartition *DiskPartitionList;
193 #ifdef AFS_DEMAND_ATTACH_FS
194 static struct DiskPartition *DiskPartitionTable[VOLMAXPARTS+1];
196 static struct DiskPartition * VLookupPartition_r(char * path);
197 static void AddPartitionToTable_r(struct DiskPartition *);
198 static void DeletePartitionFromTable_r(struct DiskPartition *);
199 #endif /* AFS_DEMAND_ATTACH_FS */
201 #ifdef AFS_SGI_XFS_IOPS_ENV
202 /* Verify that the on disk XFS inodes on the partition are large enough to
203 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
204 * too small to fit in the inode. Returns 0 if the attribute does fit in
207 #include <afs/xfsattrs.h>
209 VerifyXFSInodeSize(char *part, char *fstype)
212 int length = SIZEOF_XFS_ATTR_T;
217 if (strcmp("xfs", fstype))
220 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
221 if (((fd = open(part, O_RDONLY, 0)) != -1)
222 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
224 if (fsx.fsx_nextents) {
225 Log("Partition %s: XFS inodes too small, exiting.\n", part);
226 Log("Run xfs_size_check utility and remake partitions.\n");
233 (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
237 #endif /* AFS_SGI_XFS_IOPS_ENV */
240 VInitPartitionPackage(void)
242 #ifdef AFS_DEMAND_ATTACH_ENV
243 memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
244 #endif /* AFS_DEMAND_ATTACH_ENV */
249 VInitPartition_r(char *path, char *devname, Device dev)
251 struct DiskPartition *dp, *op;
252 dp = (struct DiskPartition *)malloc(sizeof(struct DiskPartition));
253 /* Add it to the end, to preserve order when we print statistics */
254 for (op = DiskPartitionList; op; op = op->next) {
261 DiskPartitionList = dp;
263 dp->name = (char *)malloc(strlen(path) + 1);
264 strncpy(dp->name, path, strlen(path) + 1);
265 dp->index = volutil_GetPartitionID(path);
266 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
267 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
268 dp->devName = (char *)malloc(2 * strlen(path) + 6);
269 strcpy(dp->devName, path);
270 strcat(dp->devName, "/");
271 strcat(dp->devName, "Lock");
272 mkdir(dp->devName, 0700);
273 strcat(dp->devName, path);
274 close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
275 dp->device = dp->index;
277 dp->devName = (char *)malloc(strlen(devname) + 1);
278 strncpy(dp->devName, devname, strlen(devname) + 1);
283 dp->f_files = 1; /* just a default value */
284 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
285 if (programType == fileServer)
286 (void)namei_ViceREADME(VPartitionPath(dp));
288 VSetPartitionDiskUsage_r(dp);
289 #ifdef AFS_DEMAND_ATTACH_FS
290 AddPartitionToTable_r(dp);
291 queue_Init(&dp->vol_list);
292 assert(pthread_cond_init(&dp->vol_list.cv, NULL) == 0);
293 #endif /* AFS_DEMAND_ATTACH_FS */
297 VInitPartition(char *path, char *devname, Device dev)
300 VInitPartition_r(path, devname, dev);
305 /* VAttachPartitions() finds the vice partitions on this server. Calls
306 * VCheckPartition() to do some basic checks on the partition. If the partition
307 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
309 * Returns the number of errors returned by VCheckPartition. An error in
310 * VCheckPartition means that partition is a valid vice partition but the
311 * fileserver should not start because of the error found on that partition.
314 * No specific user space file system checks, since we don't know what
315 * is being used for vice partitions.
317 * Use partition name as devname.
320 VCheckPartition(char *part, char *devname)
322 struct afs_stat status;
323 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
324 char AFSIDatPath[MAXPATHLEN];
327 /* Only keep track of "/vicepx" partitions since it can get hairy
328 * when NFS mounts are involved.. */
329 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
332 if (afs_stat(part, &status) < 0) {
333 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
336 #ifndef AFS_AIX32_ENV
337 if (programType == fileServer) {
338 char salvpath[MAXPATHLEN];
339 strcpy(salvpath, part);
340 strcat(salvpath, "/FORCESALVAGE");
341 if (afs_stat(salvpath, &status) == 0) {
342 Log("VInitVnodes: Found %s; aborting\n", salvpath);
348 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
349 strcpy(AFSIDatPath, part);
350 strcat(AFSIDatPath, "/AFSIDat");
352 if (afs_stat(AFSIDatPath, &status) < 0) {
356 dirp = opendir(part);
358 while (dp = readdir(dirp)) {
359 if (dp->d_name[0] == 'V') {
360 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);
367 #else /* AFS_NAMEI_ENV */
368 if (afs_stat(AFSIDatPath, &status) == 0) {
369 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
373 #ifdef AFS_SGI_XFS_IOPS_ENV
374 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
377 #endif /* AFS_NAMEI_ENV */
378 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
380 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
381 if (status.st_ino != ROOTINO) {
382 Log("%s is not a mounted file system; ignored.\n", part);
387 VInitPartition(part, devname, status.st_dev);
392 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
393 * attached (return value 1), or only attached when it is a separately
394 * mounted partition (return value 0). For non-NAMEI environments, it
398 VIsAlwaysAttach(char *part)
405 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
408 strncpy(checkfile, part, 100);
409 strcat(checkfile, "/");
410 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
412 ret = afs_stat(checkfile, &st);
413 return (ret < 0) ? 0 : 1;
414 #else /* AFS_NAMEI_ENV */
416 #endif /* AFS_NAMEI_ENV */
419 /* VAttachPartitions2() looks for and attaches /vicepX partitions
420 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
421 * used to attach /vicepX directories which aren't on dedicated
422 * partitions, in the NAMEI fileserver.
433 while ((de = readdir(dirp))) {
435 strncat(pname, de->d_name, 20);
436 pname[sizeof(pname) - 1] = '\0';
438 /* Only keep track of "/vicepx" partitions since automounter
440 if (VIsAlwaysAttach(pname))
441 VCheckPartition(pname, "");
444 #endif /* AFS_NAMEI_ENV */
446 #endif /* AFS_NT40_ENV */
450 VAttachPartitions(void)
456 if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
457 Log("Can't open %s\n", MNTTAB);
461 while (!getmntent(mntfile, &mnt)) {
462 /* Ignore non ufs or non read/write partitions */
463 /* but allow zfs too if we're in the NAMEI environment */
466 ((!(strcmp(mnt.mnt_fstype, "ufs") &&
467 strcmp(mnt.mnt_fstype, "zfs"))))
469 (strcmp(mnt.mnt_fstype, "ufs") != 0)
471 || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
474 /* If we're going to always attach this partition, do it later. */
475 if (VIsAlwaysAttach(mnt.mnt_mountp))
478 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0)
482 (void)fclose(mntfile);
484 /* Process the always-attach partitions, if any. */
485 VAttachPartitions2();
490 #endif /* AFS_SUN5_ENV */
491 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
493 VAttachPartitions(void)
497 struct mntent *mntent;
499 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
500 Log("Problems in getting mount entries(setmntent)\n");
503 while (mntent = getmntent(mfd)) {
504 if (!hasmntopt(mntent, MNTOPT_RW))
507 /* If we're going to always attach this partition, do it later. */
508 if (VIsAlwaysAttach(mntent->mnt_dir))
511 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
517 /* Process the always-attach partitions, if any. */
518 VAttachPartitions2();
525 * (This function was grabbed from df.c)
528 getmount(register struct vmount **vmountpp)
531 register struct vmount *vm;
534 /* set initial size of mntctl buffer to a MAGIC NUMBER */
537 /* try the operation until ok or a fatal error */
539 if ((vm = (struct vmount *)malloc(size)) == NULL) {
540 /* failed getting memory for mount status buf */
541 perror("FATAL ERROR: get_stat malloc failed\n");
546 * perform the QUERY mntctl - if it returns > 0, that is the
547 * number of vmount structures in the buffer. If it returns
548 * -1, an error occured. If it returned 0, then look in
549 * first word of buffer for needed size.
551 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
552 /* OK, got it, now return */
556 } else if (nmounts == 0) {
557 /* the buffer wasn't big enough .... */
558 /* .... get required buffer size */
563 /* some other kind of error occurred */
571 VAttachPartitions(void)
575 struct vmount *vmountp;
577 if ((nmounts = getmount(&vmountp)) <= 0) {
578 Log("Problems in getting # of mount entries(getmount)\n");
583 (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
584 char *part = vmt2dataptr(vmountp, VMT_STUB);
586 if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
587 continue; /* Ignore any "special" partitions */
590 #ifndef AFS_NAMEI_ENV
592 struct superblock fs;
593 /* The Log statements are non-sequiters in the SalvageLog and don't
594 * even appear in the VolserLog, so restrict them to the FileLog.
596 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
597 if (programType == fileServer)
598 Log("Can't read superblock for %s, ignoring it.\n", part);
601 if (IsBigFilesFileSystem(&fs)) {
602 if (programType == fileServer)
603 Log("%s is a big files filesystem, ignoring it.\n", part);
610 /* If we're going to always attach this partition, do it later. */
611 if (VIsAlwaysAttach(part))
614 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0)
618 /* Process the always-attach partitions, if any. */
619 VAttachPartitions2();
624 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
626 VAttachPartitions(void)
631 if (setfsent() < 0) {
632 Log("Error listing filesystems.\n");
636 while (fsent = getfsent()) {
637 if (strcmp(fsent->fs_type, "rw") != 0)
640 /* If we're going to always attach this partition, do it later. */
641 if (VIsAlwaysAttach(fsent->fs_file))
644 if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0)
649 /* Process the always-attach partitions, if any. */
650 VAttachPartitions2();
658 #include <sys/stat.h>
661 * validate names in vptab.
669 VValidVPTEntry(struct vptab *vpe)
671 int len = strlen(vpe->vp_name);
674 if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
676 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
679 for (i = VICE_PREFIX_SIZE; i < len; i++) {
680 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
681 Log("Invalid partition name %s in registry, ignoring it.\n",
686 if (len == VICE_PREFIX_SIZE + 2) {
687 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
688 (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
690 Log("Invalid partition name %s in registry, ignoring it.\n",
696 len = strlen(vpe->vp_dev);
697 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
698 || vpe->vp_dev[0] > 'Z') {
699 Log("Invalid device name %s in registry, ignoring it.\n",
708 VCheckPartition(char *partName)
715 /* partName is presumed to be of the form "X:" */
716 (void)sprintf(volRoot, "%c:\\", *partName);
718 if (!GetVolumeInformation(volRoot, /* volume root directory */
719 NULL, /* volume name buffer */
720 0, /* volume name size */
721 NULL, /* volume serial number */
722 &dwDummy, /* max component length */
723 &dwDummy, /* file system flags */
724 volFsType, /* file system name */
725 sizeof(volFsType))) {
726 err = GetLastError();
727 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
731 if (strcmp(volFsType, "NTFS")) {
732 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
741 VAttachPartitions(void)
743 struct DiskPartition *partP, *prevP, *nextP;
744 struct vpt_iter iter;
747 if (vpt_Start(&iter) < 0) {
748 Log("No partitions to attach.\n");
752 while (0 == vpt_NextEntry(&iter, &entry)) {
753 if (!VValidVPTEntry(&entry)) {
757 /* This test for duplicates relies on the fact that the method
758 * of storing the partition names in the NT registry means the same
759 * partition name will never appear twice in the list.
761 for (partP = DiskPartitionList; partP; partP = partP->next) {
762 if (*partP->devName == *entry.vp_dev) {
763 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
764 partP->flags = PART_DUPLICATE;
765 break; /* Only one entry will ever be in this list. */
769 continue; /* found a duplicate */
771 if (VCheckPartition(entry.vp_dev) < 0)
773 /* This test allows for manually inserting the FORCESALVAGE flag
774 * and thereby invoking the salvager. scandisk obviously won't be
777 if (programType == fileServer) {
778 struct afs_stat status;
779 char salvpath[MAXPATHLEN];
780 strcpy(salvpath, entry.vp_dev);
781 strcat(salvpath, "\\FORCESALVAGE");
782 if (afs_stat(salvpath, &status) == 0) {
783 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
787 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
791 /* Run through partition list and clear out the dupes. */
792 prevP = nextP = NULL;
793 for (partP = DiskPartitionList; partP; partP = nextP) {
795 if (partP->flags == PART_DUPLICATE) {
797 prevP->next = partP->next;
799 DiskPartitionList = partP->next;
809 #ifdef AFS_LINUX22_ENV
811 VAttachPartitions(void)
815 struct mntent *mntent;
817 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
818 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
819 Log("Problems in getting mount entries(setmntent)\n");
823 while ((mntent = getmntent(mfd))) {
824 /* If we're going to always attach this partition, do it later. */
825 if (VIsAlwaysAttach(mntent->mnt_dir))
828 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
833 /* Process the always-attach partitions, if any. */
834 VAttachPartitions2();
838 #endif /* AFS_LINUX22_ENV */
840 /* This routine is to be called whenever the actual name of the partition
841 * is required. The canonical name is still in part->name.
844 VPartitionPath(struct DiskPartition *part)
847 return part->devName;
853 /* get partition structure, abortp tells us if we should abort on failure */
854 struct DiskPartition *
855 VGetPartition_r(char *name, int abortp)
857 register struct DiskPartition *dp;
858 #ifdef AFS_DEMAND_ATTACH_FS
859 dp = VLookupPartition_r(name);
860 #else /* AFS_DEMAND_ATTACH_FS */
861 for (dp = DiskPartitionList; dp; dp = dp->next) {
862 if (strcmp(dp->name, name) == 0)
865 #endif /* AFS_DEMAND_ATTACH_FS */
871 struct DiskPartition *
872 VGetPartition(char *name, int abortp)
874 struct DiskPartition *retVal;
876 retVal = VGetPartition_r(name, abortp);
883 VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
885 ULARGE_INTEGER free_user, total, free_total;
886 int ufree, tot, tfree;
888 if (!GetDiskFreeSpaceEx
889 (VPartitionPath(dp), &free_user, &total, &free_total)) {
890 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
895 /* Convert to 1K units. */
896 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
897 tot = (int)Int64ShraMod32(total.QuadPart, 10);
898 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
900 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
901 dp->totalUsable = tot;
907 VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
909 int fd, totalblks, free, used, availblks, bsize, code;
912 struct afs_statvfs statbuf;
914 struct afs_statfs statbuf;
917 if (dp->flags & PART_DONTUPDATE)
919 /* Note: we don't bother syncing because it's only an estimate, update
920 * is syncing every 30 seconds anyway, we only have to keep the disk
921 * approximately 10% from full--you just can't get the stuff in from
922 * the net fast enough to worry */
924 code = afs_statvfs(dp->name, &statbuf);
926 code = afs_statfs(dp->name, &statbuf);
929 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
933 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
934 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
937 totalblks = statbuf.f_blocks;
938 free = statbuf.f_bfree;
939 reserved = free - statbuf.f_bavail;
941 bsize = statbuf.f_frsize;
943 bsize = statbuf.f_bsize;
945 availblks = totalblks - reserved;
946 dp->f_files = statbuf.f_files; /* max # of files in partition */
948 /* Now free and totalblks are in fragment units, but we want them in
952 free *= (bsize / 1024);
953 totalblks *= (bsize / 1024);
954 availblks *= (bsize / 1024);
955 reserved *= (bsize / 1024);
957 free /= (1024 / bsize);
958 totalblks /= (1024 / bsize);
959 availblks /= (1024 / bsize);
960 reserved /= (1024 / bsize);
962 /* now compute remaining figures */
963 used = totalblks - free;
965 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
966 dp->totalUsable = availblks;
967 dp->free = availblks - used; /* this is exactly f_bavail */
969 #endif /* AFS_NT40_ENV */
972 VSetPartitionDiskUsage(register struct DiskPartition *dp)
975 VSetPartitionDiskUsage_r(dp);
980 VResetDiskUsage_r(void)
982 struct DiskPartition *dp;
983 for (dp = DiskPartitionList; dp; dp = dp->next) {
984 VSetPartitionDiskUsage_r(dp);
985 #ifndef AFS_PTHREAD_ENV
987 #endif /* !AFS_PTHREAD_ENV */
992 VResetDiskUsage(void)
1000 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1001 afs_sfsize_t checkBlocks)
1004 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1005 * for less than BlocksSpare would skip the error-checking path, and we
1006 * could grow existing files forever, not just for another BlocksSpare
1009 #ifdef AFS_AIX32_ENV
1010 afs_int32 rem, minavail;
1012 if ((rem = vp->partition->free - checkBlocks) < (minavail =
1018 if (vp->partition->free - checkBlocks < 0)
1021 else if (V_maxquota(vp)
1022 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1025 vp->partition->free -= blocks;
1026 V_diskused(vp) += blocks;
1030 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1031 afs_sfsize_t checkBlocks)
1034 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1039 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1042 #ifdef AFS_AIX32_ENV
1043 afs_int32 rem, minavail;
1045 if ((rem = vp->partition->free - blocks) < (minavail =
1048 aixlow_water) / 100))
1050 if (vp->partition->free - blocks < 0)
1054 vp->partition->free -= blocks;
1059 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1063 retVal = VDiskUsage_r(vp, blocks);
1069 VPrintDiskStats_r(void)
1071 struct DiskPartition *dp;
1072 for (dp = DiskPartitionList; dp; dp = dp->next) {
1073 Log("Partition %s: %d available 1K blocks (minfree=%d), ", dp->name,
1074 dp->totalUsable, dp->minFree);
1076 Log("overallocated by %d blocks\n", -dp->free);
1078 Log("%d free blocks\n", dp->free);
1083 VPrintDiskStats(void)
1086 VPrintDiskStats_r();
1091 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1092 #define LOCKFILE "LOCKFILE"
1094 VLockPartition_r(char *name)
1096 struct DiskPartition *dp = VGetPartition_r(name, 0);
1101 if (dp->lock_fd == -1) {
1104 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1106 (int)CreateFile(path, GENERIC_WRITE,
1107 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1108 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1109 assert(dp->lock_fd != (int)INVALID_HANDLE_VALUE);
1111 memset((char *)&lap, 0, sizeof(lap));
1112 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1119 VUnlockPartition_r(char *name)
1121 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1125 return; /* no partition, will fail later */
1126 memset((char *)&lap, 0, sizeof(lap));
1128 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1129 CloseHandle((HANDLE) dp->lock_fd);
1132 #else /* AFS_NT40_ENV */
1134 #if defined(AFS_HPUX_ENV)
1135 #define BITS_PER_CHAR (8)
1136 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1138 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1139 #endif /* defined(AFS_HPUX_ENV) */
1142 VLockPartition_r(char *name)
1144 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1145 char *partitionName;
1147 struct timeval pausing;
1148 #if defined(AFS_HPUX_ENV)
1150 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1151 unsigned int *globalMask;
1152 int globalMaskIndex;
1153 #endif /* defined(AFS_HPUX_ENV) */
1154 #if defined(AFS_DARWIN_ENV)
1155 char lockfile[MAXPATHLEN];
1156 #endif /* defined(AFS_DARWIN_ENV) */
1157 #ifdef AFS_NAMEI_ENV
1158 #ifdef AFS_AIX42_ENV
1159 char LockFileName[MAXPATHLEN + 1];
1161 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1162 partitionName = (char *)&LockFileName;
1167 return; /* no partition, will fail later */
1168 if (dp->lock_fd != -1)
1171 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1172 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1173 partitionName = dp->devName;
1176 #elif defined(AFS_DARWIN_ENV)
1177 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1178 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1179 code = O_RDONLY | O_CREAT;
1181 partitionName = dp->name;
1185 for (retries = 25; retries; retries--) {
1186 dp->lock_fd = afs_open(partitionName, code);
1187 if (dp->lock_fd != -1)
1189 if (errno == ENOENT)
1192 pausing.tv_usec = 500000;
1193 select(0, NULL, NULL, NULL, &pausing);
1195 assert(retries != 0);
1197 #if defined (AFS_HPUX_ENV)
1199 assert(getprivgrp(privGrpList) == 0);
1202 * In general, it will difficult and time-consuming ,if not impossible,
1203 * to try to find the privgroup to which this process belongs that has the
1204 * smallest membership, to minimise the security hole. So, we use the privgrp
1205 * to which everybody belongs.
1207 /* first, we have to find the global mask */
1208 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1209 globalMaskIndex++) {
1210 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1212 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1217 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1218 /* allow everybody to set a lock on a read-only file descriptor */
1219 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1220 assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1223 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1225 /* remove the privilege granted to everybody to lock a read-only fd */
1226 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1227 assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1230 /* in this case, we should be able to do this with impunity, anyway */
1231 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1234 assert(lockfRtn != -1);
1236 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1237 assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1239 assert(flock(dp->lock_fd, LOCK_EX) == 0);
1240 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1245 VUnlockPartition_r(char *name)
1247 register struct DiskPartition *dp = VGetPartition_r(name, 0);
1249 return; /* no partition, will fail later */
1254 #endif /* AFS_NT40_ENV */
1257 VLockPartition(char *name)
1260 VLockPartition_r(name);
1265 VUnlockPartition(char *name)
1268 VUnlockPartition_r(name);
1272 #ifdef AFS_DEMAND_ATTACH_FS
1273 /* XXX not sure this will work on AFS_NT40_ENV
1274 * needs to be tested!
1276 struct DiskPartition *
1277 VGetPartitionById_r(afs_int32 id, int abortp)
1279 struct DiskPartition * dp = NULL;
1281 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1282 dp = DiskPartitionTable[id];
1291 struct DiskPartition *
1292 VGetPartitionById(afs_int32 id, int abortp)
1294 struct Diskpartition * dp;
1297 dp = VGetPartitionById_r(id, abortp);
1303 static struct DiskPartition *
1304 VLookupPartition_r(char * path)
1306 afs_int32 id = volutil_GetPartitionID(path);
1308 if (id < 0 || id > VOLMAXPARTS)
1311 return DiskPartitionTable[id];
1315 AddPartitionToTable_r(struct DiskPartition * dp)
1317 assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1318 DiskPartitionTable[dp->index] = dp;
1322 DeletePartitionFromTable_r(struct DiskPartition * dp)
1324 assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1325 DiskPartitionTable[dp->index] = NULL;
1327 #endif /* AFS_DEMAND_ATTACH_FS */