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 = 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 = strdup(path);
205 dp->index = volutil_GetPartitionID(path);
206 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
207 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
208 dp->devName = malloc(2 * strlen(path) + 6);
209 strcpy(dp->devName, path);
210 strcat(dp->devName, OS_DIRSEP);
211 strcat(dp->devName, "Lock");
212 mkdir(dp->devName, 0700);
213 strcat(dp->devName, path);
214 close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
215 dp->device = dp->index;
217 dp->devName = strdup(devname);
220 dp->lock_fd = INVALID_FD;
222 dp->f_files = 1; /* just a default value */
223 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
224 if (programType == fileServer)
225 (void)namei_ViceREADME(VPartitionPath(dp));
227 VSetPartitionDiskUsage_r(dp);
228 #ifdef AFS_DEMAND_ATTACH_FS
229 AddPartitionToTable_r(dp);
230 queue_Init(&dp->vol_list.head);
231 CV_INIT(&dp->vol_list.cv, "vol list", CV_DEFAULT, 0);
232 dp->vol_list.len = 0;
233 dp->vol_list.busy = 0;
235 char lockpath[MAXPATHLEN+1];
236 snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
237 lockpath[MAXPATHLEN] = '\0';
238 VLockFileInit(&dp->headerLockFile, lockpath);
240 snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
241 lockpath[MAXPATHLEN] = '\0';
242 VLockFileInit(&dp->volLockFile, lockpath);
244 VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
245 #endif /* AFS_DEMAND_ATTACH_FS */
249 VInitPartition(char *path, char *devname, Device dev)
252 VInitPartition_r(path, devname, dev);
257 /* VAttachPartitions() finds the vice partitions on this server. Calls
258 * VCheckPartition() to do some basic checks on the partition. If the partition
259 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
261 * Returns the number of errors returned by VCheckPartition. An error in
262 * VCheckPartition means that partition is a valid vice partition but the
263 * fileserver should not start because of the error found on that partition.
266 * No specific user space file system checks, since we don't know what
267 * is being used for vice partitions.
269 * Use partition name as devname.
272 VCheckPartition(char *part, char *devname, int logging)
274 struct afs_stat_st status;
275 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
276 char AFSIDatPath[MAXPATHLEN];
279 /* Only keep track of "/vicepx" partitions since it can get hairy
280 * when NFS mounts are involved.. */
281 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
284 if (afs_stat(part, &status) < 0) {
285 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
289 Log("This program is compiled without AFS_NAMEI_ENV, and "
290 "partition %s is mounted with the 'logging' option. "
291 "Using the inode fileserver backend with 'logging' UFS "
292 "partitions causes volume corruption, so please either "
293 "mount the partition without logging, or use the namei "
294 "fileserver backend. Aborting...\n", part);
297 #ifndef AFS_AIX32_ENV
298 if (programType == fileServer) {
299 char salvpath[MAXPATHLEN];
300 strcpy(salvpath, part);
301 strcat(salvpath, "/FORCESALVAGE");
302 if (afs_stat(salvpath, &status) == 0) {
303 Log("VInitVnodes: Found %s; aborting\n", salvpath);
309 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
310 strcpy(AFSIDatPath, part);
311 strcat(AFSIDatPath, "/AFSIDat");
313 if (afs_stat(AFSIDatPath, &status) < 0) {
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 (afs_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);
334 #ifdef AFS_SGI_XFS_IOPS_ENV
335 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
338 #endif /* AFS_NAMEI_ENV */
339 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
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 * *awouldattach will be set to 1 if the given path at least looks like a vice
359 * partition (that is, if we return 0, the only thing preventing this partition
360 * from being attached is the existence of the AlwaysAttach file), or to 0
361 * otherwise. *awouldattach is set regardless of whether or not the partition
362 * should always be attached or not.
365 VIsAlwaysAttach(char *part, int *awouldattach)
368 struct afs_stat_st st;
371 #endif /* AFS_NAMEI_ENV */
378 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
385 strncpy(checkfile, part, 100);
386 strcat(checkfile, OS_DIRSEP);
387 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
389 ret = afs_stat(checkfile, &st);
390 return (ret < 0) ? 0 : 1;
391 #else /* AFS_NAMEI_ENV */
393 #endif /* AFS_NAMEI_ENV */
396 /* VIsNeverAttach() checks whether a /vicepX directory should never be
397 * attached (return value 1), or follow the normal mounting logic. The
398 * Always Attach flag may override the NeverAttach flag.
401 VIsNeverAttach(char *part)
403 struct afs_stat_st st;
407 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
410 strncpy(checkfile, part, 100);
411 strcat(checkfile, OS_DIRSEP);
412 strcat(checkfile, VICE_NEVERATTACH_FILE);
414 ret = afs_stat(checkfile, &st);
415 return (ret < 0) ? 0 : 1;
418 /* VAttachPartitions2() looks for and attaches /vicepX partitions
419 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
420 * used to attach /vicepX directories which aren't on dedicated
421 * partitions, in the NAMEI fileserver.
424 VAttachPartitions2(void)
432 dirp = opendir(OS_DIRSEP);
433 while ((de = readdir(dirp))) {
434 strcpy(pname, OS_DIRSEP);
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, &wouldattach)) {
441 VCheckPartition(pname, "", 0);
443 struct afs_stat_st st;
444 if (wouldattach && VGetPartition(pname, 0) == NULL &&
445 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
447 /* This is a /vicep* dir, and it has not been attached as a
448 * partition. This probably means that this is a /vicep* dir
449 * that is not a separate partition, so just give a notice so
450 * admins are not confused as to why their /vicep* dirs are not
453 * It is possible that the dir _is_ a separate partition and we
454 * failed to attach it earlier, making this message a bit
455 * confusing. But that should be rare, and an error message
456 * about the failure will already be logged right before this,
457 * so it should be clear enough. */
459 Log("VAttachPartitions: not attaching %s; either it is not a "
460 "separate partition, or it failed to attach (create the "
461 "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
467 #endif /* AFS_NAMEI_ENV */
469 #endif /* AFS_NT40_ENV */
473 VAttachPartitions(void)
479 if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
480 Log("Can't open %s\n", MNTTAB);
484 while (!getmntent(mntfile, &mnt)) {
486 /* Ignore non ufs or non read/write partitions */
487 /* but allow zfs too if we're in the NAMEI environment */
490 (((strcmp(mnt.mnt_fstype, "ufs") &&
491 strcmp(mnt.mnt_fstype, "zfs"))))
493 (strcmp(mnt.mnt_fstype, "ufs") != 0)
495 || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
498 /* Skip this Partition? */
499 if (VIsNeverAttach(mnt.mnt_mountp))
502 /* If we're going to always attach this partition, do it later. */
503 if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
506 #ifndef AFS_NAMEI_ENV
507 if (hasmntopt(&mnt, "logging") != NULL) {
510 #endif /* !AFS_NAMEI_ENV */
512 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special, logging) < 0)
516 (void)fclose(mntfile);
518 /* Process the always-attach partitions, if any. */
519 VAttachPartitions2();
524 #endif /* AFS_SUN5_ENV */
525 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
527 VAttachPartitions(void)
531 struct mntent *mntent;
533 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
534 Log("Problems in getting mount entries(setmntent)\n");
537 while (mntent = getmntent(mfd)) {
538 if (!hasmntopt(mntent, MNTOPT_RW))
541 /* Skip this Partition? */
542 if (VIsNeverAttach(mntent->mnt_dir))
545 /* If we're going to always attach this partition, do it later. */
546 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
549 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
555 /* Process the always-attach partitions, if any. */
556 VAttachPartitions2();
563 * (This function was grabbed from df.c)
566 getmount(struct vmount **vmountpp)
572 /* set initial size of mntctl buffer to a MAGIC NUMBER */
575 /* try the operation until ok or a fatal error */
577 if ((vm = malloc(size)) == NULL) {
578 /* failed getting memory for mount status buf */
579 perror("FATAL ERROR: get_stat malloc failed\n");
584 * perform the QUERY mntctl - if it returns > 0, that is the
585 * number of vmount structures in the buffer. If it returns
586 * -1, an error occured. If it returned 0, then look in
587 * first word of buffer for needed size.
589 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
590 /* OK, got it, now return */
594 } else if (nmounts == 0) {
595 /* the buffer wasn't big enough .... */
596 /* .... get required buffer size */
601 /* some other kind of error occurred */
609 VAttachPartitions(void)
613 struct vmount *vmountp;
615 if ((nmounts = getmount(&vmountp)) <= 0) {
616 Log("Problems in getting # of mount entries(getmount)\n");
621 (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
622 char *part = vmt2dataptr(vmountp, VMT_STUB);
624 if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
625 continue; /* Ignore any "special" partitions */
628 #ifndef AFS_NAMEI_ENV
630 struct superblock fs;
631 /* The Log statements are non-sequiters in the SalvageLog and don't
632 * even appear in the VolserLog, so restrict them to the FileLog.
634 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
635 if (programType == fileServer)
636 Log("Can't read superblock for %s, ignoring it.\n", part);
639 if (IsBigFilesFileSystem(&fs)) {
640 if (programType == fileServer)
641 Log("%s is a big files filesystem, ignoring it.\n", part);
648 /* Skip this Partition? */
649 if (VIsNeverAttach(part))
652 /* If we're going to always attach this partition, do it later. */
653 if (VIsAlwaysAttach(part, NULL))
656 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT), 0) < 0)
660 /* Process the always-attach partitions, if any. */
661 VAttachPartitions2();
666 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
668 VAttachPartitions(void)
673 if (setfsent() < 0) {
674 Log("Error listing filesystems.\n");
678 while ((fsent = getfsent())) {
679 if (strcmp(fsent->fs_type, "rw") != 0)
682 /* Skip this Partition? */
683 if (VIsNeverAttach(fsent->fs_file))
686 /* If we're going to always attach this partition, do it later. */
687 if (VIsAlwaysAttach(fsent->fs_file, NULL))
690 if (VCheckPartition(fsent->fs_file, fsent->fs_spec, 0) < 0)
695 /* Process the always-attach partitions, if any. */
696 VAttachPartitions2();
705 * validate names in vptab.
713 VValidVPTEntry(struct vptab *vpe)
715 int len = strlen(vpe->vp_name);
718 if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
720 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
723 for (i = VICE_PREFIX_SIZE; i < len; i++) {
724 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
725 Log("Invalid partition name %s in registry, ignoring it.\n",
730 if (len == VICE_PREFIX_SIZE + 2) {
731 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
732 (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
734 Log("Invalid partition name %s in registry, ignoring it.\n",
740 len = strlen(vpe->vp_dev);
741 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
742 || vpe->vp_dev[0] > 'Z') {
743 Log("Invalid device name %s in registry, ignoring it.\n",
752 VCheckPartition(char *partName)
759 /* partName is presumed to be of the form "X:" */
760 (void)sprintf(volRoot, "%c:\\", *partName);
762 if (!GetVolumeInformation(volRoot, /* volume root directory */
763 NULL, /* volume name buffer */
764 0, /* volume name size */
765 NULL, /* volume serial number */
766 &dwDummy, /* max component length */
767 &dwDummy, /* file system flags */
768 volFsType, /* file system name */
769 sizeof(volFsType))) {
770 err = GetLastError();
771 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
775 if (strcmp(volFsType, "NTFS")) {
776 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
785 VAttachPartitions(void)
787 struct DiskPartition64 *partP, *prevP, *nextP;
788 struct vpt_iter iter;
791 if (vpt_Start(&iter) < 0) {
792 Log("No partitions to attach.\n");
796 while (0 == vpt_NextEntry(&iter, &entry)) {
797 if (!VValidVPTEntry(&entry)) {
801 /* This test for duplicates relies on the fact that the method
802 * of storing the partition names in the NT registry means the same
803 * partition name will never appear twice in the list.
805 for (partP = DiskPartitionList; partP; partP = partP->next) {
806 if (*partP->devName == *entry.vp_dev) {
807 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
808 partP->flags = PART_DUPLICATE;
809 break; /* Only one entry will ever be in this list. */
813 continue; /* found a duplicate */
815 if (VCheckPartition(entry.vp_dev) < 0)
817 /* This test allows for manually inserting the FORCESALVAGE flag
818 * and thereby invoking the salvager. scandisk obviously won't be
821 if (programType == fileServer) {
822 struct afs_stat_st status;
823 char salvpath[MAXPATHLEN];
824 strcpy(salvpath, entry.vp_dev);
825 strcat(salvpath, "\\FORCESALVAGE");
826 if (afs_stat(salvpath, &status) == 0) {
827 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
831 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
835 /* Run through partition list and clear out the dupes. */
836 prevP = nextP = NULL;
837 for (partP = DiskPartitionList; partP; partP = nextP) {
839 if (partP->flags == PART_DUPLICATE) {
841 prevP->next = partP->next;
843 DiskPartitionList = partP->next;
853 #ifdef AFS_LINUX22_ENV
855 VAttachPartitions(void)
859 struct mntent *mntent;
861 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
862 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
863 Log("Problems in getting mount entries(setmntent)\n");
867 while ((mntent = getmntent(mfd))) {
868 /* Skip this Partition? */
869 if (VIsNeverAttach(mntent->mnt_dir))
872 /* If we're going to always attach this partition, do it later. */
873 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
876 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
881 /* Process the always-attach partitions, if any. */
882 VAttachPartitions2();
886 #endif /* AFS_LINUX22_ENV */
888 /* This routine is to be called whenever the actual name of the partition
889 * is required. The canonical name is still in part->name.
892 VPartitionPath(struct DiskPartition64 *part)
895 return part->devName;
901 /* get partition structure, abortp tells us if we should abort on failure */
902 struct DiskPartition64 *
903 VGetPartition_r(char *name, int abortp)
905 struct DiskPartition64 *dp;
906 #ifdef AFS_DEMAND_ATTACH_FS
907 dp = VLookupPartition_r(name);
908 #else /* AFS_DEMAND_ATTACH_FS */
909 for (dp = DiskPartitionList; dp; dp = dp->next) {
910 if (strcmp(dp->name, name) == 0)
913 #endif /* AFS_DEMAND_ATTACH_FS */
915 osi_Assert(dp != NULL);
919 struct DiskPartition64 *
920 VGetPartition(char *name, int abortp)
922 struct DiskPartition64 *retVal;
924 retVal = VGetPartition_r(name, abortp);
931 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
933 ULARGE_INTEGER free_user, total, free_total;
934 int ufree, tot, tfree;
936 if (!GetDiskFreeSpaceEx
937 (VPartitionPath(dp), &free_user, &total, &free_total)) {
938 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
943 /* Convert to 1K units. */
944 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
945 tot = (int)Int64ShraMod32(total.QuadPart, 10);
946 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
948 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
949 dp->totalUsable = tot;
955 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
958 afs_int64 totalblks, free, used, availblks;
961 struct afs_statvfs statbuf;
963 struct afs_statfs statbuf;
966 if (dp->flags & PART_DONTUPDATE)
968 /* Note: we don't bother syncing because it's only an estimate, update
969 * is syncing every 30 seconds anyway, we only have to keep the disk
970 * approximately 10% from full--you just can't get the stuff in from
971 * the net fast enough to worry */
973 code = afs_statvfs(dp->name, &statbuf);
975 code = afs_statfs(dp->name, &statbuf);
978 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
982 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
983 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
986 totalblks = statbuf.f_blocks;
987 free = statbuf.f_bfree;
988 reserved = free - statbuf.f_bavail;
990 bsize = statbuf.f_frsize;
992 bsize = statbuf.f_bsize;
994 availblks = totalblks - reserved;
995 dp->f_files = statbuf.f_files; /* max # of files in partition */
997 /* Now free and totalblks are in fragment units, but we want them in
1000 if (bsize >= 1024) {
1001 free *= (bsize / 1024);
1002 totalblks *= (bsize / 1024);
1003 availblks *= (bsize / 1024);
1004 reserved *= (bsize / 1024);
1006 free /= (1024 / bsize);
1007 totalblks /= (1024 / bsize);
1008 availblks /= (1024 / bsize);
1009 reserved /= (1024 / bsize);
1011 /* now compute remaining figures */
1012 used = totalblks - free;
1014 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
1015 dp->totalUsable = availblks;
1016 dp->free = availblks - used; /* this is exactly f_bavail */
1018 #endif /* AFS_NT40_ENV */
1021 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
1024 VSetPartitionDiskUsage_r(dp);
1029 VResetDiskUsage_r(void)
1031 struct DiskPartition64 *dp;
1032 for (dp = DiskPartitionList; dp; dp = dp->next) {
1033 VSetPartitionDiskUsage_r(dp);
1034 #ifndef AFS_PTHREAD_ENV
1036 #endif /* !AFS_PTHREAD_ENV */
1041 VResetDiskUsage(void)
1044 VResetDiskUsage_r();
1049 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1050 afs_sfsize_t checkBlocks)
1053 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1054 * for less than BlocksSpare would skip the error-checking path, and we
1055 * could grow existing files forever, not just for another BlocksSpare
1058 #ifdef AFS_AIX32_ENV
1059 afs_int32 rem, minavail;
1061 if ((rem = vp->partition->free - checkBlocks) < (minavail =
1067 if (vp->partition->free - checkBlocks < 0)
1070 else if (V_maxquota(vp)
1071 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1074 vp->partition->free -= blocks;
1075 V_diskused(vp) += blocks;
1079 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1080 afs_sfsize_t checkBlocks)
1083 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1088 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1091 #ifdef AFS_AIX32_ENV
1092 afs_int32 rem, minavail;
1094 if ((rem = vp->partition->free - blocks) < (minavail =
1097 aixlow_water) / 100))
1099 if (vp->partition->free - blocks < 0)
1103 vp->partition->free -= blocks;
1108 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1112 retVal = VDiskUsage_r(vp, blocks);
1118 VPrintDiskStats_r(void)
1120 struct DiskPartition64 *dp;
1121 for (dp = DiskPartitionList; dp; dp = dp->next) {
1123 Log("Partition %s: %lld "
1124 " available 1K blocks (minfree=%lld), "
1125 "overallocated by %lld blocks\n", dp->name,
1126 dp->totalUsable, dp->minFree, -dp->free);
1128 Log("Partition %s: %lld"
1129 " available 1K blocks (minfree=%lld), "
1130 "%lld free blocks\n", dp->name,
1131 dp->totalUsable, dp->minFree, dp->free);
1137 VPrintDiskStats(void)
1140 VPrintDiskStats_r();
1145 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1146 #define LOCKFILE "LOCKFILE"
1148 VLockPartition_r(char *name)
1150 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1155 if (dp->lock_fd == INVALID_FD) {
1158 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1160 (FD_t)CreateFile(path, GENERIC_WRITE,
1161 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1162 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1163 osi_Assert(dp->lock_fd != INVALID_FD);
1165 memset(&lap, 0, sizeof(lap));
1166 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1173 VUnlockPartition_r(char *name)
1175 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1179 return; /* no partition, will fail later */
1180 memset(&lap, 0, sizeof(lap));
1182 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1183 CloseHandle((HANDLE) dp->lock_fd);
1184 dp->lock_fd = INVALID_FD;
1186 #else /* AFS_NT40_ENV */
1188 #if defined(AFS_HPUX_ENV)
1189 #define BITS_PER_CHAR (8)
1190 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1192 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1193 #endif /* defined(AFS_HPUX_ENV) */
1196 VLockPartition_r(char *name)
1198 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1199 char *partitionName;
1201 struct timeval pausing;
1202 #if defined(AFS_HPUX_ENV)
1204 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1205 unsigned int *globalMask;
1206 int globalMaskIndex;
1207 #endif /* defined(AFS_HPUX_ENV) */
1208 #if defined(AFS_DARWIN_ENV)
1209 char lockfile[MAXPATHLEN];
1210 #endif /* defined(AFS_DARWIN_ENV) */
1211 #ifdef AFS_NAMEI_ENV
1212 #ifdef AFS_AIX42_ENV
1213 char LockFileName[MAXPATHLEN + 1];
1215 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1216 partitionName = (char *)&LockFileName;
1221 return; /* no partition, will fail later */
1222 if (dp->lock_fd != INVALID_FD)
1225 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1226 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1227 partitionName = dp->devName;
1230 #elif defined(AFS_DARWIN_ENV)
1231 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1232 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1233 code = O_RDONLY | O_CREAT;
1235 partitionName = dp->name;
1239 for (retries = 25; retries; retries--) {
1241 dp->lock_fd = afs_open(partitionName, code, 0644);
1243 dp->lock_fd = afs_open(partitionName, code);
1245 if (dp->lock_fd != INVALID_FD)
1247 if (errno == ENOENT)
1250 pausing.tv_usec = 500000;
1251 select(0, NULL, NULL, NULL, &pausing);
1253 osi_Assert(retries != 0);
1255 #if defined (AFS_HPUX_ENV)
1257 osi_Assert(getprivgrp(privGrpList) == 0);
1260 * In general, it will difficult and time-consuming ,if not impossible,
1261 * to try to find the privgroup to which this process belongs that has the
1262 * smallest membership, to minimise the security hole. So, we use the privgrp
1263 * to which everybody belongs.
1265 /* first, we have to find the global mask */
1266 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1267 globalMaskIndex++) {
1268 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1270 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1275 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1276 /* allow everybody to set a lock on a read-only file descriptor */
1277 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1278 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1281 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1283 /* remove the privilege granted to everybody to lock a read-only fd */
1284 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1285 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1288 /* in this case, we should be able to do this with impunity, anyway */
1289 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1292 osi_Assert(lockfRtn != -1);
1294 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1295 osi_Assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1297 osi_Assert(flock(dp->lock_fd, LOCK_EX) == 0);
1298 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1303 VUnlockPartition_r(char *name)
1305 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1307 return; /* no partition, will fail later */
1309 dp->lock_fd = INVALID_FD;
1312 #endif /* AFS_NT40_ENV */
1315 VLockPartition(char *name)
1318 VLockPartition_r(name);
1323 VUnlockPartition(char *name)
1326 VUnlockPartition_r(name);
1330 #ifdef AFS_DEMAND_ATTACH_FS
1332 /* new-style partition locks; these are only to have some mutual exclusion
1333 * between the VGC scanner and volume utilies creating/altering vol headers
1337 * lock a partition's vol headers.
1339 * @param[in] dp the partition to lock
1340 * @param[in] locktype READ_LOCK or WRITE_LOCK
1342 * @return operation status
1346 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1350 /* block on acquiring the lock */
1353 code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1355 Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1356 VPartitionPath(dp));
1362 * unlock a partition's vol headers.
1364 * @param[in] dp the partition to unlock
1365 * @param[in] locktype READ_LOCK or WRITE_LOCK
1368 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1370 VReleaseDiskLock(&dp->headerLock, locktype);
1373 /* XXX not sure this will work on AFS_NT40_ENV
1374 * needs to be tested!
1378 * lookup a disk partition object by its index number.
1380 * @param[in] id partition index number
1381 * @param[in] abortp see abortp usage note below
1383 * @return disk partition object
1384 * @retval NULL no such disk partition
1386 * @note when abortp is non-zero, lookups which would return
1387 * NULL will result in an assertion failure
1389 * @pre VOL_LOCK must be held
1391 * @internal volume package internal use only
1394 struct DiskPartition64 *
1395 VGetPartitionById_r(afs_int32 id, int abortp)
1397 struct DiskPartition64 *dp = NULL;
1399 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1400 dp = DiskPartitionTable[id];
1404 osi_Assert(dp != NULL);
1410 * lookup a disk partition object by its index number.
1412 * @param[in] id partition index number
1413 * @param[in] abortp see abortp usage note below
1415 * @return disk partition object
1416 * @retval NULL no such disk partition
1418 * @note when abortp is non-zero, lookups which would return
1419 * NULL will result in an assertion failure
1422 struct DiskPartition64 *
1423 VGetPartitionById(afs_int32 id, int abortp)
1425 struct DiskPartition64 * dp;
1428 dp = VGetPartitionById_r(id, abortp);
1434 static struct DiskPartition64 *
1435 VLookupPartition_r(char * path)
1437 afs_int32 id = volutil_GetPartitionID(path);
1439 if (id < 0 || id > VOLMAXPARTS)
1442 return DiskPartitionTable[id];
1446 AddPartitionToTable_r(struct DiskPartition64 *dp)
1448 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1449 DiskPartitionTable[dp->index] = dp;
1454 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1456 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1457 DiskPartitionTable[dp->index] = NULL;
1460 #endif /* AFS_DEMAND_ATTACH_FS */