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>
123 #include "namei_ops.h"
125 #endif /* AFS_NT40_ENV */
126 #endif /* AFS_NAMEI_ENV */
129 #include "partition.h"
130 #include <afs/afs_assert.h>
132 #if defined(AFS_HPUX_ENV)
133 #include <sys/types.h>
134 #include <sys/privgrp.h>
135 #endif /* defined(AFS_HPUX_ENV) */
138 #include <jfs/filsys.h>
143 #define afs_stat stat64
144 #define afs_open open64
145 #define afs_fopen fopen64
147 #if AFS_HAVE_STATVFS64
148 # define afs_statvfs statvfs64
150 # if AFS_HAVE_STATFS64
151 # define afs_statfs statfs64
153 # if AFS_HAVE_STATVFS
154 # define afs_statvfs statvfs
156 # define afs_statfs statfs
157 # endif /* !AFS_HAVE_STATVFS */
158 # endif /* !AFS_HAVE_STATFS64 */
159 #endif /* !AFS_HAVE_STATVFS64 */
160 #endif /* !AFS_NT40_ENV */
162 #else /* !O_LARGEFILE */
164 #define afs_stat stat
165 #define afs_open open
166 #define afs_fopen fopen
169 #define afs_statvfs statvfs
170 #else /* !AFS_HAVE_STATVFS */
171 #define afs_statfs statfs
172 #endif /* !AFS_HAVE_STATVFS */
173 #endif /* !AFS_NT40_ENV */
175 #endif /* !O_LARGEFILE */
177 int aixlow_water = 8; /* default 8% */
178 struct DiskPartition64 *DiskPartitionList;
180 #ifdef AFS_DEMAND_ATTACH_FS
181 /* file to lock to conceptually "lock" the vol headers on a partition */
182 #define AFS_PARTLOCK_FILE ".volheaders.lock"
183 #define AFS_VOLUMELOCK_FILE ".volume.lock"
185 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
187 static struct DiskPartition64 * VLookupPartition_r(char * path);
188 static void AddPartitionToTable_r(struct DiskPartition64 *);
189 #endif /* AFS_DEMAND_ATTACH_FS */
191 #ifdef AFS_SGI_XFS_IOPS_ENV
192 /* Verify that the on disk XFS inodes on the partition are large enough to
193 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
194 * too small to fit in the inode. Returns 0 if the attribute does fit in
197 #include <afs/xfsattrs.h>
199 VerifyXFSInodeSize(char *part, char *fstype)
202 int length = SIZEOF_XFS_ATTR_T;
207 if (strcmp("xfs", fstype))
210 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
211 if (((fd = open(part, O_RDONLY, 0)) != -1)
212 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
214 if (fsx.fsx_nextents) {
215 Log("Partition %s: XFS inodes too small, exiting.\n", part);
216 Log("Run xfs_size_check utility and remake partitions.\n");
223 (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
227 #endif /* AFS_SGI_XFS_IOPS_ENV */
230 VInitPartitionPackage(void)
232 #ifdef AFS_DEMAND_ATTACH_ENV
233 memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
234 #endif /* AFS_DEMAND_ATTACH_ENV */
239 VInitPartition_r(char *path, char *devname, Device dev)
241 struct DiskPartition64 *dp, *op;
243 dp = (struct DiskPartition64 *)malloc(sizeof(struct DiskPartition64));
244 /* Add it to the end, to preserve order when we print statistics */
245 for (op = DiskPartitionList; op; op = op->next) {
252 DiskPartitionList = dp;
254 dp->name = (char *)malloc(strlen(path) + 1);
255 strncpy(dp->name, path, strlen(path) + 1);
256 dp->index = volutil_GetPartitionID(path);
257 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
258 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
259 dp->devName = (char *)malloc(2 * strlen(path) + 6);
260 strcpy(dp->devName, path);
261 strcat(dp->devName, "/");
262 strcat(dp->devName, "Lock");
263 mkdir(dp->devName, 0700);
264 strcat(dp->devName, path);
265 close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
266 dp->device = dp->index;
268 dp->devName = (char *)malloc(strlen(devname) + 1);
269 strncpy(dp->devName, devname, strlen(devname) + 1);
272 dp->lock_fd = INVALID_FD;
274 dp->f_files = 1; /* just a default value */
275 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
276 if (programType == fileServer)
277 (void)namei_ViceREADME(VPartitionPath(dp));
279 VSetPartitionDiskUsage_r(dp);
280 #ifdef AFS_DEMAND_ATTACH_FS
281 AddPartitionToTable_r(dp);
282 queue_Init(&dp->vol_list.head);
283 CV_INIT(&dp->vol_list.cv, "vol list", CV_DEFAULT, 0);
284 dp->vol_list.len = 0;
285 dp->vol_list.busy = 0;
287 char lockpath[MAXPATHLEN+1];
288 afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
289 lockpath[MAXPATHLEN] = '\0';
290 VLockFileInit(&dp->headerLockFile, lockpath);
292 afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
293 lockpath[MAXPATHLEN] = '\0';
294 VLockFileInit(&dp->volLockFile, lockpath);
296 VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
297 #endif /* AFS_DEMAND_ATTACH_FS */
301 VInitPartition(char *path, char *devname, Device dev)
304 VInitPartition_r(path, devname, dev);
309 /* VAttachPartitions() finds the vice partitions on this server. Calls
310 * VCheckPartition() to do some basic checks on the partition. If the partition
311 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
313 * Returns the number of errors returned by VCheckPartition. An error in
314 * VCheckPartition means that partition is a valid vice partition but the
315 * fileserver should not start because of the error found on that partition.
318 * No specific user space file system checks, since we don't know what
319 * is being used for vice partitions.
321 * Use partition name as devname.
324 VCheckPartition(char *part, char *devname)
326 struct afs_stat status;
327 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
328 char AFSIDatPath[MAXPATHLEN];
331 /* Only keep track of "/vicepx" partitions since it can get hairy
332 * when NFS mounts are involved.. */
333 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
336 if (afs_stat(part, &status) < 0) {
337 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
340 #ifndef AFS_AIX32_ENV
341 if (programType == fileServer) {
342 char salvpath[MAXPATHLEN];
343 strcpy(salvpath, part);
344 strcat(salvpath, "/FORCESALVAGE");
345 if (afs_stat(salvpath, &status) == 0) {
346 Log("VInitVnodes: Found %s; aborting\n", salvpath);
352 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
353 strcpy(AFSIDatPath, part);
354 strcat(AFSIDatPath, "/AFSIDat");
356 if (afs_stat(AFSIDatPath, &status) < 0) {
360 dirp = opendir(part);
362 while ((dp = readdir(dirp))) {
363 if (dp->d_name[0] == 'V') {
364 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);
371 #else /* AFS_NAMEI_ENV */
372 if (afs_stat(AFSIDatPath, &status) == 0) {
373 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
377 #ifdef AFS_SGI_XFS_IOPS_ENV
378 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
381 #endif /* AFS_NAMEI_ENV */
382 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
384 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
385 if (status.st_ino != ROOTINO) {
386 Log("%s is not a mounted file system; ignored.\n", part);
391 VInitPartition(part, devname, status.st_dev);
396 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
397 * attached (return value 1), or only attached when it is a separately
398 * mounted partition (return value 0). For non-NAMEI environments, it
401 * *awouldattach will be set to 1 if the given path at least looks like a vice
402 * partition (that is, if we return 0, the only thing preventing this partition
403 * from being attached is the existence of the AlwaysAttach file), or to 0
404 * otherwise. *awouldattach is set regardless of whether or not the partition
405 * should always be attached or not.
408 VIsAlwaysAttach(char *part, int *awouldattach)
414 #endif /* AFS_NAMEI_ENV */
421 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
428 strncpy(checkfile, part, 100);
429 strcat(checkfile, "/");
430 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
432 ret = afs_stat(checkfile, &st);
433 return (ret < 0) ? 0 : 1;
434 #else /* AFS_NAMEI_ENV */
436 #endif /* AFS_NAMEI_ENV */
439 /* VAttachPartitions2() looks for and attaches /vicepX partitions
440 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
441 * used to attach /vicepX directories which aren't on dedicated
442 * partitions, in the NAMEI fileserver.
445 VAttachPartitions2(void)
454 while ((de = readdir(dirp))) {
456 strncat(pname, de->d_name, 20);
457 pname[sizeof(pname) - 1] = '\0';
459 /* Only keep track of "/vicepx" partitions since automounter
461 if (VIsAlwaysAttach(pname, &wouldattach)) {
462 VCheckPartition(pname, "");
465 if (wouldattach && VGetPartition(pname, 0) == NULL &&
466 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
468 /* This is a /vicep* dir, and it has not been attached as a
469 * partition. This probably means that this is a /vicep* dir
470 * that is not a separate partition, so just give a notice so
471 * admins are not confused as to why their /vicep* dirs are not
474 * It is possible that the dir _is_ a separate partition and we
475 * failed to attach it earlier, making this message a bit
476 * confusing. But that should be rare, and an error message
477 * about the failure will already be logged right before this,
478 * so it should be clear enough. */
480 Log("VAttachPartitions: not attaching %s; either it is not a "
481 "separate partition, or it failed to attach (create the "
482 "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
488 #endif /* AFS_NAMEI_ENV */
490 #endif /* AFS_NT40_ENV */
494 VAttachPartitions(void)
500 if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
501 Log("Can't open %s\n", MNTTAB);
505 while (!getmntent(mntfile, &mnt)) {
506 /* Ignore non ufs or non read/write partitions */
507 /* but allow zfs too if we're in the NAMEI environment */
510 (((strcmp(mnt.mnt_fstype, "ufs") &&
511 strcmp(mnt.mnt_fstype, "zfs"))))
513 (strcmp(mnt.mnt_fstype, "ufs") != 0)
515 || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
518 /* If we're going to always attach this partition, do it later. */
519 if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
522 #ifndef AFS_NAMEI_ENV
523 if (hasmntopt(&mnt, "logging") != NULL) {
524 Log("This program is compiled without AFS_NAMEI_ENV, and "
525 "partition %s is mounted with the 'logging' option. "
526 "Using the inode fileserver backend with 'logging' UFS "
527 "partitions causes volume corruption, so please either "
528 "mount the partition without logging, or use the namei "
529 "fileserver backend. Aborting...\n", mnt.mnt_mountp);
532 #endif /* !AFS_NAMEI_ENV */
534 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0)
538 (void)fclose(mntfile);
540 /* Process the always-attach partitions, if any. */
541 VAttachPartitions2();
546 #endif /* AFS_SUN5_ENV */
547 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
549 VAttachPartitions(void)
553 struct mntent *mntent;
555 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
556 Log("Problems in getting mount entries(setmntent)\n");
559 while (mntent = getmntent(mfd)) {
560 if (!hasmntopt(mntent, MNTOPT_RW))
563 /* If we're going to always attach this partition, do it later. */
564 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
567 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
573 /* Process the always-attach partitions, if any. */
574 VAttachPartitions2();
581 * (This function was grabbed from df.c)
584 getmount(struct vmount **vmountpp)
590 /* set initial size of mntctl buffer to a MAGIC NUMBER */
593 /* try the operation until ok or a fatal error */
595 if ((vm = (struct vmount *)malloc(size)) == NULL) {
596 /* failed getting memory for mount status buf */
597 perror("FATAL ERROR: get_stat malloc failed\n");
602 * perform the QUERY mntctl - if it returns > 0, that is the
603 * number of vmount structures in the buffer. If it returns
604 * -1, an error occured. If it returned 0, then look in
605 * first word of buffer for needed size.
607 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
608 /* OK, got it, now return */
612 } else if (nmounts == 0) {
613 /* the buffer wasn't big enough .... */
614 /* .... get required buffer size */
619 /* some other kind of error occurred */
627 VAttachPartitions(void)
631 struct vmount *vmountp;
633 if ((nmounts = getmount(&vmountp)) <= 0) {
634 Log("Problems in getting # of mount entries(getmount)\n");
639 (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
640 char *part = vmt2dataptr(vmountp, VMT_STUB);
642 if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
643 continue; /* Ignore any "special" partitions */
646 #ifndef AFS_NAMEI_ENV
648 struct superblock fs;
649 /* The Log statements are non-sequiters in the SalvageLog and don't
650 * even appear in the VolserLog, so restrict them to the FileLog.
652 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
653 if (programType == fileServer)
654 Log("Can't read superblock for %s, ignoring it.\n", part);
657 if (IsBigFilesFileSystem(&fs)) {
658 if (programType == fileServer)
659 Log("%s is a big files filesystem, ignoring it.\n", part);
666 /* If we're going to always attach this partition, do it later. */
667 if (VIsAlwaysAttach(part, NULL))
670 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0)
674 /* Process the always-attach partitions, if any. */
675 VAttachPartitions2();
680 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
682 VAttachPartitions(void)
687 if (setfsent() < 0) {
688 Log("Error listing filesystems.\n");
692 while ((fsent = getfsent())) {
693 if (strcmp(fsent->fs_type, "rw") != 0)
696 /* If we're going to always attach this partition, do it later. */
697 if (VIsAlwaysAttach(fsent->fs_file, NULL))
700 if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0)
705 /* Process the always-attach partitions, if any. */
706 VAttachPartitions2();
714 #include <sys/stat.h>
717 * validate names in vptab.
725 VValidVPTEntry(struct vptab *vpe)
727 int len = strlen(vpe->vp_name);
730 if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
732 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
735 for (i = VICE_PREFIX_SIZE; i < len; i++) {
736 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
737 Log("Invalid partition name %s in registry, ignoring it.\n",
742 if (len == VICE_PREFIX_SIZE + 2) {
743 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
744 (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
746 Log("Invalid partition name %s in registry, ignoring it.\n",
752 len = strlen(vpe->vp_dev);
753 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
754 || vpe->vp_dev[0] > 'Z') {
755 Log("Invalid device name %s in registry, ignoring it.\n",
764 VCheckPartition(char *partName)
771 /* partName is presumed to be of the form "X:" */
772 (void)sprintf(volRoot, "%c:\\", *partName);
774 if (!GetVolumeInformation(volRoot, /* volume root directory */
775 NULL, /* volume name buffer */
776 0, /* volume name size */
777 NULL, /* volume serial number */
778 &dwDummy, /* max component length */
779 &dwDummy, /* file system flags */
780 volFsType, /* file system name */
781 sizeof(volFsType))) {
782 err = GetLastError();
783 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
787 if (strcmp(volFsType, "NTFS")) {
788 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
797 VAttachPartitions(void)
799 struct DiskPartition64 *partP, *prevP, *nextP;
800 struct vpt_iter iter;
803 if (vpt_Start(&iter) < 0) {
804 Log("No partitions to attach.\n");
808 while (0 == vpt_NextEntry(&iter, &entry)) {
809 if (!VValidVPTEntry(&entry)) {
813 /* This test for duplicates relies on the fact that the method
814 * of storing the partition names in the NT registry means the same
815 * partition name will never appear twice in the list.
817 for (partP = DiskPartitionList; partP; partP = partP->next) {
818 if (*partP->devName == *entry.vp_dev) {
819 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
820 partP->flags = PART_DUPLICATE;
821 break; /* Only one entry will ever be in this list. */
825 continue; /* found a duplicate */
827 if (VCheckPartition(entry.vp_dev) < 0)
829 /* This test allows for manually inserting the FORCESALVAGE flag
830 * and thereby invoking the salvager. scandisk obviously won't be
833 if (programType == fileServer) {
834 struct afs_stat status;
835 char salvpath[MAXPATHLEN];
836 strcpy(salvpath, entry.vp_dev);
837 strcat(salvpath, "\\FORCESALVAGE");
838 if (afs_stat(salvpath, &status) == 0) {
839 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
843 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
847 /* Run through partition list and clear out the dupes. */
848 prevP = nextP = NULL;
849 for (partP = DiskPartitionList; partP; partP = nextP) {
851 if (partP->flags == PART_DUPLICATE) {
853 prevP->next = partP->next;
855 DiskPartitionList = partP->next;
865 #ifdef AFS_LINUX22_ENV
867 VAttachPartitions(void)
871 struct mntent *mntent;
873 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
874 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
875 Log("Problems in getting mount entries(setmntent)\n");
879 while ((mntent = getmntent(mfd))) {
880 /* If we're going to always attach this partition, do it later. */
881 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
884 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
889 /* Process the always-attach partitions, if any. */
890 VAttachPartitions2();
894 #endif /* AFS_LINUX22_ENV */
896 /* This routine is to be called whenever the actual name of the partition
897 * is required. The canonical name is still in part->name.
900 VPartitionPath(struct DiskPartition64 *part)
903 return part->devName;
909 /* get partition structure, abortp tells us if we should abort on failure */
910 struct DiskPartition64 *
911 VGetPartition_r(char *name, int abortp)
913 struct DiskPartition64 *dp;
914 #ifdef AFS_DEMAND_ATTACH_FS
915 dp = VLookupPartition_r(name);
916 #else /* AFS_DEMAND_ATTACH_FS */
917 for (dp = DiskPartitionList; dp; dp = dp->next) {
918 if (strcmp(dp->name, name) == 0)
921 #endif /* AFS_DEMAND_ATTACH_FS */
923 osi_Assert(dp != NULL);
927 struct DiskPartition64 *
928 VGetPartition(char *name, int abortp)
930 struct DiskPartition64 *retVal;
932 retVal = VGetPartition_r(name, abortp);
939 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
941 ULARGE_INTEGER free_user, total, free_total;
942 int ufree, tot, tfree;
944 if (!GetDiskFreeSpaceEx
945 (VPartitionPath(dp), &free_user, &total, &free_total)) {
946 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
951 /* Convert to 1K units. */
952 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
953 tot = (int)Int64ShraMod32(total.QuadPart, 10);
954 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
956 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
957 dp->totalUsable = tot;
963 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
966 afs_int64 totalblks, free, used, availblks;
969 struct afs_statvfs statbuf;
971 struct afs_statfs statbuf;
974 if (dp->flags & PART_DONTUPDATE)
976 /* Note: we don't bother syncing because it's only an estimate, update
977 * is syncing every 30 seconds anyway, we only have to keep the disk
978 * approximately 10% from full--you just can't get the stuff in from
979 * the net fast enough to worry */
981 code = afs_statvfs(dp->name, &statbuf);
983 code = afs_statfs(dp->name, &statbuf);
986 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
990 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
991 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
994 totalblks = statbuf.f_blocks;
995 free = statbuf.f_bfree;
996 reserved = free - statbuf.f_bavail;
998 bsize = statbuf.f_frsize;
1000 bsize = statbuf.f_bsize;
1002 availblks = totalblks - reserved;
1003 dp->f_files = statbuf.f_files; /* max # of files in partition */
1005 /* Now free and totalblks are in fragment units, but we want them in
1008 if (bsize >= 1024) {
1009 free *= (bsize / 1024);
1010 totalblks *= (bsize / 1024);
1011 availblks *= (bsize / 1024);
1012 reserved *= (bsize / 1024);
1014 free /= (1024 / bsize);
1015 totalblks /= (1024 / bsize);
1016 availblks /= (1024 / bsize);
1017 reserved /= (1024 / bsize);
1019 /* now compute remaining figures */
1020 used = totalblks - free;
1022 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
1023 dp->totalUsable = availblks;
1024 dp->free = availblks - used; /* this is exactly f_bavail */
1026 #endif /* AFS_NT40_ENV */
1029 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
1032 VSetPartitionDiskUsage_r(dp);
1037 VResetDiskUsage_r(void)
1039 struct DiskPartition64 *dp;
1040 for (dp = DiskPartitionList; dp; dp = dp->next) {
1041 VSetPartitionDiskUsage_r(dp);
1042 #ifndef AFS_PTHREAD_ENV
1044 #endif /* !AFS_PTHREAD_ENV */
1049 VResetDiskUsage(void)
1052 VResetDiskUsage_r();
1057 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1058 afs_sfsize_t checkBlocks)
1061 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1062 * for less than BlocksSpare would skip the error-checking path, and we
1063 * could grow existing files forever, not just for another BlocksSpare
1066 #ifdef AFS_AIX32_ENV
1067 afs_int32 rem, minavail;
1069 if ((rem = vp->partition->free - checkBlocks) < (minavail =
1075 if (vp->partition->free - checkBlocks < 0)
1078 else if (V_maxquota(vp)
1079 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1082 vp->partition->free -= blocks;
1083 V_diskused(vp) += blocks;
1087 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1088 afs_sfsize_t checkBlocks)
1091 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1096 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1099 #ifdef AFS_AIX32_ENV
1100 afs_int32 rem, minavail;
1102 if ((rem = vp->partition->free - blocks) < (minavail =
1105 aixlow_water) / 100))
1107 if (vp->partition->free - blocks < 0)
1111 vp->partition->free -= blocks;
1116 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1120 retVal = VDiskUsage_r(vp, blocks);
1126 VPrintDiskStats_r(void)
1128 struct DiskPartition64 *dp;
1129 for (dp = DiskPartitionList; dp; dp = dp->next) {
1131 Log("Partition %s: %"AFS_INT64_FMT
1132 " available 1K blocks (minfree=%"AFS_INT64_FMT"), "
1133 "overallocated by %"AFS_INT64_FMT" blocks\n", dp->name,
1134 dp->totalUsable, dp->minFree, -dp->free);
1136 Log("Partition %s: %"AFS_INT64_FMT
1137 " available 1K blocks (minfree=%"AFS_INT64_FMT"), "
1138 "%"AFS_INT64_FMT" free blocks\n", dp->name,
1139 dp->totalUsable, dp->minFree, dp->free);
1145 VPrintDiskStats(void)
1148 VPrintDiskStats_r();
1153 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1154 #define LOCKFILE "LOCKFILE"
1156 VLockPartition_r(char *name)
1158 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1163 if (dp->lock_fd == INVALID_FD) {
1166 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1168 (FD_t)CreateFile(path, GENERIC_WRITE,
1169 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1170 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1171 osi_Assert(dp->lock_fd != INVALID_FD);
1173 memset(&lap, 0, sizeof(lap));
1174 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1181 VUnlockPartition_r(char *name)
1183 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1187 return; /* no partition, will fail later */
1188 memset(&lap, 0, sizeof(lap));
1190 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1191 CloseHandle((HANDLE) dp->lock_fd);
1192 dp->lock_fd = INVALID_FD;
1194 #else /* AFS_NT40_ENV */
1196 #if defined(AFS_HPUX_ENV)
1197 #define BITS_PER_CHAR (8)
1198 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1200 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1201 #endif /* defined(AFS_HPUX_ENV) */
1204 VLockPartition_r(char *name)
1206 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1207 char *partitionName;
1209 struct timeval pausing;
1210 #if defined(AFS_HPUX_ENV)
1212 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1213 unsigned int *globalMask;
1214 int globalMaskIndex;
1215 #endif /* defined(AFS_HPUX_ENV) */
1216 #if defined(AFS_DARWIN_ENV)
1217 char lockfile[MAXPATHLEN];
1218 #endif /* defined(AFS_DARWIN_ENV) */
1219 #ifdef AFS_NAMEI_ENV
1220 #ifdef AFS_AIX42_ENV
1221 char LockFileName[MAXPATHLEN + 1];
1223 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1224 partitionName = (char *)&LockFileName;
1229 return; /* no partition, will fail later */
1230 if (dp->lock_fd != -1)
1233 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1234 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1235 partitionName = dp->devName;
1238 #elif defined(AFS_DARWIN_ENV)
1239 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1240 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1241 code = O_RDONLY | O_CREAT;
1243 partitionName = dp->name;
1247 for (retries = 25; retries; retries--) {
1249 dp->lock_fd = afs_open(partitionName, code, 0644);
1251 dp->lock_fd = afs_open(partitionName, code);
1253 if (dp->lock_fd != -1)
1255 if (errno == ENOENT)
1258 pausing.tv_usec = 500000;
1259 select(0, NULL, NULL, NULL, &pausing);
1261 osi_Assert(retries != 0);
1263 #if defined (AFS_HPUX_ENV)
1265 osi_Assert(getprivgrp(privGrpList) == 0);
1268 * In general, it will difficult and time-consuming ,if not impossible,
1269 * to try to find the privgroup to which this process belongs that has the
1270 * smallest membership, to minimise the security hole. So, we use the privgrp
1271 * to which everybody belongs.
1273 /* first, we have to find the global mask */
1274 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1275 globalMaskIndex++) {
1276 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1278 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1283 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1284 /* allow everybody to set a lock on a read-only file descriptor */
1285 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1286 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1289 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1291 /* remove the privilege granted to everybody to lock a read-only fd */
1292 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1293 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1296 /* in this case, we should be able to do this with impunity, anyway */
1297 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1300 osi_Assert(lockfRtn != -1);
1302 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1303 osi_Assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1305 osi_Assert(flock(dp->lock_fd, LOCK_EX) == 0);
1306 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1311 VUnlockPartition_r(char *name)
1313 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1315 return; /* no partition, will fail later */
1320 #endif /* AFS_NT40_ENV */
1323 VLockPartition(char *name)
1326 VLockPartition_r(name);
1331 VUnlockPartition(char *name)
1334 VUnlockPartition_r(name);
1338 #ifdef AFS_DEMAND_ATTACH_FS
1340 /* new-style partition locks; these are only to have some mutual exclusion
1341 * between the VGC scanner and volume utilies creating/altering vol headers
1345 * lock a partition's vol headers.
1347 * @param[in] dp the partition to lock
1348 * @param[in] locktype READ_LOCK or WRITE_LOCK
1350 * @return operation status
1354 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1358 /* block on acquiring the lock */
1361 code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1363 Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1364 VPartitionPath(dp));
1370 * unlock a partition's vol headers.
1372 * @param[in] dp the partition to unlock
1373 * @param[in] locktype READ_LOCK or WRITE_LOCK
1376 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1378 VReleaseDiskLock(&dp->headerLock, locktype);
1381 /* XXX not sure this will work on AFS_NT40_ENV
1382 * needs to be tested!
1386 * lookup a disk partition object by its index number.
1388 * @param[in] id partition index number
1389 * @param[in] abortp see abortp usage note below
1391 * @return disk partition object
1392 * @retval NULL no such disk partition
1394 * @note when abortp is non-zero, lookups which would return
1395 * NULL will result in an assertion failure
1397 * @pre VOL_LOCK must be held
1399 * @internal volume package internal use only
1402 struct DiskPartition64 *
1403 VGetPartitionById_r(afs_int32 id, int abortp)
1405 struct DiskPartition64 *dp = NULL;
1407 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1408 dp = DiskPartitionTable[id];
1412 osi_Assert(dp != NULL);
1418 * lookup a disk partition object by its index number.
1420 * @param[in] id partition index number
1421 * @param[in] abortp see abortp usage note below
1423 * @return disk partition object
1424 * @retval NULL no such disk partition
1426 * @note when abortp is non-zero, lookups which would return
1427 * NULL will result in an assertion failure
1430 struct DiskPartition64 *
1431 VGetPartitionById(afs_int32 id, int abortp)
1433 struct DiskPartition64 * dp;
1436 dp = VGetPartitionById_r(id, abortp);
1442 static struct DiskPartition64 *
1443 VLookupPartition_r(char * path)
1445 afs_int32 id = volutil_GetPartitionID(path);
1447 if (id < 0 || id > VOLMAXPARTS)
1450 return DiskPartitionTable[id];
1454 AddPartitionToTable_r(struct DiskPartition64 *dp)
1456 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1457 DiskPartitionTable[dp->index] = dp;
1462 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1464 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1465 DiskPartitionTable[dp->index] = NULL;
1468 #endif /* AFS_DEMAND_ATTACH_FS */