2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
9 * Portions Copyright (c) 2003 Apple Computer, Inc.
10 * Portions Copyright (c) 2006 Sine Nomine Associates
17 Institution: The Information Technology Center, Carnegie-Mellon University
21 #include <afsconfig.h>
22 #include <afs/param.h>
34 #if AFS_HAVE_STATVFS || AFS_HAVE_STATVFS64
35 #include <sys/statvfs.h>
36 #endif /* AFS_HAVE_STATVFS */
37 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
38 #include <sys/mount.h>
41 #if !defined(AFS_SGI_ENV)
43 #include <sys/mount.h>
45 #else /* AFS_OSF_ENV */
46 #ifdef AFS_VFSINCL_ENV
49 #include <sys/fs/ufs_fs.h>
51 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ffs/fs.h>
58 #else /* AFS_VFSINCL_ENV */
59 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
62 #endif /* AFS_VFSINCL_ENV */
63 #endif /* AFS_OSF_ENV */
67 #include <sys/lockf.h>
71 #include <checklist.h>
73 #if defined(AFS_SUN_ENV)
81 #include <sys/mnttab.h>
82 #include <sys/mntent.h>
84 #ifdef AFS_LINUX22_ENV
86 #include <sys/statfs.h>
93 #endif /* AFS_SGI_ENV */
94 #endif /* AFS_NT40_ENV */
95 #if defined(AFS_SGI_ENV)
101 #include <afs/afsint.h>
103 #include <afs/errors.h>
106 #include <afs/afssyscalls.h>
113 #include "namei_ops.h"
115 #endif /* AFS_NT40_ENV */
116 #endif /* AFS_NAMEI_ENV */
119 #include "partition.h"
121 #if defined(AFS_HPUX_ENV)
122 #include <sys/types.h>
123 #include <sys/privgrp.h>
124 #endif /* defined(AFS_HPUX_ENV) */
127 #include <jfs/filsys.h>
130 int aixlow_water = 8; /* default 8% */
131 struct DiskPartition64 *DiskPartitionList;
133 #ifdef AFS_DEMAND_ATTACH_FS
134 /* file to lock to conceptually "lock" the vol headers on a partition */
135 #define AFS_PARTLOCK_FILE ".volheaders.lock"
136 #define AFS_VOLUMELOCK_FILE ".volume.lock"
138 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
140 static struct DiskPartition64 * VLookupPartition_r(char * path);
141 static void AddPartitionToTable_r(struct DiskPartition64 *);
142 #endif /* AFS_DEMAND_ATTACH_FS */
144 #ifdef AFS_SGI_XFS_IOPS_ENV
145 /* Verify that the on disk XFS inodes on the partition are large enough to
146 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
147 * too small to fit in the inode. Returns 0 if the attribute does fit in
150 #include <afs/xfsattrs.h>
152 VerifyXFSInodeSize(char *part, char *fstype)
155 int length = SIZEOF_XFS_ATTR_T;
160 if (strcmp("xfs", fstype))
163 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
164 if (((fd = open(part, O_RDONLY, 0)) != -1)
165 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
167 if (fsx.fsx_nextents) {
168 Log("Partition %s: XFS inodes too small, exiting.\n", part);
169 Log("Run xfs_size_check utility and remake partitions.\n");
176 (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
180 #endif /* AFS_SGI_XFS_IOPS_ENV */
183 VInitPartitionPackage(void)
185 #ifdef AFS_DEMAND_ATTACH_FS
186 memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
187 #endif /* AFS_DEMAND_ATTACH_FS */
192 VInitPartition_r(char *path, char *devname, Device dev)
194 struct DiskPartition64 *dp, *op;
196 dp = (struct DiskPartition64 *)malloc(sizeof(struct DiskPartition64));
197 /* Add it to the end, to preserve order when we print statistics */
198 for (op = DiskPartitionList; op; op = op->next) {
205 DiskPartitionList = dp;
207 dp->name = (char *)malloc(strlen(path) + 1);
208 strncpy(dp->name, path, strlen(path) + 1);
209 dp->index = volutil_GetPartitionID(path);
210 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
211 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
212 dp->devName = (char *)malloc(2 * strlen(path) + 6);
213 strcpy(dp->devName, path);
214 strcat(dp->devName, OS_DIRSEP);
215 strcat(dp->devName, "Lock");
216 mkdir(dp->devName, 0700);
217 strcat(dp->devName, path);
218 close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
219 dp->device = dp->index;
221 dp->devName = (char *)malloc(strlen(devname) + 1);
222 strncpy(dp->devName, devname, strlen(devname) + 1);
225 dp->lock_fd = INVALID_FD;
227 dp->f_files = 1; /* just a default value */
228 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
229 if (programType == fileServer)
230 (void)namei_ViceREADME(VPartitionPath(dp));
232 VSetPartitionDiskUsage_r(dp);
233 #ifdef AFS_DEMAND_ATTACH_FS
234 AddPartitionToTable_r(dp);
235 queue_Init(&dp->vol_list.head);
236 CV_INIT(&dp->vol_list.cv, "vol list", CV_DEFAULT, 0);
237 dp->vol_list.len = 0;
238 dp->vol_list.busy = 0;
240 char lockpath[MAXPATHLEN+1];
241 snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
242 lockpath[MAXPATHLEN] = '\0';
243 VLockFileInit(&dp->headerLockFile, lockpath);
245 snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
246 lockpath[MAXPATHLEN] = '\0';
247 VLockFileInit(&dp->volLockFile, lockpath);
249 VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
250 #endif /* AFS_DEMAND_ATTACH_FS */
254 VInitPartition(char *path, char *devname, Device dev)
257 VInitPartition_r(path, devname, dev);
262 /* VAttachPartitions() finds the vice partitions on this server. Calls
263 * VCheckPartition() to do some basic checks on the partition. If the partition
264 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
266 * Returns the number of errors returned by VCheckPartition. An error in
267 * VCheckPartition means that partition is a valid vice partition but the
268 * fileserver should not start because of the error found on that partition.
271 * No specific user space file system checks, since we don't know what
272 * is being used for vice partitions.
274 * Use partition name as devname.
277 VCheckPartition(char *part, char *devname)
279 struct afs_stat_st status;
280 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
281 char AFSIDatPath[MAXPATHLEN];
284 /* Only keep track of "/vicepx" partitions since it can get hairy
285 * when NFS mounts are involved.. */
286 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
289 if (afs_stat(part, &status) < 0) {
290 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
293 #ifndef AFS_AIX32_ENV
294 if (programType == fileServer) {
295 char salvpath[MAXPATHLEN];
296 strcpy(salvpath, part);
297 strcat(salvpath, "/FORCESALVAGE");
298 if (afs_stat(salvpath, &status) == 0) {
299 Log("VInitVnodes: Found %s; aborting\n", salvpath);
305 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
306 strcpy(AFSIDatPath, part);
307 strcat(AFSIDatPath, "/AFSIDat");
309 if (afs_stat(AFSIDatPath, &status) < 0) {
313 dirp = opendir(part);
315 while ((dp = readdir(dirp))) {
316 if (dp->d_name[0] == 'V') {
317 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);
324 #else /* AFS_NAMEI_ENV */
325 if (afs_stat(AFSIDatPath, &status) == 0) {
326 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
330 #ifdef AFS_SGI_XFS_IOPS_ENV
331 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
334 #endif /* AFS_NAMEI_ENV */
335 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
337 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
338 if (status.st_ino != ROOTINO) {
339 Log("%s is not a mounted file system; ignored.\n", part);
344 VInitPartition(part, devname, status.st_dev);
349 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
350 * attached (return value 1), or only attached when it is a separately
351 * mounted partition (return value 0). For non-NAMEI environments, it
354 * *awouldattach will be set to 1 if the given path at least looks like a vice
355 * partition (that is, if we return 0, the only thing preventing this partition
356 * from being attached is the existence of the AlwaysAttach file), or to 0
357 * otherwise. *awouldattach is set regardless of whether or not the partition
358 * should always be attached or not.
361 VIsAlwaysAttach(char *part, int *awouldattach)
364 struct afs_stat_st st;
367 #endif /* AFS_NAMEI_ENV */
374 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
381 strncpy(checkfile, part, 100);
382 strcat(checkfile, OS_DIRSEP);
383 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
385 ret = afs_stat(checkfile, &st);
386 return (ret < 0) ? 0 : 1;
387 #else /* AFS_NAMEI_ENV */
389 #endif /* AFS_NAMEI_ENV */
392 /* VAttachPartitions2() looks for and attaches /vicepX partitions
393 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
394 * used to attach /vicepX directories which aren't on dedicated
395 * partitions, in the NAMEI fileserver.
398 VAttachPartitions2(void)
406 dirp = opendir(OS_DIRSEP);
407 while ((de = readdir(dirp))) {
408 strcpy(pname, OS_DIRSEP);
409 strncat(pname, de->d_name, 20);
410 pname[sizeof(pname) - 1] = '\0';
412 /* Only keep track of "/vicepx" partitions since automounter
414 if (VIsAlwaysAttach(pname, &wouldattach)) {
415 VCheckPartition(pname, "");
417 struct afs_stat_st st;
418 if (wouldattach && VGetPartition(pname, 0) == NULL &&
419 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
421 /* This is a /vicep* dir, and it has not been attached as a
422 * partition. This probably means that this is a /vicep* dir
423 * that is not a separate partition, so just give a notice so
424 * admins are not confused as to why their /vicep* dirs are not
427 * It is possible that the dir _is_ a separate partition and we
428 * failed to attach it earlier, making this message a bit
429 * confusing. But that should be rare, and an error message
430 * about the failure will already be logged right before this,
431 * so it should be clear enough. */
433 Log("VAttachPartitions: not attaching %s; either it is not a "
434 "separate partition, or it failed to attach (create the "
435 "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
441 #endif /* AFS_NAMEI_ENV */
443 #endif /* AFS_NT40_ENV */
447 VAttachPartitions(void)
453 if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
454 Log("Can't open %s\n", MNTTAB);
458 while (!getmntent(mntfile, &mnt)) {
459 /* Ignore non ufs or non read/write partitions */
460 /* but allow zfs too if we're in the NAMEI environment */
463 (((strcmp(mnt.mnt_fstype, "ufs") &&
464 strcmp(mnt.mnt_fstype, "zfs"))))
466 (strcmp(mnt.mnt_fstype, "ufs") != 0)
468 || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
471 /* If we're going to always attach this partition, do it later. */
472 if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
475 #ifndef AFS_NAMEI_ENV
476 if (hasmntopt(&mnt, "logging") != NULL) {
477 Log("This program is compiled without AFS_NAMEI_ENV, and "
478 "partition %s is mounted with the 'logging' option. "
479 "Using the inode fileserver backend with 'logging' UFS "
480 "partitions causes volume corruption, so please either "
481 "mount the partition without logging, or use the namei "
482 "fileserver backend. Aborting...\n", mnt.mnt_mountp);
485 #endif /* !AFS_NAMEI_ENV */
487 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0)
491 (void)fclose(mntfile);
493 /* Process the always-attach partitions, if any. */
494 VAttachPartitions2();
499 #endif /* AFS_SUN5_ENV */
500 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
502 VAttachPartitions(void)
506 struct mntent *mntent;
508 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
509 Log("Problems in getting mount entries(setmntent)\n");
512 while (mntent = getmntent(mfd)) {
513 if (!hasmntopt(mntent, MNTOPT_RW))
516 /* If we're going to always attach this partition, do it later. */
517 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
520 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
526 /* Process the always-attach partitions, if any. */
527 VAttachPartitions2();
534 * (This function was grabbed from df.c)
537 getmount(struct vmount **vmountpp)
543 /* set initial size of mntctl buffer to a MAGIC NUMBER */
546 /* try the operation until ok or a fatal error */
548 if ((vm = (struct vmount *)malloc(size)) == NULL) {
549 /* failed getting memory for mount status buf */
550 perror("FATAL ERROR: get_stat malloc failed\n");
555 * perform the QUERY mntctl - if it returns > 0, that is the
556 * number of vmount structures in the buffer. If it returns
557 * -1, an error occured. If it returned 0, then look in
558 * first word of buffer for needed size.
560 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
561 /* OK, got it, now return */
565 } else if (nmounts == 0) {
566 /* the buffer wasn't big enough .... */
567 /* .... get required buffer size */
572 /* some other kind of error occurred */
580 VAttachPartitions(void)
584 struct vmount *vmountp;
586 if ((nmounts = getmount(&vmountp)) <= 0) {
587 Log("Problems in getting # of mount entries(getmount)\n");
592 (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
593 char *part = vmt2dataptr(vmountp, VMT_STUB);
595 if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
596 continue; /* Ignore any "special" partitions */
599 #ifndef AFS_NAMEI_ENV
601 struct superblock fs;
602 /* The Log statements are non-sequiters in the SalvageLog and don't
603 * even appear in the VolserLog, so restrict them to the FileLog.
605 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
606 if (programType == fileServer)
607 Log("Can't read superblock for %s, ignoring it.\n", part);
610 if (IsBigFilesFileSystem(&fs)) {
611 if (programType == fileServer)
612 Log("%s is a big files filesystem, ignoring it.\n", part);
619 /* If we're going to always attach this partition, do it later. */
620 if (VIsAlwaysAttach(part, NULL))
623 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0)
627 /* Process the always-attach partitions, if any. */
628 VAttachPartitions2();
633 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
635 VAttachPartitions(void)
640 if (setfsent() < 0) {
641 Log("Error listing filesystems.\n");
645 while ((fsent = getfsent())) {
646 if (strcmp(fsent->fs_type, "rw") != 0)
649 /* If we're going to always attach this partition, do it later. */
650 if (VIsAlwaysAttach(fsent->fs_file, NULL))
653 if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0)
658 /* Process the always-attach partitions, if any. */
659 VAttachPartitions2();
667 #include <sys/stat.h>
670 * validate names in vptab.
678 VValidVPTEntry(struct vptab *vpe)
680 int len = strlen(vpe->vp_name);
683 if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
685 if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
688 for (i = VICE_PREFIX_SIZE; i < len; i++) {
689 if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
690 Log("Invalid partition name %s in registry, ignoring it.\n",
695 if (len == VICE_PREFIX_SIZE + 2) {
696 i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
697 (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
699 Log("Invalid partition name %s in registry, ignoring it.\n",
705 len = strlen(vpe->vp_dev);
706 if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
707 || vpe->vp_dev[0] > 'Z') {
708 Log("Invalid device name %s in registry, ignoring it.\n",
717 VCheckPartition(char *partName)
724 /* partName is presumed to be of the form "X:" */
725 (void)sprintf(volRoot, "%c:\\", *partName);
727 if (!GetVolumeInformation(volRoot, /* volume root directory */
728 NULL, /* volume name buffer */
729 0, /* volume name size */
730 NULL, /* volume serial number */
731 &dwDummy, /* max component length */
732 &dwDummy, /* file system flags */
733 volFsType, /* file system name */
734 sizeof(volFsType))) {
735 err = GetLastError();
736 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
740 if (strcmp(volFsType, "NTFS")) {
741 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
750 VAttachPartitions(void)
752 struct DiskPartition64 *partP, *prevP, *nextP;
753 struct vpt_iter iter;
756 if (vpt_Start(&iter) < 0) {
757 Log("No partitions to attach.\n");
761 while (0 == vpt_NextEntry(&iter, &entry)) {
762 if (!VValidVPTEntry(&entry)) {
766 /* This test for duplicates relies on the fact that the method
767 * of storing the partition names in the NT registry means the same
768 * partition name will never appear twice in the list.
770 for (partP = DiskPartitionList; partP; partP = partP->next) {
771 if (*partP->devName == *entry.vp_dev) {
772 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
773 partP->flags = PART_DUPLICATE;
774 break; /* Only one entry will ever be in this list. */
778 continue; /* found a duplicate */
780 if (VCheckPartition(entry.vp_dev) < 0)
782 /* This test allows for manually inserting the FORCESALVAGE flag
783 * and thereby invoking the salvager. scandisk obviously won't be
786 if (programType == fileServer) {
787 struct afs_stat_st status;
788 char salvpath[MAXPATHLEN];
789 strcpy(salvpath, entry.vp_dev);
790 strcat(salvpath, "\\FORCESALVAGE");
791 if (afs_stat(salvpath, &status) == 0) {
792 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
796 VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
800 /* Run through partition list and clear out the dupes. */
801 prevP = nextP = NULL;
802 for (partP = DiskPartitionList; partP; partP = nextP) {
804 if (partP->flags == PART_DUPLICATE) {
806 prevP->next = partP->next;
808 DiskPartitionList = partP->next;
818 #ifdef AFS_LINUX22_ENV
820 VAttachPartitions(void)
824 struct mntent *mntent;
826 if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
827 if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
828 Log("Problems in getting mount entries(setmntent)\n");
832 while ((mntent = getmntent(mfd))) {
833 /* If we're going to always attach this partition, do it later. */
834 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
837 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
842 /* Process the always-attach partitions, if any. */
843 VAttachPartitions2();
847 #endif /* AFS_LINUX22_ENV */
849 /* This routine is to be called whenever the actual name of the partition
850 * is required. The canonical name is still in part->name.
853 VPartitionPath(struct DiskPartition64 *part)
856 return part->devName;
862 /* get partition structure, abortp tells us if we should abort on failure */
863 struct DiskPartition64 *
864 VGetPartition_r(char *name, int abortp)
866 struct DiskPartition64 *dp;
867 #ifdef AFS_DEMAND_ATTACH_FS
868 dp = VLookupPartition_r(name);
869 #else /* AFS_DEMAND_ATTACH_FS */
870 for (dp = DiskPartitionList; dp; dp = dp->next) {
871 if (strcmp(dp->name, name) == 0)
874 #endif /* AFS_DEMAND_ATTACH_FS */
876 osi_Assert(dp != NULL);
880 struct DiskPartition64 *
881 VGetPartition(char *name, int abortp)
883 struct DiskPartition64 *retVal;
885 retVal = VGetPartition_r(name, abortp);
892 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
894 ULARGE_INTEGER free_user, total, free_total;
895 int ufree, tot, tfree;
897 if (!GetDiskFreeSpaceEx
898 (VPartitionPath(dp), &free_user, &total, &free_total)) {
899 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
904 /* Convert to 1K units. */
905 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
906 tot = (int)Int64ShraMod32(total.QuadPart, 10);
907 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
909 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
910 dp->totalUsable = tot;
916 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
919 afs_int64 totalblks, free, used, availblks;
922 struct afs_statvfs statbuf;
924 struct afs_statfs statbuf;
927 if (dp->flags & PART_DONTUPDATE)
929 /* Note: we don't bother syncing because it's only an estimate, update
930 * is syncing every 30 seconds anyway, we only have to keep the disk
931 * approximately 10% from full--you just can't get the stuff in from
932 * the net fast enough to worry */
934 code = afs_statvfs(dp->name, &statbuf);
936 code = afs_statfs(dp->name, &statbuf);
939 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
943 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
944 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
947 totalblks = statbuf.f_blocks;
948 free = statbuf.f_bfree;
949 reserved = free - statbuf.f_bavail;
951 bsize = statbuf.f_frsize;
953 bsize = statbuf.f_bsize;
955 availblks = totalblks - reserved;
956 dp->f_files = statbuf.f_files; /* max # of files in partition */
958 /* Now free and totalblks are in fragment units, but we want them in
962 free *= (bsize / 1024);
963 totalblks *= (bsize / 1024);
964 availblks *= (bsize / 1024);
965 reserved *= (bsize / 1024);
967 free /= (1024 / bsize);
968 totalblks /= (1024 / bsize);
969 availblks /= (1024 / bsize);
970 reserved /= (1024 / bsize);
972 /* now compute remaining figures */
973 used = totalblks - free;
975 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
976 dp->totalUsable = availblks;
977 dp->free = availblks - used; /* this is exactly f_bavail */
979 #endif /* AFS_NT40_ENV */
982 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
985 VSetPartitionDiskUsage_r(dp);
990 VResetDiskUsage_r(void)
992 struct DiskPartition64 *dp;
993 for (dp = DiskPartitionList; dp; dp = dp->next) {
994 VSetPartitionDiskUsage_r(dp);
995 #ifndef AFS_PTHREAD_ENV
997 #endif /* !AFS_PTHREAD_ENV */
1002 VResetDiskUsage(void)
1005 VResetDiskUsage_r();
1010 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1011 afs_sfsize_t checkBlocks)
1014 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1015 * for less than BlocksSpare would skip the error-checking path, and we
1016 * could grow existing files forever, not just for another BlocksSpare
1019 #ifdef AFS_AIX32_ENV
1020 afs_int32 rem, minavail;
1022 if ((rem = vp->partition->free - checkBlocks) < (minavail =
1028 if (vp->partition->free - checkBlocks < 0)
1031 else if (V_maxquota(vp)
1032 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1035 vp->partition->free -= blocks;
1036 V_diskused(vp) += blocks;
1040 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1041 afs_sfsize_t checkBlocks)
1044 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1049 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1052 #ifdef AFS_AIX32_ENV
1053 afs_int32 rem, minavail;
1055 if ((rem = vp->partition->free - blocks) < (minavail =
1058 aixlow_water) / 100))
1060 if (vp->partition->free - blocks < 0)
1064 vp->partition->free -= blocks;
1069 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1073 retVal = VDiskUsage_r(vp, blocks);
1079 VPrintDiskStats_r(void)
1081 struct DiskPartition64 *dp;
1082 for (dp = DiskPartitionList; dp; dp = dp->next) {
1084 Log("Partition %s: %lld "
1085 " available 1K blocks (minfree=%lld), "
1086 "overallocated by %lld blocks\n", dp->name,
1087 dp->totalUsable, dp->minFree, -dp->free);
1089 Log("Partition %s: %lld"
1090 " available 1K blocks (minfree=%lld), "
1091 "%lld free blocks\n", dp->name,
1092 dp->totalUsable, dp->minFree, dp->free);
1098 VPrintDiskStats(void)
1101 VPrintDiskStats_r();
1106 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1107 #define LOCKFILE "LOCKFILE"
1109 VLockPartition_r(char *name)
1111 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1116 if (dp->lock_fd == INVALID_FD) {
1119 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1121 (FD_t)CreateFile(path, GENERIC_WRITE,
1122 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1123 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1124 osi_Assert(dp->lock_fd != INVALID_FD);
1126 memset(&lap, 0, sizeof(lap));
1127 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1134 VUnlockPartition_r(char *name)
1136 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1140 return; /* no partition, will fail later */
1141 memset(&lap, 0, sizeof(lap));
1143 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1144 CloseHandle((HANDLE) dp->lock_fd);
1145 dp->lock_fd = INVALID_FD;
1147 #else /* AFS_NT40_ENV */
1149 #if defined(AFS_HPUX_ENV)
1150 #define BITS_PER_CHAR (8)
1151 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1153 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1154 #endif /* defined(AFS_HPUX_ENV) */
1157 VLockPartition_r(char *name)
1159 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1160 char *partitionName;
1162 struct timeval pausing;
1163 #if defined(AFS_HPUX_ENV)
1165 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1166 unsigned int *globalMask;
1167 int globalMaskIndex;
1168 #endif /* defined(AFS_HPUX_ENV) */
1169 #if defined(AFS_DARWIN_ENV)
1170 char lockfile[MAXPATHLEN];
1171 #endif /* defined(AFS_DARWIN_ENV) */
1172 #ifdef AFS_NAMEI_ENV
1173 #ifdef AFS_AIX42_ENV
1174 char LockFileName[MAXPATHLEN + 1];
1176 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1177 partitionName = (char *)&LockFileName;
1182 return; /* no partition, will fail later */
1183 if (dp->lock_fd != INVALID_FD)
1186 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1187 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1188 partitionName = dp->devName;
1191 #elif defined(AFS_DARWIN_ENV)
1192 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1193 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1194 code = O_RDONLY | O_CREAT;
1196 partitionName = dp->name;
1200 for (retries = 25; retries; retries--) {
1202 dp->lock_fd = afs_open(partitionName, code, 0644);
1204 dp->lock_fd = afs_open(partitionName, code);
1206 if (dp->lock_fd != INVALID_FD)
1208 if (errno == ENOENT)
1211 pausing.tv_usec = 500000;
1212 select(0, NULL, NULL, NULL, &pausing);
1214 osi_Assert(retries != 0);
1216 #if defined (AFS_HPUX_ENV)
1218 osi_Assert(getprivgrp(privGrpList) == 0);
1221 * In general, it will difficult and time-consuming ,if not impossible,
1222 * to try to find the privgroup to which this process belongs that has the
1223 * smallest membership, to minimise the security hole. So, we use the privgrp
1224 * to which everybody belongs.
1226 /* first, we have to find the global mask */
1227 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1228 globalMaskIndex++) {
1229 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1231 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1236 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1237 /* allow everybody to set a lock on a read-only file descriptor */
1238 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1239 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1242 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1244 /* remove the privilege granted to everybody to lock a read-only fd */
1245 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1246 osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1249 /* in this case, we should be able to do this with impunity, anyway */
1250 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1253 osi_Assert(lockfRtn != -1);
1255 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1256 osi_Assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1258 osi_Assert(flock(dp->lock_fd, LOCK_EX) == 0);
1259 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1264 VUnlockPartition_r(char *name)
1266 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1268 return; /* no partition, will fail later */
1270 dp->lock_fd = INVALID_FD;
1273 #endif /* AFS_NT40_ENV */
1276 VLockPartition(char *name)
1279 VLockPartition_r(name);
1284 VUnlockPartition(char *name)
1287 VUnlockPartition_r(name);
1291 #ifdef AFS_DEMAND_ATTACH_FS
1293 /* new-style partition locks; these are only to have some mutual exclusion
1294 * between the VGC scanner and volume utilies creating/altering vol headers
1298 * lock a partition's vol headers.
1300 * @param[in] dp the partition to lock
1301 * @param[in] locktype READ_LOCK or WRITE_LOCK
1303 * @return operation status
1307 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1311 /* block on acquiring the lock */
1314 code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1316 Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1317 VPartitionPath(dp));
1323 * unlock a partition's vol headers.
1325 * @param[in] dp the partition to unlock
1326 * @param[in] locktype READ_LOCK or WRITE_LOCK
1329 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1331 VReleaseDiskLock(&dp->headerLock, locktype);
1334 /* XXX not sure this will work on AFS_NT40_ENV
1335 * needs to be tested!
1339 * lookup a disk partition object by its index number.
1341 * @param[in] id partition index number
1342 * @param[in] abortp see abortp usage note below
1344 * @return disk partition object
1345 * @retval NULL no such disk partition
1347 * @note when abortp is non-zero, lookups which would return
1348 * NULL will result in an assertion failure
1350 * @pre VOL_LOCK must be held
1352 * @internal volume package internal use only
1355 struct DiskPartition64 *
1356 VGetPartitionById_r(afs_int32 id, int abortp)
1358 struct DiskPartition64 *dp = NULL;
1360 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1361 dp = DiskPartitionTable[id];
1365 osi_Assert(dp != NULL);
1371 * lookup a disk partition object by its index number.
1373 * @param[in] id partition index number
1374 * @param[in] abortp see abortp usage note below
1376 * @return disk partition object
1377 * @retval NULL no such disk partition
1379 * @note when abortp is non-zero, lookups which would return
1380 * NULL will result in an assertion failure
1383 struct DiskPartition64 *
1384 VGetPartitionById(afs_int32 id, int abortp)
1386 struct DiskPartition64 * dp;
1389 dp = VGetPartitionById_r(id, abortp);
1395 static struct DiskPartition64 *
1396 VLookupPartition_r(char * path)
1398 afs_int32 id = volutil_GetPartitionID(path);
1400 if (id < 0 || id > VOLMAXPARTS)
1403 return DiskPartitionTable[id];
1407 AddPartitionToTable_r(struct DiskPartition64 *dp)
1409 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1410 DiskPartitionTable[dp->index] = dp;
1415 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1417 osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1418 DiskPartitionTable[dp->index] = NULL;
1421 #endif /* AFS_DEMAND_ATTACH_FS */