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)
80 #include <sys/mnttab.h>
81 #include <sys/mntent.h>
83 #ifdef AFS_LINUX22_ENV
85 #include <sys/statfs.h>
92 #endif /* AFS_SGI_ENV */
93 #endif /* AFS_NT40_ENV */
94 #if defined(AFS_SGI_ENV)
100 #include <afs/afsint.h>
102 #include <afs/errors.h>
105 #include <afs/afssyscalls.h>
112 #include "namei_ops.h"
113 #endif /* AFS_NT40_ENV */
114 #endif /* AFS_NAMEI_ENV */
117 #include "partition.h"
119 #if defined(AFS_HPUX_ENV)
120 #include <sys/privgrp.h>
121 #endif /* defined(AFS_HPUX_ENV) */
124 #include <jfs/filsys.h>
128 extern int VValidVPTEntry(struct vptab *vptp);
131 int aixlow_water = 8; /* default 8% */
132 struct DiskPartition64 *DiskPartitionList;
134 #ifdef AFS_DEMAND_ATTACH_FS
135 /* file to lock to conceptually "lock" the vol headers on a partition */
136 #define AFS_PARTLOCK_FILE ".volheaders.lock"
137 #define AFS_VOLUMELOCK_FILE ".volume.lock"
139 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
141 static struct DiskPartition64 * VLookupPartition_r(char * path);
142 static void AddPartitionToTable_r(struct DiskPartition64 *);
143 #endif /* AFS_DEMAND_ATTACH_FS */
145 #ifdef AFS_SGI_XFS_IOPS_ENV
146 /* Verify that the on disk XFS inodes on the partition are large enough to
147 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
148 * too small to fit in the inode. Returns 0 if the attribute does fit in
151 #include <afs/xfsattrs.h>
153 VerifyXFSInodeSize(char *part, char *fstype)
156 int length = SIZEOF_XFS_ATTR_T;
161 if (strcmp("xfs", fstype))
164 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
165 if (((fd = open(part, O_RDONLY, 0)) != -1)
166 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
168 if (fsx.fsx_nextents) {
169 Log("Partition %s: XFS inodes too small, exiting.\n", part);
170 Log("Run xfs_size_check utility and remake partitions.\n");
177 (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
181 #endif /* AFS_SGI_XFS_IOPS_ENV */
184 VInitPartitionPackage(void)
186 #ifdef AFS_DEMAND_ATTACH_FS
187 memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
188 #endif /* AFS_DEMAND_ATTACH_FS */
193 VInitPartition_r(char *path, char *devname, Device dev)
195 struct DiskPartition64 *dp, *op;
197 dp = malloc(sizeof(struct DiskPartition64));
198 /* Add it to the end, to preserve order when we print statistics */
199 for (op = DiskPartitionList; op; op = op->next) {
206 DiskPartitionList = dp;
208 dp->name = strdup(path);
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 = 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 = strdup(devname);
224 dp->lock_fd = INVALID_FD;
226 dp->f_files = 1; /* just a default value */
227 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
228 if (programType == fileServer)
229 (void)namei_ViceREADME(VPartitionPath(dp));
231 VSetPartitionDiskUsage_r(dp);
232 #ifdef AFS_DEMAND_ATTACH_FS
233 AddPartitionToTable_r(dp);
234 queue_Init(&dp->vol_list.head);
235 CV_INIT(&dp->vol_list.cv, "vol list", CV_DEFAULT, 0);
236 dp->vol_list.len = 0;
237 dp->vol_list.busy = 0;
239 char lockpath[MAXPATHLEN+1];
240 snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
241 lockpath[MAXPATHLEN] = '\0';
242 VLockFileInit(&dp->headerLockFile, lockpath);
244 snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
245 lockpath[MAXPATHLEN] = '\0';
246 VLockFileInit(&dp->volLockFile, lockpath);
248 VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
249 #endif /* AFS_DEMAND_ATTACH_FS */
253 VInitPartition(char *path, char *devname, Device dev)
256 VInitPartition_r(path, devname, dev);
261 /* VAttachPartitions() finds the vice partitions on this server. Calls
262 * VCheckPartition() to do some basic checks on the partition. If the partition
263 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
265 * Returns the number of errors returned by VCheckPartition. An error in
266 * VCheckPartition means that partition is a valid vice partition but the
267 * fileserver should not start because of the error found on that partition.
270 * No specific user space file system checks, since we don't know what
271 * is being used for vice partitions.
273 * Use partition name as devname.
276 VCheckPartition(char *part, char *devname, int logging)
278 struct afs_stat_st status;
279 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
280 char AFSIDatPath[MAXPATHLEN];
283 /* Only keep track of "/vicepx" partitions since it can get hairy
284 * when NFS mounts are involved.. */
285 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
288 if (afs_stat(part, &status) < 0) {
289 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
293 Log("This program is compiled without AFS_NAMEI_ENV, and "
294 "partition %s is mounted with the 'logging' option. "
295 "Using the inode fileserver backend with 'logging' UFS "
296 "partitions causes volume corruption, so please either "
297 "mount the partition without logging, or use the namei "
298 "fileserver backend. Aborting...\n", part);
301 #ifndef AFS_AIX32_ENV
302 if (programType == fileServer) {
303 char salvpath[MAXPATHLEN];
304 strcpy(salvpath, part);
305 strcat(salvpath, "/FORCESALVAGE");
306 if (afs_stat(salvpath, &status) == 0) {
307 Log("VInitVnodes: Found %s; aborting\n", salvpath);
313 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
314 strcpy(AFSIDatPath, part);
315 strcat(AFSIDatPath, "/AFSIDat");
317 if (afs_stat(AFSIDatPath, &status) < 0) {
321 dirp = opendir(part);
323 while ((dp = readdir(dirp))) {
324 if (dp->d_name[0] == 'V') {
325 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);
332 #else /* AFS_NAMEI_ENV */
333 if (afs_stat(AFSIDatPath, &status) == 0) {
334 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
338 #ifdef AFS_SGI_XFS_IOPS_ENV
339 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
342 #endif /* AFS_NAMEI_ENV */
343 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
345 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
346 if (status.st_ino != ROOTINO) {
347 Log("%s is not a mounted file system; ignored.\n", part);
352 VInitPartition(part, devname, status.st_dev);
357 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
358 * attached (return value 1), or only attached when it is a separately
359 * mounted partition (return value 0). For non-NAMEI environments, it
362 * *awouldattach will be set to 1 if the given path at least looks like a vice
363 * partition (that is, if we return 0, the only thing preventing this partition
364 * from being attached is the existence of the AlwaysAttach file), or to 0
365 * otherwise. *awouldattach is set regardless of whether or not the partition
366 * should always be attached or not.
369 VIsAlwaysAttach(char *part, int *awouldattach)
372 struct afs_stat_st st;
375 #endif /* AFS_NAMEI_ENV */
382 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
389 strncpy(checkfile, part, 100);
390 strcat(checkfile, OS_DIRSEP);
391 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
393 ret = afs_stat(checkfile, &st);
394 return (ret < 0) ? 0 : 1;
395 #else /* AFS_NAMEI_ENV */
397 #endif /* AFS_NAMEI_ENV */
400 /* VIsNeverAttach() checks whether a /vicepX directory should never be
401 * attached (return value 1), or follow the normal mounting logic. The
402 * Always Attach flag may override the NeverAttach flag.
405 VIsNeverAttach(char *part)
407 struct afs_stat_st st;
411 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
414 strncpy(checkfile, part, 100);
415 strcat(checkfile, OS_DIRSEP);
416 strcat(checkfile, VICE_NEVERATTACH_FILE);
418 ret = afs_stat(checkfile, &st);
419 return (ret < 0) ? 0 : 1;
422 /* VAttachPartitions2() looks for and attaches /vicepX partitions
423 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
424 * used to attach /vicepX directories which aren't on dedicated
425 * partitions, in the NAMEI fileserver.
428 VAttachPartitions2(void)
436 dirp = opendir(OS_DIRSEP);
437 while ((de = readdir(dirp))) {
438 strcpy(pname, OS_DIRSEP);
439 strncat(pname, de->d_name, 20);
440 pname[sizeof(pname) - 1] = '\0';
442 /* Only keep track of "/vicepx" partitions since automounter
444 if (VIsAlwaysAttach(pname, &wouldattach)) {
445 VCheckPartition(pname, "", 0);
447 struct afs_stat_st st;
448 if (wouldattach && VGetPartition(pname, 0) == NULL &&
449 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
451 /* This is a /vicep* dir, and it has not been attached as a
452 * partition. This probably means that this is a /vicep* dir
453 * that is not a separate partition, so just give a notice so
454 * admins are not confused as to why their /vicep* dirs are not
457 * It is possible that the dir _is_ a separate partition and we
458 * failed to attach it earlier, making this message a bit
459 * confusing. But that should be rare, and an error message
460 * about the failure will already be logged right before this,
461 * so it should be clear enough. */
463 Log("VAttachPartitions: not attaching %s; either it is not a "
464 "separate partition, or it failed to attach (create the "
465 "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
471 #endif /* AFS_NAMEI_ENV */
473 #endif /* AFS_NT40_ENV */
477 VAttachPartitions(void)
483 if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
484 Log("Can't open %s\n", MNTTAB);
488 while (!getmntent(mntfile, &mnt)) {
490 /* Ignore non ufs or non read/write partitions */
491 /* but allow zfs too if we're in the NAMEI environment */
494 (((strcmp(mnt.mnt_fstype, "ufs") &&
495 strcmp(mnt.mnt_fstype, "zfs"))))
497 (strcmp(mnt.mnt_fstype, "ufs") != 0)
499 || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
502 /* Skip this Partition? */
503 if (VIsNeverAttach(mnt.mnt_mountp))
506 /* If we're going to always attach this partition, do it later. */
507 if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
510 #ifndef AFS_NAMEI_ENV
511 if (hasmntopt(&mnt, "logging") != NULL) {
514 #endif /* !AFS_NAMEI_ENV */
516 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special, logging) < 0)
520 (void)fclose(mntfile);
522 /* Process the always-attach partitions, if any. */
523 VAttachPartitions2();
528 #endif /* AFS_SUN5_ENV */
529 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
531 VAttachPartitions(void)
535 struct mntent *mntent;
537 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
538 Log("Problems in getting mount entries(setmntent)\n");
541 while (mntent = getmntent(mfd)) {
542 if (!hasmntopt(mntent, MNTOPT_RW))
545 /* Skip this Partition? */
546 if (VIsNeverAttach(mntent->mnt_dir))
549 /* If we're going to always attach this partition, do it later. */
550 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
553 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
559 /* Process the always-attach partitions, if any. */
560 VAttachPartitions2();
567 * (This function was grabbed from df.c)
570 getmount(struct vmount **vmountpp)
576 /* set initial size of mntctl buffer to a MAGIC NUMBER */
579 /* try the operation until ok or a fatal error */
581 if ((vm = malloc(size)) == NULL) {
582 /* failed getting memory for mount status buf */
583 perror("FATAL ERROR: get_stat malloc failed\n");
588 * perform the QUERY mntctl - if it returns > 0, that is the
589 * number of vmount structures in the buffer. If it returns
590 * -1, an error occured. If it returned 0, then look in
591 * first word of buffer for needed size.
593 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
594 /* OK, got it, now return */
598 } else if (nmounts == 0) {
599 /* the buffer wasn't big enough .... */
600 /* .... get required buffer size */
605 /* some other kind of error occurred */
613 VAttachPartitions(void)
617 struct vmount *vmountp;
619 if ((nmounts = getmount(&vmountp)) <= 0) {
620 Log("Problems in getting # of mount entries(getmount)\n");
625 (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
626 char *part = vmt2dataptr(vmountp, VMT_STUB);
628 if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
629 continue; /* Ignore any "special" partitions */
632 #ifndef AFS_NAMEI_ENV
634 struct superblock fs;
635 /* The Log statements are non-sequiters in the SalvageLog and don't
636 * even appear in the VolserLog, so restrict them to the FileLog.
638 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
639 if (programType == fileServer)
640 Log("Can't read superblock for %s, ignoring it.\n", part);
643 if (IsBigFilesFileSystem(&fs)) {
644 if (programType == fileServer)
645 Log("%s is a big files filesystem, ignoring it.\n", part);
652 /* Skip this Partition? */
653 if (VIsNeverAttach(part))
656 /* If we're going to always attach this partition, do it later. */
657 if (VIsAlwaysAttach(part, NULL))
660 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT), 0) < 0)
664 /* Process the always-attach partitions, if any. */
665 VAttachPartitions2();
670 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
672 VAttachPartitions(void)
677 if (setfsent() < 0) {
678 Log("Error listing filesystems.\n");
682 while ((fsent = getfsent())) {
683 if (strcmp(fsent->fs_type, "rw") != 0)
686 /* Skip this Partition? */
687 if (VIsNeverAttach(fsent->fs_file))
690 /* If we're going to always attach this partition, do it later. */
691 if (VIsAlwaysAttach(fsent->fs_file, NULL))
694 if (VCheckPartition(fsent->fs_file, fsent->fs_spec, 0) < 0)
699 /* Process the always-attach partitions, if any. */
700 VAttachPartitions2();
709 * validate names in vptab.
717 VValidVPTEntry(struct vptab *vpe)
719 int len = strlen(vpe->vp_name);
722 if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
724 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
727 for (i = VICE_PREFIX_SIZE; i < len; i++) {
728 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
729 Log("Invalid partition name %s in registry, ignoring it.\n",
734 if (len == VICE_PREFIX_SIZE + 2) {
735 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
736 (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
738 Log("Invalid partition name %s in registry, ignoring it.\n",
744 len = strlen(vpe->vp_dev);
745 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
746 || vpe->vp_dev[0] > 'Z') {
747 Log("Invalid device name %s in registry, ignoring it.\n",
756 VCheckPartition(char *partName)
763 /* partName is presumed to be of the form "X:" */
764 (void)sprintf(volRoot, "%c:\\", *partName);
766 if (!GetVolumeInformation(volRoot, /* volume root directory */
767 NULL, /* volume name buffer */
768 0, /* volume name size */
769 NULL, /* volume serial number */
770 &dwDummy, /* max component length */
771 &dwDummy, /* file system flags */
772 volFsType, /* file system name */
773 sizeof(volFsType))) {
774 err = GetLastError();
775 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
779 if (strcmp(volFsType, "NTFS")) {
780 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
789 VAttachPartitions(void)
791 struct DiskPartition64 *partP, *prevP, *nextP;
792 struct vpt_iter iter;
795 if (vpt_Start(&iter) < 0) {
796 Log("No partitions to attach.\n");
800 while (0 == vpt_NextEntry(&iter, &entry)) {
801 if (!VValidVPTEntry(&entry)) {
805 /* This test for duplicates relies on the fact that the method
806 * of storing the partition names in the NT registry means the same
807 * partition name will never appear twice in the list.
809 for (partP = DiskPartitionList; partP; partP = partP->next) {
810 if (*partP->devName == *entry.vp_dev) {
811 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
812 partP->flags = PART_DUPLICATE;
813 break; /* Only one entry will ever be in this list. */
817 continue; /* found a duplicate */
819 if (VCheckPartition(entry.vp_dev) < 0)
821 /* This test allows for manually inserting the FORCESALVAGE flag
822 * and thereby invoking the salvager. scandisk obviously won't be
825 if (programType == fileServer) {
826 struct afs_stat_st status;
827 char salvpath[MAXPATHLEN];
828 strcpy(salvpath, entry.vp_dev);
829 strcat(salvpath, "\\FORCESALVAGE");
830 if (afs_stat(salvpath, &status) == 0) {
831 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
835 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
839 /* Run through partition list and clear out the dupes. */
840 prevP = nextP = NULL;
841 for (partP = DiskPartitionList; partP; partP = nextP) {
843 if (partP->flags == PART_DUPLICATE) {
845 prevP->next = partP->next;
847 DiskPartitionList = partP->next;
857 #ifdef AFS_LINUX22_ENV
859 VAttachPartitions(void)
863 struct mntent *mntent;
865 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
866 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
867 Log("Problems in getting mount entries(setmntent)\n");
871 while ((mntent = getmntent(mfd))) {
872 /* Skip this Partition? */
873 if (VIsNeverAttach(mntent->mnt_dir))
876 /* If we're going to always attach this partition, do it later. */
877 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
880 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
885 /* Process the always-attach partitions, if any. */
886 VAttachPartitions2();
890 #endif /* AFS_LINUX22_ENV */
892 /* This routine is to be called whenever the actual name of the partition
893 * is required. The canonical name is still in part->name.
896 VPartitionPath(struct DiskPartition64 *part)
899 return part->devName;
905 /* get partition structure, abortp tells us if we should abort on failure */
906 struct DiskPartition64 *
907 VGetPartition_r(char *name, int abortp)
909 struct DiskPartition64 *dp;
910 #ifdef AFS_DEMAND_ATTACH_FS
911 dp = VLookupPartition_r(name);
912 #else /* AFS_DEMAND_ATTACH_FS */
913 for (dp = DiskPartitionList; dp; dp = dp->next) {
914 if (strcmp(dp->name, name) == 0)
917 #endif /* AFS_DEMAND_ATTACH_FS */
919 osi_Assert(dp != NULL);
923 struct DiskPartition64 *
924 VGetPartition(char *name, int abortp)
926 struct DiskPartition64 *retVal;
928 retVal = VGetPartition_r(name, abortp);
935 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
937 ULARGE_INTEGER free_user, total, free_total;
938 int ufree, tot, tfree;
940 if (!GetDiskFreeSpaceEx
941 (VPartitionPath(dp), &free_user, &total, &free_total)) {
942 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
947 /* Convert to 1K units. */
948 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
949 tot = (int)Int64ShraMod32(total.QuadPart, 10);
950 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
952 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
953 dp->totalUsable = tot;
959 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
962 afs_int64 totalblks, free, used, availblks;
965 struct afs_statvfs statbuf;
967 struct afs_statfs statbuf;
970 if (dp->flags & PART_DONTUPDATE)
972 /* Note: we don't bother syncing because it's only an estimate, update
973 * is syncing every 30 seconds anyway, we only have to keep the disk
974 * approximately 10% from full--you just can't get the stuff in from
975 * the net fast enough to worry */
977 code = afs_statvfs(dp->name, &statbuf);
979 code = afs_statfs(dp->name, &statbuf);
982 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
986 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
987 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
990 totalblks = statbuf.f_blocks;
991 free = statbuf.f_bfree;
992 reserved = free - statbuf.f_bavail;
994 bsize = statbuf.f_frsize;
996 bsize = statbuf.f_bsize;
998 availblks = totalblks - reserved;
999 dp->f_files = statbuf.f_files; /* max # of files in partition */
1001 /* Now free and totalblks are in fragment units, but we want them in
1004 if (bsize >= 1024) {
1005 free *= (bsize / 1024);
1006 totalblks *= (bsize / 1024);
1007 availblks *= (bsize / 1024);
1008 reserved *= (bsize / 1024);
1010 free /= (1024 / bsize);
1011 totalblks /= (1024 / bsize);
1012 availblks /= (1024 / bsize);
1013 reserved /= (1024 / bsize);
1015 /* now compute remaining figures */
1016 used = totalblks - free;
1018 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
1019 dp->totalUsable = availblks;
1020 dp->free = availblks - used; /* this is exactly f_bavail */
1022 #endif /* AFS_NT40_ENV */
1025 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
1028 VSetPartitionDiskUsage_r(dp);
1033 VResetDiskUsage_r(void)
1035 struct DiskPartition64 *dp;
1036 for (dp = DiskPartitionList; dp; dp = dp->next) {
1037 VSetPartitionDiskUsage_r(dp);
1038 #ifndef AFS_PTHREAD_ENV
1040 #endif /* !AFS_PTHREAD_ENV */
1045 VResetDiskUsage(void)
1048 VResetDiskUsage_r();
1053 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1054 afs_sfsize_t checkBlocks)
1057 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1058 * for less than BlocksSpare would skip the error-checking path, and we
1059 * could grow existing files forever, not just for another BlocksSpare
1062 #ifdef AFS_AIX32_ENV
1063 afs_int32 rem, minavail;
1065 if ((rem = vp->partition->free - checkBlocks) < (minavail =
1071 if (vp->partition->free - checkBlocks < 0)
1074 else if (V_maxquota(vp)
1075 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1078 vp->partition->free -= blocks;
1079 V_diskused(vp) += blocks;
1083 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1084 afs_sfsize_t checkBlocks)
1087 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1092 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1095 #ifdef AFS_AIX32_ENV
1096 afs_int32 rem, minavail;
1098 if ((rem = vp->partition->free - blocks) < (minavail =
1101 aixlow_water) / 100))
1103 if (vp->partition->free - blocks < 0)
1107 vp->partition->free -= blocks;
1112 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1116 retVal = VDiskUsage_r(vp, blocks);
1122 VPrintDiskStats_r(void)
1124 struct DiskPartition64 *dp;
1125 for (dp = DiskPartitionList; dp; dp = dp->next) {
1127 Log("Partition %s: %lld "
1128 " available 1K blocks (minfree=%lld), "
1129 "overallocated by %lld blocks\n", dp->name,
1130 dp->totalUsable, dp->minFree, -dp->free);
1132 Log("Partition %s: %lld"
1133 " available 1K blocks (minfree=%lld), "
1134 "%lld free blocks\n", dp->name,
1135 dp->totalUsable, dp->minFree, dp->free);
1141 VPrintDiskStats(void)
1144 VPrintDiskStats_r();
1149 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1150 #define LOCKFILE "LOCKFILE"
1152 VLockPartition_r(char *name)
1154 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1159 if (dp->lock_fd == INVALID_FD) {
1162 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1164 (FD_t)CreateFile(path, GENERIC_WRITE,
1165 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1166 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1167 osi_Assert(dp->lock_fd != INVALID_FD);
1169 memset(&lap, 0, sizeof(lap));
1170 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1177 VUnlockPartition_r(char *name)
1179 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1183 return; /* no partition, will fail later */
1184 memset(&lap, 0, sizeof(lap));
1186 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1187 CloseHandle((HANDLE) dp->lock_fd);
1188 dp->lock_fd = INVALID_FD;
1190 #else /* AFS_NT40_ENV */
1192 #if defined(AFS_HPUX_ENV)
1193 #define BITS_PER_CHAR (8)
1194 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1196 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1197 #endif /* defined(AFS_HPUX_ENV) */
1200 VLockPartition_r(char *name)
1202 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1203 char *partitionName;
1205 struct timeval pausing;
1206 #if defined(AFS_HPUX_ENV)
1208 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1209 unsigned int *globalMask;
1210 int globalMaskIndex;
1211 #endif /* defined(AFS_HPUX_ENV) */
1212 #if defined(AFS_DARWIN_ENV)
1213 char lockfile[MAXPATHLEN];
1214 #endif /* defined(AFS_DARWIN_ENV) */
1215 #ifdef AFS_NAMEI_ENV
1216 #ifdef AFS_AIX42_ENV
1217 char LockFileName[MAXPATHLEN + 1];
1219 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1220 partitionName = (char *)&LockFileName;
1225 return; /* no partition, will fail later */
1226 if (dp->lock_fd != INVALID_FD)
1229 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1230 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1231 partitionName = dp->devName;
1234 #elif defined(AFS_DARWIN_ENV)
1235 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1236 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1237 code = O_RDONLY | O_CREAT;
1239 partitionName = dp->name;
1243 for (retries = 25; retries; retries--) {
1245 dp->lock_fd = afs_open(partitionName, code, 0644);
1247 dp->lock_fd = afs_open(partitionName, code);
1249 if (dp->lock_fd != INVALID_FD)
1251 if (errno == ENOENT)
1254 pausing.tv_usec = 500000;
1255 select(0, NULL, NULL, NULL, &pausing);
1257 osi_Assert(retries != 0);
1259 #if defined (AFS_HPUX_ENV)
1261 osi_Assert(getprivgrp(privGrpList) == 0);
1264 * In general, it will difficult and time-consuming ,if not impossible,
1265 * to try to find the privgroup to which this process belongs that has the
1266 * smallest membership, to minimise the security hole. So, we use the privgrp
1267 * to which everybody belongs.
1269 /* first, we have to find the global mask */
1270 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1271 globalMaskIndex++) {
1272 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1274 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1279 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1280 /* allow everybody to set a lock on a read-only file descriptor */
1281 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1282 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1285 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1287 /* remove the privilege granted to everybody to lock a read-only fd */
1288 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1289 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1292 /* in this case, we should be able to do this with impunity, anyway */
1293 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1296 osi_Assert(lockfRtn != -1);
1298 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1299 osi_Assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1301 osi_Assert(flock(dp->lock_fd, LOCK_EX) == 0);
1302 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1307 VUnlockPartition_r(char *name)
1309 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1311 return; /* no partition, will fail later */
1313 dp->lock_fd = INVALID_FD;
1316 #endif /* AFS_NT40_ENV */
1319 VLockPartition(char *name)
1322 VLockPartition_r(name);
1327 VUnlockPartition(char *name)
1330 VUnlockPartition_r(name);
1334 #ifdef AFS_DEMAND_ATTACH_FS
1336 /* new-style partition locks; these are only to have some mutual exclusion
1337 * between the VGC scanner and volume utilies creating/altering vol headers
1341 * lock a partition's vol headers.
1343 * @param[in] dp the partition to lock
1344 * @param[in] locktype READ_LOCK or WRITE_LOCK
1346 * @return operation status
1350 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1354 /* block on acquiring the lock */
1357 code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1359 Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1360 VPartitionPath(dp));
1366 * unlock a partition's vol headers.
1368 * @param[in] dp the partition to unlock
1369 * @param[in] locktype READ_LOCK or WRITE_LOCK
1372 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1374 VReleaseDiskLock(&dp->headerLock, locktype);
1377 /* XXX not sure this will work on AFS_NT40_ENV
1378 * needs to be tested!
1382 * lookup a disk partition object by its index number.
1384 * @param[in] id partition index number
1385 * @param[in] abortp see abortp usage note below
1387 * @return disk partition object
1388 * @retval NULL no such disk partition
1390 * @note when abortp is non-zero, lookups which would return
1391 * NULL will result in an assertion failure
1393 * @pre VOL_LOCK must be held
1395 * @internal volume package internal use only
1398 struct DiskPartition64 *
1399 VGetPartitionById_r(afs_int32 id, int abortp)
1401 struct DiskPartition64 *dp = NULL;
1403 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1404 dp = DiskPartitionTable[id];
1408 osi_Assert(dp != NULL);
1414 * lookup a disk partition object by its index number.
1416 * @param[in] id partition index number
1417 * @param[in] abortp see abortp usage note below
1419 * @return disk partition object
1420 * @retval NULL no such disk partition
1422 * @note when abortp is non-zero, lookups which would return
1423 * NULL will result in an assertion failure
1426 struct DiskPartition64 *
1427 VGetPartitionById(afs_int32 id, int abortp)
1429 struct DiskPartition64 * dp;
1432 dp = VGetPartitionById_r(id, abortp);
1438 static struct DiskPartition64 *
1439 VLookupPartition_r(char * path)
1441 afs_int32 id = volutil_GetPartitionID(path);
1443 if (id < 0 || id > VOLMAXPARTS)
1446 return DiskPartitionTable[id];
1450 AddPartitionToTable_r(struct DiskPartition64 *dp)
1452 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1453 DiskPartitionTable[dp->index] = dp;
1458 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1460 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1461 DiskPartitionTable[dp->index] = NULL;
1464 #endif /* AFS_DEMAND_ATTACH_FS */