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>
32 #include <sys/param.h>
33 #include <sys/types.h>
36 #if AFS_HAVE_STATVFS || AFS_HAVE_STATVFS64
37 #include <sys/statvfs.h>
38 #endif /* AFS_HAVE_STATVFS */
39 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
40 #include <sys/mount.h>
43 #if !defined(AFS_SGI_ENV)
45 #include <sys/mount.h>
47 #else /* AFS_OSF_ENV */
48 #ifdef AFS_VFSINCL_ENV
51 #include <sys/fs/ufs_fs.h>
53 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
54 #include <ufs/ufs/dinode.h>
55 #include <ufs/ffs/fs.h>
60 #else /* AFS_VFSINCL_ENV */
61 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
64 #endif /* AFS_VFSINCL_ENV */
65 #endif /* AFS_OSF_ENV */
72 #include <sys/lockf.h>
78 #include <checklist.h>
80 #if defined(AFS_SUN_ENV)
88 #include <sys/mnttab.h>
89 #include <sys/mntent.h>
91 #ifdef AFS_LINUX22_ENV
93 #include <sys/statfs.h>
100 #endif /* AFS_SGI_ENV */
101 #endif /* AFS_NT40_ENV */
102 #if defined(AFS_SGI_ENV)
103 #include <sys/errno.h>
104 #include <sys/stat.h>
106 #include <sys/file.h>
111 #include <afs/afsint.h>
113 #include <afs/errors.h>
116 #include <afs/afssyscalls.h>
122 #include "namei_ops.h"
124 #endif /* AFS_NT40_ENV */
125 #endif /* AFS_NAMEI_ENV */
128 #include "partition.h"
129 #ifdef AFS_PTHREAD_ENV
131 #else /* AFS_PTHREAD_ENV */
132 #include <afs/assert.h>
133 #endif /* AFS_PTHREAD_ENV */
135 #if defined(AFS_HPUX_ENV)
136 #include <sys/types.h>
137 #include <sys/privgrp.h>
138 #endif /* defined(AFS_HPUX_ENV) */
141 #include <jfs/filsys.h>
146 #define afs_stat stat64
147 #define afs_open open64
148 #define afs_fopen fopen64
150 #if AFS_HAVE_STATVFS64
151 # define afs_statvfs statvfs64
153 # if AFS_HAVE_STATFS64
154 # define afs_statfs statfs64
156 # if AFS_HAVE_STATVFS
157 # define afs_statvfs statvfs
159 # define afs_statfs statfs
160 # endif /* !AFS_HAVE_STATVFS */
161 # endif /* !AFS_HAVE_STATFS64 */
162 #endif /* !AFS_HAVE_STATVFS64 */
163 #endif /* !AFS_NT40_ENV */
165 #else /* !O_LARGEFILE */
167 #define afs_stat stat
168 #define afs_open open
169 #define afs_fopen fopen
172 #define afs_statvfs statvfs
173 #else /* !AFS_HAVE_STATVFS */
174 #define afs_statfs statfs
175 #endif /* !AFS_HAVE_STATVFS */
176 #endif /* !AFS_NT40_ENV */
178 #endif /* !O_LARGEFILE */
180 /*@printflike@*/ extern void Log(const char *format, ...);
182 int aixlow_water = 8; /* default 8% */
183 struct DiskPartition64 *DiskPartitionList;
185 #ifdef AFS_DEMAND_ATTACH_FS
186 /* file to lock to conceptually "lock" the vol headers on a partition */
187 #define AFS_PARTLOCK_FILE ".volheaders.lock"
188 #define AFS_VOLUMELOCK_FILE ".volume.lock"
190 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
192 static struct DiskPartition64 * VLookupPartition_r(char * path);
193 static void AddPartitionToTable_r(struct DiskPartition64 *);
194 #endif /* AFS_DEMAND_ATTACH_FS */
196 #ifdef AFS_SGI_XFS_IOPS_ENV
197 /* Verify that the on disk XFS inodes on the partition are large enough to
198 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
199 * too small to fit in the inode. Returns 0 if the attribute does fit in
202 #include <afs/xfsattrs.h>
204 VerifyXFSInodeSize(char *part, char *fstype)
207 int length = SIZEOF_XFS_ATTR_T;
212 if (strcmp("xfs", fstype))
215 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
216 if (((fd = open(part, O_RDONLY, 0)) != -1)
217 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
219 if (fsx.fsx_nextents) {
220 Log("Partition %s: XFS inodes too small, exiting.\n", part);
221 Log("Run xfs_size_check utility and remake partitions.\n");
228 (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
232 #endif /* AFS_SGI_XFS_IOPS_ENV */
235 VInitPartitionPackage(void)
237 #ifdef AFS_DEMAND_ATTACH_ENV
238 memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
239 #endif /* AFS_DEMAND_ATTACH_ENV */
244 VInitPartition_r(char *path, char *devname, Device dev)
246 struct DiskPartition64 *dp, *op;
248 dp = (struct DiskPartition64 *)malloc(sizeof(struct DiskPartition64));
249 /* Add it to the end, to preserve order when we print statistics */
250 for (op = DiskPartitionList; op; op = op->next) {
257 DiskPartitionList = dp;
259 dp->name = (char *)malloc(strlen(path) + 1);
260 strncpy(dp->name, path, strlen(path) + 1);
261 dp->index = volutil_GetPartitionID(path);
262 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
263 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
264 dp->devName = (char *)malloc(2 * strlen(path) + 6);
265 strcpy(dp->devName, path);
266 strcat(dp->devName, "/");
267 strcat(dp->devName, "Lock");
268 mkdir(dp->devName, 0700);
269 strcat(dp->devName, path);
270 close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
271 dp->device = dp->index;
273 dp->devName = (char *)malloc(strlen(devname) + 1);
274 strncpy(dp->devName, devname, strlen(devname) + 1);
277 dp->lock_fd = INVALID_FD;
279 dp->f_files = 1; /* just a default value */
280 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
281 if (programType == fileServer)
282 (void)namei_ViceREADME(VPartitionPath(dp));
284 VSetPartitionDiskUsage_r(dp);
285 #ifdef AFS_DEMAND_ATTACH_FS
286 AddPartitionToTable_r(dp);
287 queue_Init(&dp->vol_list.head);
288 assert(pthread_cond_init(&dp->vol_list.cv, NULL) == 0);
289 dp->vol_list.len = 0;
290 dp->vol_list.busy = 0;
292 char lockpath[MAXPATHLEN+1];
293 afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
294 lockpath[MAXPATHLEN] = '\0';
295 VLockFileInit(&dp->headerLockFile, lockpath);
297 afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
298 lockpath[MAXPATHLEN] = '\0';
299 VLockFileInit(&dp->volLockFile, lockpath);
301 VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
302 #endif /* AFS_DEMAND_ATTACH_FS */
306 VInitPartition(char *path, char *devname, Device dev)
309 VInitPartition_r(path, devname, dev);
314 /* VAttachPartitions() finds the vice partitions on this server. Calls
315 * VCheckPartition() to do some basic checks on the partition. If the partition
316 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
318 * Returns the number of errors returned by VCheckPartition. An error in
319 * VCheckPartition means that partition is a valid vice partition but the
320 * fileserver should not start because of the error found on that partition.
323 * No specific user space file system checks, since we don't know what
324 * is being used for vice partitions.
326 * Use partition name as devname.
329 VCheckPartition(char *part, char *devname)
331 struct afs_stat status;
332 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
333 char AFSIDatPath[MAXPATHLEN];
336 /* Only keep track of "/vicepx" partitions since it can get hairy
337 * when NFS mounts are involved.. */
338 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
341 if (afs_stat(part, &status) < 0) {
342 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
345 #ifndef AFS_AIX32_ENV
346 if (programType == fileServer) {
347 char salvpath[MAXPATHLEN];
348 strcpy(salvpath, part);
349 strcat(salvpath, "/FORCESALVAGE");
350 if (afs_stat(salvpath, &status) == 0) {
351 Log("VInitVnodes: Found %s; aborting\n", salvpath);
357 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
358 strcpy(AFSIDatPath, part);
359 strcat(AFSIDatPath, "/AFSIDat");
361 if (afs_stat(AFSIDatPath, &status) < 0) {
365 dirp = opendir(part);
367 while ((dp = readdir(dirp))) {
368 if (dp->d_name[0] == 'V') {
369 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);
376 #else /* AFS_NAMEI_ENV */
377 if (afs_stat(AFSIDatPath, &status) == 0) {
378 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
382 #ifdef AFS_SGI_XFS_IOPS_ENV
383 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
386 #endif /* AFS_NAMEI_ENV */
387 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
389 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
390 if (status.st_ino != ROOTINO) {
391 Log("%s is not a mounted file system; ignored.\n", part);
396 VInitPartition(part, devname, status.st_dev);
401 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
402 * attached (return value 1), or only attached when it is a separately
403 * mounted partition (return value 0). For non-NAMEI environments, it
407 VIsAlwaysAttach(char *part)
414 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
417 strncpy(checkfile, part, 100);
418 strcat(checkfile, "/");
419 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
421 ret = afs_stat(checkfile, &st);
422 return (ret < 0) ? 0 : 1;
423 #else /* AFS_NAMEI_ENV */
425 #endif /* AFS_NAMEI_ENV */
428 /* VAttachPartitions2() looks for and attaches /vicepX partitions
429 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
430 * used to attach /vicepX directories which aren't on dedicated
431 * partitions, in the NAMEI fileserver.
434 VAttachPartitions2(void)
442 while ((de = readdir(dirp))) {
444 strncat(pname, de->d_name, 20);
445 pname[sizeof(pname) - 1] = '\0';
447 /* Only keep track of "/vicepx" partitions since automounter
449 if (VIsAlwaysAttach(pname))
450 VCheckPartition(pname, "");
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))
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))
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(register struct vmount **vmountpp)
552 register struct vmount *vm;
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))
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))
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))
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 register 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 */
892 struct DiskPartition64 *
893 VGetPartition(char *name, int abortp)
895 struct DiskPartition64 *retVal;
897 retVal = VGetPartition_r(name, abortp);
904 VSetPartitionDiskUsage_r(register 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(register 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(register 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) {
1095 Log("Partition %s: %d available 1K blocks (minfree=%d), ", dp->name,
1096 dp->totalUsable, dp->minFree);
1098 Log("overallocated by %d blocks\n", -dp->free);
1100 Log("%d free blocks\n", dp->free);
1105 VPrintDiskStats(void)
1108 VPrintDiskStats_r();
1113 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1114 #define LOCKFILE "LOCKFILE"
1116 VLockPartition_r(char *name)
1118 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1123 if (dp->lock_fd == INVALID_FD) {
1126 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1128 (FD_t)CreateFile(path, GENERIC_WRITE,
1129 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1130 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1131 assert(dp->lock_fd != INVALID_FD);
1133 memset(&lap, 0, sizeof(lap));
1134 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1141 VUnlockPartition_r(char *name)
1143 register struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1147 return; /* no partition, will fail later */
1148 memset(&lap, 0, sizeof(lap));
1150 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1151 CloseHandle((HANDLE) dp->lock_fd);
1152 dp->lock_fd = INVALID_FD;
1154 #else /* AFS_NT40_ENV */
1156 #if defined(AFS_HPUX_ENV)
1157 #define BITS_PER_CHAR (8)
1158 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1160 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1161 #endif /* defined(AFS_HPUX_ENV) */
1164 VLockPartition_r(char *name)
1166 register struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1167 char *partitionName;
1169 struct timeval pausing;
1170 #if defined(AFS_HPUX_ENV)
1172 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1173 unsigned int *globalMask;
1174 int globalMaskIndex;
1175 #endif /* defined(AFS_HPUX_ENV) */
1176 #if defined(AFS_DARWIN_ENV)
1177 char lockfile[MAXPATHLEN];
1178 #endif /* defined(AFS_DARWIN_ENV) */
1179 #ifdef AFS_NAMEI_ENV
1180 #ifdef AFS_AIX42_ENV
1181 char LockFileName[MAXPATHLEN + 1];
1183 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1184 partitionName = (char *)&LockFileName;
1189 return; /* no partition, will fail later */
1190 if (dp->lock_fd != -1)
1193 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1194 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1195 partitionName = dp->devName;
1198 #elif defined(AFS_DARWIN_ENV)
1199 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1200 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1201 code = O_RDONLY | O_CREAT;
1203 partitionName = dp->name;
1207 for (retries = 25; retries; retries--) {
1209 dp->lock_fd = afs_open(partitionName, code, 0644);
1211 dp->lock_fd = afs_open(partitionName, code);
1213 if (dp->lock_fd != -1)
1215 if (errno == ENOENT)
1218 pausing.tv_usec = 500000;
1219 select(0, NULL, NULL, NULL, &pausing);
1221 assert(retries != 0);
1223 #if defined (AFS_HPUX_ENV)
1225 assert(getprivgrp(privGrpList) == 0);
1228 * In general, it will difficult and time-consuming ,if not impossible,
1229 * to try to find the privgroup to which this process belongs that has the
1230 * smallest membership, to minimise the security hole. So, we use the privgrp
1231 * to which everybody belongs.
1233 /* first, we have to find the global mask */
1234 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1235 globalMaskIndex++) {
1236 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1238 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1243 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1244 /* allow everybody to set a lock on a read-only file descriptor */
1245 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1246 assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1249 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1251 /* remove the privilege granted to everybody to lock a read-only fd */
1252 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1253 assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1256 /* in this case, we should be able to do this with impunity, anyway */
1257 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1260 assert(lockfRtn != -1);
1262 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1263 assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1265 assert(flock(dp->lock_fd, LOCK_EX) == 0);
1266 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1271 VUnlockPartition_r(char *name)
1273 register struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1275 return; /* no partition, will fail later */
1280 #endif /* AFS_NT40_ENV */
1283 VLockPartition(char *name)
1286 VLockPartition_r(name);
1291 VUnlockPartition(char *name)
1294 VUnlockPartition_r(name);
1298 #ifdef AFS_DEMAND_ATTACH_FS
1300 /* new-style partition locks; these are only to have some mutual exclusion
1301 * between the VGC scanner and volume utilies creating/altering vol headers
1305 * lock a partition's vol headers.
1307 * @param[in] dp the partition to lock
1308 * @param[in] locktype READ_LOCK or WRITE_LOCK
1310 * @return operation status
1314 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1318 /* block on acquiring the lock */
1321 code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1323 Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1324 VPartitionPath(dp));
1330 * unlock a partition's vol headers.
1332 * @param[in] dp the partition to unlock
1333 * @param[in] locktype READ_LOCK or WRITE_LOCK
1336 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1338 VReleaseDiskLock(&dp->headerLock, locktype);
1341 /* XXX not sure this will work on AFS_NT40_ENV
1342 * needs to be tested!
1346 * lookup a disk partition object by its index number.
1348 * @param[in] id partition index number
1349 * @param[in] abortp see abortp usage note below
1351 * @return disk partition object
1352 * @retval NULL no such disk partition
1354 * @note when abortp is non-zero, lookups which would return
1355 * NULL will result in an assertion failure
1357 * @pre VOL_LOCK must be held
1359 * @internal volume package internal use only
1362 struct DiskPartition64 *
1363 VGetPartitionById_r(afs_int32 id, int abortp)
1365 struct DiskPartition64 *dp = NULL;
1367 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1368 dp = DiskPartitionTable[id];
1378 * lookup a disk partition object by its index number.
1380 * @param[in] id partition index number
1381 * @param[in] abortp see abortp usage note below
1383 * @return disk partition object
1384 * @retval NULL no such disk partition
1386 * @note when abortp is non-zero, lookups which would return
1387 * NULL will result in an assertion failure
1390 struct DiskPartition64 *
1391 VGetPartitionById(afs_int32 id, int abortp)
1393 struct DiskPartition64 * dp;
1396 dp = VGetPartitionById_r(id, abortp);
1402 static struct DiskPartition64 *
1403 VLookupPartition_r(char * path)
1405 afs_int32 id = volutil_GetPartitionID(path);
1407 if (id < 0 || id > VOLMAXPARTS)
1410 return DiskPartitionTable[id];
1414 AddPartitionToTable_r(struct DiskPartition64 *dp)
1416 assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1417 DiskPartitionTable[dp->index] = dp;
1422 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1424 assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1425 DiskPartitionTable[dp->index] = NULL;
1428 #endif /* AFS_DEMAND_ATTACH_FS */