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>
144 #define afs_stat stat64
145 #define afs_open open64
146 #define afs_fopen fopen64
148 #if AFS_HAVE_STATVFS64
149 # define afs_statvfs statvfs64
151 # if AFS_HAVE_STATFS64
152 # define afs_statfs statfs64
154 # if AFS_HAVE_STATVFS
155 # define afs_statvfs statvfs
157 # define afs_statfs statfs
158 # endif /* !AFS_HAVE_STATVFS */
159 # endif /* !AFS_HAVE_STATFS64 */
160 #endif /* !AFS_HAVE_STATVFS64 */
161 #endif /* !AFS_NT40_ENV */
163 #else /* !O_LARGEFILE */
165 #define afs_stat stat
166 #define afs_open open
167 #define afs_fopen fopen
170 #define afs_statvfs statvfs
171 #else /* !AFS_HAVE_STATVFS */
172 #define afs_statfs statfs
173 #endif /* !AFS_HAVE_STATVFS */
174 #endif /* !AFS_NT40_ENV */
176 #endif /* !O_LARGEFILE */
178 int aixlow_water = 8; /* default 8% */
179 struct DiskPartition64 *DiskPartitionList;
181 #ifdef AFS_DEMAND_ATTACH_FS
182 /* file to lock to conceptually "lock" the vol headers on a partition */
183 #define AFS_PARTLOCK_FILE ".volheaders.lock"
184 #define AFS_VOLUMELOCK_FILE ".volume.lock"
186 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
188 static struct DiskPartition64 * VLookupPartition_r(char * path);
189 static void AddPartitionToTable_r(struct DiskPartition64 *);
190 #endif /* AFS_DEMAND_ATTACH_FS */
192 #ifdef AFS_SGI_XFS_IOPS_ENV
193 /* Verify that the on disk XFS inodes on the partition are large enough to
194 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
195 * too small to fit in the inode. Returns 0 if the attribute does fit in
198 #include <afs/xfsattrs.h>
200 VerifyXFSInodeSize(char *part, char *fstype)
203 int length = SIZEOF_XFS_ATTR_T;
208 if (strcmp("xfs", fstype))
211 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
212 if (((fd = open(part, O_RDONLY, 0)) != -1)
213 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
215 if (fsx.fsx_nextents) {
216 Log("Partition %s: XFS inodes too small, exiting.\n", part);
217 Log("Run xfs_size_check utility and remake partitions.\n");
224 (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
228 #endif /* AFS_SGI_XFS_IOPS_ENV */
231 VInitPartitionPackage(void)
233 #ifdef AFS_DEMAND_ATTACH_ENV
234 memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
235 #endif /* AFS_DEMAND_ATTACH_ENV */
240 VInitPartition_r(char *path, char *devname, Device dev)
242 struct DiskPartition64 *dp, *op;
244 dp = (struct DiskPartition64 *)malloc(sizeof(struct DiskPartition64));
245 /* Add it to the end, to preserve order when we print statistics */
246 for (op = DiskPartitionList; op; op = op->next) {
253 DiskPartitionList = dp;
255 dp->name = (char *)malloc(strlen(path) + 1);
256 strncpy(dp->name, path, strlen(path) + 1);
257 dp->index = volutil_GetPartitionID(path);
258 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
259 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
260 dp->devName = (char *)malloc(2 * strlen(path) + 6);
261 strcpy(dp->devName, path);
262 strcat(dp->devName, "/");
263 strcat(dp->devName, "Lock");
264 mkdir(dp->devName, 0700);
265 strcat(dp->devName, path);
266 close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
267 dp->device = dp->index;
269 dp->devName = (char *)malloc(strlen(devname) + 1);
270 strncpy(dp->devName, devname, strlen(devname) + 1);
273 dp->lock_fd = INVALID_FD;
275 dp->f_files = 1; /* just a default value */
276 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
277 if (programType == fileServer)
278 (void)namei_ViceREADME(VPartitionPath(dp));
280 VSetPartitionDiskUsage_r(dp);
281 #ifdef AFS_DEMAND_ATTACH_FS
282 AddPartitionToTable_r(dp);
283 queue_Init(&dp->vol_list.head);
284 CV_INIT(&dp->vol_list.cv, "vol list", CV_DEFAULT, 0);
285 dp->vol_list.len = 0;
286 dp->vol_list.busy = 0;
288 char lockpath[MAXPATHLEN+1];
289 afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
290 lockpath[MAXPATHLEN] = '\0';
291 VLockFileInit(&dp->headerLockFile, lockpath);
293 afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
294 lockpath[MAXPATHLEN] = '\0';
295 VLockFileInit(&dp->volLockFile, lockpath);
297 VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
298 #endif /* AFS_DEMAND_ATTACH_FS */
302 VInitPartition(char *path, char *devname, Device dev)
305 VInitPartition_r(path, devname, dev);
310 /* VAttachPartitions() finds the vice partitions on this server. Calls
311 * VCheckPartition() to do some basic checks on the partition. If the partition
312 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
314 * Returns the number of errors returned by VCheckPartition. An error in
315 * VCheckPartition means that partition is a valid vice partition but the
316 * fileserver should not start because of the error found on that partition.
319 * No specific user space file system checks, since we don't know what
320 * is being used for vice partitions.
322 * Use partition name as devname.
325 VCheckPartition(char *part, char *devname)
327 struct afs_stat status;
328 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
329 char AFSIDatPath[MAXPATHLEN];
332 /* Only keep track of "/vicepx" partitions since it can get hairy
333 * when NFS mounts are involved.. */
334 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
337 if (afs_stat(part, &status) < 0) {
338 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
341 #ifndef AFS_AIX32_ENV
342 if (programType == fileServer) {
343 char salvpath[MAXPATHLEN];
344 strcpy(salvpath, part);
345 strcat(salvpath, "/FORCESALVAGE");
346 if (afs_stat(salvpath, &status) == 0) {
347 Log("VInitVnodes: Found %s; aborting\n", salvpath);
353 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
354 strcpy(AFSIDatPath, part);
355 strcat(AFSIDatPath, "/AFSIDat");
357 if (afs_stat(AFSIDatPath, &status) < 0) {
361 dirp = opendir(part);
363 while ((dp = readdir(dirp))) {
364 if (dp->d_name[0] == 'V') {
365 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);
372 #else /* AFS_NAMEI_ENV */
373 if (afs_stat(AFSIDatPath, &status) == 0) {
374 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
378 #ifdef AFS_SGI_XFS_IOPS_ENV
379 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
382 #endif /* AFS_NAMEI_ENV */
383 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
385 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
386 if (status.st_ino != ROOTINO) {
387 Log("%s is not a mounted file system; ignored.\n", part);
392 VInitPartition(part, devname, status.st_dev);
397 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
398 * attached (return value 1), or only attached when it is a separately
399 * mounted partition (return value 0). For non-NAMEI environments, it
402 * *awouldattach will be set to 1 if the given path at least looks like a vice
403 * partition (that is, if we return 0, the only thing preventing this partition
404 * from being attached is the existence of the AlwaysAttach file), or to 0
405 * otherwise. *awouldattach is set regardless of whether or not the partition
406 * should always be attached or not.
409 VIsAlwaysAttach(char *part, int *awouldattach)
415 #endif /* AFS_NAMEI_ENV */
422 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
429 strncpy(checkfile, part, 100);
430 strcat(checkfile, "/");
431 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
433 ret = afs_stat(checkfile, &st);
434 return (ret < 0) ? 0 : 1;
435 #else /* AFS_NAMEI_ENV */
437 #endif /* AFS_NAMEI_ENV */
440 /* VAttachPartitions2() looks for and attaches /vicepX partitions
441 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
442 * used to attach /vicepX directories which aren't on dedicated
443 * partitions, in the NAMEI fileserver.
446 VAttachPartitions2(void)
455 while ((de = readdir(dirp))) {
457 strncat(pname, de->d_name, 20);
458 pname[sizeof(pname) - 1] = '\0';
460 /* Only keep track of "/vicepx" partitions since automounter
462 if (VIsAlwaysAttach(pname, &wouldattach)) {
463 VCheckPartition(pname, "");
466 if (wouldattach && VGetPartition(pname, 0) == NULL &&
467 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
469 /* This is a /vicep* dir, and it has not been attached as a
470 * partition. This probably means that this is a /vicep* dir
471 * that is not a separate partition, so just give a notice so
472 * admins are not confused as to why their /vicep* dirs are not
475 * It is possible that the dir _is_ a separate partition and we
476 * failed to attach it earlier, making this message a bit
477 * confusing. But that should be rare, and an error message
478 * about the failure will already be logged right before this,
479 * so it should be clear enough. */
481 Log("VAttachPartitions: not attaching %s; either it is not a "
482 "separate partition, or it failed to attach (create the "
483 "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
489 #endif /* AFS_NAMEI_ENV */
491 #endif /* AFS_NT40_ENV */
495 VAttachPartitions(void)
501 if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
502 Log("Can't open %s\n", MNTTAB);
506 while (!getmntent(mntfile, &mnt)) {
507 /* Ignore non ufs or non read/write partitions */
508 /* but allow zfs too if we're in the NAMEI environment */
511 (((strcmp(mnt.mnt_fstype, "ufs") &&
512 strcmp(mnt.mnt_fstype, "zfs"))))
514 (strcmp(mnt.mnt_fstype, "ufs") != 0)
516 || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
519 /* If we're going to always attach this partition, do it later. */
520 if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
523 #ifndef AFS_NAMEI_ENV
524 if (hasmntopt(&mnt, "logging") != NULL) {
525 Log("This program is compiled without AFS_NAMEI_ENV, and "
526 "partition %s is mounted with the 'logging' option. "
527 "Using the inode fileserver backend with 'logging' UFS "
528 "partitions causes volume corruption, so please either "
529 "mount the partition without logging, or use the namei "
530 "fileserver backend. Aborting...\n", mnt.mnt_mountp);
533 #endif /* !AFS_NAMEI_ENV */
535 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0)
539 (void)fclose(mntfile);
541 /* Process the always-attach partitions, if any. */
542 VAttachPartitions2();
547 #endif /* AFS_SUN5_ENV */
548 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
550 VAttachPartitions(void)
554 struct mntent *mntent;
556 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
557 Log("Problems in getting mount entries(setmntent)\n");
560 while (mntent = getmntent(mfd)) {
561 if (!hasmntopt(mntent, MNTOPT_RW))
564 /* If we're going to always attach this partition, do it later. */
565 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
568 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
574 /* Process the always-attach partitions, if any. */
575 VAttachPartitions2();
582 * (This function was grabbed from df.c)
585 getmount(struct vmount **vmountpp)
591 /* set initial size of mntctl buffer to a MAGIC NUMBER */
594 /* try the operation until ok or a fatal error */
596 if ((vm = (struct vmount *)malloc(size)) == NULL) {
597 /* failed getting memory for mount status buf */
598 perror("FATAL ERROR: get_stat malloc failed\n");
603 * perform the QUERY mntctl - if it returns > 0, that is the
604 * number of vmount structures in the buffer. If it returns
605 * -1, an error occured. If it returned 0, then look in
606 * first word of buffer for needed size.
608 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
609 /* OK, got it, now return */
613 } else if (nmounts == 0) {
614 /* the buffer wasn't big enough .... */
615 /* .... get required buffer size */
620 /* some other kind of error occurred */
628 VAttachPartitions(void)
632 struct vmount *vmountp;
634 if ((nmounts = getmount(&vmountp)) <= 0) {
635 Log("Problems in getting # of mount entries(getmount)\n");
640 (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
641 char *part = vmt2dataptr(vmountp, VMT_STUB);
643 if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
644 continue; /* Ignore any "special" partitions */
647 #ifndef AFS_NAMEI_ENV
649 struct superblock fs;
650 /* The Log statements are non-sequiters in the SalvageLog and don't
651 * even appear in the VolserLog, so restrict them to the FileLog.
653 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
654 if (programType == fileServer)
655 Log("Can't read superblock for %s, ignoring it.\n", part);
658 if (IsBigFilesFileSystem(&fs)) {
659 if (programType == fileServer)
660 Log("%s is a big files filesystem, ignoring it.\n", part);
667 /* If we're going to always attach this partition, do it later. */
668 if (VIsAlwaysAttach(part, NULL))
671 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0)
675 /* Process the always-attach partitions, if any. */
676 VAttachPartitions2();
681 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
683 VAttachPartitions(void)
688 if (setfsent() < 0) {
689 Log("Error listing filesystems.\n");
693 while ((fsent = getfsent())) {
694 if (strcmp(fsent->fs_type, "rw") != 0)
697 /* If we're going to always attach this partition, do it later. */
698 if (VIsAlwaysAttach(fsent->fs_file, NULL))
701 if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0)
706 /* Process the always-attach partitions, if any. */
707 VAttachPartitions2();
715 #include <sys/stat.h>
718 * validate names in vptab.
726 VValidVPTEntry(struct vptab *vpe)
728 int len = strlen(vpe->vp_name);
731 if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
733 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
736 for (i = VICE_PREFIX_SIZE; i < len; i++) {
737 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
738 Log("Invalid partition name %s in registry, ignoring it.\n",
743 if (len == VICE_PREFIX_SIZE + 2) {
744 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
745 (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
747 Log("Invalid partition name %s in registry, ignoring it.\n",
753 len = strlen(vpe->vp_dev);
754 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
755 || vpe->vp_dev[0] > 'Z') {
756 Log("Invalid device name %s in registry, ignoring it.\n",
765 VCheckPartition(char *partName)
772 /* partName is presumed to be of the form "X:" */
773 (void)sprintf(volRoot, "%c:\\", *partName);
775 if (!GetVolumeInformation(volRoot, /* volume root directory */
776 NULL, /* volume name buffer */
777 0, /* volume name size */
778 NULL, /* volume serial number */
779 &dwDummy, /* max component length */
780 &dwDummy, /* file system flags */
781 volFsType, /* file system name */
782 sizeof(volFsType))) {
783 err = GetLastError();
784 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
788 if (strcmp(volFsType, "NTFS")) {
789 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
798 VAttachPartitions(void)
800 struct DiskPartition64 *partP, *prevP, *nextP;
801 struct vpt_iter iter;
804 if (vpt_Start(&iter) < 0) {
805 Log("No partitions to attach.\n");
809 while (0 == vpt_NextEntry(&iter, &entry)) {
810 if (!VValidVPTEntry(&entry)) {
814 /* This test for duplicates relies on the fact that the method
815 * of storing the partition names in the NT registry means the same
816 * partition name will never appear twice in the list.
818 for (partP = DiskPartitionList; partP; partP = partP->next) {
819 if (*partP->devName == *entry.vp_dev) {
820 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
821 partP->flags = PART_DUPLICATE;
822 break; /* Only one entry will ever be in this list. */
826 continue; /* found a duplicate */
828 if (VCheckPartition(entry.vp_dev) < 0)
830 /* This test allows for manually inserting the FORCESALVAGE flag
831 * and thereby invoking the salvager. scandisk obviously won't be
834 if (programType == fileServer) {
835 struct afs_stat status;
836 char salvpath[MAXPATHLEN];
837 strcpy(salvpath, entry.vp_dev);
838 strcat(salvpath, "\\FORCESALVAGE");
839 if (afs_stat(salvpath, &status) == 0) {
840 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
844 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
848 /* Run through partition list and clear out the dupes. */
849 prevP = nextP = NULL;
850 for (partP = DiskPartitionList; partP; partP = nextP) {
852 if (partP->flags == PART_DUPLICATE) {
854 prevP->next = partP->next;
856 DiskPartitionList = partP->next;
866 #ifdef AFS_LINUX22_ENV
868 VAttachPartitions(void)
872 struct mntent *mntent;
874 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
875 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
876 Log("Problems in getting mount entries(setmntent)\n");
880 while ((mntent = getmntent(mfd))) {
881 /* If we're going to always attach this partition, do it later. */
882 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
885 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
890 /* Process the always-attach partitions, if any. */
891 VAttachPartitions2();
895 #endif /* AFS_LINUX22_ENV */
897 /* This routine is to be called whenever the actual name of the partition
898 * is required. The canonical name is still in part->name.
901 VPartitionPath(struct DiskPartition64 *part)
904 return part->devName;
910 /* get partition structure, abortp tells us if we should abort on failure */
911 struct DiskPartition64 *
912 VGetPartition_r(char *name, int abortp)
914 struct DiskPartition64 *dp;
915 #ifdef AFS_DEMAND_ATTACH_FS
916 dp = VLookupPartition_r(name);
917 #else /* AFS_DEMAND_ATTACH_FS */
918 for (dp = DiskPartitionList; dp; dp = dp->next) {
919 if (strcmp(dp->name, name) == 0)
922 #endif /* AFS_DEMAND_ATTACH_FS */
924 osi_Assert(dp != NULL);
928 struct DiskPartition64 *
929 VGetPartition(char *name, int abortp)
931 struct DiskPartition64 *retVal;
933 retVal = VGetPartition_r(name, abortp);
940 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
942 ULARGE_INTEGER free_user, total, free_total;
943 int ufree, tot, tfree;
945 if (!GetDiskFreeSpaceEx
946 (VPartitionPath(dp), &free_user, &total, &free_total)) {
947 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
952 /* Convert to 1K units. */
953 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
954 tot = (int)Int64ShraMod32(total.QuadPart, 10);
955 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
957 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
958 dp->totalUsable = tot;
964 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
967 afs_int64 totalblks, free, used, availblks;
970 struct afs_statvfs statbuf;
972 struct afs_statfs statbuf;
975 if (dp->flags & PART_DONTUPDATE)
977 /* Note: we don't bother syncing because it's only an estimate, update
978 * is syncing every 30 seconds anyway, we only have to keep the disk
979 * approximately 10% from full--you just can't get the stuff in from
980 * the net fast enough to worry */
982 code = afs_statvfs(dp->name, &statbuf);
984 code = afs_statfs(dp->name, &statbuf);
987 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
991 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
992 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
995 totalblks = statbuf.f_blocks;
996 free = statbuf.f_bfree;
997 reserved = free - statbuf.f_bavail;
999 bsize = statbuf.f_frsize;
1001 bsize = statbuf.f_bsize;
1003 availblks = totalblks - reserved;
1004 dp->f_files = statbuf.f_files; /* max # of files in partition */
1006 /* Now free and totalblks are in fragment units, but we want them in
1009 if (bsize >= 1024) {
1010 free *= (bsize / 1024);
1011 totalblks *= (bsize / 1024);
1012 availblks *= (bsize / 1024);
1013 reserved *= (bsize / 1024);
1015 free /= (1024 / bsize);
1016 totalblks /= (1024 / bsize);
1017 availblks /= (1024 / bsize);
1018 reserved /= (1024 / bsize);
1020 /* now compute remaining figures */
1021 used = totalblks - free;
1023 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
1024 dp->totalUsable = availblks;
1025 dp->free = availblks - used; /* this is exactly f_bavail */
1027 #endif /* AFS_NT40_ENV */
1030 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
1033 VSetPartitionDiskUsage_r(dp);
1038 VResetDiskUsage_r(void)
1040 struct DiskPartition64 *dp;
1041 for (dp = DiskPartitionList; dp; dp = dp->next) {
1042 VSetPartitionDiskUsage_r(dp);
1043 #ifndef AFS_PTHREAD_ENV
1045 #endif /* !AFS_PTHREAD_ENV */
1050 VResetDiskUsage(void)
1053 VResetDiskUsage_r();
1058 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1059 afs_sfsize_t checkBlocks)
1062 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1063 * for less than BlocksSpare would skip the error-checking path, and we
1064 * could grow existing files forever, not just for another BlocksSpare
1067 #ifdef AFS_AIX32_ENV
1068 afs_int32 rem, minavail;
1070 if ((rem = vp->partition->free - checkBlocks) < (minavail =
1076 if (vp->partition->free - checkBlocks < 0)
1079 else if (V_maxquota(vp)
1080 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1083 vp->partition->free -= blocks;
1084 V_diskused(vp) += blocks;
1088 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1089 afs_sfsize_t checkBlocks)
1092 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1097 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1100 #ifdef AFS_AIX32_ENV
1101 afs_int32 rem, minavail;
1103 if ((rem = vp->partition->free - blocks) < (minavail =
1106 aixlow_water) / 100))
1108 if (vp->partition->free - blocks < 0)
1112 vp->partition->free -= blocks;
1117 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1121 retVal = VDiskUsage_r(vp, blocks);
1127 VPrintDiskStats_r(void)
1129 struct DiskPartition64 *dp;
1130 for (dp = DiskPartitionList; dp; dp = dp->next) {
1132 Log("Partition %s: %"AFS_INT64_FMT
1133 " available 1K blocks (minfree=%"AFS_INT64_FMT"), "
1134 "overallocated by %"AFS_INT64_FMT" blocks\n", dp->name,
1135 dp->totalUsable, dp->minFree, -dp->free);
1137 Log("Partition %s: %"AFS_INT64_FMT
1138 " available 1K blocks (minfree=%"AFS_INT64_FMT"), "
1139 "%"AFS_INT64_FMT" free blocks\n", dp->name,
1140 dp->totalUsable, dp->minFree, dp->free);
1146 VPrintDiskStats(void)
1149 VPrintDiskStats_r();
1154 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1155 #define LOCKFILE "LOCKFILE"
1157 VLockPartition_r(char *name)
1159 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1164 if (dp->lock_fd == INVALID_FD) {
1167 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1169 (FD_t)CreateFile(path, GENERIC_WRITE,
1170 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1171 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1172 osi_Assert(dp->lock_fd != INVALID_FD);
1174 memset(&lap, 0, sizeof(lap));
1175 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1182 VUnlockPartition_r(char *name)
1184 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1188 return; /* no partition, will fail later */
1189 memset(&lap, 0, sizeof(lap));
1191 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1192 CloseHandle((HANDLE) dp->lock_fd);
1193 dp->lock_fd = INVALID_FD;
1195 #else /* AFS_NT40_ENV */
1197 #if defined(AFS_HPUX_ENV)
1198 #define BITS_PER_CHAR (8)
1199 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1201 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1202 #endif /* defined(AFS_HPUX_ENV) */
1205 VLockPartition_r(char *name)
1207 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1208 char *partitionName;
1210 struct timeval pausing;
1211 #if defined(AFS_HPUX_ENV)
1213 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1214 unsigned int *globalMask;
1215 int globalMaskIndex;
1216 #endif /* defined(AFS_HPUX_ENV) */
1217 #if defined(AFS_DARWIN_ENV)
1218 char lockfile[MAXPATHLEN];
1219 #endif /* defined(AFS_DARWIN_ENV) */
1220 #ifdef AFS_NAMEI_ENV
1221 #ifdef AFS_AIX42_ENV
1222 char LockFileName[MAXPATHLEN + 1];
1224 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1225 partitionName = (char *)&LockFileName;
1230 return; /* no partition, will fail later */
1231 if (dp->lock_fd != INVALID_FD)
1234 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1235 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1236 partitionName = dp->devName;
1239 #elif defined(AFS_DARWIN_ENV)
1240 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1241 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1242 code = O_RDONLY | O_CREAT;
1244 partitionName = dp->name;
1248 for (retries = 25; retries; retries--) {
1250 dp->lock_fd = afs_open(partitionName, code, 0644);
1252 dp->lock_fd = afs_open(partitionName, code);
1254 if (dp->lock_fd != INVALID_FD)
1256 if (errno == ENOENT)
1259 pausing.tv_usec = 500000;
1260 select(0, NULL, NULL, NULL, &pausing);
1262 osi_Assert(retries != 0);
1264 #if defined (AFS_HPUX_ENV)
1266 osi_Assert(getprivgrp(privGrpList) == 0);
1269 * In general, it will difficult and time-consuming ,if not impossible,
1270 * to try to find the privgroup to which this process belongs that has the
1271 * smallest membership, to minimise the security hole. So, we use the privgrp
1272 * to which everybody belongs.
1274 /* first, we have to find the global mask */
1275 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1276 globalMaskIndex++) {
1277 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1279 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1284 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1285 /* allow everybody to set a lock on a read-only file descriptor */
1286 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1287 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1290 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1292 /* remove the privilege granted to everybody to lock a read-only fd */
1293 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1294 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1297 /* in this case, we should be able to do this with impunity, anyway */
1298 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1301 osi_Assert(lockfRtn != -1);
1303 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1304 osi_Assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1306 osi_Assert(flock(dp->lock_fd, LOCK_EX) == 0);
1307 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1312 VUnlockPartition_r(char *name)
1314 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1316 return; /* no partition, will fail later */
1318 dp->lock_fd = INVALID_FD;
1321 #endif /* AFS_NT40_ENV */
1324 VLockPartition(char *name)
1327 VLockPartition_r(name);
1332 VUnlockPartition(char *name)
1335 VUnlockPartition_r(name);
1339 #ifdef AFS_DEMAND_ATTACH_FS
1341 /* new-style partition locks; these are only to have some mutual exclusion
1342 * between the VGC scanner and volume utilies creating/altering vol headers
1346 * lock a partition's vol headers.
1348 * @param[in] dp the partition to lock
1349 * @param[in] locktype READ_LOCK or WRITE_LOCK
1351 * @return operation status
1355 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1359 /* block on acquiring the lock */
1362 code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1364 Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1365 VPartitionPath(dp));
1371 * unlock a partition's vol headers.
1373 * @param[in] dp the partition to unlock
1374 * @param[in] locktype READ_LOCK or WRITE_LOCK
1377 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1379 VReleaseDiskLock(&dp->headerLock, locktype);
1382 /* XXX not sure this will work on AFS_NT40_ENV
1383 * needs to be tested!
1387 * lookup a disk partition object by its index number.
1389 * @param[in] id partition index number
1390 * @param[in] abortp see abortp usage note below
1392 * @return disk partition object
1393 * @retval NULL no such disk partition
1395 * @note when abortp is non-zero, lookups which would return
1396 * NULL will result in an assertion failure
1398 * @pre VOL_LOCK must be held
1400 * @internal volume package internal use only
1403 struct DiskPartition64 *
1404 VGetPartitionById_r(afs_int32 id, int abortp)
1406 struct DiskPartition64 *dp = NULL;
1408 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1409 dp = DiskPartitionTable[id];
1413 osi_Assert(dp != NULL);
1419 * lookup a disk partition object by its index number.
1421 * @param[in] id partition index number
1422 * @param[in] abortp see abortp usage note below
1424 * @return disk partition object
1425 * @retval NULL no such disk partition
1427 * @note when abortp is non-zero, lookups which would return
1428 * NULL will result in an assertion failure
1431 struct DiskPartition64 *
1432 VGetPartitionById(afs_int32 id, int abortp)
1434 struct DiskPartition64 * dp;
1437 dp = VGetPartitionById_r(id, abortp);
1443 static struct DiskPartition64 *
1444 VLookupPartition_r(char * path)
1446 afs_int32 id = volutil_GetPartitionID(path);
1448 if (id < 0 || id > VOLMAXPARTS)
1451 return DiskPartitionTable[id];
1455 AddPartitionToTable_r(struct DiskPartition64 *dp)
1457 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1458 DiskPartitionTable[dp->index] = dp;
1463 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1465 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1466 DiskPartitionTable[dp->index] = NULL;
1469 #endif /* AFS_DEMAND_ATTACH_FS */