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>
32 #include <sys/param.h>
33 #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>
111 #include <afs/afsint.h>
113 #include <afs/errors.h>
116 #include <afs/afssyscalls.h>
122 #include "namei_ops.h"
124 #endif /* AFS_NT40_ENV */
125 #endif /* AFS_NAMEI_ENV */
128 #include "partition.h"
129 #ifdef AFS_PTHREAD_ENV
131 #else /* AFS_PTHREAD_ENV */
132 #include <afs/assert.h>
133 #endif /* AFS_PTHREAD_ENV */
135 #if defined(AFS_HPUX_ENV)
136 #include <sys/types.h>
137 #include <sys/privgrp.h>
138 #endif /* defined(AFS_HPUX_ENV) */
141 #include <jfs/filsys.h>
146 #define afs_stat stat64
147 #define afs_open open64
148 #define afs_fopen fopen64
150 #if AFS_HAVE_STATVFS64
151 # define afs_statvfs statvfs64
153 # if AFS_HAVE_STATFS64
154 # define afs_statfs statfs64
156 # if AFS_HAVE_STATVFS
157 # define afs_statvfs statvfs
159 # define afs_statfs statfs
160 # endif /* !AFS_HAVE_STATVFS */
161 # endif /* !AFS_HAVE_STATFS64 */
162 #endif /* !AFS_HAVE_STATVFS64 */
163 #endif /* !AFS_NT40_ENV */
165 #else /* !O_LARGEFILE */
167 #define afs_stat stat
168 #define afs_open open
169 #define afs_fopen fopen
172 #define afs_statvfs statvfs
173 #else /* !AFS_HAVE_STATVFS */
174 #define afs_statfs statfs
175 #endif /* !AFS_HAVE_STATVFS */
176 #endif /* !AFS_NT40_ENV */
178 #endif /* !O_LARGEFILE */
180 /*@printflike@*/ extern void Log(const char *format, ...);
182 int aixlow_water = 8; /* default 8% */
183 struct DiskPartition64 *DiskPartitionList;
185 #ifdef AFS_DEMAND_ATTACH_FS
186 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
188 static struct DiskPartition64 * VLookupPartition_r(char * path);
189 static void AddPartitionToTable_r(struct DiskPartition64 *);
190 #endif /* AFS_DEMAND_ATTACH_FS */
192 #ifdef AFS_SGI_XFS_IOPS_ENV
193 /* Verify that the on disk XFS inodes on the partition are large enough to
194 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
195 * too small to fit in the inode. Returns 0 if the attribute does fit in
198 #include <afs/xfsattrs.h>
200 VerifyXFSInodeSize(char *part, char *fstype)
203 int length = SIZEOF_XFS_ATTR_T;
208 if (strcmp("xfs", fstype))
211 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
212 if (((fd = open(part, O_RDONLY, 0)) != -1)
213 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
215 if (fsx.fsx_nextents) {
216 Log("Partition %s: XFS inodes too small, exiting.\n", part);
217 Log("Run xfs_size_check utility and remake partitions.\n");
224 (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
228 #endif /* AFS_SGI_XFS_IOPS_ENV */
231 VInitPartitionPackage(void)
233 #ifdef AFS_DEMAND_ATTACH_ENV
234 memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
235 #endif /* AFS_DEMAND_ATTACH_ENV */
240 VInitPartition_r(char *path, char *devname, Device dev)
242 struct DiskPartition64 *dp, *op;
243 dp = (struct DiskPartition64 *)malloc(sizeof(struct DiskPartition64));
244 /* Add it to the end, to preserve order when we print statistics */
245 for (op = DiskPartitionList; op; op = op->next) {
252 DiskPartitionList = dp;
254 dp->name = (char *)malloc(strlen(path) + 1);
255 strncpy(dp->name, path, strlen(path) + 1);
256 dp->index = volutil_GetPartitionID(path);
257 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
258 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
259 dp->devName = (char *)malloc(2 * strlen(path) + 6);
260 strcpy(dp->devName, path);
261 strcat(dp->devName, "/");
262 strcat(dp->devName, "Lock");
263 mkdir(dp->devName, 0700);
264 strcat(dp->devName, path);
265 close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
266 dp->device = dp->index;
268 dp->devName = (char *)malloc(strlen(devname) + 1);
269 strncpy(dp->devName, devname, strlen(devname) + 1);
272 dp->lock_fd = INVALID_FD;
274 dp->f_files = 1; /* just a default value */
275 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
276 if (programType == fileServer)
277 (void)namei_ViceREADME(VPartitionPath(dp));
279 VSetPartitionDiskUsage_r(dp);
280 #ifdef AFS_DEMAND_ATTACH_FS
281 AddPartitionToTable_r(dp);
282 queue_Init(&dp->vol_list.head);
283 assert(pthread_cond_init(&dp->vol_list.cv, NULL) == 0);
284 dp->vol_list.len = 0;
285 dp->vol_list.busy = 0;
286 #endif /* AFS_DEMAND_ATTACH_FS */
290 VInitPartition(char *path, char *devname, Device dev)
293 VInitPartition_r(path, devname, dev);
298 /* VAttachPartitions() finds the vice partitions on this server. Calls
299 * VCheckPartition() to do some basic checks on the partition. If the partition
300 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
302 * Returns the number of errors returned by VCheckPartition. An error in
303 * VCheckPartition means that partition is a valid vice partition but the
304 * fileserver should not start because of the error found on that partition.
307 * No specific user space file system checks, since we don't know what
308 * is being used for vice partitions.
310 * Use partition name as devname.
313 VCheckPartition(char *part, char *devname)
315 struct afs_stat status;
316 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
317 char AFSIDatPath[MAXPATHLEN];
320 /* Only keep track of "/vicepx" partitions since it can get hairy
321 * when NFS mounts are involved.. */
322 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
325 if (afs_stat(part, &status) < 0) {
326 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
329 #ifndef AFS_AIX32_ENV
330 if (programType == fileServer) {
331 char salvpath[MAXPATHLEN];
332 strcpy(salvpath, part);
333 strcat(salvpath, "/FORCESALVAGE");
334 if (afs_stat(salvpath, &status) == 0) {
335 Log("VInitVnodes: Found %s; aborting\n", salvpath);
341 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
342 strcpy(AFSIDatPath, part);
343 strcat(AFSIDatPath, "/AFSIDat");
345 if (afs_stat(AFSIDatPath, &status) < 0) {
349 dirp = opendir(part);
351 while ((dp = readdir(dirp))) {
352 if (dp->d_name[0] == 'V') {
353 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);
360 #else /* AFS_NAMEI_ENV */
361 if (afs_stat(AFSIDatPath, &status) == 0) {
362 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
366 #ifdef AFS_SGI_XFS_IOPS_ENV
367 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
370 #endif /* AFS_NAMEI_ENV */
371 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
373 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
374 if (status.st_ino != ROOTINO) {
375 Log("%s is not a mounted file system; ignored.\n", part);
380 VInitPartition(part, devname, status.st_dev);
385 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
386 * attached (return value 1), or only attached when it is a separately
387 * mounted partition (return value 0). For non-NAMEI environments, it
391 VIsAlwaysAttach(char *part)
398 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
401 strncpy(checkfile, part, 100);
402 strcat(checkfile, "/");
403 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
405 ret = afs_stat(checkfile, &st);
406 return (ret < 0) ? 0 : 1;
407 #else /* AFS_NAMEI_ENV */
409 #endif /* AFS_NAMEI_ENV */
412 /* VAttachPartitions2() looks for and attaches /vicepX partitions
413 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
414 * used to attach /vicepX directories which aren't on dedicated
415 * partitions, in the NAMEI fileserver.
418 VAttachPartitions2(void)
426 while ((de = readdir(dirp))) {
428 strncat(pname, de->d_name, 20);
429 pname[sizeof(pname) - 1] = '\0';
431 /* Only keep track of "/vicepx" partitions since automounter
433 if (VIsAlwaysAttach(pname))
434 VCheckPartition(pname, "");
437 #endif /* AFS_NAMEI_ENV */
439 #endif /* AFS_NT40_ENV */
443 VAttachPartitions(void)
449 if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
450 Log("Can't open %s\n", MNTTAB);
454 while (!getmntent(mntfile, &mnt)) {
455 /* Ignore non ufs or non read/write partitions */
456 /* but allow zfs too if we're in the NAMEI environment */
459 (((strcmp(mnt.mnt_fstype, "ufs") &&
460 strcmp(mnt.mnt_fstype, "zfs"))))
462 (strcmp(mnt.mnt_fstype, "ufs") != 0)
464 || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
467 /* If we're going to always attach this partition, do it later. */
468 if (VIsAlwaysAttach(mnt.mnt_mountp))
471 #ifndef AFS_NAMEI_ENV
472 if (hasmntopt(&mnt, "logging") != NULL) {
473 Log("This program is compiled without AFS_NAMEI_ENV, and "
474 "partition %s is mounted with the 'logging' option. "
475 "Using the inode fileserver backend with 'logging' UFS "
476 "partitions causes volume corruption, so please either "
477 "mount the partition without logging, or use the namei "
478 "fileserver backend. Aborting...\n", mnt.mnt_mountp);
481 #endif /* !AFS_NAMEI_ENV */
483 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0)
487 (void)fclose(mntfile);
489 /* Process the always-attach partitions, if any. */
490 VAttachPartitions2();
495 #endif /* AFS_SUN5_ENV */
496 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
498 VAttachPartitions(void)
502 struct mntent *mntent;
504 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
505 Log("Problems in getting mount entries(setmntent)\n");
508 while (mntent = getmntent(mfd)) {
509 if (!hasmntopt(mntent, MNTOPT_RW))
512 /* If we're going to always attach this partition, do it later. */
513 if (VIsAlwaysAttach(mntent->mnt_dir))
516 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
522 /* Process the always-attach partitions, if any. */
523 VAttachPartitions2();
530 * (This function was grabbed from df.c)
533 getmount(register struct vmount **vmountpp)
536 register struct vmount *vm;
539 /* set initial size of mntctl buffer to a MAGIC NUMBER */
542 /* try the operation until ok or a fatal error */
544 if ((vm = (struct vmount *)malloc(size)) == NULL) {
545 /* failed getting memory for mount status buf */
546 perror("FATAL ERROR: get_stat malloc failed\n");
551 * perform the QUERY mntctl - if it returns > 0, that is the
552 * number of vmount structures in the buffer. If it returns
553 * -1, an error occured. If it returned 0, then look in
554 * first word of buffer for needed size.
556 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
557 /* OK, got it, now return */
561 } else if (nmounts == 0) {
562 /* the buffer wasn't big enough .... */
563 /* .... get required buffer size */
568 /* some other kind of error occurred */
576 VAttachPartitions(void)
580 struct vmount *vmountp;
582 if ((nmounts = getmount(&vmountp)) <= 0) {
583 Log("Problems in getting # of mount entries(getmount)\n");
588 (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
589 char *part = vmt2dataptr(vmountp, VMT_STUB);
591 if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
592 continue; /* Ignore any "special" partitions */
595 #ifndef AFS_NAMEI_ENV
597 struct superblock fs;
598 /* The Log statements are non-sequiters in the SalvageLog and don't
599 * even appear in the VolserLog, so restrict them to the FileLog.
601 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
602 if (programType == fileServer)
603 Log("Can't read superblock for %s, ignoring it.\n", part);
606 if (IsBigFilesFileSystem(&fs)) {
607 if (programType == fileServer)
608 Log("%s is a big files filesystem, ignoring it.\n", part);
615 /* If we're going to always attach this partition, do it later. */
616 if (VIsAlwaysAttach(part))
619 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0)
623 /* Process the always-attach partitions, if any. */
624 VAttachPartitions2();
629 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
631 VAttachPartitions(void)
636 if (setfsent() < 0) {
637 Log("Error listing filesystems.\n");
641 while ((fsent = getfsent())) {
642 if (strcmp(fsent->fs_type, "rw") != 0)
645 /* If we're going to always attach this partition, do it later. */
646 if (VIsAlwaysAttach(fsent->fs_file))
649 if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0)
654 /* Process the always-attach partitions, if any. */
655 VAttachPartitions2();
663 #include <sys/stat.h>
666 * validate names in vptab.
674 VValidVPTEntry(struct vptab *vpe)
676 int len = strlen(vpe->vp_name);
679 if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
681 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
684 for (i = VICE_PREFIX_SIZE; i < len; i++) {
685 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
686 Log("Invalid partition name %s in registry, ignoring it.\n",
691 if (len == VICE_PREFIX_SIZE + 2) {
692 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
693 (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
695 Log("Invalid partition name %s in registry, ignoring it.\n",
701 len = strlen(vpe->vp_dev);
702 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
703 || vpe->vp_dev[0] > 'Z') {
704 Log("Invalid device name %s in registry, ignoring it.\n",
713 VCheckPartition(char *partName)
720 /* partName is presumed to be of the form "X:" */
721 (void)sprintf(volRoot, "%c:\\", *partName);
723 if (!GetVolumeInformation(volRoot, /* volume root directory */
724 NULL, /* volume name buffer */
725 0, /* volume name size */
726 NULL, /* volume serial number */
727 &dwDummy, /* max component length */
728 &dwDummy, /* file system flags */
729 volFsType, /* file system name */
730 sizeof(volFsType))) {
731 err = GetLastError();
732 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
736 if (strcmp(volFsType, "NTFS")) {
737 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
746 VAttachPartitions(void)
748 struct DiskPartition64 *partP, *prevP, *nextP;
749 struct vpt_iter iter;
752 if (vpt_Start(&iter) < 0) {
753 Log("No partitions to attach.\n");
757 while (0 == vpt_NextEntry(&iter, &entry)) {
758 if (!VValidVPTEntry(&entry)) {
762 /* This test for duplicates relies on the fact that the method
763 * of storing the partition names in the NT registry means the same
764 * partition name will never appear twice in the list.
766 for (partP = DiskPartitionList; partP; partP = partP->next) {
767 if (*partP->devName == *entry.vp_dev) {
768 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
769 partP->flags = PART_DUPLICATE;
770 break; /* Only one entry will ever be in this list. */
774 continue; /* found a duplicate */
776 if (VCheckPartition(entry.vp_dev) < 0)
778 /* This test allows for manually inserting the FORCESALVAGE flag
779 * and thereby invoking the salvager. scandisk obviously won't be
782 if (programType == fileServer) {
783 struct afs_stat status;
784 char salvpath[MAXPATHLEN];
785 strcpy(salvpath, entry.vp_dev);
786 strcat(salvpath, "\\FORCESALVAGE");
787 if (afs_stat(salvpath, &status) == 0) {
788 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
792 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
796 /* Run through partition list and clear out the dupes. */
797 prevP = nextP = NULL;
798 for (partP = DiskPartitionList; partP; partP = nextP) {
800 if (partP->flags == PART_DUPLICATE) {
802 prevP->next = partP->next;
804 DiskPartitionList = partP->next;
814 #ifdef AFS_LINUX22_ENV
816 VAttachPartitions(void)
820 struct mntent *mntent;
822 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
823 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
824 Log("Problems in getting mount entries(setmntent)\n");
828 while ((mntent = getmntent(mfd))) {
829 /* If we're going to always attach this partition, do it later. */
830 if (VIsAlwaysAttach(mntent->mnt_dir))
833 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
838 /* Process the always-attach partitions, if any. */
839 VAttachPartitions2();
843 #endif /* AFS_LINUX22_ENV */
845 /* This routine is to be called whenever the actual name of the partition
846 * is required. The canonical name is still in part->name.
849 VPartitionPath(struct DiskPartition64 *part)
852 return part->devName;
858 /* get partition structure, abortp tells us if we should abort on failure */
859 struct DiskPartition64 *
860 VGetPartition_r(char *name, int abortp)
862 register struct DiskPartition64 *dp;
863 #ifdef AFS_DEMAND_ATTACH_FS
864 dp = VLookupPartition_r(name);
865 #else /* AFS_DEMAND_ATTACH_FS */
866 for (dp = DiskPartitionList; dp; dp = dp->next) {
867 if (strcmp(dp->name, name) == 0)
870 #endif /* AFS_DEMAND_ATTACH_FS */
876 struct DiskPartition64 *
877 VGetPartition(char *name, int abortp)
879 struct DiskPartition64 *retVal;
881 retVal = VGetPartition_r(name, abortp);
888 VSetPartitionDiskUsage_r(register struct DiskPartition64 *dp)
890 ULARGE_INTEGER free_user, total, free_total;
891 int ufree, tot, tfree;
893 if (!GetDiskFreeSpaceEx
894 (VPartitionPath(dp), &free_user, &total, &free_total)) {
895 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
900 /* Convert to 1K units. */
901 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
902 tot = (int)Int64ShraMod32(total.QuadPart, 10);
903 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
905 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
906 dp->totalUsable = tot;
912 VSetPartitionDiskUsage_r(register struct DiskPartition64 *dp)
915 afs_int64 totalblks, free, used, availblks;
918 struct afs_statvfs statbuf;
920 struct afs_statfs statbuf;
923 if (dp->flags & PART_DONTUPDATE)
925 /* Note: we don't bother syncing because it's only an estimate, update
926 * is syncing every 30 seconds anyway, we only have to keep the disk
927 * approximately 10% from full--you just can't get the stuff in from
928 * the net fast enough to worry */
930 code = afs_statvfs(dp->name, &statbuf);
932 code = afs_statfs(dp->name, &statbuf);
935 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
939 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
940 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
943 totalblks = statbuf.f_blocks;
944 free = statbuf.f_bfree;
945 reserved = free - statbuf.f_bavail;
947 bsize = statbuf.f_frsize;
949 bsize = statbuf.f_bsize;
951 availblks = totalblks - reserved;
952 dp->f_files = statbuf.f_files; /* max # of files in partition */
954 /* Now free and totalblks are in fragment units, but we want them in
958 free *= (bsize / 1024);
959 totalblks *= (bsize / 1024);
960 availblks *= (bsize / 1024);
961 reserved *= (bsize / 1024);
963 free /= (1024 / bsize);
964 totalblks /= (1024 / bsize);
965 availblks /= (1024 / bsize);
966 reserved /= (1024 / bsize);
968 /* now compute remaining figures */
969 used = totalblks - free;
971 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
972 dp->totalUsable = availblks;
973 dp->free = availblks - used; /* this is exactly f_bavail */
975 #endif /* AFS_NT40_ENV */
978 VSetPartitionDiskUsage(register struct DiskPartition64 *dp)
981 VSetPartitionDiskUsage_r(dp);
986 VResetDiskUsage_r(void)
988 struct DiskPartition64 *dp;
989 for (dp = DiskPartitionList; dp; dp = dp->next) {
990 VSetPartitionDiskUsage_r(dp);
991 #ifndef AFS_PTHREAD_ENV
993 #endif /* !AFS_PTHREAD_ENV */
998 VResetDiskUsage(void)
1001 VResetDiskUsage_r();
1006 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1007 afs_sfsize_t checkBlocks)
1010 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1011 * for less than BlocksSpare would skip the error-checking path, and we
1012 * could grow existing files forever, not just for another BlocksSpare
1015 #ifdef AFS_AIX32_ENV
1016 afs_int32 rem, minavail;
1018 if ((rem = vp->partition->free - checkBlocks) < (minavail =
1024 if (vp->partition->free - checkBlocks < 0)
1027 else if (V_maxquota(vp)
1028 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1031 vp->partition->free -= blocks;
1032 V_diskused(vp) += blocks;
1036 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1037 afs_sfsize_t checkBlocks)
1040 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1045 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1048 #ifdef AFS_AIX32_ENV
1049 afs_int32 rem, minavail;
1051 if ((rem = vp->partition->free - blocks) < (minavail =
1054 aixlow_water) / 100))
1056 if (vp->partition->free - blocks < 0)
1060 vp->partition->free -= blocks;
1065 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1069 retVal = VDiskUsage_r(vp, blocks);
1075 VPrintDiskStats_r(void)
1077 struct DiskPartition64 *dp;
1078 for (dp = DiskPartitionList; dp; dp = dp->next) {
1079 Log("Partition %s: %d available 1K blocks (minfree=%d), ", dp->name,
1080 dp->totalUsable, dp->minFree);
1082 Log("overallocated by %d blocks\n", -dp->free);
1084 Log("%d free blocks\n", dp->free);
1089 VPrintDiskStats(void)
1092 VPrintDiskStats_r();
1097 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1098 #define LOCKFILE "LOCKFILE"
1100 VLockPartition_r(char *name)
1102 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1107 if (dp->lock_fd == INVALID_FD) {
1110 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1112 (FD_t)CreateFile(path, GENERIC_WRITE,
1113 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1114 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1115 assert(dp->lock_fd != INVALID_FD);
1117 memset(&lap, 0, sizeof(lap));
1118 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1125 VUnlockPartition_r(char *name)
1127 register struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1131 return; /* no partition, will fail later */
1132 memset(&lap, 0, sizeof(lap));
1134 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1135 CloseHandle((HANDLE) dp->lock_fd);
1136 dp->lock_fd = INVALID_FD;
1138 #else /* AFS_NT40_ENV */
1140 #if defined(AFS_HPUX_ENV)
1141 #define BITS_PER_CHAR (8)
1142 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1144 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1145 #endif /* defined(AFS_HPUX_ENV) */
1148 VLockPartition_r(char *name)
1150 register struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1151 char *partitionName;
1153 struct timeval pausing;
1154 #if defined(AFS_HPUX_ENV)
1156 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1157 unsigned int *globalMask;
1158 int globalMaskIndex;
1159 #endif /* defined(AFS_HPUX_ENV) */
1160 #if defined(AFS_DARWIN_ENV)
1161 char lockfile[MAXPATHLEN];
1162 #endif /* defined(AFS_DARWIN_ENV) */
1163 #ifdef AFS_NAMEI_ENV
1164 #ifdef AFS_AIX42_ENV
1165 char LockFileName[MAXPATHLEN + 1];
1167 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1168 partitionName = (char *)&LockFileName;
1173 return; /* no partition, will fail later */
1174 if (dp->lock_fd != -1)
1177 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1178 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1179 partitionName = dp->devName;
1182 #elif defined(AFS_DARWIN_ENV)
1183 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1184 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1185 code = O_RDONLY | O_CREAT;
1187 partitionName = dp->name;
1191 for (retries = 25; retries; retries--) {
1192 dp->lock_fd = afs_open(partitionName, code);
1193 if (dp->lock_fd != -1)
1195 if (errno == ENOENT)
1198 pausing.tv_usec = 500000;
1199 select(0, NULL, NULL, NULL, &pausing);
1201 assert(retries != 0);
1203 #if defined (AFS_HPUX_ENV)
1205 assert(getprivgrp(privGrpList) == 0);
1208 * In general, it will difficult and time-consuming ,if not impossible,
1209 * to try to find the privgroup to which this process belongs that has the
1210 * smallest membership, to minimise the security hole. So, we use the privgrp
1211 * to which everybody belongs.
1213 /* first, we have to find the global mask */
1214 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1215 globalMaskIndex++) {
1216 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1218 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1223 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1224 /* allow everybody to set a lock on a read-only file descriptor */
1225 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1226 assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1229 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1231 /* remove the privilege granted to everybody to lock a read-only fd */
1232 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1233 assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1236 /* in this case, we should be able to do this with impunity, anyway */
1237 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1240 assert(lockfRtn != -1);
1242 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1243 assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1245 assert(flock(dp->lock_fd, LOCK_EX) == 0);
1246 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1251 VUnlockPartition_r(char *name)
1253 register struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1255 return; /* no partition, will fail later */
1260 #endif /* AFS_NT40_ENV */
1263 VLockPartition(char *name)
1266 VLockPartition_r(name);
1271 VUnlockPartition(char *name)
1274 VUnlockPartition_r(name);
1278 #ifdef AFS_DEMAND_ATTACH_FS
1280 /* XXX not sure this will work on AFS_NT40_ENV
1281 * needs to be tested!
1285 * lookup a disk partition object by its index number.
1287 * @param[in] id partition index number
1288 * @param[in] abortp see abortp usage note below
1290 * @return disk partition object
1291 * @retval NULL no such disk partition
1293 * @note when abortp is non-zero, lookups which would return
1294 * NULL will result in an assertion failure
1296 * @pre VOL_LOCK must be held
1298 * @internal volume package internal use only
1301 struct DiskPartition64 *
1302 VGetPartitionById_r(afs_int32 id, int abortp)
1304 struct DiskPartition64 *dp = NULL;
1306 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1307 dp = DiskPartitionTable[id];
1317 * lookup a disk partition object by its index number.
1319 * @param[in] id partition index number
1320 * @param[in] abortp see abortp usage note below
1322 * @return disk partition object
1323 * @retval NULL no such disk partition
1325 * @note when abortp is non-zero, lookups which would return
1326 * NULL will result in an assertion failure
1329 struct DiskPartition64 *
1330 VGetPartitionById(afs_int32 id, int abortp)
1332 struct DiskPartition64 * dp;
1335 dp = VGetPartitionById_r(id, abortp);
1341 static struct DiskPartition64 *
1342 VLookupPartition_r(char * path)
1344 afs_int32 id = volutil_GetPartitionID(path);
1346 if (id < 0 || id > VOLMAXPARTS)
1349 return DiskPartitionTable[id];
1353 AddPartitionToTable_r(struct DiskPartition64 *dp)
1355 assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1356 DiskPartitionTable[dp->index] = dp;
1361 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1363 assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1364 DiskPartitionTable[dp->index] = NULL;
1367 #endif /* AFS_DEMAND_ATTACH_FS */