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 /* VAttachPartitions2() looks for and attaches /vicepX partitions
402 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
403 * used to attach /vicepX directories which aren't on dedicated
404 * partitions, in the NAMEI fileserver.
407 VAttachPartitions2(void)
415 dirp = opendir(OS_DIRSEP);
416 while ((de = readdir(dirp))) {
417 strcpy(pname, OS_DIRSEP);
418 strncat(pname, de->d_name, 20);
419 pname[sizeof(pname) - 1] = '\0';
421 /* Only keep track of "/vicepx" partitions since automounter
423 if (VIsAlwaysAttach(pname, &wouldattach)) {
424 VCheckPartition(pname, "", 0);
426 struct afs_stat_st st;
427 if (wouldattach && VGetPartition(pname, 0) == NULL &&
428 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
430 /* This is a /vicep* dir, and it has not been attached as a
431 * partition. This probably means that this is a /vicep* dir
432 * that is not a separate partition, so just give a notice so
433 * admins are not confused as to why their /vicep* dirs are not
436 * It is possible that the dir _is_ a separate partition and we
437 * failed to attach it earlier, making this message a bit
438 * confusing. But that should be rare, and an error message
439 * about the failure will already be logged right before this,
440 * so it should be clear enough. */
442 Log("VAttachPartitions: not attaching %s; either it is not a "
443 "separate partition, or it failed to attach (create the "
444 "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
450 #endif /* AFS_NAMEI_ENV */
452 #endif /* AFS_NT40_ENV */
456 VAttachPartitions(void)
462 if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
463 Log("Can't open %s\n", MNTTAB);
467 while (!getmntent(mntfile, &mnt)) {
469 /* Ignore non ufs or non read/write partitions */
470 /* but allow zfs too if we're in the NAMEI environment */
473 (((strcmp(mnt.mnt_fstype, "ufs") &&
474 strcmp(mnt.mnt_fstype, "zfs"))))
476 (strcmp(mnt.mnt_fstype, "ufs") != 0)
478 || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
481 /* If we're going to always attach this partition, do it later. */
482 if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
485 #ifndef AFS_NAMEI_ENV
486 if (hasmntopt(&mnt, "logging") != NULL) {
489 #endif /* !AFS_NAMEI_ENV */
491 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special, logging) < 0)
495 (void)fclose(mntfile);
497 /* Process the always-attach partitions, if any. */
498 VAttachPartitions2();
503 #endif /* AFS_SUN5_ENV */
504 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
506 VAttachPartitions(void)
510 struct mntent *mntent;
512 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
513 Log("Problems in getting mount entries(setmntent)\n");
516 while (mntent = getmntent(mfd)) {
517 if (!hasmntopt(mntent, MNTOPT_RW))
520 /* If we're going to always attach this partition, do it later. */
521 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
524 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
530 /* Process the always-attach partitions, if any. */
531 VAttachPartitions2();
538 * (This function was grabbed from df.c)
541 getmount(struct vmount **vmountpp)
547 /* set initial size of mntctl buffer to a MAGIC NUMBER */
550 /* try the operation until ok or a fatal error */
552 if ((vm = (struct vmount *)malloc(size)) == NULL) {
553 /* failed getting memory for mount status buf */
554 perror("FATAL ERROR: get_stat malloc failed\n");
559 * perform the QUERY mntctl - if it returns > 0, that is the
560 * number of vmount structures in the buffer. If it returns
561 * -1, an error occured. If it returned 0, then look in
562 * first word of buffer for needed size.
564 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
565 /* OK, got it, now return */
569 } else if (nmounts == 0) {
570 /* the buffer wasn't big enough .... */
571 /* .... get required buffer size */
576 /* some other kind of error occurred */
584 VAttachPartitions(void)
588 struct vmount *vmountp;
590 if ((nmounts = getmount(&vmountp)) <= 0) {
591 Log("Problems in getting # of mount entries(getmount)\n");
596 (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
597 char *part = vmt2dataptr(vmountp, VMT_STUB);
599 if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
600 continue; /* Ignore any "special" partitions */
603 #ifndef AFS_NAMEI_ENV
605 struct superblock fs;
606 /* The Log statements are non-sequiters in the SalvageLog and don't
607 * even appear in the VolserLog, so restrict them to the FileLog.
609 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
610 if (programType == fileServer)
611 Log("Can't read superblock for %s, ignoring it.\n", part);
614 if (IsBigFilesFileSystem(&fs)) {
615 if (programType == fileServer)
616 Log("%s is a big files filesystem, ignoring it.\n", part);
623 /* If we're going to always attach this partition, do it later. */
624 if (VIsAlwaysAttach(part, NULL))
627 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT), 0) < 0)
631 /* Process the always-attach partitions, if any. */
632 VAttachPartitions2();
637 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
639 VAttachPartitions(void)
644 if (setfsent() < 0) {
645 Log("Error listing filesystems.\n");
649 while ((fsent = getfsent())) {
650 if (strcmp(fsent->fs_type, "rw") != 0)
653 /* If we're going to always attach this partition, do it later. */
654 if (VIsAlwaysAttach(fsent->fs_file, NULL))
657 if (VCheckPartition(fsent->fs_file, fsent->fs_spec, 0) < 0)
662 /* Process the always-attach partitions, if any. */
663 VAttachPartitions2();
671 #include <sys/stat.h>
674 * validate names in vptab.
682 VValidVPTEntry(struct vptab *vpe)
684 int len = strlen(vpe->vp_name);
687 if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
689 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
692 for (i = VICE_PREFIX_SIZE; i < len; i++) {
693 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
694 Log("Invalid partition name %s in registry, ignoring it.\n",
699 if (len == VICE_PREFIX_SIZE + 2) {
700 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
701 (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
703 Log("Invalid partition name %s in registry, ignoring it.\n",
709 len = strlen(vpe->vp_dev);
710 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
711 || vpe->vp_dev[0] > 'Z') {
712 Log("Invalid device name %s in registry, ignoring it.\n",
721 VCheckPartition(char *partName)
728 /* partName is presumed to be of the form "X:" */
729 (void)sprintf(volRoot, "%c:\\", *partName);
731 if (!GetVolumeInformation(volRoot, /* volume root directory */
732 NULL, /* volume name buffer */
733 0, /* volume name size */
734 NULL, /* volume serial number */
735 &dwDummy, /* max component length */
736 &dwDummy, /* file system flags */
737 volFsType, /* file system name */
738 sizeof(volFsType))) {
739 err = GetLastError();
740 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
744 if (strcmp(volFsType, "NTFS")) {
745 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
754 VAttachPartitions(void)
756 struct DiskPartition64 *partP, *prevP, *nextP;
757 struct vpt_iter iter;
760 if (vpt_Start(&iter) < 0) {
761 Log("No partitions to attach.\n");
765 while (0 == vpt_NextEntry(&iter, &entry)) {
766 if (!VValidVPTEntry(&entry)) {
770 /* This test for duplicates relies on the fact that the method
771 * of storing the partition names in the NT registry means the same
772 * partition name will never appear twice in the list.
774 for (partP = DiskPartitionList; partP; partP = partP->next) {
775 if (*partP->devName == *entry.vp_dev) {
776 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
777 partP->flags = PART_DUPLICATE;
778 break; /* Only one entry will ever be in this list. */
782 continue; /* found a duplicate */
784 if (VCheckPartition(entry.vp_dev) < 0)
786 /* This test allows for manually inserting the FORCESALVAGE flag
787 * and thereby invoking the salvager. scandisk obviously won't be
790 if (programType == fileServer) {
791 struct afs_stat_st status;
792 char salvpath[MAXPATHLEN];
793 strcpy(salvpath, entry.vp_dev);
794 strcat(salvpath, "\\FORCESALVAGE");
795 if (afs_stat(salvpath, &status) == 0) {
796 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
800 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
804 /* Run through partition list and clear out the dupes. */
805 prevP = nextP = NULL;
806 for (partP = DiskPartitionList; partP; partP = nextP) {
808 if (partP->flags == PART_DUPLICATE) {
810 prevP->next = partP->next;
812 DiskPartitionList = partP->next;
822 #ifdef AFS_LINUX22_ENV
824 VAttachPartitions(void)
828 struct mntent *mntent;
830 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
831 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
832 Log("Problems in getting mount entries(setmntent)\n");
836 while ((mntent = getmntent(mfd))) {
837 /* If we're going to always attach this partition, do it later. */
838 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
841 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
846 /* Process the always-attach partitions, if any. */
847 VAttachPartitions2();
851 #endif /* AFS_LINUX22_ENV */
853 /* This routine is to be called whenever the actual name of the partition
854 * is required. The canonical name is still in part->name.
857 VPartitionPath(struct DiskPartition64 *part)
860 return part->devName;
866 /* get partition structure, abortp tells us if we should abort on failure */
867 struct DiskPartition64 *
868 VGetPartition_r(char *name, int abortp)
870 struct DiskPartition64 *dp;
871 #ifdef AFS_DEMAND_ATTACH_FS
872 dp = VLookupPartition_r(name);
873 #else /* AFS_DEMAND_ATTACH_FS */
874 for (dp = DiskPartitionList; dp; dp = dp->next) {
875 if (strcmp(dp->name, name) == 0)
878 #endif /* AFS_DEMAND_ATTACH_FS */
880 osi_Assert(dp != NULL);
884 struct DiskPartition64 *
885 VGetPartition(char *name, int abortp)
887 struct DiskPartition64 *retVal;
889 retVal = VGetPartition_r(name, abortp);
896 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
898 ULARGE_INTEGER free_user, total, free_total;
899 int ufree, tot, tfree;
901 if (!GetDiskFreeSpaceEx
902 (VPartitionPath(dp), &free_user, &total, &free_total)) {
903 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
908 /* Convert to 1K units. */
909 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
910 tot = (int)Int64ShraMod32(total.QuadPart, 10);
911 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
913 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
914 dp->totalUsable = tot;
920 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
923 afs_int64 totalblks, free, used, availblks;
926 struct afs_statvfs statbuf;
928 struct afs_statfs statbuf;
931 if (dp->flags & PART_DONTUPDATE)
933 /* Note: we don't bother syncing because it's only an estimate, update
934 * is syncing every 30 seconds anyway, we only have to keep the disk
935 * approximately 10% from full--you just can't get the stuff in from
936 * the net fast enough to worry */
938 code = afs_statvfs(dp->name, &statbuf);
940 code = afs_statfs(dp->name, &statbuf);
943 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
947 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
948 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
951 totalblks = statbuf.f_blocks;
952 free = statbuf.f_bfree;
953 reserved = free - statbuf.f_bavail;
955 bsize = statbuf.f_frsize;
957 bsize = statbuf.f_bsize;
959 availblks = totalblks - reserved;
960 dp->f_files = statbuf.f_files; /* max # of files in partition */
962 /* Now free and totalblks are in fragment units, but we want them in
966 free *= (bsize / 1024);
967 totalblks *= (bsize / 1024);
968 availblks *= (bsize / 1024);
969 reserved *= (bsize / 1024);
971 free /= (1024 / bsize);
972 totalblks /= (1024 / bsize);
973 availblks /= (1024 / bsize);
974 reserved /= (1024 / bsize);
976 /* now compute remaining figures */
977 used = totalblks - free;
979 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
980 dp->totalUsable = availblks;
981 dp->free = availblks - used; /* this is exactly f_bavail */
983 #endif /* AFS_NT40_ENV */
986 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
989 VSetPartitionDiskUsage_r(dp);
994 VResetDiskUsage_r(void)
996 struct DiskPartition64 *dp;
997 for (dp = DiskPartitionList; dp; dp = dp->next) {
998 VSetPartitionDiskUsage_r(dp);
999 #ifndef AFS_PTHREAD_ENV
1001 #endif /* !AFS_PTHREAD_ENV */
1006 VResetDiskUsage(void)
1009 VResetDiskUsage_r();
1014 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1015 afs_sfsize_t checkBlocks)
1018 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1019 * for less than BlocksSpare would skip the error-checking path, and we
1020 * could grow existing files forever, not just for another BlocksSpare
1023 #ifdef AFS_AIX32_ENV
1024 afs_int32 rem, minavail;
1026 if ((rem = vp->partition->free - checkBlocks) < (minavail =
1032 if (vp->partition->free - checkBlocks < 0)
1035 else if (V_maxquota(vp)
1036 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1039 vp->partition->free -= blocks;
1040 V_diskused(vp) += blocks;
1044 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1045 afs_sfsize_t checkBlocks)
1048 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1053 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1056 #ifdef AFS_AIX32_ENV
1057 afs_int32 rem, minavail;
1059 if ((rem = vp->partition->free - blocks) < (minavail =
1062 aixlow_water) / 100))
1064 if (vp->partition->free - blocks < 0)
1068 vp->partition->free -= blocks;
1073 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1077 retVal = VDiskUsage_r(vp, blocks);
1083 VPrintDiskStats_r(void)
1085 struct DiskPartition64 *dp;
1086 for (dp = DiskPartitionList; dp; dp = dp->next) {
1088 Log("Partition %s: %lld "
1089 " available 1K blocks (minfree=%lld), "
1090 "overallocated by %lld blocks\n", dp->name,
1091 dp->totalUsable, dp->minFree, -dp->free);
1093 Log("Partition %s: %lld"
1094 " available 1K blocks (minfree=%lld), "
1095 "%lld free blocks\n", dp->name,
1096 dp->totalUsable, dp->minFree, dp->free);
1102 VPrintDiskStats(void)
1105 VPrintDiskStats_r();
1110 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1111 #define LOCKFILE "LOCKFILE"
1113 VLockPartition_r(char *name)
1115 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1120 if (dp->lock_fd == INVALID_FD) {
1123 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1125 (FD_t)CreateFile(path, GENERIC_WRITE,
1126 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1127 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1128 osi_Assert(dp->lock_fd != INVALID_FD);
1130 memset(&lap, 0, sizeof(lap));
1131 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1138 VUnlockPartition_r(char *name)
1140 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1144 return; /* no partition, will fail later */
1145 memset(&lap, 0, sizeof(lap));
1147 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1148 CloseHandle((HANDLE) dp->lock_fd);
1149 dp->lock_fd = INVALID_FD;
1151 #else /* AFS_NT40_ENV */
1153 #if defined(AFS_HPUX_ENV)
1154 #define BITS_PER_CHAR (8)
1155 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1157 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1158 #endif /* defined(AFS_HPUX_ENV) */
1161 VLockPartition_r(char *name)
1163 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1164 char *partitionName;
1166 struct timeval pausing;
1167 #if defined(AFS_HPUX_ENV)
1169 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1170 unsigned int *globalMask;
1171 int globalMaskIndex;
1172 #endif /* defined(AFS_HPUX_ENV) */
1173 #if defined(AFS_DARWIN_ENV)
1174 char lockfile[MAXPATHLEN];
1175 #endif /* defined(AFS_DARWIN_ENV) */
1176 #ifdef AFS_NAMEI_ENV
1177 #ifdef AFS_AIX42_ENV
1178 char LockFileName[MAXPATHLEN + 1];
1180 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1181 partitionName = (char *)&LockFileName;
1186 return; /* no partition, will fail later */
1187 if (dp->lock_fd != INVALID_FD)
1190 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1191 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1192 partitionName = dp->devName;
1195 #elif defined(AFS_DARWIN_ENV)
1196 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1197 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1198 code = O_RDONLY | O_CREAT;
1200 partitionName = dp->name;
1204 for (retries = 25; retries; retries--) {
1206 dp->lock_fd = afs_open(partitionName, code, 0644);
1208 dp->lock_fd = afs_open(partitionName, code);
1210 if (dp->lock_fd != INVALID_FD)
1212 if (errno == ENOENT)
1215 pausing.tv_usec = 500000;
1216 select(0, NULL, NULL, NULL, &pausing);
1218 osi_Assert(retries != 0);
1220 #if defined (AFS_HPUX_ENV)
1222 osi_Assert(getprivgrp(privGrpList) == 0);
1225 * In general, it will difficult and time-consuming ,if not impossible,
1226 * to try to find the privgroup to which this process belongs that has the
1227 * smallest membership, to minimise the security hole. So, we use the privgrp
1228 * to which everybody belongs.
1230 /* first, we have to find the global mask */
1231 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1232 globalMaskIndex++) {
1233 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1235 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1240 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1241 /* allow everybody to set a lock on a read-only file descriptor */
1242 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1243 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1246 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1248 /* remove the privilege granted to everybody to lock a read-only fd */
1249 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1250 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1253 /* in this case, we should be able to do this with impunity, anyway */
1254 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1257 osi_Assert(lockfRtn != -1);
1259 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1260 osi_Assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1262 osi_Assert(flock(dp->lock_fd, LOCK_EX) == 0);
1263 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1268 VUnlockPartition_r(char *name)
1270 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1272 return; /* no partition, will fail later */
1274 dp->lock_fd = INVALID_FD;
1277 #endif /* AFS_NT40_ENV */
1280 VLockPartition(char *name)
1283 VLockPartition_r(name);
1288 VUnlockPartition(char *name)
1291 VUnlockPartition_r(name);
1295 #ifdef AFS_DEMAND_ATTACH_FS
1297 /* new-style partition locks; these are only to have some mutual exclusion
1298 * between the VGC scanner and volume utilies creating/altering vol headers
1302 * lock a partition's vol headers.
1304 * @param[in] dp the partition to lock
1305 * @param[in] locktype READ_LOCK or WRITE_LOCK
1307 * @return operation status
1311 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1315 /* block on acquiring the lock */
1318 code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1320 Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1321 VPartitionPath(dp));
1327 * unlock a partition's vol headers.
1329 * @param[in] dp the partition to unlock
1330 * @param[in] locktype READ_LOCK or WRITE_LOCK
1333 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1335 VReleaseDiskLock(&dp->headerLock, locktype);
1338 /* XXX not sure this will work on AFS_NT40_ENV
1339 * needs to be tested!
1343 * lookup a disk partition object by its index number.
1345 * @param[in] id partition index number
1346 * @param[in] abortp see abortp usage note below
1348 * @return disk partition object
1349 * @retval NULL no such disk partition
1351 * @note when abortp is non-zero, lookups which would return
1352 * NULL will result in an assertion failure
1354 * @pre VOL_LOCK must be held
1356 * @internal volume package internal use only
1359 struct DiskPartition64 *
1360 VGetPartitionById_r(afs_int32 id, int abortp)
1362 struct DiskPartition64 *dp = NULL;
1364 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1365 dp = DiskPartitionTable[id];
1369 osi_Assert(dp != NULL);
1375 * lookup a disk partition object by its index number.
1377 * @param[in] id partition index number
1378 * @param[in] abortp see abortp usage note below
1380 * @return disk partition object
1381 * @retval NULL no such disk partition
1383 * @note when abortp is non-zero, lookups which would return
1384 * NULL will result in an assertion failure
1387 struct DiskPartition64 *
1388 VGetPartitionById(afs_int32 id, int abortp)
1390 struct DiskPartition64 * dp;
1393 dp = VGetPartitionById_r(id, abortp);
1399 static struct DiskPartition64 *
1400 VLookupPartition_r(char * path)
1402 afs_int32 id = volutil_GetPartitionID(path);
1404 if (id < 0 || id > VOLMAXPARTS)
1407 return DiskPartitionTable[id];
1411 AddPartitionToTable_r(struct DiskPartition64 *dp)
1413 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1414 DiskPartitionTable[dp->index] = dp;
1419 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1421 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1422 DiskPartitionTable[dp->index] = NULL;
1425 #endif /* AFS_DEMAND_ATTACH_FS */