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)
81 #include <sys/mnttab.h>
82 #include <sys/mntent.h>
84 #ifdef AFS_LINUX22_ENV
86 #include <sys/statfs.h>
93 #endif /* AFS_SGI_ENV */
94 #endif /* AFS_NT40_ENV */
95 #if defined(AFS_SGI_ENV)
101 #include <afs/afsint.h>
103 #include <afs/errors.h>
106 #include <afs/afssyscalls.h>
113 #include "namei_ops.h"
115 #endif /* AFS_NT40_ENV */
116 #endif /* AFS_NAMEI_ENV */
119 #include "partition.h"
121 #if defined(AFS_HPUX_ENV)
122 #include <sys/types.h>
123 #include <sys/privgrp.h>
124 #endif /* defined(AFS_HPUX_ENV) */
127 #include <jfs/filsys.h>
130 int aixlow_water = 8; /* default 8% */
131 struct DiskPartition64 *DiskPartitionList;
133 #ifdef AFS_DEMAND_ATTACH_FS
134 /* file to lock to conceptually "lock" the vol headers on a partition */
135 #define AFS_PARTLOCK_FILE ".volheaders.lock"
136 #define AFS_VOLUMELOCK_FILE ".volume.lock"
138 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
140 static struct DiskPartition64 * VLookupPartition_r(char * path);
141 static void AddPartitionToTable_r(struct DiskPartition64 *);
142 #endif /* AFS_DEMAND_ATTACH_FS */
144 #ifdef AFS_SGI_XFS_IOPS_ENV
145 /* Verify that the on disk XFS inodes on the partition are large enough to
146 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
147 * too small to fit in the inode. Returns 0 if the attribute does fit in
150 #include <afs/xfsattrs.h>
152 VerifyXFSInodeSize(char *part, char *fstype)
155 int length = SIZEOF_XFS_ATTR_T;
160 if (strcmp("xfs", fstype))
163 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
164 if (((fd = open(part, O_RDONLY, 0)) != -1)
165 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
167 if (fsx.fsx_nextents) {
168 Log("Partition %s: XFS inodes too small, exiting.\n", part);
169 Log("Run xfs_size_check utility and remake partitions.\n");
176 (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
180 #endif /* AFS_SGI_XFS_IOPS_ENV */
183 VInitPartitionPackage(void)
185 #ifdef AFS_DEMAND_ATTACH_FS
186 memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
187 #endif /* AFS_DEMAND_ATTACH_FS */
192 VInitPartition_r(char *path, char *devname, Device dev)
194 struct DiskPartition64 *dp, *op;
196 dp = (struct DiskPartition64 *)malloc(sizeof(struct DiskPartition64));
197 /* Add it to the end, to preserve order when we print statistics */
198 for (op = DiskPartitionList; op; op = op->next) {
205 DiskPartitionList = dp;
207 dp->name = (char *)malloc(strlen(path) + 1);
208 strncpy(dp->name, path, strlen(path) + 1);
209 dp->index = volutil_GetPartitionID(path);
210 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
211 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
212 dp->devName = (char *)malloc(2 * strlen(path) + 6);
213 strcpy(dp->devName, path);
214 strcat(dp->devName, OS_DIRSEP);
215 strcat(dp->devName, "Lock");
216 mkdir(dp->devName, 0700);
217 strcat(dp->devName, path);
218 close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
219 dp->device = dp->index;
221 dp->devName = (char *)malloc(strlen(devname) + 1);
222 strncpy(dp->devName, devname, strlen(devname) + 1);
225 dp->lock_fd = INVALID_FD;
227 dp->f_files = 1; /* just a default value */
228 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
229 if (programType == fileServer)
230 (void)namei_ViceREADME(VPartitionPath(dp));
232 VSetPartitionDiskUsage_r(dp);
233 #ifdef AFS_DEMAND_ATTACH_FS
234 AddPartitionToTable_r(dp);
235 queue_Init(&dp->vol_list.head);
236 CV_INIT(&dp->vol_list.cv, "vol list", CV_DEFAULT, 0);
237 dp->vol_list.len = 0;
238 dp->vol_list.busy = 0;
240 char lockpath[MAXPATHLEN+1];
241 snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
242 lockpath[MAXPATHLEN] = '\0';
243 VLockFileInit(&dp->headerLockFile, lockpath);
245 snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
246 lockpath[MAXPATHLEN] = '\0';
247 VLockFileInit(&dp->volLockFile, lockpath);
249 VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
250 #endif /* AFS_DEMAND_ATTACH_FS */
254 VInitPartition(char *path, char *devname, Device dev)
257 VInitPartition_r(path, devname, dev);
262 /* VAttachPartitions() finds the vice partitions on this server. Calls
263 * VCheckPartition() to do some basic checks on the partition. If the partition
264 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
266 * Returns the number of errors returned by VCheckPartition. An error in
267 * VCheckPartition means that partition is a valid vice partition but the
268 * fileserver should not start because of the error found on that partition.
271 * No specific user space file system checks, since we don't know what
272 * is being used for vice partitions.
274 * Use partition name as devname.
277 VCheckPartition(char *part, char *devname, int logging)
279 struct afs_stat_st status;
280 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
281 char AFSIDatPath[MAXPATHLEN];
284 /* Only keep track of "/vicepx" partitions since it can get hairy
285 * when NFS mounts are involved.. */
286 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
289 if (afs_stat(part, &status) < 0) {
290 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
294 Log("This program is compiled without AFS_NAMEI_ENV, and "
295 "partition %s is mounted with the 'logging' option. "
296 "Using the inode fileserver backend with 'logging' UFS "
297 "partitions causes volume corruption, so please either "
298 "mount the partition without logging, or use the namei "
299 "fileserver backend. Aborting...\n", part);
302 #ifndef AFS_AIX32_ENV
303 if (programType == fileServer) {
304 char salvpath[MAXPATHLEN];
305 strcpy(salvpath, part);
306 strcat(salvpath, "/FORCESALVAGE");
307 if (afs_stat(salvpath, &status) == 0) {
308 Log("VInitVnodes: Found %s; aborting\n", salvpath);
314 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
315 strcpy(AFSIDatPath, part);
316 strcat(AFSIDatPath, "/AFSIDat");
318 if (afs_stat(AFSIDatPath, &status) < 0) {
322 dirp = opendir(part);
324 while ((dp = readdir(dirp))) {
325 if (dp->d_name[0] == 'V') {
326 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);
333 #else /* AFS_NAMEI_ENV */
334 if (afs_stat(AFSIDatPath, &status) == 0) {
335 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
339 #ifdef AFS_SGI_XFS_IOPS_ENV
340 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
343 #endif /* AFS_NAMEI_ENV */
344 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
346 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
347 if (status.st_ino != ROOTINO) {
348 Log("%s is not a mounted file system; ignored.\n", part);
353 VInitPartition(part, devname, status.st_dev);
358 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
359 * attached (return value 1), or only attached when it is a separately
360 * mounted partition (return value 0). For non-NAMEI environments, it
363 * *awouldattach will be set to 1 if the given path at least looks like a vice
364 * partition (that is, if we return 0, the only thing preventing this partition
365 * from being attached is the existence of the AlwaysAttach file), or to 0
366 * otherwise. *awouldattach is set regardless of whether or not the partition
367 * should always be attached or not.
370 VIsAlwaysAttach(char *part, int *awouldattach)
373 struct afs_stat_st st;
376 #endif /* AFS_NAMEI_ENV */
383 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
390 strncpy(checkfile, part, 100);
391 strcat(checkfile, OS_DIRSEP);
392 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
394 ret = afs_stat(checkfile, &st);
395 return (ret < 0) ? 0 : 1;
396 #else /* AFS_NAMEI_ENV */
398 #endif /* AFS_NAMEI_ENV */
401 /* VIsNeverAttach() checks whether a /vicepX directory should never be
402 * attached (return value 1), or follow the normal mounting logic. The
403 * Always Attach flag may override the NeverAttach flag.
406 VIsNeverAttach(char *part)
408 struct afs_stat_st st;
412 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
415 strncpy(checkfile, part, 100);
416 strcat(checkfile, OS_DIRSEP);
417 strcat(checkfile, VICE_NEVERATTACH_FILE);
419 ret = afs_stat(checkfile, &st);
420 return (ret < 0) ? 0 : 1;
423 /* VAttachPartitions2() looks for and attaches /vicepX partitions
424 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
425 * used to attach /vicepX directories which aren't on dedicated
426 * partitions, in the NAMEI fileserver.
429 VAttachPartitions2(void)
437 dirp = opendir(OS_DIRSEP);
438 while ((de = readdir(dirp))) {
439 strcpy(pname, OS_DIRSEP);
440 strncat(pname, de->d_name, 20);
441 pname[sizeof(pname) - 1] = '\0';
443 /* Only keep track of "/vicepx" partitions since automounter
445 if (VIsAlwaysAttach(pname, &wouldattach)) {
446 VCheckPartition(pname, "", 0);
448 struct afs_stat_st st;
449 if (wouldattach && VGetPartition(pname, 0) == NULL &&
450 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
452 /* This is a /vicep* dir, and it has not been attached as a
453 * partition. This probably means that this is a /vicep* dir
454 * that is not a separate partition, so just give a notice so
455 * admins are not confused as to why their /vicep* dirs are not
458 * It is possible that the dir _is_ a separate partition and we
459 * failed to attach it earlier, making this message a bit
460 * confusing. But that should be rare, and an error message
461 * about the failure will already be logged right before this,
462 * so it should be clear enough. */
464 Log("VAttachPartitions: not attaching %s; either it is not a "
465 "separate partition, or it failed to attach (create the "
466 "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
472 #endif /* AFS_NAMEI_ENV */
474 #endif /* AFS_NT40_ENV */
478 VAttachPartitions(void)
484 if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
485 Log("Can't open %s\n", MNTTAB);
489 while (!getmntent(mntfile, &mnt)) {
491 /* Ignore non ufs or non read/write partitions */
492 /* but allow zfs too if we're in the NAMEI environment */
495 (((strcmp(mnt.mnt_fstype, "ufs") &&
496 strcmp(mnt.mnt_fstype, "zfs"))))
498 (strcmp(mnt.mnt_fstype, "ufs") != 0)
500 || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
503 /* Skip this Partition? */
504 if (VIsNeverAttach(mnt.mnt_mountp))
507 /* If we're going to always attach this partition, do it later. */
508 if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
511 #ifndef AFS_NAMEI_ENV
512 if (hasmntopt(&mnt, "logging") != NULL) {
515 #endif /* !AFS_NAMEI_ENV */
517 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special, logging) < 0)
521 (void)fclose(mntfile);
523 /* Process the always-attach partitions, if any. */
524 VAttachPartitions2();
529 #endif /* AFS_SUN5_ENV */
530 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
532 VAttachPartitions(void)
536 struct mntent *mntent;
538 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
539 Log("Problems in getting mount entries(setmntent)\n");
542 while (mntent = getmntent(mfd)) {
543 if (!hasmntopt(mntent, MNTOPT_RW))
546 /* Skip this Partition? */
547 if (VIsNeverAttach(mntent->mnt_dir))
550 /* If we're going to always attach this partition, do it later. */
551 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
554 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
560 /* Process the always-attach partitions, if any. */
561 VAttachPartitions2();
568 * (This function was grabbed from df.c)
571 getmount(struct vmount **vmountpp)
577 /* set initial size of mntctl buffer to a MAGIC NUMBER */
580 /* try the operation until ok or a fatal error */
582 if ((vm = (struct vmount *)malloc(size)) == NULL) {
583 /* failed getting memory for mount status buf */
584 perror("FATAL ERROR: get_stat malloc failed\n");
589 * perform the QUERY mntctl - if it returns > 0, that is the
590 * number of vmount structures in the buffer. If it returns
591 * -1, an error occured. If it returned 0, then look in
592 * first word of buffer for needed size.
594 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
595 /* OK, got it, now return */
599 } else if (nmounts == 0) {
600 /* the buffer wasn't big enough .... */
601 /* .... get required buffer size */
606 /* some other kind of error occurred */
614 VAttachPartitions(void)
618 struct vmount *vmountp;
620 if ((nmounts = getmount(&vmountp)) <= 0) {
621 Log("Problems in getting # of mount entries(getmount)\n");
626 (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
627 char *part = vmt2dataptr(vmountp, VMT_STUB);
629 if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
630 continue; /* Ignore any "special" partitions */
633 #ifndef AFS_NAMEI_ENV
635 struct superblock fs;
636 /* The Log statements are non-sequiters in the SalvageLog and don't
637 * even appear in the VolserLog, so restrict them to the FileLog.
639 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
640 if (programType == fileServer)
641 Log("Can't read superblock for %s, ignoring it.\n", part);
644 if (IsBigFilesFileSystem(&fs)) {
645 if (programType == fileServer)
646 Log("%s is a big files filesystem, ignoring it.\n", part);
653 /* Skip this Partition? */
654 if (VIsNeverAttach(part))
657 /* If we're going to always attach this partition, do it later. */
658 if (VIsAlwaysAttach(part, NULL))
661 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT), 0) < 0)
665 /* Process the always-attach partitions, if any. */
666 VAttachPartitions2();
671 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
673 VAttachPartitions(void)
678 if (setfsent() < 0) {
679 Log("Error listing filesystems.\n");
683 while ((fsent = getfsent())) {
684 if (strcmp(fsent->fs_type, "rw") != 0)
687 /* Skip this Partition? */
688 if (VIsNeverAttach(fsent->fs_file))
691 /* If we're going to always attach this partition, do it later. */
692 if (VIsAlwaysAttach(fsent->fs_file, NULL))
695 if (VCheckPartition(fsent->fs_file, fsent->fs_spec, 0) < 0)
700 /* Process the always-attach partitions, if any. */
701 VAttachPartitions2();
709 #include <sys/stat.h>
712 * validate names in vptab.
720 VValidVPTEntry(struct vptab *vpe)
722 int len = strlen(vpe->vp_name);
725 if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
727 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
730 for (i = VICE_PREFIX_SIZE; i < len; i++) {
731 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
732 Log("Invalid partition name %s in registry, ignoring it.\n",
737 if (len == VICE_PREFIX_SIZE + 2) {
738 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
739 (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
741 Log("Invalid partition name %s in registry, ignoring it.\n",
747 len = strlen(vpe->vp_dev);
748 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
749 || vpe->vp_dev[0] > 'Z') {
750 Log("Invalid device name %s in registry, ignoring it.\n",
759 VCheckPartition(char *partName)
766 /* partName is presumed to be of the form "X:" */
767 (void)sprintf(volRoot, "%c:\\", *partName);
769 if (!GetVolumeInformation(volRoot, /* volume root directory */
770 NULL, /* volume name buffer */
771 0, /* volume name size */
772 NULL, /* volume serial number */
773 &dwDummy, /* max component length */
774 &dwDummy, /* file system flags */
775 volFsType, /* file system name */
776 sizeof(volFsType))) {
777 err = GetLastError();
778 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
782 if (strcmp(volFsType, "NTFS")) {
783 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
792 VAttachPartitions(void)
794 struct DiskPartition64 *partP, *prevP, *nextP;
795 struct vpt_iter iter;
798 if (vpt_Start(&iter) < 0) {
799 Log("No partitions to attach.\n");
803 while (0 == vpt_NextEntry(&iter, &entry)) {
804 if (!VValidVPTEntry(&entry)) {
808 /* This test for duplicates relies on the fact that the method
809 * of storing the partition names in the NT registry means the same
810 * partition name will never appear twice in the list.
812 for (partP = DiskPartitionList; partP; partP = partP->next) {
813 if (*partP->devName == *entry.vp_dev) {
814 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
815 partP->flags = PART_DUPLICATE;
816 break; /* Only one entry will ever be in this list. */
820 continue; /* found a duplicate */
822 if (VCheckPartition(entry.vp_dev) < 0)
824 /* This test allows for manually inserting the FORCESALVAGE flag
825 * and thereby invoking the salvager. scandisk obviously won't be
828 if (programType == fileServer) {
829 struct afs_stat_st status;
830 char salvpath[MAXPATHLEN];
831 strcpy(salvpath, entry.vp_dev);
832 strcat(salvpath, "\\FORCESALVAGE");
833 if (afs_stat(salvpath, &status) == 0) {
834 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
838 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
842 /* Run through partition list and clear out the dupes. */
843 prevP = nextP = NULL;
844 for (partP = DiskPartitionList; partP; partP = nextP) {
846 if (partP->flags == PART_DUPLICATE) {
848 prevP->next = partP->next;
850 DiskPartitionList = partP->next;
860 #ifdef AFS_LINUX22_ENV
862 VAttachPartitions(void)
866 struct mntent *mntent;
868 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
869 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
870 Log("Problems in getting mount entries(setmntent)\n");
874 while ((mntent = getmntent(mfd))) {
875 /* Skip this Partition? */
876 if (VIsNeverAttach(mntent->mnt_dir))
879 /* If we're going to always attach this partition, do it later. */
880 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
883 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
888 /* Process the always-attach partitions, if any. */
889 VAttachPartitions2();
893 #endif /* AFS_LINUX22_ENV */
895 /* This routine is to be called whenever the actual name of the partition
896 * is required. The canonical name is still in part->name.
899 VPartitionPath(struct DiskPartition64 *part)
902 return part->devName;
908 /* get partition structure, abortp tells us if we should abort on failure */
909 struct DiskPartition64 *
910 VGetPartition_r(char *name, int abortp)
912 struct DiskPartition64 *dp;
913 #ifdef AFS_DEMAND_ATTACH_FS
914 dp = VLookupPartition_r(name);
915 #else /* AFS_DEMAND_ATTACH_FS */
916 for (dp = DiskPartitionList; dp; dp = dp->next) {
917 if (strcmp(dp->name, name) == 0)
920 #endif /* AFS_DEMAND_ATTACH_FS */
922 osi_Assert(dp != NULL);
926 struct DiskPartition64 *
927 VGetPartition(char *name, int abortp)
929 struct DiskPartition64 *retVal;
931 retVal = VGetPartition_r(name, abortp);
938 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
940 ULARGE_INTEGER free_user, total, free_total;
941 int ufree, tot, tfree;
943 if (!GetDiskFreeSpaceEx
944 (VPartitionPath(dp), &free_user, &total, &free_total)) {
945 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
950 /* Convert to 1K units. */
951 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
952 tot = (int)Int64ShraMod32(total.QuadPart, 10);
953 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
955 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
956 dp->totalUsable = tot;
962 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
965 afs_int64 totalblks, free, used, availblks;
968 struct afs_statvfs statbuf;
970 struct afs_statfs statbuf;
973 if (dp->flags & PART_DONTUPDATE)
975 /* Note: we don't bother syncing because it's only an estimate, update
976 * is syncing every 30 seconds anyway, we only have to keep the disk
977 * approximately 10% from full--you just can't get the stuff in from
978 * the net fast enough to worry */
980 code = afs_statvfs(dp->name, &statbuf);
982 code = afs_statfs(dp->name, &statbuf);
985 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
989 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
990 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
993 totalblks = statbuf.f_blocks;
994 free = statbuf.f_bfree;
995 reserved = free - statbuf.f_bavail;
997 bsize = statbuf.f_frsize;
999 bsize = statbuf.f_bsize;
1001 availblks = totalblks - reserved;
1002 dp->f_files = statbuf.f_files; /* max # of files in partition */
1004 /* Now free and totalblks are in fragment units, but we want them in
1007 if (bsize >= 1024) {
1008 free *= (bsize / 1024);
1009 totalblks *= (bsize / 1024);
1010 availblks *= (bsize / 1024);
1011 reserved *= (bsize / 1024);
1013 free /= (1024 / bsize);
1014 totalblks /= (1024 / bsize);
1015 availblks /= (1024 / bsize);
1016 reserved /= (1024 / bsize);
1018 /* now compute remaining figures */
1019 used = totalblks - free;
1021 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
1022 dp->totalUsable = availblks;
1023 dp->free = availblks - used; /* this is exactly f_bavail */
1025 #endif /* AFS_NT40_ENV */
1028 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
1031 VSetPartitionDiskUsage_r(dp);
1036 VResetDiskUsage_r(void)
1038 struct DiskPartition64 *dp;
1039 for (dp = DiskPartitionList; dp; dp = dp->next) {
1040 VSetPartitionDiskUsage_r(dp);
1041 #ifndef AFS_PTHREAD_ENV
1043 #endif /* !AFS_PTHREAD_ENV */
1048 VResetDiskUsage(void)
1051 VResetDiskUsage_r();
1056 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1057 afs_sfsize_t checkBlocks)
1060 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1061 * for less than BlocksSpare would skip the error-checking path, and we
1062 * could grow existing files forever, not just for another BlocksSpare
1065 #ifdef AFS_AIX32_ENV
1066 afs_int32 rem, minavail;
1068 if ((rem = vp->partition->free - checkBlocks) < (minavail =
1074 if (vp->partition->free - checkBlocks < 0)
1077 else if (V_maxquota(vp)
1078 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1081 vp->partition->free -= blocks;
1082 V_diskused(vp) += blocks;
1086 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1087 afs_sfsize_t checkBlocks)
1090 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1095 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1098 #ifdef AFS_AIX32_ENV
1099 afs_int32 rem, minavail;
1101 if ((rem = vp->partition->free - blocks) < (minavail =
1104 aixlow_water) / 100))
1106 if (vp->partition->free - blocks < 0)
1110 vp->partition->free -= blocks;
1115 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1119 retVal = VDiskUsage_r(vp, blocks);
1125 VPrintDiskStats_r(void)
1127 struct DiskPartition64 *dp;
1128 for (dp = DiskPartitionList; dp; dp = dp->next) {
1130 Log("Partition %s: %lld "
1131 " available 1K blocks (minfree=%lld), "
1132 "overallocated by %lld blocks\n", dp->name,
1133 dp->totalUsable, dp->minFree, -dp->free);
1135 Log("Partition %s: %lld"
1136 " available 1K blocks (minfree=%lld), "
1137 "%lld free blocks\n", dp->name,
1138 dp->totalUsable, dp->minFree, dp->free);
1144 VPrintDiskStats(void)
1147 VPrintDiskStats_r();
1152 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1153 #define LOCKFILE "LOCKFILE"
1155 VLockPartition_r(char *name)
1157 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1162 if (dp->lock_fd == INVALID_FD) {
1165 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1167 (FD_t)CreateFile(path, GENERIC_WRITE,
1168 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1169 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1170 osi_Assert(dp->lock_fd != INVALID_FD);
1172 memset(&lap, 0, sizeof(lap));
1173 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1180 VUnlockPartition_r(char *name)
1182 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1186 return; /* no partition, will fail later */
1187 memset(&lap, 0, sizeof(lap));
1189 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1190 CloseHandle((HANDLE) dp->lock_fd);
1191 dp->lock_fd = INVALID_FD;
1193 #else /* AFS_NT40_ENV */
1195 #if defined(AFS_HPUX_ENV)
1196 #define BITS_PER_CHAR (8)
1197 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1199 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1200 #endif /* defined(AFS_HPUX_ENV) */
1203 VLockPartition_r(char *name)
1205 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1206 char *partitionName;
1208 struct timeval pausing;
1209 #if defined(AFS_HPUX_ENV)
1211 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1212 unsigned int *globalMask;
1213 int globalMaskIndex;
1214 #endif /* defined(AFS_HPUX_ENV) */
1215 #if defined(AFS_DARWIN_ENV)
1216 char lockfile[MAXPATHLEN];
1217 #endif /* defined(AFS_DARWIN_ENV) */
1218 #ifdef AFS_NAMEI_ENV
1219 #ifdef AFS_AIX42_ENV
1220 char LockFileName[MAXPATHLEN + 1];
1222 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1223 partitionName = (char *)&LockFileName;
1228 return; /* no partition, will fail later */
1229 if (dp->lock_fd != INVALID_FD)
1232 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1233 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1234 partitionName = dp->devName;
1237 #elif defined(AFS_DARWIN_ENV)
1238 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1239 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1240 code = O_RDONLY | O_CREAT;
1242 partitionName = dp->name;
1246 for (retries = 25; retries; retries--) {
1248 dp->lock_fd = afs_open(partitionName, code, 0644);
1250 dp->lock_fd = afs_open(partitionName, code);
1252 if (dp->lock_fd != INVALID_FD)
1254 if (errno == ENOENT)
1257 pausing.tv_usec = 500000;
1258 select(0, NULL, NULL, NULL, &pausing);
1260 osi_Assert(retries != 0);
1262 #if defined (AFS_HPUX_ENV)
1264 osi_Assert(getprivgrp(privGrpList) == 0);
1267 * In general, it will difficult and time-consuming ,if not impossible,
1268 * to try to find the privgroup to which this process belongs that has the
1269 * smallest membership, to minimise the security hole. So, we use the privgrp
1270 * to which everybody belongs.
1272 /* first, we have to find the global mask */
1273 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1274 globalMaskIndex++) {
1275 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1277 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1282 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1283 /* allow everybody to set a lock on a read-only file descriptor */
1284 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1285 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1288 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1290 /* remove the privilege granted to everybody to lock a read-only fd */
1291 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1292 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1295 /* in this case, we should be able to do this with impunity, anyway */
1296 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1299 osi_Assert(lockfRtn != -1);
1301 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1302 osi_Assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1304 osi_Assert(flock(dp->lock_fd, LOCK_EX) == 0);
1305 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1310 VUnlockPartition_r(char *name)
1312 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1314 return; /* no partition, will fail later */
1316 dp->lock_fd = INVALID_FD;
1319 #endif /* AFS_NT40_ENV */
1322 VLockPartition(char *name)
1325 VLockPartition_r(name);
1330 VUnlockPartition(char *name)
1333 VUnlockPartition_r(name);
1337 #ifdef AFS_DEMAND_ATTACH_FS
1339 /* new-style partition locks; these are only to have some mutual exclusion
1340 * between the VGC scanner and volume utilies creating/altering vol headers
1344 * lock a partition's vol headers.
1346 * @param[in] dp the partition to lock
1347 * @param[in] locktype READ_LOCK or WRITE_LOCK
1349 * @return operation status
1353 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1357 /* block on acquiring the lock */
1360 code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1362 Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1363 VPartitionPath(dp));
1369 * unlock a partition's vol headers.
1371 * @param[in] dp the partition to unlock
1372 * @param[in] locktype READ_LOCK or WRITE_LOCK
1375 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1377 VReleaseDiskLock(&dp->headerLock, locktype);
1380 /* XXX not sure this will work on AFS_NT40_ENV
1381 * needs to be tested!
1385 * lookup a disk partition object by its index number.
1387 * @param[in] id partition index number
1388 * @param[in] abortp see abortp usage note below
1390 * @return disk partition object
1391 * @retval NULL no such disk partition
1393 * @note when abortp is non-zero, lookups which would return
1394 * NULL will result in an assertion failure
1396 * @pre VOL_LOCK must be held
1398 * @internal volume package internal use only
1401 struct DiskPartition64 *
1402 VGetPartitionById_r(afs_int32 id, int abortp)
1404 struct DiskPartition64 *dp = NULL;
1406 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1407 dp = DiskPartitionTable[id];
1411 osi_Assert(dp != NULL);
1417 * lookup a disk partition object by its index number.
1419 * @param[in] id partition index number
1420 * @param[in] abortp see abortp usage note below
1422 * @return disk partition object
1423 * @retval NULL no such disk partition
1425 * @note when abortp is non-zero, lookups which would return
1426 * NULL will result in an assertion failure
1429 struct DiskPartition64 *
1430 VGetPartitionById(afs_int32 id, int abortp)
1432 struct DiskPartition64 * dp;
1435 dp = VGetPartitionById_r(id, abortp);
1441 static struct DiskPartition64 *
1442 VLookupPartition_r(char * path)
1444 afs_int32 id = volutil_GetPartitionID(path);
1446 if (id < 0 || id > VOLMAXPARTS)
1449 return DiskPartitionTable[id];
1453 AddPartitionToTable_r(struct DiskPartition64 *dp)
1455 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1456 DiskPartitionTable[dp->index] = dp;
1461 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1463 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1464 DiskPartitionTable[dp->index] = NULL;
1467 #endif /* AFS_DEMAND_ATTACH_FS */