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>
34 #if AFS_HAVE_STATVFS || AFS_HAVE_STATVFS64
35 #include <sys/statvfs.h>
36 #endif /* AFS_HAVE_STATVFS */
37 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
38 #include <sys/mount.h>
41 #if !defined(AFS_SGI_ENV)
43 #include <sys/mount.h>
45 #else /* AFS_OSF_ENV */
46 #ifdef AFS_VFSINCL_ENV
49 #include <sys/fs/ufs_fs.h>
51 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ffs/fs.h>
58 #else /* AFS_VFSINCL_ENV */
59 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
62 #endif /* AFS_VFSINCL_ENV */
63 #endif /* AFS_OSF_ENV */
67 #include <sys/lockf.h>
71 #include <checklist.h>
73 #if defined(AFS_SUN_ENV)
80 #include <sys/mnttab.h>
81 #include <sys/mntent.h>
83 #ifdef AFS_LINUX22_ENV
85 #include <sys/statfs.h>
92 #endif /* AFS_SGI_ENV */
93 #endif /* AFS_NT40_ENV */
94 #if defined(AFS_SGI_ENV)
100 #include <afs/afsint.h>
102 #include <afs/errors.h>
105 #include <afs/afssyscalls.h>
112 #include "namei_ops.h"
113 #endif /* AFS_NT40_ENV */
114 #endif /* AFS_NAMEI_ENV */
117 #include "partition.h"
119 #if defined(AFS_HPUX_ENV)
120 #include <sys/privgrp.h>
121 #endif /* defined(AFS_HPUX_ENV) */
124 #include <jfs/filsys.h>
127 int aixlow_water = 8; /* default 8% */
128 struct DiskPartition64 *DiskPartitionList;
130 #ifdef AFS_DEMAND_ATTACH_FS
131 /* file to lock to conceptually "lock" the vol headers on a partition */
132 #define AFS_PARTLOCK_FILE ".volheaders.lock"
133 #define AFS_VOLUMELOCK_FILE ".volume.lock"
135 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
137 static struct DiskPartition64 * VLookupPartition_r(char * path);
138 static void AddPartitionToTable_r(struct DiskPartition64 *);
139 #endif /* AFS_DEMAND_ATTACH_FS */
141 #ifdef AFS_SGI_XFS_IOPS_ENV
142 /* Verify that the on disk XFS inodes on the partition are large enough to
143 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
144 * too small to fit in the inode. Returns 0 if the attribute does fit in
147 #include <afs/xfsattrs.h>
149 VerifyXFSInodeSize(char *part, char *fstype)
152 int length = SIZEOF_XFS_ATTR_T;
157 if (strcmp("xfs", fstype))
160 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
161 if (((fd = open(part, O_RDONLY, 0)) != -1)
162 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
164 if (fsx.fsx_nextents) {
165 Log("Partition %s: XFS inodes too small, exiting.\n", part);
166 Log("Run xfs_size_check utility and remake partitions.\n");
173 (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
177 #endif /* AFS_SGI_XFS_IOPS_ENV */
180 VInitPartitionPackage(void)
182 #ifdef AFS_DEMAND_ATTACH_FS
183 memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
184 #endif /* AFS_DEMAND_ATTACH_FS */
189 VInitPartition_r(char *path, char *devname, Device dev)
191 struct DiskPartition64 *dp, *op;
193 dp = (struct DiskPartition64 *)malloc(sizeof(struct DiskPartition64));
194 /* Add it to the end, to preserve order when we print statistics */
195 for (op = DiskPartitionList; op; op = op->next) {
202 DiskPartitionList = dp;
204 dp->name = (char *)malloc(strlen(path) + 1);
205 strncpy(dp->name, path, strlen(path) + 1);
206 dp->index = volutil_GetPartitionID(path);
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, OS_DIRSEP);
212 strcat(dp->devName, "Lock");
213 mkdir(dp->devName, 0700);
214 strcat(dp->devName, path);
215 close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
216 dp->device = dp->index;
218 dp->devName = (char *)malloc(strlen(devname) + 1);
219 strncpy(dp->devName, devname, strlen(devname) + 1);
222 dp->lock_fd = INVALID_FD;
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);
230 #ifdef AFS_DEMAND_ATTACH_FS
231 AddPartitionToTable_r(dp);
232 queue_Init(&dp->vol_list.head);
233 CV_INIT(&dp->vol_list.cv, "vol list", CV_DEFAULT, 0);
234 dp->vol_list.len = 0;
235 dp->vol_list.busy = 0;
237 char lockpath[MAXPATHLEN+1];
238 snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
239 lockpath[MAXPATHLEN] = '\0';
240 VLockFileInit(&dp->headerLockFile, lockpath);
242 snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
243 lockpath[MAXPATHLEN] = '\0';
244 VLockFileInit(&dp->volLockFile, lockpath);
246 VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
247 #endif /* AFS_DEMAND_ATTACH_FS */
251 VInitPartition(char *path, char *devname, Device dev)
254 VInitPartition_r(path, devname, dev);
259 /* VAttachPartitions() finds the vice partitions on this server. Calls
260 * VCheckPartition() to do some basic checks on the partition. If the partition
261 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
263 * Returns the number of errors returned by VCheckPartition. An error in
264 * VCheckPartition means that partition is a valid vice partition but the
265 * fileserver should not start because of the error found on that partition.
268 * No specific user space file system checks, since we don't know what
269 * is being used for vice partitions.
271 * Use partition name as devname.
274 VCheckPartition(char *part, char *devname, int logging)
276 struct afs_stat_st status;
277 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
278 char AFSIDatPath[MAXPATHLEN];
281 /* Only keep track of "/vicepx" partitions since it can get hairy
282 * when NFS mounts are involved.. */
283 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
286 if (afs_stat(part, &status) < 0) {
287 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
291 Log("This program is compiled without AFS_NAMEI_ENV, and "
292 "partition %s is mounted with the 'logging' option. "
293 "Using the inode fileserver backend with 'logging' UFS "
294 "partitions causes volume corruption, so please either "
295 "mount the partition without logging, or use the namei "
296 "fileserver backend. Aborting...\n", part);
299 #ifndef AFS_AIX32_ENV
300 if (programType == fileServer) {
301 char salvpath[MAXPATHLEN];
302 strcpy(salvpath, part);
303 strcat(salvpath, "/FORCESALVAGE");
304 if (afs_stat(salvpath, &status) == 0) {
305 Log("VInitVnodes: Found %s; aborting\n", salvpath);
311 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
312 strcpy(AFSIDatPath, part);
313 strcat(AFSIDatPath, "/AFSIDat");
315 if (afs_stat(AFSIDatPath, &status) < 0) {
319 dirp = opendir(part);
321 while ((dp = readdir(dirp))) {
322 if (dp->d_name[0] == 'V') {
323 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);
330 #else /* AFS_NAMEI_ENV */
331 if (afs_stat(AFSIDatPath, &status) == 0) {
332 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
336 #ifdef AFS_SGI_XFS_IOPS_ENV
337 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
340 #endif /* AFS_NAMEI_ENV */
341 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
343 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
344 if (status.st_ino != ROOTINO) {
345 Log("%s is not a mounted file system; ignored.\n", part);
350 VInitPartition(part, devname, status.st_dev);
355 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
356 * attached (return value 1), or only attached when it is a separately
357 * mounted partition (return value 0). For non-NAMEI environments, it
360 * *awouldattach will be set to 1 if the given path at least looks like a vice
361 * partition (that is, if we return 0, the only thing preventing this partition
362 * from being attached is the existence of the AlwaysAttach file), or to 0
363 * otherwise. *awouldattach is set regardless of whether or not the partition
364 * should always be attached or not.
367 VIsAlwaysAttach(char *part, int *awouldattach)
370 struct afs_stat_st st;
373 #endif /* AFS_NAMEI_ENV */
380 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
387 strncpy(checkfile, part, 100);
388 strcat(checkfile, OS_DIRSEP);
389 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
391 ret = afs_stat(checkfile, &st);
392 return (ret < 0) ? 0 : 1;
393 #else /* AFS_NAMEI_ENV */
395 #endif /* AFS_NAMEI_ENV */
398 /* VIsNeverAttach() checks whether a /vicepX directory should never be
399 * attached (return value 1), or follow the normal mounting logic. The
400 * Always Attach flag may override the NeverAttach flag.
403 VIsNeverAttach(char *part)
405 struct afs_stat_st st;
409 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
412 strncpy(checkfile, part, 100);
413 strcat(checkfile, OS_DIRSEP);
414 strcat(checkfile, VICE_NEVERATTACH_FILE);
416 ret = afs_stat(checkfile, &st);
417 return (ret < 0) ? 0 : 1;
420 /* VAttachPartitions2() looks for and attaches /vicepX partitions
421 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
422 * used to attach /vicepX directories which aren't on dedicated
423 * partitions, in the NAMEI fileserver.
426 VAttachPartitions2(void)
434 dirp = opendir(OS_DIRSEP);
435 while ((de = readdir(dirp))) {
436 strcpy(pname, OS_DIRSEP);
437 strncat(pname, de->d_name, 20);
438 pname[sizeof(pname) - 1] = '\0';
440 /* Only keep track of "/vicepx" partitions since automounter
442 if (VIsAlwaysAttach(pname, &wouldattach)) {
443 VCheckPartition(pname, "", 0);
445 struct afs_stat_st st;
446 if (wouldattach && VGetPartition(pname, 0) == NULL &&
447 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
449 /* This is a /vicep* dir, and it has not been attached as a
450 * partition. This probably means that this is a /vicep* dir
451 * that is not a separate partition, so just give a notice so
452 * admins are not confused as to why their /vicep* dirs are not
455 * It is possible that the dir _is_ a separate partition and we
456 * failed to attach it earlier, making this message a bit
457 * confusing. But that should be rare, and an error message
458 * about the failure will already be logged right before this,
459 * so it should be clear enough. */
461 Log("VAttachPartitions: not attaching %s; either it is not a "
462 "separate partition, or it failed to attach (create the "
463 "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
469 #endif /* AFS_NAMEI_ENV */
471 #endif /* AFS_NT40_ENV */
475 VAttachPartitions(void)
481 if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
482 Log("Can't open %s\n", MNTTAB);
486 while (!getmntent(mntfile, &mnt)) {
488 /* Ignore non ufs or non read/write partitions */
489 /* but allow zfs too if we're in the NAMEI environment */
492 (((strcmp(mnt.mnt_fstype, "ufs") &&
493 strcmp(mnt.mnt_fstype, "zfs"))))
495 (strcmp(mnt.mnt_fstype, "ufs") != 0)
497 || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
500 /* Skip this Partition? */
501 if (VIsNeverAttach(mnt.mnt_mountp))
504 /* If we're going to always attach this partition, do it later. */
505 if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
508 #ifndef AFS_NAMEI_ENV
509 if (hasmntopt(&mnt, "logging") != NULL) {
512 #endif /* !AFS_NAMEI_ENV */
514 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special, logging) < 0)
518 (void)fclose(mntfile);
520 /* Process the always-attach partitions, if any. */
521 VAttachPartitions2();
526 #endif /* AFS_SUN5_ENV */
527 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
529 VAttachPartitions(void)
533 struct mntent *mntent;
535 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
536 Log("Problems in getting mount entries(setmntent)\n");
539 while (mntent = getmntent(mfd)) {
540 if (!hasmntopt(mntent, MNTOPT_RW))
543 /* Skip this Partition? */
544 if (VIsNeverAttach(mntent->mnt_dir))
547 /* If we're going to always attach this partition, do it later. */
548 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
551 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
557 /* Process the always-attach partitions, if any. */
558 VAttachPartitions2();
565 * (This function was grabbed from df.c)
568 getmount(struct vmount **vmountpp)
574 /* set initial size of mntctl buffer to a MAGIC NUMBER */
577 /* try the operation until ok or a fatal error */
579 if ((vm = (struct vmount *)malloc(size)) == NULL) {
580 /* failed getting memory for mount status buf */
581 perror("FATAL ERROR: get_stat malloc failed\n");
586 * perform the QUERY mntctl - if it returns > 0, that is the
587 * number of vmount structures in the buffer. If it returns
588 * -1, an error occured. If it returned 0, then look in
589 * first word of buffer for needed size.
591 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
592 /* OK, got it, now return */
596 } else if (nmounts == 0) {
597 /* the buffer wasn't big enough .... */
598 /* .... get required buffer size */
603 /* some other kind of error occurred */
611 VAttachPartitions(void)
615 struct vmount *vmountp;
617 if ((nmounts = getmount(&vmountp)) <= 0) {
618 Log("Problems in getting # of mount entries(getmount)\n");
623 (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
624 char *part = vmt2dataptr(vmountp, VMT_STUB);
626 if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
627 continue; /* Ignore any "special" partitions */
630 #ifndef AFS_NAMEI_ENV
632 struct superblock fs;
633 /* The Log statements are non-sequiters in the SalvageLog and don't
634 * even appear in the VolserLog, so restrict them to the FileLog.
636 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
637 if (programType == fileServer)
638 Log("Can't read superblock for %s, ignoring it.\n", part);
641 if (IsBigFilesFileSystem(&fs)) {
642 if (programType == fileServer)
643 Log("%s is a big files filesystem, ignoring it.\n", part);
650 /* Skip this Partition? */
651 if (VIsNeverAttach(part))
654 /* If we're going to always attach this partition, do it later. */
655 if (VIsAlwaysAttach(part, NULL))
658 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT), 0) < 0)
662 /* Process the always-attach partitions, if any. */
663 VAttachPartitions2();
668 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
670 VAttachPartitions(void)
675 if (setfsent() < 0) {
676 Log("Error listing filesystems.\n");
680 while ((fsent = getfsent())) {
681 if (strcmp(fsent->fs_type, "rw") != 0)
684 /* Skip this Partition? */
685 if (VIsNeverAttach(fsent->fs_file))
688 /* If we're going to always attach this partition, do it later. */
689 if (VIsAlwaysAttach(fsent->fs_file, NULL))
692 if (VCheckPartition(fsent->fs_file, fsent->fs_spec, 0) < 0)
697 /* Process the always-attach partitions, if any. */
698 VAttachPartitions2();
707 * validate names in vptab.
715 VValidVPTEntry(struct vptab *vpe)
717 int len = strlen(vpe->vp_name);
720 if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
722 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
725 for (i = VICE_PREFIX_SIZE; i < len; i++) {
726 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
727 Log("Invalid partition name %s in registry, ignoring it.\n",
732 if (len == VICE_PREFIX_SIZE + 2) {
733 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
734 (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
736 Log("Invalid partition name %s in registry, ignoring it.\n",
742 len = strlen(vpe->vp_dev);
743 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
744 || vpe->vp_dev[0] > 'Z') {
745 Log("Invalid device name %s in registry, ignoring it.\n",
754 VCheckPartition(char *partName)
761 /* partName is presumed to be of the form "X:" */
762 (void)sprintf(volRoot, "%c:\\", *partName);
764 if (!GetVolumeInformation(volRoot, /* volume root directory */
765 NULL, /* volume name buffer */
766 0, /* volume name size */
767 NULL, /* volume serial number */
768 &dwDummy, /* max component length */
769 &dwDummy, /* file system flags */
770 volFsType, /* file system name */
771 sizeof(volFsType))) {
772 err = GetLastError();
773 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
777 if (strcmp(volFsType, "NTFS")) {
778 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
787 VAttachPartitions(void)
789 struct DiskPartition64 *partP, *prevP, *nextP;
790 struct vpt_iter iter;
793 if (vpt_Start(&iter) < 0) {
794 Log("No partitions to attach.\n");
798 while (0 == vpt_NextEntry(&iter, &entry)) {
799 if (!VValidVPTEntry(&entry)) {
803 /* This test for duplicates relies on the fact that the method
804 * of storing the partition names in the NT registry means the same
805 * partition name will never appear twice in the list.
807 for (partP = DiskPartitionList; partP; partP = partP->next) {
808 if (*partP->devName == *entry.vp_dev) {
809 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
810 partP->flags = PART_DUPLICATE;
811 break; /* Only one entry will ever be in this list. */
815 continue; /* found a duplicate */
817 if (VCheckPartition(entry.vp_dev) < 0)
819 /* This test allows for manually inserting the FORCESALVAGE flag
820 * and thereby invoking the salvager. scandisk obviously won't be
823 if (programType == fileServer) {
824 struct afs_stat_st status;
825 char salvpath[MAXPATHLEN];
826 strcpy(salvpath, entry.vp_dev);
827 strcat(salvpath, "\\FORCESALVAGE");
828 if (afs_stat(salvpath, &status) == 0) {
829 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
833 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
837 /* Run through partition list and clear out the dupes. */
838 prevP = nextP = NULL;
839 for (partP = DiskPartitionList; partP; partP = nextP) {
841 if (partP->flags == PART_DUPLICATE) {
843 prevP->next = partP->next;
845 DiskPartitionList = partP->next;
855 #ifdef AFS_LINUX22_ENV
857 VAttachPartitions(void)
861 struct mntent *mntent;
863 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
864 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
865 Log("Problems in getting mount entries(setmntent)\n");
869 while ((mntent = getmntent(mfd))) {
870 /* Skip this Partition? */
871 if (VIsNeverAttach(mntent->mnt_dir))
874 /* If we're going to always attach this partition, do it later. */
875 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
878 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
883 /* Process the always-attach partitions, if any. */
884 VAttachPartitions2();
888 #endif /* AFS_LINUX22_ENV */
890 /* This routine is to be called whenever the actual name of the partition
891 * is required. The canonical name is still in part->name.
894 VPartitionPath(struct DiskPartition64 *part)
897 return part->devName;
903 /* get partition structure, abortp tells us if we should abort on failure */
904 struct DiskPartition64 *
905 VGetPartition_r(char *name, int abortp)
907 struct DiskPartition64 *dp;
908 #ifdef AFS_DEMAND_ATTACH_FS
909 dp = VLookupPartition_r(name);
910 #else /* AFS_DEMAND_ATTACH_FS */
911 for (dp = DiskPartitionList; dp; dp = dp->next) {
912 if (strcmp(dp->name, name) == 0)
915 #endif /* AFS_DEMAND_ATTACH_FS */
917 osi_Assert(dp != NULL);
921 struct DiskPartition64 *
922 VGetPartition(char *name, int abortp)
924 struct DiskPartition64 *retVal;
926 retVal = VGetPartition_r(name, abortp);
933 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
935 ULARGE_INTEGER free_user, total, free_total;
936 int ufree, tot, tfree;
938 if (!GetDiskFreeSpaceEx
939 (VPartitionPath(dp), &free_user, &total, &free_total)) {
940 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
945 /* Convert to 1K units. */
946 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
947 tot = (int)Int64ShraMod32(total.QuadPart, 10);
948 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
950 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
951 dp->totalUsable = tot;
957 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
960 afs_int64 totalblks, free, used, availblks;
963 struct afs_statvfs statbuf;
965 struct afs_statfs statbuf;
968 if (dp->flags & PART_DONTUPDATE)
970 /* Note: we don't bother syncing because it's only an estimate, update
971 * is syncing every 30 seconds anyway, we only have to keep the disk
972 * approximately 10% from full--you just can't get the stuff in from
973 * the net fast enough to worry */
975 code = afs_statvfs(dp->name, &statbuf);
977 code = afs_statfs(dp->name, &statbuf);
980 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
984 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
985 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
988 totalblks = statbuf.f_blocks;
989 free = statbuf.f_bfree;
990 reserved = free - statbuf.f_bavail;
992 bsize = statbuf.f_frsize;
994 bsize = statbuf.f_bsize;
996 availblks = totalblks - reserved;
997 dp->f_files = statbuf.f_files; /* max # of files in partition */
999 /* Now free and totalblks are in fragment units, but we want them in
1002 if (bsize >= 1024) {
1003 free *= (bsize / 1024);
1004 totalblks *= (bsize / 1024);
1005 availblks *= (bsize / 1024);
1006 reserved *= (bsize / 1024);
1008 free /= (1024 / bsize);
1009 totalblks /= (1024 / bsize);
1010 availblks /= (1024 / bsize);
1011 reserved /= (1024 / bsize);
1013 /* now compute remaining figures */
1014 used = totalblks - free;
1016 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
1017 dp->totalUsable = availblks;
1018 dp->free = availblks - used; /* this is exactly f_bavail */
1020 #endif /* AFS_NT40_ENV */
1023 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
1026 VSetPartitionDiskUsage_r(dp);
1031 VResetDiskUsage_r(void)
1033 struct DiskPartition64 *dp;
1034 for (dp = DiskPartitionList; dp; dp = dp->next) {
1035 VSetPartitionDiskUsage_r(dp);
1036 #ifndef AFS_PTHREAD_ENV
1038 #endif /* !AFS_PTHREAD_ENV */
1043 VResetDiskUsage(void)
1046 VResetDiskUsage_r();
1051 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1052 afs_sfsize_t checkBlocks)
1055 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1056 * for less than BlocksSpare would skip the error-checking path, and we
1057 * could grow existing files forever, not just for another BlocksSpare
1060 #ifdef AFS_AIX32_ENV
1061 afs_int32 rem, minavail;
1063 if ((rem = vp->partition->free - checkBlocks) < (minavail =
1069 if (vp->partition->free - checkBlocks < 0)
1072 else if (V_maxquota(vp)
1073 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1076 vp->partition->free -= blocks;
1077 V_diskused(vp) += blocks;
1081 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1082 afs_sfsize_t checkBlocks)
1085 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1090 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1093 #ifdef AFS_AIX32_ENV
1094 afs_int32 rem, minavail;
1096 if ((rem = vp->partition->free - blocks) < (minavail =
1099 aixlow_water) / 100))
1101 if (vp->partition->free - blocks < 0)
1105 vp->partition->free -= blocks;
1110 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1114 retVal = VDiskUsage_r(vp, blocks);
1120 VPrintDiskStats_r(void)
1122 struct DiskPartition64 *dp;
1123 for (dp = DiskPartitionList; dp; dp = dp->next) {
1125 Log("Partition %s: %lld "
1126 " available 1K blocks (minfree=%lld), "
1127 "overallocated by %lld blocks\n", dp->name,
1128 dp->totalUsable, dp->minFree, -dp->free);
1130 Log("Partition %s: %lld"
1131 " available 1K blocks (minfree=%lld), "
1132 "%lld free blocks\n", dp->name,
1133 dp->totalUsable, dp->minFree, dp->free);
1139 VPrintDiskStats(void)
1142 VPrintDiskStats_r();
1147 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1148 #define LOCKFILE "LOCKFILE"
1150 VLockPartition_r(char *name)
1152 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1157 if (dp->lock_fd == INVALID_FD) {
1160 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1162 (FD_t)CreateFile(path, GENERIC_WRITE,
1163 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1164 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1165 osi_Assert(dp->lock_fd != INVALID_FD);
1167 memset(&lap, 0, sizeof(lap));
1168 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1175 VUnlockPartition_r(char *name)
1177 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1181 return; /* no partition, will fail later */
1182 memset(&lap, 0, sizeof(lap));
1184 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1185 CloseHandle((HANDLE) dp->lock_fd);
1186 dp->lock_fd = INVALID_FD;
1188 #else /* AFS_NT40_ENV */
1190 #if defined(AFS_HPUX_ENV)
1191 #define BITS_PER_CHAR (8)
1192 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1194 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1195 #endif /* defined(AFS_HPUX_ENV) */
1198 VLockPartition_r(char *name)
1200 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1201 char *partitionName;
1203 struct timeval pausing;
1204 #if defined(AFS_HPUX_ENV)
1206 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1207 unsigned int *globalMask;
1208 int globalMaskIndex;
1209 #endif /* defined(AFS_HPUX_ENV) */
1210 #if defined(AFS_DARWIN_ENV)
1211 char lockfile[MAXPATHLEN];
1212 #endif /* defined(AFS_DARWIN_ENV) */
1213 #ifdef AFS_NAMEI_ENV
1214 #ifdef AFS_AIX42_ENV
1215 char LockFileName[MAXPATHLEN + 1];
1217 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1218 partitionName = (char *)&LockFileName;
1223 return; /* no partition, will fail later */
1224 if (dp->lock_fd != INVALID_FD)
1227 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1228 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1229 partitionName = dp->devName;
1232 #elif defined(AFS_DARWIN_ENV)
1233 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1234 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1235 code = O_RDONLY | O_CREAT;
1237 partitionName = dp->name;
1241 for (retries = 25; retries; retries--) {
1243 dp->lock_fd = afs_open(partitionName, code, 0644);
1245 dp->lock_fd = afs_open(partitionName, code);
1247 if (dp->lock_fd != INVALID_FD)
1249 if (errno == ENOENT)
1252 pausing.tv_usec = 500000;
1253 select(0, NULL, NULL, NULL, &pausing);
1255 osi_Assert(retries != 0);
1257 #if defined (AFS_HPUX_ENV)
1259 osi_Assert(getprivgrp(privGrpList) == 0);
1262 * In general, it will difficult and time-consuming ,if not impossible,
1263 * to try to find the privgroup to which this process belongs that has the
1264 * smallest membership, to minimise the security hole. So, we use the privgrp
1265 * to which everybody belongs.
1267 /* first, we have to find the global mask */
1268 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1269 globalMaskIndex++) {
1270 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1272 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1277 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1278 /* allow everybody to set a lock on a read-only file descriptor */
1279 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1280 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1283 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1285 /* remove the privilege granted to everybody to lock a read-only fd */
1286 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1287 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1290 /* in this case, we should be able to do this with impunity, anyway */
1291 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1294 osi_Assert(lockfRtn != -1);
1296 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1297 osi_Assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1299 osi_Assert(flock(dp->lock_fd, LOCK_EX) == 0);
1300 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1305 VUnlockPartition_r(char *name)
1307 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1309 return; /* no partition, will fail later */
1311 dp->lock_fd = INVALID_FD;
1314 #endif /* AFS_NT40_ENV */
1317 VLockPartition(char *name)
1320 VLockPartition_r(name);
1325 VUnlockPartition(char *name)
1328 VUnlockPartition_r(name);
1332 #ifdef AFS_DEMAND_ATTACH_FS
1334 /* new-style partition locks; these are only to have some mutual exclusion
1335 * between the VGC scanner and volume utilies creating/altering vol headers
1339 * lock a partition's vol headers.
1341 * @param[in] dp the partition to lock
1342 * @param[in] locktype READ_LOCK or WRITE_LOCK
1344 * @return operation status
1348 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1352 /* block on acquiring the lock */
1355 code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1357 Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1358 VPartitionPath(dp));
1364 * unlock a partition's vol headers.
1366 * @param[in] dp the partition to unlock
1367 * @param[in] locktype READ_LOCK or WRITE_LOCK
1370 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1372 VReleaseDiskLock(&dp->headerLock, locktype);
1375 /* XXX not sure this will work on AFS_NT40_ENV
1376 * needs to be tested!
1380 * lookup a disk partition object by its index number.
1382 * @param[in] id partition index number
1383 * @param[in] abortp see abortp usage note below
1385 * @return disk partition object
1386 * @retval NULL no such disk partition
1388 * @note when abortp is non-zero, lookups which would return
1389 * NULL will result in an assertion failure
1391 * @pre VOL_LOCK must be held
1393 * @internal volume package internal use only
1396 struct DiskPartition64 *
1397 VGetPartitionById_r(afs_int32 id, int abortp)
1399 struct DiskPartition64 *dp = NULL;
1401 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1402 dp = DiskPartitionTable[id];
1406 osi_Assert(dp != NULL);
1412 * lookup a disk partition object by its index number.
1414 * @param[in] id partition index number
1415 * @param[in] abortp see abortp usage note below
1417 * @return disk partition object
1418 * @retval NULL no such disk partition
1420 * @note when abortp is non-zero, lookups which would return
1421 * NULL will result in an assertion failure
1424 struct DiskPartition64 *
1425 VGetPartitionById(afs_int32 id, int abortp)
1427 struct DiskPartition64 * dp;
1430 dp = VGetPartitionById_r(id, abortp);
1436 static struct DiskPartition64 *
1437 VLookupPartition_r(char * path)
1439 afs_int32 id = volutil_GetPartitionID(path);
1441 if (id < 0 || id > VOLMAXPARTS)
1444 return DiskPartitionTable[id];
1448 AddPartitionToTable_r(struct DiskPartition64 *dp)
1450 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1451 DiskPartitionTable[dp->index] = dp;
1456 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1458 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1459 DiskPartitionTable[dp->index] = NULL;
1462 #endif /* AFS_DEMAND_ATTACH_FS */