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>
33 #include <sys/param.h>
34 #include <sys/types.h>
37 #if AFS_HAVE_STATVFS || AFS_HAVE_STATVFS64
38 #include <sys/statvfs.h>
39 #endif /* AFS_HAVE_STATVFS */
40 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
41 #include <sys/mount.h>
44 #if !defined(AFS_SGI_ENV)
46 #include <sys/mount.h>
48 #else /* AFS_OSF_ENV */
49 #ifdef AFS_VFSINCL_ENV
52 #include <sys/fs/ufs_fs.h>
54 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
55 #include <ufs/ufs/dinode.h>
56 #include <ufs/ffs/fs.h>
61 #else /* AFS_VFSINCL_ENV */
62 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
65 #endif /* AFS_VFSINCL_ENV */
66 #endif /* AFS_OSF_ENV */
73 #include <sys/lockf.h>
79 #include <checklist.h>
81 #if defined(AFS_SUN_ENV)
89 #include <sys/mnttab.h>
90 #include <sys/mntent.h>
92 #ifdef AFS_LINUX22_ENV
94 #include <sys/statfs.h>
101 #endif /* AFS_SGI_ENV */
102 #endif /* AFS_NT40_ENV */
103 #if defined(AFS_SGI_ENV)
104 #include <sys/errno.h>
105 #include <sys/stat.h>
107 #include <sys/file.h>
112 #include <afs/afsint.h>
114 #include <afs/errors.h>
117 #include <afs/afssyscalls.h>
124 #include "namei_ops.h"
126 #endif /* AFS_NT40_ENV */
127 #endif /* AFS_NAMEI_ENV */
130 #include "partition.h"
131 #include <afs/afs_assert.h>
133 #if defined(AFS_HPUX_ENV)
134 #include <sys/types.h>
135 #include <sys/privgrp.h>
136 #endif /* defined(AFS_HPUX_ENV) */
139 #include <jfs/filsys.h>
142 int aixlow_water = 8; /* default 8% */
143 struct DiskPartition64 *DiskPartitionList;
145 #ifdef AFS_DEMAND_ATTACH_FS
146 /* file to lock to conceptually "lock" the vol headers on a partition */
147 #define AFS_PARTLOCK_FILE ".volheaders.lock"
148 #define AFS_VOLUMELOCK_FILE ".volume.lock"
150 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
152 static struct DiskPartition64 * VLookupPartition_r(char * path);
153 static void AddPartitionToTable_r(struct DiskPartition64 *);
154 #endif /* AFS_DEMAND_ATTACH_FS */
156 #ifdef AFS_SGI_XFS_IOPS_ENV
157 /* Verify that the on disk XFS inodes on the partition are large enough to
158 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
159 * too small to fit in the inode. Returns 0 if the attribute does fit in
162 #include <afs/xfsattrs.h>
164 VerifyXFSInodeSize(char *part, char *fstype)
167 int length = SIZEOF_XFS_ATTR_T;
172 if (strcmp("xfs", fstype))
175 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
176 if (((fd = open(part, O_RDONLY, 0)) != -1)
177 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
179 if (fsx.fsx_nextents) {
180 Log("Partition %s: XFS inodes too small, exiting.\n", part);
181 Log("Run xfs_size_check utility and remake partitions.\n");
188 (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
192 #endif /* AFS_SGI_XFS_IOPS_ENV */
195 VInitPartitionPackage(void)
197 #ifdef AFS_DEMAND_ATTACH_FS
198 memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
199 #endif /* AFS_DEMAND_ATTACH_FS */
204 VInitPartition_r(char *path, char *devname, Device dev)
206 struct DiskPartition64 *dp, *op;
208 dp = (struct DiskPartition64 *)malloc(sizeof(struct DiskPartition64));
209 /* Add it to the end, to preserve order when we print statistics */
210 for (op = DiskPartitionList; op; op = op->next) {
217 DiskPartitionList = dp;
219 dp->name = (char *)malloc(strlen(path) + 1);
220 strncpy(dp->name, path, strlen(path) + 1);
221 dp->index = volutil_GetPartitionID(path);
222 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
223 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
224 dp->devName = (char *)malloc(2 * strlen(path) + 6);
225 strcpy(dp->devName, path);
226 strcat(dp->devName, OS_DIRSEP);
227 strcat(dp->devName, "Lock");
228 mkdir(dp->devName, 0700);
229 strcat(dp->devName, path);
230 close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
231 dp->device = dp->index;
233 dp->devName = (char *)malloc(strlen(devname) + 1);
234 strncpy(dp->devName, devname, strlen(devname) + 1);
237 dp->lock_fd = INVALID_FD;
239 dp->f_files = 1; /* just a default value */
240 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
241 if (programType == fileServer)
242 (void)namei_ViceREADME(VPartitionPath(dp));
244 VSetPartitionDiskUsage_r(dp);
245 #ifdef AFS_DEMAND_ATTACH_FS
246 AddPartitionToTable_r(dp);
247 queue_Init(&dp->vol_list.head);
248 CV_INIT(&dp->vol_list.cv, "vol list", CV_DEFAULT, 0);
249 dp->vol_list.len = 0;
250 dp->vol_list.busy = 0;
252 char lockpath[MAXPATHLEN+1];
253 afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
254 lockpath[MAXPATHLEN] = '\0';
255 VLockFileInit(&dp->headerLockFile, lockpath);
257 afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
258 lockpath[MAXPATHLEN] = '\0';
259 VLockFileInit(&dp->volLockFile, lockpath);
261 VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
262 #endif /* AFS_DEMAND_ATTACH_FS */
266 VInitPartition(char *path, char *devname, Device dev)
269 VInitPartition_r(path, devname, dev);
274 /* VAttachPartitions() finds the vice partitions on this server. Calls
275 * VCheckPartition() to do some basic checks on the partition. If the partition
276 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
278 * Returns the number of errors returned by VCheckPartition. An error in
279 * VCheckPartition means that partition is a valid vice partition but the
280 * fileserver should not start because of the error found on that partition.
283 * No specific user space file system checks, since we don't know what
284 * is being used for vice partitions.
286 * Use partition name as devname.
289 VCheckPartition(char *part, char *devname)
291 struct afs_stat status;
292 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
293 char AFSIDatPath[MAXPATHLEN];
296 /* Only keep track of "/vicepx" partitions since it can get hairy
297 * when NFS mounts are involved.. */
298 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
301 if (afs_stat(part, &status) < 0) {
302 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
305 #ifndef AFS_AIX32_ENV
306 if (programType == fileServer) {
307 char salvpath[MAXPATHLEN];
308 strcpy(salvpath, part);
309 strcat(salvpath, "/FORCESALVAGE");
310 if (afs_stat(salvpath, &status) == 0) {
311 Log("VInitVnodes: Found %s; aborting\n", salvpath);
317 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
318 strcpy(AFSIDatPath, part);
319 strcat(AFSIDatPath, "/AFSIDat");
321 if (afs_stat(AFSIDatPath, &status) < 0) {
325 dirp = opendir(part);
327 while ((dp = readdir(dirp))) {
328 if (dp->d_name[0] == 'V') {
329 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);
336 #else /* AFS_NAMEI_ENV */
337 if (afs_stat(AFSIDatPath, &status) == 0) {
338 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
342 #ifdef AFS_SGI_XFS_IOPS_ENV
343 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
346 #endif /* AFS_NAMEI_ENV */
347 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
349 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
350 if (status.st_ino != ROOTINO) {
351 Log("%s is not a mounted file system; ignored.\n", part);
356 VInitPartition(part, devname, status.st_dev);
361 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
362 * attached (return value 1), or only attached when it is a separately
363 * mounted partition (return value 0). For non-NAMEI environments, it
366 * *awouldattach will be set to 1 if the given path at least looks like a vice
367 * partition (that is, if we return 0, the only thing preventing this partition
368 * from being attached is the existence of the AlwaysAttach file), or to 0
369 * otherwise. *awouldattach is set regardless of whether or not the partition
370 * should always be attached or not.
373 VIsAlwaysAttach(char *part, int *awouldattach)
379 #endif /* AFS_NAMEI_ENV */
386 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
393 strncpy(checkfile, part, 100);
394 strcat(checkfile, OS_DIRSEP);
395 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
397 ret = afs_stat(checkfile, &st);
398 return (ret < 0) ? 0 : 1;
399 #else /* AFS_NAMEI_ENV */
401 #endif /* AFS_NAMEI_ENV */
404 /* VAttachPartitions2() looks for and attaches /vicepX partitions
405 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
406 * used to attach /vicepX directories which aren't on dedicated
407 * partitions, in the NAMEI fileserver.
410 VAttachPartitions2(void)
418 dirp = opendir(OS_DIRSEP);
419 while ((de = readdir(dirp))) {
420 strcpy(pname, OS_DIRSEP);
421 strncat(pname, de->d_name, 20);
422 pname[sizeof(pname) - 1] = '\0';
424 /* Only keep track of "/vicepx" partitions since automounter
426 if (VIsAlwaysAttach(pname, &wouldattach)) {
427 VCheckPartition(pname, "");
430 if (wouldattach && VGetPartition(pname, 0) == NULL &&
431 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
433 /* This is a /vicep* dir, and it has not been attached as a
434 * partition. This probably means that this is a /vicep* dir
435 * that is not a separate partition, so just give a notice so
436 * admins are not confused as to why their /vicep* dirs are not
439 * It is possible that the dir _is_ a separate partition and we
440 * failed to attach it earlier, making this message a bit
441 * confusing. But that should be rare, and an error message
442 * about the failure will already be logged right before this,
443 * so it should be clear enough. */
445 Log("VAttachPartitions: not attaching %s; either it is not a "
446 "separate partition, or it failed to attach (create the "
447 "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
453 #endif /* AFS_NAMEI_ENV */
455 #endif /* AFS_NT40_ENV */
459 VAttachPartitions(void)
465 if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
466 Log("Can't open %s\n", MNTTAB);
470 while (!getmntent(mntfile, &mnt)) {
471 /* Ignore non ufs or non read/write partitions */
472 /* but allow zfs too if we're in the NAMEI environment */
475 (((strcmp(mnt.mnt_fstype, "ufs") &&
476 strcmp(mnt.mnt_fstype, "zfs"))))
478 (strcmp(mnt.mnt_fstype, "ufs") != 0)
480 || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
483 /* If we're going to always attach this partition, do it later. */
484 if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
487 #ifndef AFS_NAMEI_ENV
488 if (hasmntopt(&mnt, "logging") != NULL) {
489 Log("This program is compiled without AFS_NAMEI_ENV, and "
490 "partition %s is mounted with the 'logging' option. "
491 "Using the inode fileserver backend with 'logging' UFS "
492 "partitions causes volume corruption, so please either "
493 "mount the partition without logging, or use the namei "
494 "fileserver backend. Aborting...\n", mnt.mnt_mountp);
497 #endif /* !AFS_NAMEI_ENV */
499 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0)
503 (void)fclose(mntfile);
505 /* Process the always-attach partitions, if any. */
506 VAttachPartitions2();
511 #endif /* AFS_SUN5_ENV */
512 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
514 VAttachPartitions(void)
518 struct mntent *mntent;
520 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
521 Log("Problems in getting mount entries(setmntent)\n");
524 while (mntent = getmntent(mfd)) {
525 if (!hasmntopt(mntent, MNTOPT_RW))
528 /* If we're going to always attach this partition, do it later. */
529 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
532 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
538 /* Process the always-attach partitions, if any. */
539 VAttachPartitions2();
546 * (This function was grabbed from df.c)
549 getmount(struct vmount **vmountpp)
555 /* set initial size of mntctl buffer to a MAGIC NUMBER */
558 /* try the operation until ok or a fatal error */
560 if ((vm = (struct vmount *)malloc(size)) == NULL) {
561 /* failed getting memory for mount status buf */
562 perror("FATAL ERROR: get_stat malloc failed\n");
567 * perform the QUERY mntctl - if it returns > 0, that is the
568 * number of vmount structures in the buffer. If it returns
569 * -1, an error occured. If it returned 0, then look in
570 * first word of buffer for needed size.
572 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
573 /* OK, got it, now return */
577 } else if (nmounts == 0) {
578 /* the buffer wasn't big enough .... */
579 /* .... get required buffer size */
584 /* some other kind of error occurred */
592 VAttachPartitions(void)
596 struct vmount *vmountp;
598 if ((nmounts = getmount(&vmountp)) <= 0) {
599 Log("Problems in getting # of mount entries(getmount)\n");
604 (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
605 char *part = vmt2dataptr(vmountp, VMT_STUB);
607 if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
608 continue; /* Ignore any "special" partitions */
611 #ifndef AFS_NAMEI_ENV
613 struct superblock fs;
614 /* The Log statements are non-sequiters in the SalvageLog and don't
615 * even appear in the VolserLog, so restrict them to the FileLog.
617 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
618 if (programType == fileServer)
619 Log("Can't read superblock for %s, ignoring it.\n", part);
622 if (IsBigFilesFileSystem(&fs)) {
623 if (programType == fileServer)
624 Log("%s is a big files filesystem, ignoring it.\n", part);
631 /* If we're going to always attach this partition, do it later. */
632 if (VIsAlwaysAttach(part, NULL))
635 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0)
639 /* Process the always-attach partitions, if any. */
640 VAttachPartitions2();
645 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
647 VAttachPartitions(void)
652 if (setfsent() < 0) {
653 Log("Error listing filesystems.\n");
657 while ((fsent = getfsent())) {
658 if (strcmp(fsent->fs_type, "rw") != 0)
661 /* If we're going to always attach this partition, do it later. */
662 if (VIsAlwaysAttach(fsent->fs_file, NULL))
665 if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0)
670 /* Process the always-attach partitions, if any. */
671 VAttachPartitions2();
679 #include <sys/stat.h>
682 * validate names in vptab.
690 VValidVPTEntry(struct vptab *vpe)
692 int len = strlen(vpe->vp_name);
695 if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
697 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
700 for (i = VICE_PREFIX_SIZE; i < len; i++) {
701 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
702 Log("Invalid partition name %s in registry, ignoring it.\n",
707 if (len == VICE_PREFIX_SIZE + 2) {
708 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
709 (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
711 Log("Invalid partition name %s in registry, ignoring it.\n",
717 len = strlen(vpe->vp_dev);
718 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
719 || vpe->vp_dev[0] > 'Z') {
720 Log("Invalid device name %s in registry, ignoring it.\n",
729 VCheckPartition(char *partName)
736 /* partName is presumed to be of the form "X:" */
737 (void)sprintf(volRoot, "%c:\\", *partName);
739 if (!GetVolumeInformation(volRoot, /* volume root directory */
740 NULL, /* volume name buffer */
741 0, /* volume name size */
742 NULL, /* volume serial number */
743 &dwDummy, /* max component length */
744 &dwDummy, /* file system flags */
745 volFsType, /* file system name */
746 sizeof(volFsType))) {
747 err = GetLastError();
748 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
752 if (strcmp(volFsType, "NTFS")) {
753 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
762 VAttachPartitions(void)
764 struct DiskPartition64 *partP, *prevP, *nextP;
765 struct vpt_iter iter;
768 if (vpt_Start(&iter) < 0) {
769 Log("No partitions to attach.\n");
773 while (0 == vpt_NextEntry(&iter, &entry)) {
774 if (!VValidVPTEntry(&entry)) {
778 /* This test for duplicates relies on the fact that the method
779 * of storing the partition names in the NT registry means the same
780 * partition name will never appear twice in the list.
782 for (partP = DiskPartitionList; partP; partP = partP->next) {
783 if (*partP->devName == *entry.vp_dev) {
784 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
785 partP->flags = PART_DUPLICATE;
786 break; /* Only one entry will ever be in this list. */
790 continue; /* found a duplicate */
792 if (VCheckPartition(entry.vp_dev) < 0)
794 /* This test allows for manually inserting the FORCESALVAGE flag
795 * and thereby invoking the salvager. scandisk obviously won't be
798 if (programType == fileServer) {
799 struct afs_stat status;
800 char salvpath[MAXPATHLEN];
801 strcpy(salvpath, entry.vp_dev);
802 strcat(salvpath, "\\FORCESALVAGE");
803 if (afs_stat(salvpath, &status) == 0) {
804 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
808 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
812 /* Run through partition list and clear out the dupes. */
813 prevP = nextP = NULL;
814 for (partP = DiskPartitionList; partP; partP = nextP) {
816 if (partP->flags == PART_DUPLICATE) {
818 prevP->next = partP->next;
820 DiskPartitionList = partP->next;
830 #ifdef AFS_LINUX22_ENV
832 VAttachPartitions(void)
836 struct mntent *mntent;
838 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
839 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
840 Log("Problems in getting mount entries(setmntent)\n");
844 while ((mntent = getmntent(mfd))) {
845 /* If we're going to always attach this partition, do it later. */
846 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
849 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
854 /* Process the always-attach partitions, if any. */
855 VAttachPartitions2();
859 #endif /* AFS_LINUX22_ENV */
861 /* This routine is to be called whenever the actual name of the partition
862 * is required. The canonical name is still in part->name.
865 VPartitionPath(struct DiskPartition64 *part)
868 return part->devName;
874 /* get partition structure, abortp tells us if we should abort on failure */
875 struct DiskPartition64 *
876 VGetPartition_r(char *name, int abortp)
878 struct DiskPartition64 *dp;
879 #ifdef AFS_DEMAND_ATTACH_FS
880 dp = VLookupPartition_r(name);
881 #else /* AFS_DEMAND_ATTACH_FS */
882 for (dp = DiskPartitionList; dp; dp = dp->next) {
883 if (strcmp(dp->name, name) == 0)
886 #endif /* AFS_DEMAND_ATTACH_FS */
888 osi_Assert(dp != NULL);
892 struct DiskPartition64 *
893 VGetPartition(char *name, int abortp)
895 struct DiskPartition64 *retVal;
897 retVal = VGetPartition_r(name, abortp);
904 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
906 ULARGE_INTEGER free_user, total, free_total;
907 int ufree, tot, tfree;
909 if (!GetDiskFreeSpaceEx
910 (VPartitionPath(dp), &free_user, &total, &free_total)) {
911 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
916 /* Convert to 1K units. */
917 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
918 tot = (int)Int64ShraMod32(total.QuadPart, 10);
919 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
921 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
922 dp->totalUsable = tot;
928 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
931 afs_int64 totalblks, free, used, availblks;
934 struct afs_statvfs statbuf;
936 struct afs_statfs statbuf;
939 if (dp->flags & PART_DONTUPDATE)
941 /* Note: we don't bother syncing because it's only an estimate, update
942 * is syncing every 30 seconds anyway, we only have to keep the disk
943 * approximately 10% from full--you just can't get the stuff in from
944 * the net fast enough to worry */
946 code = afs_statvfs(dp->name, &statbuf);
948 code = afs_statfs(dp->name, &statbuf);
951 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
955 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
956 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
959 totalblks = statbuf.f_blocks;
960 free = statbuf.f_bfree;
961 reserved = free - statbuf.f_bavail;
963 bsize = statbuf.f_frsize;
965 bsize = statbuf.f_bsize;
967 availblks = totalblks - reserved;
968 dp->f_files = statbuf.f_files; /* max # of files in partition */
970 /* Now free and totalblks are in fragment units, but we want them in
974 free *= (bsize / 1024);
975 totalblks *= (bsize / 1024);
976 availblks *= (bsize / 1024);
977 reserved *= (bsize / 1024);
979 free /= (1024 / bsize);
980 totalblks /= (1024 / bsize);
981 availblks /= (1024 / bsize);
982 reserved /= (1024 / bsize);
984 /* now compute remaining figures */
985 used = totalblks - free;
987 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
988 dp->totalUsable = availblks;
989 dp->free = availblks - used; /* this is exactly f_bavail */
991 #endif /* AFS_NT40_ENV */
994 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
997 VSetPartitionDiskUsage_r(dp);
1002 VResetDiskUsage_r(void)
1004 struct DiskPartition64 *dp;
1005 for (dp = DiskPartitionList; dp; dp = dp->next) {
1006 VSetPartitionDiskUsage_r(dp);
1007 #ifndef AFS_PTHREAD_ENV
1009 #endif /* !AFS_PTHREAD_ENV */
1014 VResetDiskUsage(void)
1017 VResetDiskUsage_r();
1022 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1023 afs_sfsize_t checkBlocks)
1026 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1027 * for less than BlocksSpare would skip the error-checking path, and we
1028 * could grow existing files forever, not just for another BlocksSpare
1031 #ifdef AFS_AIX32_ENV
1032 afs_int32 rem, minavail;
1034 if ((rem = vp->partition->free - checkBlocks) < (minavail =
1040 if (vp->partition->free - checkBlocks < 0)
1043 else if (V_maxquota(vp)
1044 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1047 vp->partition->free -= blocks;
1048 V_diskused(vp) += blocks;
1052 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1053 afs_sfsize_t checkBlocks)
1056 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1061 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1064 #ifdef AFS_AIX32_ENV
1065 afs_int32 rem, minavail;
1067 if ((rem = vp->partition->free - blocks) < (minavail =
1070 aixlow_water) / 100))
1072 if (vp->partition->free - blocks < 0)
1076 vp->partition->free -= blocks;
1081 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1085 retVal = VDiskUsage_r(vp, blocks);
1091 VPrintDiskStats_r(void)
1093 struct DiskPartition64 *dp;
1094 for (dp = DiskPartitionList; dp; dp = dp->next) {
1096 Log("Partition %s: %"AFS_INT64_FMT
1097 " available 1K blocks (minfree=%"AFS_INT64_FMT"), "
1098 "overallocated by %"AFS_INT64_FMT" blocks\n", dp->name,
1099 dp->totalUsable, dp->minFree, -dp->free);
1101 Log("Partition %s: %"AFS_INT64_FMT
1102 " available 1K blocks (minfree=%"AFS_INT64_FMT"), "
1103 "%"AFS_INT64_FMT" free blocks\n", dp->name,
1104 dp->totalUsable, dp->minFree, dp->free);
1110 VPrintDiskStats(void)
1113 VPrintDiskStats_r();
1118 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1119 #define LOCKFILE "LOCKFILE"
1121 VLockPartition_r(char *name)
1123 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1128 if (dp->lock_fd == INVALID_FD) {
1131 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1133 (FD_t)CreateFile(path, GENERIC_WRITE,
1134 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1135 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1136 osi_Assert(dp->lock_fd != INVALID_FD);
1138 memset(&lap, 0, sizeof(lap));
1139 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1146 VUnlockPartition_r(char *name)
1148 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1152 return; /* no partition, will fail later */
1153 memset(&lap, 0, sizeof(lap));
1155 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1156 CloseHandle((HANDLE) dp->lock_fd);
1157 dp->lock_fd = INVALID_FD;
1159 #else /* AFS_NT40_ENV */
1161 #if defined(AFS_HPUX_ENV)
1162 #define BITS_PER_CHAR (8)
1163 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1165 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1166 #endif /* defined(AFS_HPUX_ENV) */
1169 VLockPartition_r(char *name)
1171 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1172 char *partitionName;
1174 struct timeval pausing;
1175 #if defined(AFS_HPUX_ENV)
1177 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1178 unsigned int *globalMask;
1179 int globalMaskIndex;
1180 #endif /* defined(AFS_HPUX_ENV) */
1181 #if defined(AFS_DARWIN_ENV)
1182 char lockfile[MAXPATHLEN];
1183 #endif /* defined(AFS_DARWIN_ENV) */
1184 #ifdef AFS_NAMEI_ENV
1185 #ifdef AFS_AIX42_ENV
1186 char LockFileName[MAXPATHLEN + 1];
1188 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1189 partitionName = (char *)&LockFileName;
1194 return; /* no partition, will fail later */
1195 if (dp->lock_fd != INVALID_FD)
1198 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1199 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1200 partitionName = dp->devName;
1203 #elif defined(AFS_DARWIN_ENV)
1204 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1205 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1206 code = O_RDONLY | O_CREAT;
1208 partitionName = dp->name;
1212 for (retries = 25; retries; retries--) {
1214 dp->lock_fd = afs_open(partitionName, code, 0644);
1216 dp->lock_fd = afs_open(partitionName, code);
1218 if (dp->lock_fd != INVALID_FD)
1220 if (errno == ENOENT)
1223 pausing.tv_usec = 500000;
1224 select(0, NULL, NULL, NULL, &pausing);
1226 osi_Assert(retries != 0);
1228 #if defined (AFS_HPUX_ENV)
1230 osi_Assert(getprivgrp(privGrpList) == 0);
1233 * In general, it will difficult and time-consuming ,if not impossible,
1234 * to try to find the privgroup to which this process belongs that has the
1235 * smallest membership, to minimise the security hole. So, we use the privgrp
1236 * to which everybody belongs.
1238 /* first, we have to find the global mask */
1239 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1240 globalMaskIndex++) {
1241 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1243 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1248 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1249 /* allow everybody to set a lock on a read-only file descriptor */
1250 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1251 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1254 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1256 /* remove the privilege granted to everybody to lock a read-only fd */
1257 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1258 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1261 /* in this case, we should be able to do this with impunity, anyway */
1262 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1265 osi_Assert(lockfRtn != -1);
1267 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1268 osi_Assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1270 osi_Assert(flock(dp->lock_fd, LOCK_EX) == 0);
1271 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1276 VUnlockPartition_r(char *name)
1278 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1280 return; /* no partition, will fail later */
1282 dp->lock_fd = INVALID_FD;
1285 #endif /* AFS_NT40_ENV */
1288 VLockPartition(char *name)
1291 VLockPartition_r(name);
1296 VUnlockPartition(char *name)
1299 VUnlockPartition_r(name);
1303 #ifdef AFS_DEMAND_ATTACH_FS
1305 /* new-style partition locks; these are only to have some mutual exclusion
1306 * between the VGC scanner and volume utilies creating/altering vol headers
1310 * lock a partition's vol headers.
1312 * @param[in] dp the partition to lock
1313 * @param[in] locktype READ_LOCK or WRITE_LOCK
1315 * @return operation status
1319 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1323 /* block on acquiring the lock */
1326 code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1328 Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1329 VPartitionPath(dp));
1335 * unlock a partition's vol headers.
1337 * @param[in] dp the partition to unlock
1338 * @param[in] locktype READ_LOCK or WRITE_LOCK
1341 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1343 VReleaseDiskLock(&dp->headerLock, locktype);
1346 /* XXX not sure this will work on AFS_NT40_ENV
1347 * needs to be tested!
1351 * lookup a disk partition object by its index number.
1353 * @param[in] id partition index number
1354 * @param[in] abortp see abortp usage note below
1356 * @return disk partition object
1357 * @retval NULL no such disk partition
1359 * @note when abortp is non-zero, lookups which would return
1360 * NULL will result in an assertion failure
1362 * @pre VOL_LOCK must be held
1364 * @internal volume package internal use only
1367 struct DiskPartition64 *
1368 VGetPartitionById_r(afs_int32 id, int abortp)
1370 struct DiskPartition64 *dp = NULL;
1372 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1373 dp = DiskPartitionTable[id];
1377 osi_Assert(dp != NULL);
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
1395 struct DiskPartition64 *
1396 VGetPartitionById(afs_int32 id, int abortp)
1398 struct DiskPartition64 * dp;
1401 dp = VGetPartitionById_r(id, abortp);
1407 static struct DiskPartition64 *
1408 VLookupPartition_r(char * path)
1410 afs_int32 id = volutil_GetPartitionID(path);
1412 if (id < 0 || id > VOLMAXPARTS)
1415 return DiskPartitionTable[id];
1419 AddPartitionToTable_r(struct DiskPartition64 *dp)
1421 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1422 DiskPartitionTable[dp->index] = dp;
1427 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1429 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1430 DiskPartitionTable[dp->index] = NULL;
1433 #endif /* AFS_DEMAND_ATTACH_FS */