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 #ifdef AFS_PTHREAD_ENV
101 # include <opr/lock.h>
103 #include <afs/afsint.h>
104 #include <rx/rx_queue.h>
106 #include <afs/errors.h>
109 #include <afs/afssyscalls.h>
116 #include "namei_ops.h"
117 #endif /* AFS_NT40_ENV */
118 #endif /* AFS_NAMEI_ENV */
121 #include "partition.h"
123 #if defined(AFS_HPUX_ENV)
124 #include <sys/privgrp.h>
125 #endif /* defined(AFS_HPUX_ENV) */
128 #include <jfs/filsys.h>
132 extern int VValidVPTEntry(struct vptab *vptp);
135 int aixlow_water = 8; /* default 8% */
136 struct DiskPartition64 *DiskPartitionList;
138 #ifdef AFS_DEMAND_ATTACH_FS
139 /* file to lock to conceptually "lock" the vol headers on a partition */
140 #define AFS_PARTLOCK_FILE ".volheaders.lock"
141 #define AFS_VOLUMELOCK_FILE ".volume.lock"
143 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
145 static struct DiskPartition64 * VLookupPartition_r(char * path);
146 static void AddPartitionToTable_r(struct DiskPartition64 *);
147 #endif /* AFS_DEMAND_ATTACH_FS */
149 #ifdef AFS_SGI_XFS_IOPS_ENV
150 /* Verify that the on disk XFS inodes on the partition are large enough to
151 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
152 * too small to fit in the inode. Returns 0 if the attribute does fit in
155 #include <afs/xfsattrs.h>
157 VerifyXFSInodeSize(char *part, char *fstype)
160 int length = SIZEOF_XFS_ATTR_T;
165 if (strcmp("xfs", fstype))
168 if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
169 if (((fd = open(part, O_RDONLY, 0)) != -1)
170 && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
172 if (fsx.fsx_nextents) {
173 Log("Partition %s: XFS inodes too small, exiting.\n", part);
174 Log("Run xfs_size_check utility and remake partitions.\n");
181 (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
185 #endif /* AFS_SGI_XFS_IOPS_ENV */
188 VInitPartitionPackage(void)
190 #ifdef AFS_DEMAND_ATTACH_FS
191 memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
192 #endif /* AFS_DEMAND_ATTACH_FS */
197 VInitPartition_r(char *path, char *devname, Device dev)
199 struct DiskPartition64 *dp, *op;
201 dp = malloc(sizeof(struct DiskPartition64));
202 /* Add it to the end, to preserve order when we print statistics */
203 for (op = DiskPartitionList; op; op = op->next) {
210 DiskPartitionList = dp;
212 dp->name = strdup(path);
213 dp->index = volutil_GetPartitionID(path);
214 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
215 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
216 dp->devName = malloc(2 * strlen(path) + 6);
217 strcpy(dp->devName, path);
218 strcat(dp->devName, OS_DIRSEP);
219 strcat(dp->devName, "Lock");
220 mkdir(dp->devName, 0700);
221 strcat(dp->devName, path);
222 close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
223 dp->device = dp->index;
225 dp->devName = strdup(devname);
228 dp->lock_fd = INVALID_FD;
230 dp->f_files = 1; /* just a default value */
231 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
232 if (programType == fileServer)
233 (void)namei_ViceREADME(VPartitionPath(dp));
235 VSetPartitionDiskUsage_r(dp);
236 #ifdef AFS_DEMAND_ATTACH_FS
237 AddPartitionToTable_r(dp);
238 queue_Init(&dp->vol_list.head);
239 CV_INIT(&dp->vol_list.cv, "vol list", CV_DEFAULT, 0);
240 dp->vol_list.len = 0;
241 dp->vol_list.busy = 0;
243 char lockpath[MAXPATHLEN+1];
244 snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
245 lockpath[MAXPATHLEN] = '\0';
246 VLockFileInit(&dp->headerLockFile, lockpath);
248 snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
249 lockpath[MAXPATHLEN] = '\0';
250 VLockFileInit(&dp->volLockFile, lockpath);
252 VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
253 #endif /* AFS_DEMAND_ATTACH_FS */
257 VInitPartition(char *path, char *devname, Device dev)
260 VInitPartition_r(path, devname, dev);
265 /* VAttachPartitions() finds the vice partitions on this server. Calls
266 * VCheckPartition() to do some basic checks on the partition. If the partition
267 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
269 * Returns the number of errors returned by VCheckPartition. An error in
270 * VCheckPartition means that partition is a valid vice partition but the
271 * fileserver should not start because of the error found on that partition.
274 * No specific user space file system checks, since we don't know what
275 * is being used for vice partitions.
277 * Use partition name as devname.
280 VCheckPartition(char *part, char *devname, int logging)
282 struct afs_stat_st status;
283 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
284 char AFSIDatPath[MAXPATHLEN];
287 /* Only keep track of "/vicepx" partitions since it can get hairy
288 * when NFS mounts are involved.. */
289 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
292 if (volutil_GetPartitionID(part) == -1) {
293 Log("Warning: %s is a bad partition name; ignored.\n", part);
296 if (afs_stat(part, &status) < 0) {
297 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
301 Log("This program is compiled without AFS_NAMEI_ENV, and "
302 "partition %s is mounted with the 'logging' option. "
303 "Using the inode fileserver backend with 'logging' UFS "
304 "partitions causes volume corruption, so please either "
305 "mount the partition without logging, or use the namei "
306 "fileserver backend. Aborting...\n", part);
309 #ifndef AFS_AIX32_ENV
310 if (programType == fileServer) {
311 char salvpath[MAXPATHLEN];
312 strcpy(salvpath, part);
313 strcat(salvpath, "/FORCESALVAGE");
314 if (afs_stat(salvpath, &status) == 0) {
315 Log("VInitVnodes: Found %s; aborting\n", salvpath);
321 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
322 strcpy(AFSIDatPath, part);
323 strcat(AFSIDatPath, "/AFSIDat");
325 if (afs_stat(AFSIDatPath, &status) < 0) {
329 dirp = opendir(part);
331 while ((dp = readdir(dirp))) {
332 if (dp->d_name[0] == 'V') {
333 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);
340 #else /* AFS_NAMEI_ENV */
341 if (afs_stat(AFSIDatPath, &status) == 0) {
342 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
346 #ifdef AFS_SGI_XFS_IOPS_ENV
347 if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
350 #endif /* AFS_NAMEI_ENV */
351 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
353 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
354 if (status.st_ino != ROOTINO) {
355 Log("%s is not a mounted file system; ignored.\n", part);
360 VInitPartition(part, devname, status.st_dev);
365 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
366 * attached (return value 1), or only attached when it is a separately
367 * mounted partition (return value 0). For non-NAMEI environments, it
370 * *awouldattach will be set to 1 if the given path at least looks like a vice
371 * partition (that is, if we return 0, the only thing preventing this partition
372 * from being attached is the existence of the AlwaysAttach file), or to 0
373 * otherwise. *awouldattach is set regardless of whether or not the partition
374 * should always be attached or not.
377 VIsAlwaysAttach(char *part, int *awouldattach)
380 struct afs_stat_st st;
383 #endif /* AFS_NAMEI_ENV */
390 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
397 strncpy(checkfile, part, 100);
398 strcat(checkfile, OS_DIRSEP);
399 strcat(checkfile, VICE_ALWAYSATTACH_FILE);
401 ret = afs_stat(checkfile, &st);
402 return (ret < 0) ? 0 : 1;
403 #else /* AFS_NAMEI_ENV */
405 #endif /* AFS_NAMEI_ENV */
408 /* VIsNeverAttach() checks whether a /vicepX directory should never be
409 * attached (return value 1), or follow the normal mounting logic. The
410 * Always Attach flag may override the NeverAttach flag.
413 VIsNeverAttach(char *part)
415 struct afs_stat_st st;
419 if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
422 strncpy(checkfile, part, 100);
423 strcat(checkfile, OS_DIRSEP);
424 strcat(checkfile, VICE_NEVERATTACH_FILE);
426 ret = afs_stat(checkfile, &st);
427 return (ret < 0) ? 0 : 1;
430 /* VAttachPartitions2() looks for and attaches /vicepX partitions
431 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
432 * used to attach /vicepX directories which aren't on dedicated
433 * partitions, in the NAMEI fileserver.
436 VAttachPartitions2(void)
444 dirp = opendir(OS_DIRSEP);
445 while ((de = readdir(dirp))) {
446 strcpy(pname, OS_DIRSEP);
447 strncat(pname, de->d_name, 20);
448 pname[sizeof(pname) - 1] = '\0';
450 /* Only keep track of "/vicepx" partitions since automounter
452 if (VIsAlwaysAttach(pname, &wouldattach)) {
453 VCheckPartition(pname, "", 0);
455 struct afs_stat_st st;
456 if (wouldattach && VGetPartition(pname, 0) == NULL &&
457 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
459 /* This is a /vicep* dir, and it has not been attached as a
460 * partition. This probably means that this is a /vicep* dir
461 * that is not a separate partition, so just give a notice so
462 * admins are not confused as to why their /vicep* dirs are not
465 * It is possible that the dir _is_ a separate partition and we
466 * failed to attach it earlier, making this message a bit
467 * confusing. But that should be rare, and an error message
468 * about the failure will already be logged right before this,
469 * so it should be clear enough. */
471 Log("VAttachPartitions: not attaching %s; either it is not a "
472 "separate partition, or it failed to attach (create the "
473 "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
479 #endif /* AFS_NAMEI_ENV */
481 #endif /* AFS_NT40_ENV */
485 VAttachPartitions(void)
491 if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
492 Log("Can't open %s\n", MNTTAB);
496 while (!getmntent(mntfile, &mnt)) {
498 /* Ignore non ufs or non read/write partitions */
499 /* but allow zfs too if we're in the NAMEI environment */
502 (((strcmp(mnt.mnt_fstype, "ufs") &&
503 strcmp(mnt.mnt_fstype, "zfs"))))
505 (strcmp(mnt.mnt_fstype, "ufs") != 0)
507 || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
510 /* Skip this Partition? */
511 if (VIsNeverAttach(mnt.mnt_mountp))
514 /* If we're going to always attach this partition, do it later. */
515 if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
518 #ifndef AFS_NAMEI_ENV
519 if (hasmntopt(&mnt, "logging") != NULL) {
522 #endif /* !AFS_NAMEI_ENV */
524 if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special, logging) < 0)
528 (void)fclose(mntfile);
530 /* Process the always-attach partitions, if any. */
531 VAttachPartitions2();
536 #endif /* AFS_SUN5_ENV */
537 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
539 VAttachPartitions(void)
543 struct mntent *mntent;
545 if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
546 Log("Problems in getting mount entries(setmntent)\n");
549 while (mntent = getmntent(mfd)) {
550 if (!hasmntopt(mntent, MNTOPT_RW))
553 /* Skip this Partition? */
554 if (VIsNeverAttach(mntent->mnt_dir))
557 /* If we're going to always attach this partition, do it later. */
558 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
561 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
567 /* Process the always-attach partitions, if any. */
568 VAttachPartitions2();
575 * (This function was grabbed from df.c)
578 getmount(struct vmount **vmountpp)
584 /* set initial size of mntctl buffer to a MAGIC NUMBER */
587 /* try the operation until ok or a fatal error */
589 if ((vm = malloc(size)) == NULL) {
590 /* failed getting memory for mount status buf */
591 perror("FATAL ERROR: get_stat malloc failed\n");
596 * perform the QUERY mntctl - if it returns > 0, that is the
597 * number of vmount structures in the buffer. If it returns
598 * -1, an error occured. If it returned 0, then look in
599 * first word of buffer for needed size.
601 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
602 /* OK, got it, now return */
606 } else if (nmounts == 0) {
607 /* the buffer wasn't big enough .... */
608 /* .... get required buffer size */
613 /* some other kind of error occurred */
621 VAttachPartitions(void)
625 struct vmount *vmountp;
627 if ((nmounts = getmount(&vmountp)) <= 0) {
628 Log("Problems in getting # of mount entries(getmount)\n");
633 (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
634 char *part = vmt2dataptr(vmountp, VMT_STUB);
636 if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
637 continue; /* Ignore any "special" partitions */
640 #ifndef AFS_NAMEI_ENV
642 struct superblock fs;
643 /* The Log statements are non-sequiters in the SalvageLog and don't
644 * even appear in the VolserLog, so restrict them to the FileLog.
646 if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
647 if (programType == fileServer)
648 Log("Can't read superblock for %s, ignoring it.\n", part);
651 if (IsBigFilesFileSystem(&fs)) {
652 if (programType == fileServer)
653 Log("%s is a big files filesystem, ignoring it.\n", part);
660 /* Skip this Partition? */
661 if (VIsNeverAttach(part))
664 /* If we're going to always attach this partition, do it later. */
665 if (VIsAlwaysAttach(part, NULL))
668 if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT), 0) < 0)
672 /* Process the always-attach partitions, if any. */
673 VAttachPartitions2();
678 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
680 VAttachPartitions(void)
685 if (setfsent() < 0) {
686 Log("Error listing filesystems.\n");
690 while ((fsent = getfsent())) {
691 if (strcmp(fsent->fs_type, "rw") != 0)
694 /* Skip this Partition? */
695 if (VIsNeverAttach(fsent->fs_file))
698 /* If we're going to always attach this partition, do it later. */
699 if (VIsAlwaysAttach(fsent->fs_file, NULL))
702 if (VCheckPartition(fsent->fs_file, fsent->fs_spec, 0) < 0)
707 /* Process the always-attach partitions, if any. */
708 VAttachPartitions2();
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_st 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 /* Skip this Partition? */
881 if (VIsNeverAttach(mntent->mnt_dir))
884 /* If we're going to always attach this partition, do it later. */
885 if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
888 if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
893 /* Process the always-attach partitions, if any. */
894 VAttachPartitions2();
898 #endif /* AFS_LINUX22_ENV */
900 /* This routine is to be called whenever the actual name of the partition
901 * is required. The canonical name is still in part->name.
904 VPartitionPath(struct DiskPartition64 *part)
907 return part->devName;
913 /* get partition structure, abortp tells us if we should abort on failure */
914 struct DiskPartition64 *
915 VGetPartition_r(char *name, int abortp)
917 struct DiskPartition64 *dp;
918 #ifdef AFS_DEMAND_ATTACH_FS
919 dp = VLookupPartition_r(name);
920 #else /* AFS_DEMAND_ATTACH_FS */
921 for (dp = DiskPartitionList; dp; dp = dp->next) {
922 if (strcmp(dp->name, name) == 0)
925 #endif /* AFS_DEMAND_ATTACH_FS */
927 opr_Assert(dp != NULL);
931 struct DiskPartition64 *
932 VGetPartition(char *name, int abortp)
934 struct DiskPartition64 *retVal;
936 retVal = VGetPartition_r(name, abortp);
943 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
945 ULARGE_INTEGER free_user, total, free_total;
946 int ufree, tot, tfree;
948 if (!GetDiskFreeSpaceEx
949 (VPartitionPath(dp), &free_user, &total, &free_total)) {
950 printf("Failed to get disk space info for %s, error = %d\n", dp->name,
955 /* Convert to 1K units. */
956 ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
957 tot = (int)Int64ShraMod32(total.QuadPart, 10);
958 tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
960 dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
961 dp->totalUsable = tot;
967 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
970 afs_int64 totalblks, free, used, availblks;
973 struct afs_statvfs statbuf;
975 struct afs_statfs statbuf;
978 if (dp->flags & PART_DONTUPDATE)
980 /* Note: we don't bother syncing because it's only an estimate, update
981 * is syncing every 30 seconds anyway, we only have to keep the disk
982 * approximately 10% from full--you just can't get the stuff in from
983 * the net fast enough to worry */
985 code = afs_statvfs(dp->name, &statbuf);
987 code = afs_statfs(dp->name, &statbuf);
990 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
994 if (statbuf.f_blocks == -1) { /* Undefined; skip stats.. */
995 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
998 totalblks = statbuf.f_blocks;
999 free = statbuf.f_bfree;
1000 reserved = free - statbuf.f_bavail;
1002 bsize = statbuf.f_frsize;
1004 bsize = statbuf.f_bsize;
1006 availblks = totalblks - reserved;
1007 dp->f_files = statbuf.f_files; /* max # of files in partition */
1009 /* Now free and totalblks are in fragment units, but we want them in
1012 if (bsize >= 1024) {
1013 free *= (bsize / 1024);
1014 totalblks *= (bsize / 1024);
1015 availblks *= (bsize / 1024);
1016 reserved *= (bsize / 1024);
1018 free /= (1024 / bsize);
1019 totalblks /= (1024 / bsize);
1020 availblks /= (1024 / bsize);
1021 reserved /= (1024 / bsize);
1023 /* now compute remaining figures */
1024 used = totalblks - free;
1026 dp->minFree = reserved; /* only used in VPrintDiskStats_r */
1027 dp->totalUsable = availblks;
1028 dp->free = availblks - used; /* this is exactly f_bavail */
1030 #endif /* AFS_NT40_ENV */
1033 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
1036 VSetPartitionDiskUsage_r(dp);
1041 VResetDiskUsage_r(void)
1043 struct DiskPartition64 *dp;
1044 for (dp = DiskPartitionList; dp; dp = dp->next) {
1045 VSetPartitionDiskUsage_r(dp);
1046 #ifndef AFS_PTHREAD_ENV
1048 #endif /* !AFS_PTHREAD_ENV */
1053 VResetDiskUsage(void)
1056 VResetDiskUsage_r();
1061 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1062 afs_sfsize_t checkBlocks)
1065 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1066 * for less than BlocksSpare would skip the error-checking path, and we
1067 * could grow existing files forever, not just for another BlocksSpare
1070 #ifdef AFS_AIX32_ENV
1071 afs_int32 rem, minavail;
1073 if ((rem = vp->partition->free - checkBlocks) < (minavail =
1079 if (vp->partition->free - checkBlocks < 0)
1082 else if (V_maxquota(vp)
1083 && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1086 vp->partition->free -= blocks;
1087 V_diskused(vp) += blocks;
1091 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1092 afs_sfsize_t checkBlocks)
1095 VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1100 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1103 #ifdef AFS_AIX32_ENV
1104 afs_int32 rem, minavail;
1106 if ((rem = vp->partition->free - blocks) < (minavail =
1109 aixlow_water) / 100))
1111 if (vp->partition->free - blocks < 0)
1115 vp->partition->free -= blocks;
1120 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1124 retVal = VDiskUsage_r(vp, blocks);
1130 VPrintDiskStats_r(void)
1132 struct DiskPartition64 *dp;
1133 for (dp = DiskPartitionList; dp; dp = dp->next) {
1135 Log("Partition %s: %lld "
1136 " available 1K blocks (minfree=%lld), "
1137 "overallocated by %lld blocks\n", dp->name,
1138 dp->totalUsable, dp->minFree, -dp->free);
1140 Log("Partition %s: %lld"
1141 " available 1K blocks (minfree=%lld), "
1142 "%lld free blocks\n", dp->name,
1143 dp->totalUsable, dp->minFree, dp->free);
1149 VPrintDiskStats(void)
1152 VPrintDiskStats_r();
1157 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1158 #define LOCKFILE "LOCKFILE"
1160 VLockPartition_r(char *name)
1162 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1167 if (dp->lock_fd == INVALID_FD) {
1170 (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1172 (FD_t)CreateFile(path, GENERIC_WRITE,
1173 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1174 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1175 opr_Assert(dp->lock_fd != INVALID_FD);
1177 memset(&lap, 0, sizeof(lap));
1178 rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1185 VUnlockPartition_r(char *name)
1187 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1191 return; /* no partition, will fail later */
1192 memset(&lap, 0, sizeof(lap));
1194 UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1195 CloseHandle((HANDLE) dp->lock_fd);
1196 dp->lock_fd = INVALID_FD;
1198 #else /* AFS_NT40_ENV */
1200 #if defined(AFS_HPUX_ENV)
1201 #define BITS_PER_CHAR (8)
1202 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1204 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1205 #endif /* defined(AFS_HPUX_ENV) */
1208 VLockPartition_r(char *name)
1210 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1211 char *partitionName;
1213 struct timeval pausing;
1214 #if defined(AFS_HPUX_ENV)
1216 struct privgrp_map privGrpList[PRIV_MAXGRPS];
1217 unsigned int *globalMask;
1218 int globalMaskIndex;
1219 #endif /* defined(AFS_HPUX_ENV) */
1220 #if defined(AFS_DARWIN_ENV)
1221 char lockfile[MAXPATHLEN];
1222 #endif /* defined(AFS_DARWIN_ENV) */
1223 #ifdef AFS_NAMEI_ENV
1224 #ifdef AFS_AIX42_ENV
1225 char LockFileName[MAXPATHLEN + 1];
1227 sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1228 partitionName = (char *)&LockFileName;
1233 return; /* no partition, will fail later */
1234 if (dp->lock_fd != INVALID_FD)
1237 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1238 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1239 partitionName = dp->devName;
1242 #elif defined(AFS_DARWIN_ENV)
1243 strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1244 strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1245 code = O_RDONLY | O_CREAT;
1247 partitionName = dp->name;
1251 for (retries = 25; retries; retries--) {
1253 dp->lock_fd = afs_open(partitionName, code, 0644);
1255 dp->lock_fd = afs_open(partitionName, code);
1257 if (dp->lock_fd != INVALID_FD)
1259 if (errno == ENOENT)
1262 pausing.tv_usec = 500000;
1263 select(0, NULL, NULL, NULL, &pausing);
1265 opr_Assert(retries != 0);
1267 #if defined (AFS_HPUX_ENV)
1269 opr_Verify(getprivgrp(privGrpList) == 0);
1272 * In general, it will difficult and time-consuming ,if not impossible,
1273 * to try to find the privgroup to which this process belongs that has the
1274 * smallest membership, to minimise the security hole. So, we use the privgrp
1275 * to which everybody belongs.
1277 /* first, we have to find the global mask */
1278 for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1279 globalMaskIndex++) {
1280 if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1282 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1287 if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1288 /* allow everybody to set a lock on a read-only file descriptor */
1289 (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1290 opr_Verify(setprivgrp(PRIV_GLOBAL,
1291 privGrpList[globalMaskIndex].priv_mask) == 0);
1293 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1295 /* remove the privilege granted to everybody to lock a read-only fd */
1296 (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1297 opr_Verify(setprivgrp(PRIV_GLOBAL,
1298 privGrpList[globalMaskIndex].priv_mask) == 0);
1300 /* in this case, we should be able to do this with impunity, anyway */
1301 lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1304 opr_Assert(lockfRtn != -1);
1306 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1307 opr_Verify(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1309 opr_Verify(flock(dp->lock_fd, LOCK_EX) == 0);
1310 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1315 VUnlockPartition_r(char *name)
1317 struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1319 return; /* no partition, will fail later */
1321 dp->lock_fd = INVALID_FD;
1324 #endif /* AFS_NT40_ENV */
1327 VLockPartition(char *name)
1330 VLockPartition_r(name);
1335 VUnlockPartition(char *name)
1338 VUnlockPartition_r(name);
1342 #ifdef AFS_DEMAND_ATTACH_FS
1344 /* new-style partition locks; these are only to have some mutual exclusion
1345 * between the VGC scanner and volume utilies creating/altering vol headers
1349 * lock a partition's vol headers.
1351 * @param[in] dp the partition to lock
1352 * @param[in] locktype READ_LOCK or WRITE_LOCK
1354 * @return operation status
1358 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1362 /* block on acquiring the lock */
1365 code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1367 Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1368 VPartitionPath(dp));
1374 * unlock a partition's vol headers.
1376 * @param[in] dp the partition to unlock
1377 * @param[in] locktype READ_LOCK or WRITE_LOCK
1380 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1382 VReleaseDiskLock(&dp->headerLock, locktype);
1385 /* XXX not sure this will work on AFS_NT40_ENV
1386 * needs to be tested!
1390 * lookup a disk partition object by its index number.
1392 * @param[in] id partition index number
1393 * @param[in] abortp see abortp usage note below
1395 * @return disk partition object
1396 * @retval NULL no such disk partition
1398 * @note when abortp is non-zero, lookups which would return
1399 * NULL will result in an assertion failure
1401 * @pre VOL_LOCK must be held
1403 * @internal volume package internal use only
1406 struct DiskPartition64 *
1407 VGetPartitionById_r(afs_int32 id, int abortp)
1409 struct DiskPartition64 *dp = NULL;
1411 if ((id >= 0) && (id <= VOLMAXPARTS)) {
1412 dp = DiskPartitionTable[id];
1416 opr_Assert(dp != NULL);
1422 * lookup a disk partition object by its index number.
1424 * @param[in] id partition index number
1425 * @param[in] abortp see abortp usage note below
1427 * @return disk partition object
1428 * @retval NULL no such disk partition
1430 * @note when abortp is non-zero, lookups which would return
1431 * NULL will result in an assertion failure
1434 struct DiskPartition64 *
1435 VGetPartitionById(afs_int32 id, int abortp)
1437 struct DiskPartition64 * dp;
1440 dp = VGetPartitionById_r(id, abortp);
1446 static struct DiskPartition64 *
1447 VLookupPartition_r(char * path)
1449 afs_int32 id = volutil_GetPartitionID(path);
1451 if (id < 0 || id > VOLMAXPARTS)
1454 return DiskPartitionTable[id];
1458 AddPartitionToTable_r(struct DiskPartition64 *dp)
1460 opr_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1461 DiskPartitionTable[dp->index] = dp;
1466 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1468 opr_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1469 DiskPartitionTable[dp->index] = NULL;
1472 #endif /* AFS_DEMAND_ATTACH_FS */