vol: Log ignored dirs that look like partitions
[openafs.git] / src / vol / partition.c
index cc3fba8..d3f0a80 100644 (file)
@@ -1,10 +1,13 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
+ *
+ * Portions Copyright (c) 2003 Apple Computer, Inc.
+ * Portions Copyright (c) 2006 Sine Nomine Associates
  */
 
 /*
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
 
 #include <ctype.h>
+#include <string.h>
 #ifdef AFS_NT40_ENV
 #include <windows.h>
 #include <winbase.h>
@@ -29,8 +31,9 @@ RCSID
 #else
 #include <sys/param.h>
 #include <sys/types.h>
+#include <unistd.h>
 
-#if AFS_HAVE_STATVFS
+#if AFS_HAVE_STATVFS || AFS_HAVE_STATVFS64
 #include <sys/statvfs.h>
 #endif /* AFS_HAVE_STATVFS */
 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
@@ -104,15 +107,6 @@ RCSID
 #include <mntent.h>
 #endif
 
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
-
-
 #include <rx/xdr.h>
 #include <afs/afsint.h>
 #include "nfs.h"
@@ -121,6 +115,7 @@ RCSID
 #include "lwp.h"
 #include <afs/afssyscalls.h>
 #include "ihandle.h"
+#include "common.h"
 #ifdef AFS_NAMEI_ENV
 #ifdef AFS_NT40_ENV
 #include "ntops.h"
@@ -147,10 +142,55 @@ RCSID
 #include <jfs/filsys.h>
 #endif
 
-/*@printflike@*/ extern void Log(const char *format, ...);
+#ifdef O_LARGEFILE
+
+#define afs_stat       stat64
+#define afs_open       open64
+#define afs_fopen      fopen64
+#ifndef AFS_NT40_ENV
+#if AFS_HAVE_STATVFS64
+# define afs_statvfs   statvfs64
+#else
+# if AFS_HAVE_STATFS64
+#  define afs_statfs   statfs64
+#else
+#  if AFS_HAVE_STATVFS
+#   define afs_statvfs statvfs
+#  else
+#   define afs_statfs  statfs
+#  endif /* !AFS_HAVE_STATVFS */
+# endif        /* !AFS_HAVE_STATFS64 */
+#endif /* !AFS_HAVE_STATVFS64 */
+#endif /* !AFS_NT40_ENV */
+
+#else /* !O_LARGEFILE */
+
+#define afs_stat       stat
+#define afs_open       open
+#define afs_fopen      fopen
+#ifndef AFS_NT40_ENV
+#if AFS_HAVE_STATVFS
+#define afs_statvfs    statvfs
+#else /* !AFS_HAVE_STATVFS */
+#define afs_statfs     statfs
+#endif /* !AFS_HAVE_STATVFS */
+#endif /* !AFS_NT40_ENV */
+
+#endif /* !O_LARGEFILE */
 
 int aixlow_water = 8;          /* default 8% */
-struct DiskPartition *DiskPartitionList;
+struct DiskPartition64 *DiskPartitionList;
+
+#ifdef AFS_DEMAND_ATTACH_FS
+/* file to lock to conceptually "lock" the vol headers on a partition */
+#define AFS_PARTLOCK_FILE ".volheaders.lock"
+#define AFS_VOLUMELOCK_FILE ".volume.lock"
+
+static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
+
+static struct DiskPartition64 * VLookupPartition_r(char * path);
+static void AddPartitionToTable_r(struct DiskPartition64 *);
+#endif /* AFS_DEMAND_ATTACH_FS */
 
 #ifdef AFS_SGI_XFS_IOPS_ENV
 /* Verify that the on disk XFS inodes on the partition are large enough to
@@ -188,14 +228,23 @@ VerifyXFSInodeSize(char *part, char *fstype)
     }
     return code;
 }
-#endif
+#endif /* AFS_SGI_XFS_IOPS_ENV */
 
+int
+VInitPartitionPackage(void)
+{
+#ifdef AFS_DEMAND_ATTACH_ENV
+    memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
+#endif /* AFS_DEMAND_ATTACH_ENV */
+    return 0;
+}
 
 static void
 VInitPartition_r(char *path, char *devname, Device dev)
 {
-    struct DiskPartition *dp, *op;
-    dp = (struct DiskPartition *)malloc(sizeof(struct DiskPartition));
+    struct DiskPartition64 *dp, *op;
+
+    dp = (struct DiskPartition64 *)malloc(sizeof(struct DiskPartition64));
     /* Add it to the end, to preserve order when we print statistics */
     for (op = DiskPartitionList; op; op = op->next) {
        if (!op->next)
@@ -208,6 +257,7 @@ VInitPartition_r(char *path, char *devname, Device dev)
     dp->next = 0;
     dp->name = (char *)malloc(strlen(path) + 1);
     strncpy(dp->name, path, strlen(path) + 1);
+    dp->index = volutil_GetPartitionID(path);
 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
     /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
     dp->devName = (char *)malloc(2 * strlen(path) + 6);
@@ -216,14 +266,14 @@ VInitPartition_r(char *path, char *devname, Device dev)
     strcat(dp->devName, "Lock");
     mkdir(dp->devName, 0700);
     strcat(dp->devName, path);
-    close(open(dp->devName, O_RDWR | O_CREAT, 0600));
-    dp->device = volutil_GetPartitionID(path);
+    close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
+    dp->device = dp->index;
 #else
     dp->devName = (char *)malloc(strlen(devname) + 1);
     strncpy(dp->devName, devname, strlen(devname) + 1);
     dp->device = dev;
 #endif
-    dp->lock_fd = -1;
+    dp->lock_fd = INVALID_FD;
     dp->flags = 0;
     dp->f_files = 1;           /* just a default value */
 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
@@ -231,13 +281,33 @@ VInitPartition_r(char *path, char *devname, Device dev)
        (void)namei_ViceREADME(VPartitionPath(dp));
 #endif
     VSetPartitionDiskUsage_r(dp);
+#ifdef AFS_DEMAND_ATTACH_FS
+    AddPartitionToTable_r(dp);
+    queue_Init(&dp->vol_list.head);
+    assert(pthread_cond_init(&dp->vol_list.cv, NULL) == 0);
+    dp->vol_list.len = 0;
+    dp->vol_list.busy = 0;
+    {
+       char lockpath[MAXPATHLEN+1];
+       afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
+       lockpath[MAXPATHLEN] = '\0';
+       VLockFileInit(&dp->headerLockFile, lockpath);
+
+       afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
+       lockpath[MAXPATHLEN] = '\0';
+       VLockFileInit(&dp->volLockFile, lockpath);
+    }
+    VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
+#endif /* AFS_DEMAND_ATTACH_FS */
 }
 
 static void
 VInitPartition(char *path, char *devname, Device dev)
 {
-    VOL_LOCK VInitPartition_r(path, devname, dev);
-VOL_UNLOCK}
+    VOL_LOCK;
+    VInitPartition_r(path, devname, dev);
+    VOL_UNLOCK;
+}
 
 #ifndef AFS_NT40_ENV
 /* VAttachPartitions() finds the vice partitions on this server. Calls
@@ -257,7 +327,7 @@ VOL_UNLOCK}
 int
 VCheckPartition(char *part, char *devname)
 {
-    struct stat status;
+    struct afs_stat status;
 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
     char AFSIDatPath[MAXPATHLEN];
 #endif
@@ -267,7 +337,7 @@ VCheckPartition(char *part, char *devname)
     if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
        return 0;
     }
-    if (stat(part, &status) < 0) {
+    if (afs_stat(part, &status) < 0) {
        Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
        return 0;
     }
@@ -276,7 +346,7 @@ VCheckPartition(char *part, char *devname)
        char salvpath[MAXPATHLEN];
        strcpy(salvpath, part);
        strcat(salvpath, "/FORCESALVAGE");
-       if (stat(salvpath, &status) == 0) {
+       if (afs_stat(salvpath, &status) == 0) {
            Log("VInitVnodes: Found %s; aborting\n", salvpath);
            return -1;
        }
@@ -287,13 +357,13 @@ VCheckPartition(char *part, char *devname)
     strcpy(AFSIDatPath, part);
     strcat(AFSIDatPath, "/AFSIDat");
 #ifdef AFS_NAMEI_ENV
-    if (stat(AFSIDatPath, &status) < 0) {
+    if (afs_stat(AFSIDatPath, &status) < 0) {
        DIR *dirp;
        struct dirent *dp;
 
        dirp = opendir(part);
        assert(dirp);
-       while (dp = readdir(dirp)) {
+       while ((dp = readdir(dirp))) {
            if (dp->d_name[0] == 'V') {
                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);
                closedir(dirp);
@@ -303,17 +373,17 @@ VCheckPartition(char *part, char *devname)
        closedir(dirp);
     }
 #else /* AFS_NAMEI_ENV */
-    if (stat(AFSIDatPath, &status) == 0) {
+    if (afs_stat(AFSIDatPath, &status) == 0) {
        Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
        return -1;
     }
-#endif /* AFS_NAMEI_ENV */
-#endif
 
 #ifdef AFS_SGI_XFS_IOPS_ENV
     if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
        return -1;
 #endif
+#endif /* AFS_NAMEI_ENV */
+#endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
 
 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
     if (status.st_ino != ROOTINO) {
@@ -331,23 +401,39 @@ VCheckPartition(char *part, char *devname)
  * attached (return value 1), or only attached when it is a separately
  * mounted partition (return value 0).  For non-NAMEI environments, it
  * always returns 0.
+ *
+ * *awouldattach will be set to 1 if the given path at least looks like a vice
+ * partition (that is, if we return 0, the only thing preventing this partition
+ * from being attached is the existence of the AlwaysAttach file), or to 0
+ * otherwise. *awouldattach is set regardless of whether or not the partition
+ * should always be attached or not.
  */
 static int
-VIsAlwaysAttach(char *part)
+VIsAlwaysAttach(char *part, int *awouldattach)
 {
 #ifdef AFS_NAMEI_ENV
-    struct stat st;
+    struct afs_stat st;
     char checkfile[256];
     int ret;
+#endif /* AFS_NAMEI_ENV */
+
+    if (awouldattach) {
+       *awouldattach = 0;
+    }
 
+#ifdef AFS_NAMEI_ENV
     if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
        return 0;
 
+    if (awouldattach) {
+       *awouldattach = 1;
+    }
+
     strncpy(checkfile, part, 100);
     strcat(checkfile, "/");
     strcat(checkfile, VICE_ALWAYSATTACH_FILE);
 
-    ret = stat(checkfile, &st);
+    ret = afs_stat(checkfile, &st);
     return (ret < 0) ? 0 : 1;
 #else /* AFS_NAMEI_ENV */
     return 0;
@@ -360,12 +446,13 @@ VIsAlwaysAttach(char *part)
  * partitions, in the NAMEI fileserver.
  */
 void
-VAttachPartitions2()
+VAttachPartitions2(void)
 {
 #ifdef AFS_NAMEI_ENV
     DIR *dirp;
     struct dirent *de;
     char pname[32];
+    int wouldattach;
 
     dirp = opendir("/");
     while ((de = readdir(dirp))) {
@@ -375,8 +462,31 @@ VAttachPartitions2()
 
        /* Only keep track of "/vicepx" partitions since automounter
         * may hose us */
-       if (VIsAlwaysAttach(pname))
+       if (VIsAlwaysAttach(pname, &wouldattach)) {
            VCheckPartition(pname, "");
+       } else {
+           struct afs_stat st;
+           if (wouldattach && VGetPartition(pname, 0) == NULL &&
+               afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
+
+               /* This is a /vicep* dir, and it has not been attached as a
+                * partition. This probably means that this is a /vicep* dir
+                * that is not a separate partition, so just give a notice so
+                * admins are not confused as to why their /vicep* dirs are not
+                * being attached.
+                *
+                * It is possible that the dir _is_ a separate partition and we
+                * failed to attach it earlier, making this message a bit
+                * confusing. But that should be rare, and an error message
+                * about the failure will already be logged right before this,
+                * so it should be clear enough. */
+
+               Log("VAttachPartitions: not attaching %s; either it is not a "
+                   "separate partition, or it failed to attach (create the "
+                   "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
+                   pname, pname);
+           }
+       }
     }
     closedir(dirp);
 #endif /* AFS_NAMEI_ENV */
@@ -391,21 +501,40 @@ VAttachPartitions(void)
     struct mnttab mnt;
     FILE *mntfile;
 
-    if (!(mntfile = fopen(MNTTAB, "r"))) {
+    if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
        Log("Can't open %s\n", MNTTAB);
        perror(MNTTAB);
        exit(-1);
     }
     while (!getmntent(mntfile, &mnt)) {
        /* Ignore non ufs or non read/write partitions */
-       if ((strcmp(mnt.mnt_fstype, "ufs") != 0)
+       /* but allow zfs too if we're in the NAMEI environment */
+       if (
+#ifdef AFS_NAMEI_ENV
+           (((strcmp(mnt.mnt_fstype, "ufs") &&
+               strcmp(mnt.mnt_fstype, "zfs"))))
+#else
+           (strcmp(mnt.mnt_fstype, "ufs") != 0)
+#endif
            || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
            continue;
 
        /* If we're going to always attach this partition, do it later. */
-       if (VIsAlwaysAttach(mnt.mnt_mountp))
+       if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
            continue;
 
+#ifndef AFS_NAMEI_ENV
+       if (hasmntopt(&mnt, "logging") != NULL) {
+           Log("This program is compiled without AFS_NAMEI_ENV, and "
+               "partition %s is mounted with the 'logging' option. "
+               "Using the inode fileserver backend with 'logging' UFS "
+               "partitions causes volume corruption, so please either "
+               "mount the partition without logging, or use the namei "
+               "fileserver backend. Aborting...\n", mnt.mnt_mountp);
+           errors++;
+       }
+#endif /* !AFS_NAMEI_ENV */
+
        if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0)
            errors++;
     }
@@ -436,7 +565,7 @@ VAttachPartitions(void)
            continue;
 
        /* If we're going to always attach this partition, do it later. */
-       if (VIsAlwaysAttach(mntent->mnt_dir))
+       if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
            continue;
 
        if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
@@ -456,10 +585,10 @@ VAttachPartitions(void)
  * (This function was grabbed from df.c)
  */
 int
-getmount(register struct vmount **vmountpp)
+getmount(struct vmount **vmountpp)
 {
     int size;
-    register struct vmount *vm;
+    struct vmount *vm;
     int nmounts;
 
     /* set initial size of mntctl buffer to a MAGIC NUMBER */
@@ -539,7 +668,7 @@ VAttachPartitions(void)
 #endif
 
        /* If we're going to always attach this partition, do it later. */
-       if (VIsAlwaysAttach(part))
+       if (VIsAlwaysAttach(part, NULL))
            continue;
 
        if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0)
@@ -564,12 +693,12 @@ VAttachPartitions(void)
        exit(-1);
     }
 
-    while (fsent = getfsent()) {
+    while ((fsent = getfsent())) {
        if (strcmp(fsent->fs_type, "rw") != 0)
            continue;
 
        /* If we're going to always attach this partition, do it later. */
-       if (VIsAlwaysAttach(fsent->fs_file))
+       if (VIsAlwaysAttach(fsent->fs_file, NULL))
            continue;
 
        if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0)
@@ -671,7 +800,7 @@ VCheckPartition(char *partName)
 int
 VAttachPartitions(void)
 {
-    struct DiskPartition *partP, *prevP, *nextP;
+    struct DiskPartition64 *partP, *prevP, *nextP;
     struct vpt_iter iter;
     struct vptab entry;
 
@@ -706,11 +835,11 @@ VAttachPartitions(void)
         * doing this for us.
         */
        if (programType == fileServer) {
-           struct stat status;
+           struct afs_stat status;
            char salvpath[MAXPATHLEN];
            strcpy(salvpath, entry.vp_dev);
            strcat(salvpath, "\\FORCESALVAGE");
-           if (stat(salvpath, &status) == 0) {
+           if (afs_stat(salvpath, &status) == 0) {
                Log("VAttachPartitions: Found %s; aborting\n", salvpath);
                exit(1);
            }
@@ -753,7 +882,7 @@ VAttachPartitions(void)
     }
     while ((mntent = getmntent(mfd))) {
        /* If we're going to always attach this partition, do it later. */
-       if (VIsAlwaysAttach(mntent->mnt_dir))
+       if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
            continue;
 
        if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
@@ -772,7 +901,7 @@ VAttachPartitions(void)
  * is required. The canonical name is still in part->name.
  */
 char *
-VPartitionPath(struct DiskPartition *part)
+VPartitionPath(struct DiskPartition64 *part)
 {
 #ifdef AFS_NT40_ENV
     return part->devName;
@@ -782,30 +911,36 @@ VPartitionPath(struct DiskPartition *part)
 }
 
 /* get partition structure, abortp tells us if we should abort on failure */
-struct DiskPartition *
+struct DiskPartition64 *
 VGetPartition_r(char *name, int abortp)
 {
-    register struct DiskPartition *dp;
+    struct DiskPartition64 *dp;
+#ifdef AFS_DEMAND_ATTACH_FS
+    dp = VLookupPartition_r(name);
+#else /* AFS_DEMAND_ATTACH_FS */
     for (dp = DiskPartitionList; dp; dp = dp->next) {
        if (strcmp(dp->name, name) == 0)
            break;
     }
+#endif /* AFS_DEMAND_ATTACH_FS */
     if (abortp)
        assert(dp != NULL);
     return dp;
 }
 
-struct DiskPartition *
+struct DiskPartition64 *
 VGetPartition(char *name, int abortp)
 {
-    struct DiskPartition *retVal;
-    VOL_LOCK retVal = VGetPartition_r(name, abortp);
-    VOL_UNLOCK return retVal;
+    struct DiskPartition64 *retVal;
+    VOL_LOCK;
+    retVal = VGetPartition_r(name, abortp);
+    VOL_UNLOCK;
+    return retVal;
 }
 
 #ifdef AFS_NT40_ENV
 void
-VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
+VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
 {
     ULARGE_INTEGER free_user, total, free_total;
     int ufree, tot, tfree;
@@ -829,14 +964,15 @@ VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
 
 #else
 void
-VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
+VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
 {
-    int fd, totalblks, free, used, availblks, bsize, code;
+    int bsize, code;
+    afs_int64 totalblks, free, used, availblks;
     int reserved;
-#if AFS_HAVE_STATVFS
-    struct statvfs statbuf;
+#ifdef afs_statvfs
+    struct afs_statvfs statbuf;
 #else
-    struct statfs statbuf;
+    struct afs_statfs statbuf;
 #endif
 
     if (dp->flags & PART_DONTUPDATE)
@@ -845,10 +981,10 @@ VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
      * is syncing every 30 seconds anyway, we only have to keep the disk
      * approximately 10% from full--you just can't get the stuff in from
      * the net fast enough to worry */
-#if AFS_HAVE_STATVFS
-    code = statvfs(dp->name, &statbuf);
+#ifdef afs_statvfs
+    code = afs_statvfs(dp->name, &statbuf);
 #else
-    code = statfs(dp->name, &statbuf);
+    code = afs_statfs(dp->name, &statbuf);
 #endif
     if (code < 0) {
        Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
@@ -862,7 +998,7 @@ VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
     totalblks = statbuf.f_blocks;
     free = statbuf.f_bfree;
     reserved = free - statbuf.f_bavail;
-#if AFS_HAVE_STATVFS
+#ifdef afs_statvfs
     bsize = statbuf.f_frsize;
 #else
     bsize = statbuf.f_bsize;
@@ -894,15 +1030,17 @@ VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
 #endif /* AFS_NT40_ENV */
 
 void
-VSetPartitionDiskUsage(register struct DiskPartition *dp)
+VSetPartitionDiskUsage(struct DiskPartition64 *dp)
 {
-    VOL_LOCK VSetPartitionDiskUsage_r(dp);
-VOL_UNLOCK}
+    VOL_LOCK;
+    VSetPartitionDiskUsage_r(dp);
+    VOL_UNLOCK;
+}
 
 void
 VResetDiskUsage_r(void)
 {
-    struct DiskPartition *dp;
+    struct DiskPartition64 *dp;
     for (dp = DiskPartitionList; dp; dp = dp->next) {
        VSetPartitionDiskUsage_r(dp);
 #ifndef AFS_PTHREAD_ENV
@@ -914,12 +1052,14 @@ VResetDiskUsage_r(void)
 void
 VResetDiskUsage(void)
 {
-    VOL_LOCK VResetDiskUsage_r();
-VOL_UNLOCK}
+    VOL_LOCK;
+    VResetDiskUsage_r();
+    VOL_UNLOCK;
+}
 
 void
-VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_int32 blocks,
-                  afs_int32 checkBlocks)
+VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
+                  afs_sfsize_t checkBlocks)
 {
     *ec = 0;
     /* why blocks instead of checkBlocks in the check below?  Otherwise, any check
@@ -948,14 +1088,16 @@ VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_int32 blocks,
 }
 
 void
-VAdjustDiskUsage(Error * ec, Volume * vp, afs_int32 blocks,
-                afs_int32 checkBlocks)
+VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
+                afs_sfsize_t checkBlocks)
 {
-    VOL_LOCK VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
-VOL_UNLOCK}
+    VOL_LOCK;
+    VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
+    VOL_UNLOCK;
+}
 
 int
-VDiskUsage_r(Volume * vp, afs_int32 blocks)
+VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
 {
     if (blocks > 0) {
 #ifdef AFS_AIX32_ENV
@@ -975,32 +1117,41 @@ VDiskUsage_r(Volume * vp, afs_int32 blocks)
 }
 
 int
-VDiskUsage(Volume * vp, afs_int32 blocks)
+VDiskUsage(Volume * vp, afs_sfsize_t blocks)
 {
     int retVal;
-    VOL_LOCK retVal = VDiskUsage_r(vp, blocks);
-    VOL_UNLOCK return retVal;
+    VOL_LOCK;
+    retVal = VDiskUsage_r(vp, blocks);
+    VOL_UNLOCK;
+    return retVal;
 }
 
 void
 VPrintDiskStats_r(void)
 {
-    struct DiskPartition *dp;
+    struct DiskPartition64 *dp;
     for (dp = DiskPartitionList; dp; dp = dp->next) {
-       Log("Partition %s: %d available 1K blocks (minfree=%d), ", dp->name,
-           dp->totalUsable, dp->minFree);
-       if (dp->free < 0)
-           Log("overallocated by %d blocks\n", -dp->free);
-       else
-           Log("%d free blocks\n", dp->free);
+       if (dp->free < 0) {
+           Log("Partition %s: %"AFS_INT64_FMT
+               " available 1K blocks (minfree=%"AFS_INT64_FMT"), "
+               "overallocated by %"AFS_INT64_FMT" blocks\n", dp->name,
+               dp->totalUsable, dp->minFree, -dp->free);
+       } else {
+           Log("Partition %s: %"AFS_INT64_FMT
+               " available 1K blocks (minfree=%"AFS_INT64_FMT"), "
+               "%"AFS_INT64_FMT" free blocks\n", dp->name,
+               dp->totalUsable, dp->minFree, dp->free);
+       }
     }
 }
 
 void
 VPrintDiskStats(void)
 {
-    VOL_LOCK VPrintDiskStats_r();
-VOL_UNLOCK}
+    VOL_LOCK;
+    VPrintDiskStats_r();
+    VOL_UNLOCK;
+}
 
 #ifdef AFS_NT40_ENV
 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
@@ -1008,22 +1159,22 @@ VOL_UNLOCK}
 void
 VLockPartition_r(char *name)
 {
-    struct DiskPartition *dp = VGetPartition_r(name, 0);
+    struct DiskPartition64 *dp = VGetPartition_r(name, 0);
     OVERLAPPED lap;
 
     if (!dp)
        return;
-    if (dp->lock_fd == -1) {
+    if (dp->lock_fd == INVALID_FD) {
        char path[64];
        int rc;
        (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
        dp->lock_fd =
-           (int)CreateFile(path, GENERIC_WRITE,
+           (FD_t)CreateFile(path, GENERIC_WRITE,
                            FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                            CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
-       assert(dp->lock_fd != (int)INVALID_HANDLE_VALUE);
+       assert(dp->lock_fd != INVALID_FD);
 
-       memset((char *)&lap, 0, sizeof(lap));
+       memset(&lap, 0, sizeof(lap));
        rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
                        0, &lap);
        assert(rc);
@@ -1033,16 +1184,16 @@ VLockPartition_r(char *name)
 void
 VUnlockPartition_r(char *name)
 {
-    register struct DiskPartition *dp = VGetPartition_r(name, 0);
+    struct DiskPartition64 *dp = VGetPartition_r(name, 0);
     OVERLAPPED lap;
 
     if (!dp)
        return;                 /* no partition, will fail later */
-    memset((char *)&lap, 0, sizeof(lap));
+    memset(&lap, 0, sizeof(lap));
 
     UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
     CloseHandle((HANDLE) dp->lock_fd);
-    dp->lock_fd = -1;
+    dp->lock_fd = INVALID_FD;
 }
 #else /* AFS_NT40_ENV */
 
@@ -1056,7 +1207,7 @@ VUnlockPartition_r(char *name)
 void
 VLockPartition_r(char *name)
 {
-    register struct DiskPartition *dp = VGetPartition_r(name, 0);
+    struct DiskPartition64 *dp = VGetPartition_r(name, 0);
     char *partitionName;
     int retries, code;
     struct timeval pausing;
@@ -1066,6 +1217,9 @@ VLockPartition_r(char *name)
     unsigned int *globalMask;
     int globalMaskIndex;
 #endif /* defined(AFS_HPUX_ENV) */
+#if defined(AFS_DARWIN_ENV)
+    char lockfile[MAXPATHLEN];
+#endif /* defined(AFS_DARWIN_ENV) */
 #ifdef AFS_NAMEI_ENV
 #ifdef AFS_AIX42_ENV
     char LockFileName[MAXPATHLEN + 1];
@@ -1085,13 +1239,21 @@ VLockPartition_r(char *name)
     partitionName = dp->devName;
 #endif
     code = O_RDWR;
+#elif defined(AFS_DARWIN_ENV)
+    strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
+    strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
+    code = O_RDONLY | O_CREAT;
 #else
     partitionName = dp->name;
     code = O_RDONLY;
 #endif
 
     for (retries = 25; retries; retries--) {
-       dp->lock_fd = open(partitionName, code);
+       if (code & O_CREAT)
+           dp->lock_fd = afs_open(partitionName, code, 0644);
+       else
+           dp->lock_fd = afs_open(partitionName, code);
+
        if (dp->lock_fd != -1)
            break;
        if (errno == ENOENT)
@@ -1152,7 +1314,7 @@ VLockPartition_r(char *name)
 void
 VUnlockPartition_r(char *name)
 {
-    register struct DiskPartition *dp = VGetPartition_r(name, 0);
+    struct DiskPartition64 *dp = VGetPartition_r(name, 0);
     if (!dp)
        return;                 /* no partition, will fail later */
     close(dp->lock_fd);
@@ -1164,11 +1326,147 @@ VUnlockPartition_r(char *name)
 void
 VLockPartition(char *name)
 {
-    VOL_LOCK VLockPartition_r(name);
-VOL_UNLOCK}
+    VOL_LOCK;
+    VLockPartition_r(name);
+    VOL_UNLOCK;
+}
 
 void
 VUnlockPartition(char *name)
 {
-    VOL_LOCK VUnlockPartition_r(name);
-VOL_UNLOCK}
+    VOL_LOCK;
+    VUnlockPartition_r(name);
+    VOL_UNLOCK;
+}
+
+#ifdef AFS_DEMAND_ATTACH_FS
+
+/* new-style partition locks; these are only to have some mutual exclusion
+ * between the VGC scanner and volume utilies creating/altering vol headers
+ */
+
+/**
+ * lock a partition's vol headers.
+ *
+ * @param[in] dp       the partition to lock
+ * @param[in] locktype READ_LOCK or WRITE_LOCK
+ *
+ * @return operation status
+ *  @retval 0 success
+ */
+int
+VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
+{
+    int code;
+
+    /* block on acquiring the lock */
+    int nonblock = 0;
+
+    code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
+    if (code) {
+       Log("VPartHeaderLock: error %d locking partititon %s\n", code,
+           VPartitionPath(dp));
+    }
+    return code;
+}
+
+/**
+ * unlock a partition's vol headers.
+ *
+ * @param[in] dp       the partition to unlock
+ * @param[in] locktype READ_LOCK or WRITE_LOCK
+ */
+void
+VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
+{
+    VReleaseDiskLock(&dp->headerLock, locktype);
+}
+
+/* XXX not sure this will work on AFS_NT40_ENV
+ * needs to be tested!
+ */
+
+/**
+ * lookup a disk partition object by its index number.
+ *
+ * @param[in] id      partition index number
+ * @param[in] abortp  see abortp usage note below
+ *
+ * @return disk partition object
+ *   @retval NULL no such disk partition
+ *
+ * @note when abortp is non-zero, lookups which would return
+ *       NULL will result in an assertion failure
+ *
+ * @pre VOL_LOCK must be held
+ *
+ * @internal volume package internal use only
+ */
+
+struct DiskPartition64 *
+VGetPartitionById_r(afs_int32 id, int abortp)
+{
+    struct DiskPartition64 *dp = NULL;
+
+    if ((id >= 0) && (id <= VOLMAXPARTS)) {
+       dp = DiskPartitionTable[id];
+    }
+
+    if (abortp) {
+       assert(dp != NULL);
+    }
+    return dp;
+}
+
+/**
+ * lookup a disk partition object by its index number.
+ *
+ * @param[in] id      partition index number
+ * @param[in] abortp  see abortp usage note below
+ *
+ * @return disk partition object
+ *   @retval NULL no such disk partition
+ *
+ * @note when abortp is non-zero, lookups which would return
+ *       NULL will result in an assertion failure
+ */
+
+struct DiskPartition64 *
+VGetPartitionById(afs_int32 id, int abortp)
+{
+    struct DiskPartition64 * dp;
+
+    VOL_LOCK;
+    dp = VGetPartitionById_r(id, abortp);
+    VOL_UNLOCK;
+
+    return dp;
+}
+
+static struct DiskPartition64 *
+VLookupPartition_r(char * path)
+{
+    afs_int32 id = volutil_GetPartitionID(path);
+
+    if (id < 0 || id > VOLMAXPARTS)
+       return NULL;
+
+    return DiskPartitionTable[id];
+}
+
+static void
+AddPartitionToTable_r(struct DiskPartition64 *dp)
+{
+    assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
+    DiskPartitionTable[dp->index] = dp;
+}
+
+#if 0
+static void
+DeletePartitionFromTable_r(struct DiskPartition64 *dp)
+{
+    assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
+    DiskPartitionTable[dp->index] = NULL;
+}
+#endif
+#endif /* AFS_DEMAND_ATTACH_FS */