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>
101 #include <rx/rx_queue.h>
103 #include <afs/errors.h>
106 #include <afs/afssyscalls.h>
113 #include "namei_ops.h"
114 #endif /* AFS_NT40_ENV */
115 #endif /* AFS_NAMEI_ENV */
118 #include "partition.h"
120 #if defined(AFS_HPUX_ENV)
121 #include <sys/privgrp.h>
122 #endif /* defined(AFS_HPUX_ENV) */
125 #include <jfs/filsys.h>
129 extern int VValidVPTEntry(struct vptab *vptp);
132 int aixlow_water = 8; /* default 8% */
133 struct DiskPartition64 *DiskPartitionList;
135 #ifdef AFS_DEMAND_ATTACH_FS
136 /* file to lock to conceptually "lock" the vol headers on a partition */
137 #define AFS_PARTLOCK_FILE ".volheaders.lock"
138 #define AFS_VOLUMELOCK_FILE ".volume.lock"
140 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
142 static struct DiskPartition64 * VLookupPartition_r(char * path);
143 static void AddPartitionToTable_r(struct DiskPartition64 *);
144 #endif /* AFS_DEMAND_ATTACH_FS */
146 #ifdef AFS_SGI_XFS_IOPS_ENV
147 /* Verify that the on disk XFS inodes on the partition are large enough to
148 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
149 * too small to fit in the inode. Returns 0 if the attribute does fit in
152 #include <afs/xfsattrs.h>
154 VerifyXFSInodeSize(char *part, char *fstype)
157 int length = SIZEOF_XFS_ATTR_T;
162 if (strcmp("xfs", fstype))
165 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
166 if (((fd = open(part, O_RDONLY, 0)) != -1)
167 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
169 if (fsx.fsx_nextents) {
170 Log("Partition %s: XFS inodes too small, exiting.\n", part);
171 Log("Run xfs_size_check utility and remake partitions.\n");
178 (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
182 #endif /* AFS_SGI_XFS_IOPS_ENV */
185 VInitPartitionPackage(void)
187 #ifdef AFS_DEMAND_ATTACH_FS
188 memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
189 #endif /* AFS_DEMAND_ATTACH_FS */
194 VInitPartition_r(char *path, char *devname, Device dev)
196 struct DiskPartition64 *dp, *op;
198 dp = malloc(sizeof(struct DiskPartition64));
199 /* Add it to the end, to preserve order when we print statistics */
200 for (op = DiskPartitionList; op; op = op->next) {
207 DiskPartitionList = dp;
209 dp->name = strdup(path);
210 dp->index = volutil_GetPartitionID(path);
211 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
212 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
213 dp->devName = malloc(2 * strlen(path) + 6);
214 strcpy(dp->devName, path);
215 strcat(dp->devName, OS_DIRSEP);
216 strcat(dp->devName, "Lock");
217 mkdir(dp->devName, 0700);
218 strcat(dp->devName, path);
219 close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
220 dp->device = dp->index;
222 dp->devName = strdup(devname);
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 = 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();
710 * validate names in vptab.
718 VValidVPTEntry(struct vptab *vpe)
720 int len = strlen(vpe->vp_name);
723 if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
725 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
728 for (i = VICE_PREFIX_SIZE; i < len; i++) {
729 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
730 Log("Invalid partition name %s in registry, ignoring it.\n",
735 if (len == VICE_PREFIX_SIZE + 2) {
736 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
737 (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
739 Log("Invalid partition name %s in registry, ignoring it.\n",
745 len = strlen(vpe->vp_dev);
746 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
747 || vpe->vp_dev[0] > 'Z') {
748 Log("Invalid device name %s in registry, ignoring it.\n",
757 VCheckPartition(char *partName)
764 /* partName is presumed to be of the form "X:" */
765 (void)sprintf(volRoot, "%c:\\", *partName);
767 if (!GetVolumeInformation(volRoot, /* volume root directory */
768 NULL, /* volume name buffer */
769 0, /* volume name size */
770 NULL, /* volume serial number */
771 &dwDummy, /* max component length */
772 &dwDummy, /* file system flags */
773 volFsType, /* file system name */
774 sizeof(volFsType))) {
775 err = GetLastError();
776 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
780 if (strcmp(volFsType, "NTFS")) {
781 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
790 VAttachPartitions(void)
792 struct DiskPartition64 *partP, *prevP, *nextP;
793 struct vpt_iter iter;
796 if (vpt_Start(&iter) < 0) {
797 Log("No partitions to attach.\n");
801 while (0 == vpt_NextEntry(&iter, &entry)) {
802 if (!VValidVPTEntry(&entry)) {
806 /* This test for duplicates relies on the fact that the method
807 * of storing the partition names in the NT registry means the same
808 * partition name will never appear twice in the list.
810 for (partP = DiskPartitionList; partP; partP = partP->next) {
811 if (*partP->devName == *entry.vp_dev) {
812 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
813 partP->flags = PART_DUPLICATE;
814 break; /* Only one entry will ever be in this list. */
818 continue; /* found a duplicate */
820 if (VCheckPartition(entry.vp_dev) < 0)
822 /* This test allows for manually inserting the FORCESALVAGE flag
823 * and thereby invoking the salvager. scandisk obviously won't be
826 if (programType == fileServer) {
827 struct afs_stat_st status;
828 char salvpath[MAXPATHLEN];
829 strcpy(salvpath, entry.vp_dev);
830 strcat(salvpath, "\\FORCESALVAGE");
831 if (afs_stat(salvpath, &status) == 0) {
832 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
836 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
840 /* Run through partition list and clear out the dupes. */
841 prevP = nextP = NULL;
842 for (partP = DiskPartitionList; partP; partP = nextP) {
844 if (partP->flags == PART_DUPLICATE) {
846 prevP->next = partP->next;
848 DiskPartitionList = partP->next;
858 #ifdef AFS_LINUX22_ENV
860 VAttachPartitions(void)
864 struct mntent *mntent;
866 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
867 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
868 Log("Problems in getting mount entries(setmntent)\n");
872 while ((mntent = getmntent(mfd))) {
873 /* Skip this Partition? */
874 if (VIsNeverAttach(mntent->mnt_dir))
877 /* If we're going to always attach this partition, do it later. */
878 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
881 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
886 /* Process the always-attach partitions, if any. */
887 VAttachPartitions2();
891 #endif /* AFS_LINUX22_ENV */
893 /* This routine is to be called whenever the actual name of the partition
894 * is required. The canonical name is still in part->name.
897 VPartitionPath(struct DiskPartition64 *part)
900 return part->devName;
906 /* get partition structure, abortp tells us if we should abort on failure */
907 struct DiskPartition64 *
908 VGetPartition_r(char *name, int abortp)
910 struct DiskPartition64 *dp;
911 #ifdef AFS_DEMAND_ATTACH_FS
912 dp = VLookupPartition_r(name);
913 #else /* AFS_DEMAND_ATTACH_FS */
914 for (dp = DiskPartitionList; dp; dp = dp->next) {
915 if (strcmp(dp->name, name) == 0)
918 #endif /* AFS_DEMAND_ATTACH_FS */
920 opr_Assert(dp != NULL);
924 struct DiskPartition64 *
925 VGetPartition(char *name, int abortp)
927 struct DiskPartition64 *retVal;
929 retVal = VGetPartition_r(name, abortp);
936 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
938 ULARGE_INTEGER free_user, total, free_total;
939 int ufree, tot, tfree;
941 if (!GetDiskFreeSpaceEx
942 (VPartitionPath(dp), &free_user, &total, &free_total)) {
943 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
948 /* Convert to 1K units. */
949 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
950 tot = (int)Int64ShraMod32(total.QuadPart, 10);
951 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
953 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
954 dp->totalUsable = tot;
960 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
963 afs_int64 totalblks, free, used, availblks;
966 struct afs_statvfs statbuf;
968 struct afs_statfs statbuf;
971 if (dp->flags & PART_DONTUPDATE)
973 /* Note: we don't bother syncing because it's only an estimate, update
974 * is syncing every 30 seconds anyway, we only have to keep the disk
975 * approximately 10% from full--you just can't get the stuff in from
976 * the net fast enough to worry */
978 code = afs_statvfs(dp->name, &statbuf);
980 code = afs_statfs(dp->name, &statbuf);
983 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
987 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
988 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
991 totalblks = statbuf.f_blocks;
992 free = statbuf.f_bfree;
993 reserved = free - statbuf.f_bavail;
995 bsize = statbuf.f_frsize;
997 bsize = statbuf.f_bsize;
999 availblks = totalblks - reserved;
1000 dp->f_files = statbuf.f_files; /* max # of files in partition */
1002 /* Now free and totalblks are in fragment units, but we want them in
1005 if (bsize >= 1024) {
1006 free *= (bsize / 1024);
1007 totalblks *= (bsize / 1024);
1008 availblks *= (bsize / 1024);
1009 reserved *= (bsize / 1024);
1011 free /= (1024 / bsize);
1012 totalblks /= (1024 / bsize);
1013 availblks /= (1024 / bsize);
1014 reserved /= (1024 / bsize);
1016 /* now compute remaining figures */
1017 used = totalblks - free;
1019 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
1020 dp->totalUsable = availblks;
1021 dp->free = availblks - used; /* this is exactly f_bavail */
1023 #endif /* AFS_NT40_ENV */
1026 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
1029 VSetPartitionDiskUsage_r(dp);
1034 VResetDiskUsage_r(void)
1036 struct DiskPartition64 *dp;
1037 for (dp = DiskPartitionList; dp; dp = dp->next) {
1038 VSetPartitionDiskUsage_r(dp);
1039 #ifndef AFS_PTHREAD_ENV
1041 #endif /* !AFS_PTHREAD_ENV */
1046 VResetDiskUsage(void)
1049 VResetDiskUsage_r();
1054 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1055 afs_sfsize_t checkBlocks)
1058 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1059 * for less than BlocksSpare would skip the error-checking path, and we
1060 * could grow existing files forever, not just for another BlocksSpare
1063 #ifdef AFS_AIX32_ENV
1064 afs_int32 rem, minavail;
1066 if ((rem = vp->partition->free - checkBlocks) < (minavail =
1072 if (vp->partition->free - checkBlocks < 0)
1075 else if (V_maxquota(vp)
1076 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1079 vp->partition->free -= blocks;
1080 V_diskused(vp) += blocks;
1084 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1085 afs_sfsize_t checkBlocks)
1088 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1093 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1096 #ifdef AFS_AIX32_ENV
1097 afs_int32 rem, minavail;
1099 if ((rem = vp->partition->free - blocks) < (minavail =
1102 aixlow_water) / 100))
1104 if (vp->partition->free - blocks < 0)
1108 vp->partition->free -= blocks;
1113 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1117 retVal = VDiskUsage_r(vp, blocks);
1123 VPrintDiskStats_r(void)
1125 struct DiskPartition64 *dp;
1126 for (dp = DiskPartitionList; dp; dp = dp->next) {
1128 Log("Partition %s: %lld "
1129 " available 1K blocks (minfree=%lld), "
1130 "overallocated by %lld blocks\n", dp->name,
1131 dp->totalUsable, dp->minFree, -dp->free);
1133 Log("Partition %s: %lld"
1134 " available 1K blocks (minfree=%lld), "
1135 "%lld free blocks\n", dp->name,
1136 dp->totalUsable, dp->minFree, dp->free);
1142 VPrintDiskStats(void)
1145 VPrintDiskStats_r();
1150 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1151 #define LOCKFILE "LOCKFILE"
1153 VLockPartition_r(char *name)
1155 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1160 if (dp->lock_fd == INVALID_FD) {
1163 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1165 (FD_t)CreateFile(path, GENERIC_WRITE,
1166 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1167 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1168 opr_Assert(dp->lock_fd != INVALID_FD);
1170 memset(&lap, 0, sizeof(lap));
1171 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1178 VUnlockPartition_r(char *name)
1180 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1184 return; /* no partition, will fail later */
1185 memset(&lap, 0, sizeof(lap));
1187 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1188 CloseHandle((HANDLE) dp->lock_fd);
1189 dp->lock_fd = INVALID_FD;
1191 #else /* AFS_NT40_ENV */
1193 #if defined(AFS_HPUX_ENV)
1194 #define BITS_PER_CHAR (8)
1195 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1197 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1198 #endif /* defined(AFS_HPUX_ENV) */
1201 VLockPartition_r(char *name)
1203 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1204 char *partitionName;
1206 struct timeval pausing;
1207 #if defined(AFS_HPUX_ENV)
1209 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1210 unsigned int *globalMask;
1211 int globalMaskIndex;
1212 #endif /* defined(AFS_HPUX_ENV) */
1213 #if defined(AFS_DARWIN_ENV)
1214 char lockfile[MAXPATHLEN];
1215 #endif /* defined(AFS_DARWIN_ENV) */
1216 #ifdef AFS_NAMEI_ENV
1217 #ifdef AFS_AIX42_ENV
1218 char LockFileName[MAXPATHLEN + 1];
1220 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1221 partitionName = (char *)&LockFileName;
1226 return; /* no partition, will fail later */
1227 if (dp->lock_fd != INVALID_FD)
1230 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1231 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1232 partitionName = dp->devName;
1235 #elif defined(AFS_DARWIN_ENV)
1236 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1237 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1238 code = O_RDONLY | O_CREAT;
1240 partitionName = dp->name;
1244 for (retries = 25; retries; retries--) {
1246 dp->lock_fd = afs_open(partitionName, code, 0644);
1248 dp->lock_fd = afs_open(partitionName, code);
1250 if (dp->lock_fd != INVALID_FD)
1252 if (errno == ENOENT)
1255 pausing.tv_usec = 500000;
1256 select(0, NULL, NULL, NULL, &pausing);
1258 opr_Assert(retries != 0);
1260 #if defined (AFS_HPUX_ENV)
1262 opr_Verify(getprivgrp(privGrpList) == 0);
1265 * In general, it will difficult and time-consuming ,if not impossible,
1266 * to try to find the privgroup to which this process belongs that has the
1267 * smallest membership, to minimise the security hole. So, we use the privgrp
1268 * to which everybody belongs.
1270 /* first, we have to find the global mask */
1271 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1272 globalMaskIndex++) {
1273 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1275 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1280 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1281 /* allow everybody to set a lock on a read-only file descriptor */
1282 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1283 opr_Verify(setprivgrp(PRIV_GLOBAL,
1284 privGrpList[globalMaskIndex].priv_mask) == 0);
1286 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1288 /* remove the privilege granted to everybody to lock a read-only fd */
1289 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1290 opr_Verify(setprivgrp(PRIV_GLOBAL,
1291 privGrpList[globalMaskIndex].priv_mask) == 0);
1293 /* in this case, we should be able to do this with impunity, anyway */
1294 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1297 opr_Assert(lockfRtn != -1);
1299 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1300 opr_Verify(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1302 opr_Verify(flock(dp->lock_fd, LOCK_EX) == 0);
1303 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1308 VUnlockPartition_r(char *name)
1310 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1312 return; /* no partition, will fail later */
1314 dp->lock_fd = INVALID_FD;
1317 #endif /* AFS_NT40_ENV */
1320 VLockPartition(char *name)
1323 VLockPartition_r(name);
1328 VUnlockPartition(char *name)
1331 VUnlockPartition_r(name);
1335 #ifdef AFS_DEMAND_ATTACH_FS
1337 /* new-style partition locks; these are only to have some mutual exclusion
1338 * between the VGC scanner and volume utilies creating/altering vol headers
1342 * lock a partition's vol headers.
1344 * @param[in] dp the partition to lock
1345 * @param[in] locktype READ_LOCK or WRITE_LOCK
1347 * @return operation status
1351 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1355 /* block on acquiring the lock */
1358 code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1360 Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1361 VPartitionPath(dp));
1367 * unlock a partition's vol headers.
1369 * @param[in] dp the partition to unlock
1370 * @param[in] locktype READ_LOCK or WRITE_LOCK
1373 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1375 VReleaseDiskLock(&dp->headerLock, locktype);
1378 /* XXX not sure this will work on AFS_NT40_ENV
1379 * needs to be tested!
1383 * lookup a disk partition object by its index number.
1385 * @param[in] id partition index number
1386 * @param[in] abortp see abortp usage note below
1388 * @return disk partition object
1389 * @retval NULL no such disk partition
1391 * @note when abortp is non-zero, lookups which would return
1392 * NULL will result in an assertion failure
1394 * @pre VOL_LOCK must be held
1396 * @internal volume package internal use only
1399 struct DiskPartition64 *
1400 VGetPartitionById_r(afs_int32 id, int abortp)
1402 struct DiskPartition64 *dp = NULL;
1404 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1405 dp = DiskPartitionTable[id];
1409 opr_Assert(dp != NULL);
1415 * lookup a disk partition object by its index number.
1417 * @param[in] id partition index number
1418 * @param[in] abortp see abortp usage note below
1420 * @return disk partition object
1421 * @retval NULL no such disk partition
1423 * @note when abortp is non-zero, lookups which would return
1424 * NULL will result in an assertion failure
1427 struct DiskPartition64 *
1428 VGetPartitionById(afs_int32 id, int abortp)
1430 struct DiskPartition64 * dp;
1433 dp = VGetPartitionById_r(id, abortp);
1439 static struct DiskPartition64 *
1440 VLookupPartition_r(char * path)
1442 afs_int32 id = volutil_GetPartitionID(path);
1444 if (id < 0 || id > VOLMAXPARTS)
1447 return DiskPartitionTable[id];
1451 AddPartitionToTable_r(struct DiskPartition64 *dp)
1453 opr_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1454 DiskPartitionTable[dp->index] = dp;
1459 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1461 opr_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1462 DiskPartitionTable[dp->index] = NULL;
1465 #endif /* AFS_DEMAND_ATTACH_FS */