DAFS: fix ifdef
[openafs.git] / src / vol / partition.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  *
9  * Portions Copyright (c) 2003 Apple Computer, Inc.
10  * Portions Copyright (c) 2006 Sine Nomine Associates
11  */
12
13 /*
14
15         System:         VICE-TWO
16         Module:         partition.c
17         Institution:    The Information Technology Center, Carnegie-Mellon University
18
19  */
20
21 #include <afsconfig.h>
22 #include <afs/param.h>
23
24 #include <roken.h>
25
26 #include <ctype.h>
27 #include <string.h>
28 #ifdef AFS_NT40_ENV
29 #include <windows.h>
30 #include <winbase.h>
31 #include <winioctl.h>
32 #else
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #if AFS_HAVE_STATVFS || AFS_HAVE_STATVFS64
38 #include <sys/statvfs.h>
39 #endif /* AFS_HAVE_STATVFS */
40 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
41 #include <sys/mount.h>
42 #endif
43
44 #if !defined(AFS_SGI_ENV)
45 #ifdef  AFS_OSF_ENV
46 #include <sys/mount.h>
47 #include <ufs/fs.h>
48 #else /* AFS_OSF_ENV */
49 #ifdef AFS_VFSINCL_ENV
50 #define VFS
51 #ifdef  AFS_SUN5_ENV
52 #include <sys/fs/ufs_fs.h>
53 #else
54 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
55 #include <ufs/ufs/dinode.h>
56 #include <ufs/ffs/fs.h>
57 #else
58 #include <ufs/fs.h>
59 #endif
60 #endif
61 #else /* AFS_VFSINCL_ENV */
62 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
63 #include <sys/fs.h>
64 #endif
65 #endif /* AFS_VFSINCL_ENV */
66 #endif /* AFS_OSF_ENV */
67 #include <errno.h>
68 #include <sys/stat.h>
69 #include <stdio.h>
70 #include <sys/file.h>
71 #ifdef  AFS_AIX_ENV
72 #include <sys/vfs.h>
73 #include <sys/lockf.h>
74 #else
75 #ifdef  AFS_HPUX_ENV
76 #include <sys/vfs.h>
77 #include <unistd.h>
78 #include <fcntl.h>
79 #include <checklist.h>
80 #else
81 #if     defined(AFS_SUN_ENV)
82 #include <sys/vfs.h>
83 #ifndef AFS_SUN5_ENV
84 #include <mntent.h>
85 #endif
86 #endif
87 #ifdef AFS_SUN5_ENV
88 #include <unistd.h>
89 #include <sys/mnttab.h>
90 #include <sys/mntent.h>
91 #else
92 #ifdef AFS_LINUX22_ENV
93 #include <mntent.h>
94 #include <sys/statfs.h>
95 #else
96 #include <fstab.h>
97 #endif
98 #endif
99 #endif
100 #endif
101 #endif /* AFS_SGI_ENV */
102 #endif /* AFS_NT40_ENV */
103 #if defined(AFS_SGI_ENV)
104 #include <sys/errno.h>
105 #include <sys/stat.h>
106 #include <stdio.h>
107 #include <sys/file.h>
108 #include <mntent.h>
109 #endif
110
111 #include <rx/xdr.h>
112 #include <afs/afsint.h>
113 #include "nfs.h"
114 #include <afs/errors.h>
115 #include "lock.h"
116 #include "lwp.h"
117 #include <afs/afssyscalls.h>
118 #include "ihandle.h"
119 #include "common.h"
120 #ifdef AFS_NAMEI_ENV
121 #ifdef AFS_NT40_ENV
122 #include "ntops.h"
123 #else
124 #include "namei_ops.h"
125 #include <dirent.h>
126 #endif /* AFS_NT40_ENV */
127 #endif /* AFS_NAMEI_ENV */
128 #include "vnode.h"
129 #include "volume.h"
130 #include "partition.h"
131 #include <afs/afs_assert.h>
132
133 #if defined(AFS_HPUX_ENV)
134 #include <sys/types.h>
135 #include <sys/privgrp.h>
136 #endif /* defined(AFS_HPUX_ENV) */
137
138 #ifdef AFS_AIX42_ENV
139 #include <jfs/filsys.h>
140 #endif
141
142 #ifdef O_LARGEFILE
143
144 #define afs_stat        stat64
145 #define afs_open        open64
146 #define afs_fopen       fopen64
147 #ifndef AFS_NT40_ENV
148 #if AFS_HAVE_STATVFS64
149 # define afs_statvfs    statvfs64
150 #else
151 # if AFS_HAVE_STATFS64
152 #  define afs_statfs    statfs64
153 #else
154 #  if AFS_HAVE_STATVFS
155 #   define afs_statvfs  statvfs
156 #  else
157 #   define afs_statfs   statfs
158 #  endif /* !AFS_HAVE_STATVFS */
159 # endif /* !AFS_HAVE_STATFS64 */
160 #endif /* !AFS_HAVE_STATVFS64 */
161 #endif /* !AFS_NT40_ENV */
162
163 #else /* !O_LARGEFILE */
164
165 #define afs_stat        stat
166 #define afs_open        open
167 #define afs_fopen       fopen
168 #ifndef AFS_NT40_ENV
169 #if AFS_HAVE_STATVFS
170 #define afs_statvfs     statvfs
171 #else /* !AFS_HAVE_STATVFS */
172 #define afs_statfs      statfs
173 #endif /* !AFS_HAVE_STATVFS */
174 #endif /* !AFS_NT40_ENV */
175
176 #endif /* !O_LARGEFILE */
177
178 int aixlow_water = 8;           /* default 8% */
179 struct DiskPartition64 *DiskPartitionList;
180
181 #ifdef AFS_DEMAND_ATTACH_FS
182 /* file to lock to conceptually "lock" the vol headers on a partition */
183 #define AFS_PARTLOCK_FILE ".volheaders.lock"
184 #define AFS_VOLUMELOCK_FILE ".volume.lock"
185
186 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
187
188 static struct DiskPartition64 * VLookupPartition_r(char * path);
189 static void AddPartitionToTable_r(struct DiskPartition64 *);
190 #endif /* AFS_DEMAND_ATTACH_FS */
191
192 #ifdef AFS_SGI_XFS_IOPS_ENV
193 /* Verify that the on disk XFS inodes on the partition are large enough to
194  * hold the AFS attribute. Returns -1 if the attribute can't be set or is
195  * too small to fit in the inode. Returns 0 if the attribute does fit in
196  * the XFS inode.
197  */
198 #include <afs/xfsattrs.h>
199 static int
200 VerifyXFSInodeSize(char *part, char *fstype)
201 {
202     afs_xfs_attr_t junk;
203     int length = SIZEOF_XFS_ATTR_T;
204     int fd = 0;
205     int code = -1;
206     struct fsxattr fsx;
207
208     if (strcmp("xfs", fstype))
209         return 0;
210
211     if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
212         if (((fd = open(part, O_RDONLY, 0)) != -1)
213             && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
214
215             if (fsx.fsx_nextents) {
216                 Log("Partition %s: XFS inodes too small, exiting.\n", part);
217                 Log("Run xfs_size_check utility and remake partitions.\n");
218             } else
219                 code = 0;
220         }
221
222         if (fd > 0)
223             close(fd);
224         (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
225     }
226     return code;
227 }
228 #endif /* AFS_SGI_XFS_IOPS_ENV */
229
230 int
231 VInitPartitionPackage(void)
232 {
233 #ifdef AFS_DEMAND_ATTACH_FS
234     memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
235 #endif /* AFS_DEMAND_ATTACH_FS */
236     return 0;
237 }
238
239 static void
240 VInitPartition_r(char *path, char *devname, Device dev)
241 {
242     struct DiskPartition64 *dp, *op;
243
244     dp = (struct DiskPartition64 *)malloc(sizeof(struct DiskPartition64));
245     /* Add it to the end, to preserve order when we print statistics */
246     for (op = DiskPartitionList; op; op = op->next) {
247         if (!op->next)
248             break;
249     }
250     if (op)
251         op->next = dp;
252     else
253         DiskPartitionList = dp;
254     dp->next = 0;
255     dp->name = (char *)malloc(strlen(path) + 1);
256     strncpy(dp->name, path, strlen(path) + 1);
257     dp->index = volutil_GetPartitionID(path);
258 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
259     /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
260     dp->devName = (char *)malloc(2 * strlen(path) + 6);
261     strcpy(dp->devName, path);
262     strcat(dp->devName, "/");
263     strcat(dp->devName, "Lock");
264     mkdir(dp->devName, 0700);
265     strcat(dp->devName, path);
266     close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
267     dp->device = dp->index;
268 #else
269     dp->devName = (char *)malloc(strlen(devname) + 1);
270     strncpy(dp->devName, devname, strlen(devname) + 1);
271     dp->device = dev;
272 #endif
273     dp->lock_fd = INVALID_FD;
274     dp->flags = 0;
275     dp->f_files = 1;            /* just a default value */
276 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
277     if (programType == fileServer)
278         (void)namei_ViceREADME(VPartitionPath(dp));
279 #endif
280     VSetPartitionDiskUsage_r(dp);
281 #ifdef AFS_DEMAND_ATTACH_FS
282     AddPartitionToTable_r(dp);
283     queue_Init(&dp->vol_list.head);
284     CV_INIT(&dp->vol_list.cv, "vol list", CV_DEFAULT, 0);
285     dp->vol_list.len = 0;
286     dp->vol_list.busy = 0;
287     {
288         char lockpath[MAXPATHLEN+1];
289         afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
290         lockpath[MAXPATHLEN] = '\0';
291         VLockFileInit(&dp->headerLockFile, lockpath);
292
293         afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
294         lockpath[MAXPATHLEN] = '\0';
295         VLockFileInit(&dp->volLockFile, lockpath);
296     }
297     VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
298 #endif /* AFS_DEMAND_ATTACH_FS */
299 }
300
301 static void
302 VInitPartition(char *path, char *devname, Device dev)
303 {
304     VOL_LOCK;
305     VInitPartition_r(path, devname, dev);
306     VOL_UNLOCK;
307 }
308
309 #ifndef AFS_NT40_ENV
310 /* VAttachPartitions() finds the vice partitions on this server. Calls
311  * VCheckPartition() to do some basic checks on the partition. If the partition
312  * is a valid vice partition, VCheckPartition will add it to the DiskPartition
313  * list.
314  * Returns the number of errors returned by VCheckPartition. An error in
315  * VCheckPartition means that partition is a valid vice partition but the
316  * fileserver should not start because of the error found on that partition.
317  *
318  * AFS_NAMEI_ENV
319  * No specific user space file system checks, since we don't know what
320  * is being used for vice partitions.
321  *
322  * Use partition name as devname.
323  */
324 int
325 VCheckPartition(char *part, char *devname)
326 {
327     struct afs_stat status;
328 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
329     char AFSIDatPath[MAXPATHLEN];
330 #endif
331
332     /* Only keep track of "/vicepx" partitions since it can get hairy
333      * when NFS mounts are involved.. */
334     if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
335         return 0;
336     }
337     if (afs_stat(part, &status) < 0) {
338         Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
339         return 0;
340     }
341 #ifndef AFS_AIX32_ENV
342     if (programType == fileServer) {
343         char salvpath[MAXPATHLEN];
344         strcpy(salvpath, part);
345         strcat(salvpath, "/FORCESALVAGE");
346         if (afs_stat(salvpath, &status) == 0) {
347             Log("VInitVnodes: Found %s; aborting\n", salvpath);
348             return -1;
349         }
350     }
351 #endif
352
353 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
354     strcpy(AFSIDatPath, part);
355     strcat(AFSIDatPath, "/AFSIDat");
356 #ifdef AFS_NAMEI_ENV
357     if (afs_stat(AFSIDatPath, &status) < 0) {
358         DIR *dirp;
359         struct dirent *dp;
360
361         dirp = opendir(part);
362         osi_Assert(dirp);
363         while ((dp = readdir(dirp))) {
364             if (dp->d_name[0] == 'V') {
365                 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);
366                 closedir(dirp);
367                 return -1;
368             }
369         }
370         closedir(dirp);
371     }
372 #else /* AFS_NAMEI_ENV */
373     if (afs_stat(AFSIDatPath, &status) == 0) {
374         Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
375         return -1;
376     }
377
378 #ifdef AFS_SGI_XFS_IOPS_ENV
379     if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
380         return -1;
381 #endif
382 #endif /* AFS_NAMEI_ENV */
383 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
384
385 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
386     if (status.st_ino != ROOTINO) {
387         Log("%s is not a mounted file system; ignored.\n", part);
388         return 0;
389     }
390 #endif
391
392     VInitPartition(part, devname, status.st_dev);
393
394     return 0;
395 }
396
397 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
398  * attached (return value 1), or only attached when it is a separately
399  * mounted partition (return value 0).  For non-NAMEI environments, it
400  * always returns 0.
401  *
402  * *awouldattach will be set to 1 if the given path at least looks like a vice
403  * partition (that is, if we return 0, the only thing preventing this partition
404  * from being attached is the existence of the AlwaysAttach file), or to 0
405  * otherwise. *awouldattach is set regardless of whether or not the partition
406  * should always be attached or not.
407  */
408 static int
409 VIsAlwaysAttach(char *part, int *awouldattach)
410 {
411 #ifdef AFS_NAMEI_ENV
412     struct afs_stat st;
413     char checkfile[256];
414     int ret;
415 #endif /* AFS_NAMEI_ENV */
416
417     if (awouldattach) {
418         *awouldattach = 0;
419     }
420
421 #ifdef AFS_NAMEI_ENV
422     if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
423         return 0;
424
425     if (awouldattach) {
426         *awouldattach = 1;
427     }
428
429     strncpy(checkfile, part, 100);
430     strcat(checkfile, "/");
431     strcat(checkfile, VICE_ALWAYSATTACH_FILE);
432
433     ret = afs_stat(checkfile, &st);
434     return (ret < 0) ? 0 : 1;
435 #else /* AFS_NAMEI_ENV */
436     return 0;
437 #endif /* AFS_NAMEI_ENV */
438 }
439
440 /* VAttachPartitions2() looks for and attaches /vicepX partitions
441  * where a special file (VICE_ALWAYSATTACH_FILE) exists.  This is
442  * used to attach /vicepX directories which aren't on dedicated
443  * partitions, in the NAMEI fileserver.
444  */
445 void
446 VAttachPartitions2(void)
447 {
448 #ifdef AFS_NAMEI_ENV
449     DIR *dirp;
450     struct dirent *de;
451     char pname[32];
452     int wouldattach;
453
454     dirp = opendir("/");
455     while ((de = readdir(dirp))) {
456         strcpy(pname, "/");
457         strncat(pname, de->d_name, 20);
458         pname[sizeof(pname) - 1] = '\0';
459
460         /* Only keep track of "/vicepx" partitions since automounter
461          * may hose us */
462         if (VIsAlwaysAttach(pname, &wouldattach)) {
463             VCheckPartition(pname, "");
464         } else {
465             struct afs_stat st;
466             if (wouldattach && VGetPartition(pname, 0) == NULL &&
467                 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
468
469                 /* This is a /vicep* dir, and it has not been attached as a
470                  * partition. This probably means that this is a /vicep* dir
471                  * that is not a separate partition, so just give a notice so
472                  * admins are not confused as to why their /vicep* dirs are not
473                  * being attached.
474                  *
475                  * It is possible that the dir _is_ a separate partition and we
476                  * failed to attach it earlier, making this message a bit
477                  * confusing. But that should be rare, and an error message
478                  * about the failure will already be logged right before this,
479                  * so it should be clear enough. */
480
481                 Log("VAttachPartitions: not attaching %s; either it is not a "
482                     "separate partition, or it failed to attach (create the "
483                     "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
484                     pname, pname);
485             }
486         }
487     }
488     closedir(dirp);
489 #endif /* AFS_NAMEI_ENV */
490 }
491 #endif /* AFS_NT40_ENV */
492
493 #ifdef AFS_SUN5_ENV
494 int
495 VAttachPartitions(void)
496 {
497     int errors = 0;
498     struct mnttab mnt;
499     FILE *mntfile;
500
501     if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
502         Log("Can't open %s\n", MNTTAB);
503         perror(MNTTAB);
504         exit(-1);
505     }
506     while (!getmntent(mntfile, &mnt)) {
507         /* Ignore non ufs or non read/write partitions */
508         /* but allow zfs too if we're in the NAMEI environment */
509         if (
510 #ifdef AFS_NAMEI_ENV
511             (((strcmp(mnt.mnt_fstype, "ufs") &&
512                 strcmp(mnt.mnt_fstype, "zfs"))))
513 #else
514             (strcmp(mnt.mnt_fstype, "ufs") != 0)
515 #endif
516             || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
517             continue;
518
519         /* If we're going to always attach this partition, do it later. */
520         if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
521             continue;
522
523 #ifndef AFS_NAMEI_ENV
524         if (hasmntopt(&mnt, "logging") != NULL) {
525             Log("This program is compiled without AFS_NAMEI_ENV, and "
526                 "partition %s is mounted with the 'logging' option. "
527                 "Using the inode fileserver backend with 'logging' UFS "
528                 "partitions causes volume corruption, so please either "
529                 "mount the partition without logging, or use the namei "
530                 "fileserver backend. Aborting...\n", mnt.mnt_mountp);
531             errors++;
532         }
533 #endif /* !AFS_NAMEI_ENV */
534
535         if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0)
536             errors++;
537     }
538
539     (void)fclose(mntfile);
540
541     /* Process the always-attach partitions, if any. */
542     VAttachPartitions2();
543
544     return errors;
545 }
546
547 #endif /* AFS_SUN5_ENV */
548 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
549 int
550 VAttachPartitions(void)
551 {
552     int errors = 0;
553     FILE *mfd;
554     struct mntent *mntent;
555
556     if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
557         Log("Problems in getting mount entries(setmntent)\n");
558         exit(-1);
559     }
560     while (mntent = getmntent(mfd)) {
561         if (!hasmntopt(mntent, MNTOPT_RW))
562             continue;
563
564         /* If we're going to always attach this partition, do it later. */
565         if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
566             continue;
567
568         if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
569             errors++;
570     }
571
572     endmntent(mfd);
573
574     /* Process the always-attach partitions, if any. */
575     VAttachPartitions2();
576
577     return errors;
578 }
579 #endif
580 #ifdef AFS_AIX_ENV
581 /*
582  * (This function was grabbed from df.c)
583  */
584 int
585 getmount(struct vmount **vmountpp)
586 {
587     int size;
588     struct vmount *vm;
589     int nmounts;
590
591     /* set initial size of mntctl buffer to a MAGIC NUMBER */
592     size = BUFSIZ;
593
594     /* try the operation until ok or a fatal error */
595     while (1) {
596         if ((vm = (struct vmount *)malloc(size)) == NULL) {
597             /* failed getting memory for mount status buf */
598             perror("FATAL ERROR: get_stat malloc failed\n");
599             exit(-1);
600         }
601
602         /*
603          * perform the QUERY mntctl - if it returns > 0, that is the
604          * number of vmount structures in the buffer.  If it returns
605          * -1, an error occured.  If it returned 0, then look in
606          * first word of buffer for needed size.
607          */
608         if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
609             /* OK, got it, now return */
610             *vmountpp = vm;
611             return (nmounts);
612
613         } else if (nmounts == 0) {
614             /* the buffer wasn't big enough .... */
615             /* .... get required buffer size */
616             size = *(int *)vm;
617             free(vm);
618
619         } else {
620             /* some other kind of error occurred */
621             free(vm);
622             return (-1);
623         }
624     }
625 }
626
627 int
628 VAttachPartitions(void)
629 {
630     int errors = 0;
631     int nmounts;
632     struct vmount *vmountp;
633
634     if ((nmounts = getmount(&vmountp)) <= 0) {
635         Log("Problems in getting # of mount entries(getmount)\n");
636         exit(-1);
637     }
638     for (; nmounts;
639          nmounts--, vmountp =
640          (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
641         char *part = vmt2dataptr(vmountp, VMT_STUB);
642
643         if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
644             continue;           /* Ignore any "special" partitions */
645
646 #ifdef AFS_AIX42_ENV
647 #ifndef AFS_NAMEI_ENV
648         {
649             struct superblock fs;
650             /* The Log statements are non-sequiters in the SalvageLog and don't
651              * even appear in the VolserLog, so restrict them to the FileLog.
652              */
653             if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
654                 if (programType == fileServer)
655                     Log("Can't read superblock for %s, ignoring it.\n", part);
656                 continue;
657             }
658             if (IsBigFilesFileSystem(&fs)) {
659                 if (programType == fileServer)
660                     Log("%s is a big files filesystem, ignoring it.\n", part);
661                 continue;
662             }
663         }
664 #endif
665 #endif
666
667         /* If we're going to always attach this partition, do it later. */
668         if (VIsAlwaysAttach(part, NULL))
669             continue;
670
671         if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0)
672             errors++;
673     }
674
675     /* Process the always-attach partitions, if any. */
676     VAttachPartitions2();
677
678     return errors;
679 }
680 #endif
681 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
682 int
683 VAttachPartitions(void)
684 {
685     int errors = 0;
686     struct fstab *fsent;
687
688     if (setfsent() < 0) {
689         Log("Error listing filesystems.\n");
690         exit(-1);
691     }
692
693     while ((fsent = getfsent())) {
694         if (strcmp(fsent->fs_type, "rw") != 0)
695             continue;
696
697         /* If we're going to always attach this partition, do it later. */
698         if (VIsAlwaysAttach(fsent->fs_file, NULL))
699             continue;
700
701         if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0)
702             errors++;
703     }
704     endfsent();
705
706     /* Process the always-attach partitions, if any. */
707     VAttachPartitions2();
708
709     return errors;
710 }
711 #endif
712
713 #ifdef AFS_NT40_ENV
714 #include <string.h>
715 #include <sys/stat.h>
716 /* VValidVPTEntry
717  *
718  * validate names in vptab.
719  *
720  * Return value:
721  * 1 valid entry
722  * 0 invalid entry
723  */
724
725 int
726 VValidVPTEntry(struct vptab *vpe)
727 {
728     int len = strlen(vpe->vp_name);
729     int i;
730
731     if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
732         return 0;
733     if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
734         return 0;
735
736     for (i = VICE_PREFIX_SIZE; i < len; i++) {
737         if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
738             Log("Invalid partition name %s in registry, ignoring it.\n",
739                 vpe->vp_name);
740             return 0;
741         }
742     }
743     if (len == VICE_PREFIX_SIZE + 2) {
744         i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
745             (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
746         if (i > 255) {
747             Log("Invalid partition name %s in registry, ignoring it.\n",
748                 vpe->vp_name);
749             return 0;
750         }
751     }
752
753     len = strlen(vpe->vp_dev);
754     if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
755         || vpe->vp_dev[0] > 'Z') {
756         Log("Invalid device name %s in registry, ignoring it.\n",
757             vpe->vp_dev);
758         return 0;
759     }
760
761     return 1;
762 }
763
764 int
765 VCheckPartition(char *partName)
766 {
767     char volRoot[4];
768     char volFsType[64];
769     DWORD dwDummy;
770     int err;
771
772     /* partName is presumed to be of the form "X:" */
773     (void)sprintf(volRoot, "%c:\\", *partName);
774
775     if (!GetVolumeInformation(volRoot,  /* volume root directory */
776                               NULL,     /* volume name buffer */
777                               0,        /* volume name size */
778                               NULL,     /* volume serial number */
779                               &dwDummy, /* max component length */
780                               &dwDummy, /* file system flags */
781                               volFsType,        /* file system name */
782                               sizeof(volFsType))) {
783         err = GetLastError();
784         Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
785         return -1;
786     }
787
788     if (strcmp(volFsType, "NTFS")) {
789         Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
790         return -1;
791     }
792
793     return 0;
794 }
795
796
797 int
798 VAttachPartitions(void)
799 {
800     struct DiskPartition64 *partP, *prevP, *nextP;
801     struct vpt_iter iter;
802     struct vptab entry;
803
804     if (vpt_Start(&iter) < 0) {
805         Log("No partitions to attach.\n");
806         return 0;
807     }
808
809     while (0 == vpt_NextEntry(&iter, &entry)) {
810         if (!VValidVPTEntry(&entry)) {
811             continue;
812         }
813
814         /* This test for duplicates relies on the fact that the method
815          * of storing the partition names in the NT registry means the same
816          * partition name will never appear twice in the list.
817          */
818         for (partP = DiskPartitionList; partP; partP = partP->next) {
819             if (*partP->devName == *entry.vp_dev) {
820                 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
821                 partP->flags = PART_DUPLICATE;
822                 break;          /* Only one entry will ever be in this list. */
823             }
824         }
825         if (partP)
826             continue;           /* found a duplicate */
827
828         if (VCheckPartition(entry.vp_dev) < 0)
829             continue;
830         /* This test allows for manually inserting the FORCESALVAGE flag
831          * and thereby invoking the salvager. scandisk obviously won't be
832          * doing this for us.
833          */
834         if (programType == fileServer) {
835             struct afs_stat status;
836             char salvpath[MAXPATHLEN];
837             strcpy(salvpath, entry.vp_dev);
838             strcat(salvpath, "\\FORCESALVAGE");
839             if (afs_stat(salvpath, &status) == 0) {
840                 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
841                 exit(1);
842             }
843         }
844         VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
845     }
846     vpt_Finish(&iter);
847
848     /* Run through partition list and clear out the dupes. */
849     prevP = nextP = NULL;
850     for (partP = DiskPartitionList; partP; partP = nextP) {
851         nextP = partP->next;
852         if (partP->flags == PART_DUPLICATE) {
853             if (prevP)
854                 prevP->next = partP->next;
855             else
856                 DiskPartitionList = partP->next;
857             free(partP);
858         } else
859             prevP = partP;
860     }
861
862     return 0;
863 }
864 #endif
865
866 #ifdef AFS_LINUX22_ENV
867 int
868 VAttachPartitions(void)
869 {
870     int errors = 0;
871     FILE *mfd;
872     struct mntent *mntent;
873
874     if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
875         if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
876             Log("Problems in getting mount entries(setmntent)\n");
877             exit(-1);
878         }
879     }
880     while ((mntent = getmntent(mfd))) {
881         /* If we're going to always attach this partition, do it later. */
882         if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
883             continue;
884
885         if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
886             errors++;
887     }
888     endmntent(mfd);
889
890     /* Process the always-attach partitions, if any. */
891     VAttachPartitions2();
892
893     return errors;
894 }
895 #endif /* AFS_LINUX22_ENV */
896
897 /* This routine is to be called whenever the actual name of the partition
898  * is required. The canonical name is still in part->name.
899  */
900 char *
901 VPartitionPath(struct DiskPartition64 *part)
902 {
903 #ifdef AFS_NT40_ENV
904     return part->devName;
905 #else
906     return part->name;
907 #endif
908 }
909
910 /* get partition structure, abortp tells us if we should abort on failure */
911 struct DiskPartition64 *
912 VGetPartition_r(char *name, int abortp)
913 {
914     struct DiskPartition64 *dp;
915 #ifdef AFS_DEMAND_ATTACH_FS
916     dp = VLookupPartition_r(name);
917 #else /* AFS_DEMAND_ATTACH_FS */
918     for (dp = DiskPartitionList; dp; dp = dp->next) {
919         if (strcmp(dp->name, name) == 0)
920             break;
921     }
922 #endif /* AFS_DEMAND_ATTACH_FS */
923     if (abortp)
924         osi_Assert(dp != NULL);
925     return dp;
926 }
927
928 struct DiskPartition64 *
929 VGetPartition(char *name, int abortp)
930 {
931     struct DiskPartition64 *retVal;
932     VOL_LOCK;
933     retVal = VGetPartition_r(name, abortp);
934     VOL_UNLOCK;
935     return retVal;
936 }
937
938 #ifdef AFS_NT40_ENV
939 void
940 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
941 {
942     ULARGE_INTEGER free_user, total, free_total;
943     int ufree, tot, tfree;
944
945     if (!GetDiskFreeSpaceEx
946         (VPartitionPath(dp), &free_user, &total, &free_total)) {
947         printf("Failed to get disk space info for %s, error = %d\n", dp->name,
948                GetLastError());
949         return;
950     }
951
952     /* Convert to 1K units. */
953     ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
954     tot = (int)Int64ShraMod32(total.QuadPart, 10);
955     tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
956
957     dp->minFree = tfree - ufree;        /* only used in VPrintDiskStats_r */
958     dp->totalUsable = tot;
959     dp->free = tfree;
960 }
961
962 #else
963 void
964 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
965 {
966     int bsize, code;
967     afs_int64 totalblks, free, used, availblks;
968     int reserved;
969 #ifdef afs_statvfs
970     struct afs_statvfs statbuf;
971 #else
972     struct afs_statfs statbuf;
973 #endif
974
975     if (dp->flags & PART_DONTUPDATE)
976         return;
977     /* Note:  we don't bother syncing because it's only an estimate, update
978      * is syncing every 30 seconds anyway, we only have to keep the disk
979      * approximately 10% from full--you just can't get the stuff in from
980      * the net fast enough to worry */
981 #ifdef afs_statvfs
982     code = afs_statvfs(dp->name, &statbuf);
983 #else
984     code = afs_statfs(dp->name, &statbuf);
985 #endif
986     if (code < 0) {
987         Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
988             dp->name, errno);
989         return;
990     }
991     if (statbuf.f_blocks == -1) {       /* Undefined; skip stats.. */
992         Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
993         return;
994     }
995     totalblks = statbuf.f_blocks;
996     free = statbuf.f_bfree;
997     reserved = free - statbuf.f_bavail;
998 #ifdef afs_statvfs
999     bsize = statbuf.f_frsize;
1000 #else
1001     bsize = statbuf.f_bsize;
1002 #endif
1003     availblks = totalblks - reserved;
1004     dp->f_files = statbuf.f_files;      /* max # of files in partition */
1005
1006     /* Now free and totalblks are in fragment units, but we want them in
1007      * 1K units.
1008      */
1009     if (bsize >= 1024) {
1010         free *= (bsize / 1024);
1011         totalblks *= (bsize / 1024);
1012         availblks *= (bsize / 1024);
1013         reserved *= (bsize / 1024);
1014     } else {
1015         free /= (1024 / bsize);
1016         totalblks /= (1024 / bsize);
1017         availblks /= (1024 / bsize);
1018         reserved /= (1024 / bsize);
1019     }
1020     /* now compute remaining figures */
1021     used = totalblks - free;
1022
1023     dp->minFree = reserved;     /* only used in VPrintDiskStats_r */
1024     dp->totalUsable = availblks;
1025     dp->free = availblks - used;        /* this is exactly f_bavail */
1026 }
1027 #endif /* AFS_NT40_ENV */
1028
1029 void
1030 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
1031 {
1032     VOL_LOCK;
1033     VSetPartitionDiskUsage_r(dp);
1034     VOL_UNLOCK;
1035 }
1036
1037 void
1038 VResetDiskUsage_r(void)
1039 {
1040     struct DiskPartition64 *dp;
1041     for (dp = DiskPartitionList; dp; dp = dp->next) {
1042         VSetPartitionDiskUsage_r(dp);
1043 #ifndef AFS_PTHREAD_ENV
1044         IOMGR_Poll();
1045 #endif /* !AFS_PTHREAD_ENV */
1046     }
1047 }
1048
1049 void
1050 VResetDiskUsage(void)
1051 {
1052     VOL_LOCK;
1053     VResetDiskUsage_r();
1054     VOL_UNLOCK;
1055 }
1056
1057 void
1058 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1059                    afs_sfsize_t checkBlocks)
1060 {
1061     *ec = 0;
1062     /* why blocks instead of checkBlocks in the check below?  Otherwise, any check
1063      * for less than BlocksSpare would skip the error-checking path, and we
1064      * could grow existing files forever, not just for another BlocksSpare
1065      * blocks. */
1066     if (blocks > 0) {
1067 #ifdef  AFS_AIX32_ENV
1068         afs_int32 rem, minavail;
1069
1070         if ((rem = vp->partition->free - checkBlocks) < (minavail =
1071                                                          (vp->partition->
1072                                                           totalUsable *
1073                                                           aixlow_water) /
1074                                                          100))
1075 #else
1076         if (vp->partition->free - checkBlocks < 0)
1077 #endif
1078             *ec = VDISKFULL;
1079         else if (V_maxquota(vp)
1080                  && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1081             *ec = VOVERQUOTA;
1082     }
1083     vp->partition->free -= blocks;
1084     V_diskused(vp) += blocks;
1085 }
1086
1087 void
1088 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1089                  afs_sfsize_t checkBlocks)
1090 {
1091     VOL_LOCK;
1092     VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1093     VOL_UNLOCK;
1094 }
1095
1096 int
1097 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1098 {
1099     if (blocks > 0) {
1100 #ifdef  AFS_AIX32_ENV
1101         afs_int32 rem, minavail;
1102
1103         if ((rem = vp->partition->free - blocks) < (minavail =
1104                                                     (vp->partition->
1105                                                      totalUsable *
1106                                                      aixlow_water) / 100))
1107 #else
1108         if (vp->partition->free - blocks < 0)
1109 #endif
1110             return (VDISKFULL);
1111     }
1112     vp->partition->free -= blocks;
1113     return 0;
1114 }
1115
1116 int
1117 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1118 {
1119     int retVal;
1120     VOL_LOCK;
1121     retVal = VDiskUsage_r(vp, blocks);
1122     VOL_UNLOCK;
1123     return retVal;
1124 }
1125
1126 void
1127 VPrintDiskStats_r(void)
1128 {
1129     struct DiskPartition64 *dp;
1130     for (dp = DiskPartitionList; dp; dp = dp->next) {
1131         if (dp->free < 0) {
1132             Log("Partition %s: %"AFS_INT64_FMT
1133                 " available 1K blocks (minfree=%"AFS_INT64_FMT"), "
1134                 "overallocated by %"AFS_INT64_FMT" blocks\n", dp->name,
1135                 dp->totalUsable, dp->minFree, -dp->free);
1136         } else {
1137             Log("Partition %s: %"AFS_INT64_FMT
1138                 " available 1K blocks (minfree=%"AFS_INT64_FMT"), "
1139                 "%"AFS_INT64_FMT" free blocks\n", dp->name,
1140                 dp->totalUsable, dp->minFree, dp->free);
1141         }
1142     }
1143 }
1144
1145 void
1146 VPrintDiskStats(void)
1147 {
1148     VOL_LOCK;
1149     VPrintDiskStats_r();
1150     VOL_UNLOCK;
1151 }
1152
1153 #ifdef AFS_NT40_ENV
1154 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1155 #define LOCKFILE "LOCKFILE"
1156 void
1157 VLockPartition_r(char *name)
1158 {
1159     struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1160     OVERLAPPED lap;
1161
1162     if (!dp)
1163         return;
1164     if (dp->lock_fd == INVALID_FD) {
1165         char path[64];
1166         int rc;
1167         (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1168         dp->lock_fd =
1169             (FD_t)CreateFile(path, GENERIC_WRITE,
1170                             FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1171                             CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1172         osi_Assert(dp->lock_fd != INVALID_FD);
1173
1174         memset(&lap, 0, sizeof(lap));
1175         rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1176                         0, &lap);
1177         osi_Assert(rc);
1178     }
1179 }
1180
1181 void
1182 VUnlockPartition_r(char *name)
1183 {
1184     struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1185     OVERLAPPED lap;
1186
1187     if (!dp)
1188         return;                 /* no partition, will fail later */
1189     memset(&lap, 0, sizeof(lap));
1190
1191     UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1192     CloseHandle((HANDLE) dp->lock_fd);
1193     dp->lock_fd = INVALID_FD;
1194 }
1195 #else /* AFS_NT40_ENV */
1196
1197 #if defined(AFS_HPUX_ENV)
1198 #define BITS_PER_CHAR   (8)
1199 #define BITS(type)      (sizeof(type) * BITS_PER_CHAR)
1200
1201 #define LOCKRDONLY_OFFSET       ((PRIV_LOCKRDONLY - 1) / BITS(int))
1202 #endif /* defined(AFS_HPUX_ENV) */
1203
1204 void
1205 VLockPartition_r(char *name)
1206 {
1207     struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1208     char *partitionName;
1209     int retries, code;
1210     struct timeval pausing;
1211 #if defined(AFS_HPUX_ENV)
1212     int lockfRtn;
1213     struct privgrp_map privGrpList[PRIV_MAXGRPS];
1214     unsigned int *globalMask;
1215     int globalMaskIndex;
1216 #endif /* defined(AFS_HPUX_ENV) */
1217 #if defined(AFS_DARWIN_ENV)
1218     char lockfile[MAXPATHLEN];
1219 #endif /* defined(AFS_DARWIN_ENV) */
1220 #ifdef AFS_NAMEI_ENV
1221 #ifdef AFS_AIX42_ENV
1222     char LockFileName[MAXPATHLEN + 1];
1223
1224     sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1225     partitionName = (char *)&LockFileName;
1226 #endif
1227 #endif
1228
1229     if (!dp)
1230         return;                 /* no partition, will fail later */
1231     if (dp->lock_fd != INVALID_FD)
1232         return;
1233
1234 #if    defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1235 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1236     partitionName = dp->devName;
1237 #endif
1238     code = O_RDWR;
1239 #elif defined(AFS_DARWIN_ENV)
1240     strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1241     strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1242     code = O_RDONLY | O_CREAT;
1243 #else
1244     partitionName = dp->name;
1245     code = O_RDONLY;
1246 #endif
1247
1248     for (retries = 25; retries; retries--) {
1249         if (code & O_CREAT)
1250             dp->lock_fd = afs_open(partitionName, code, 0644);
1251         else
1252             dp->lock_fd = afs_open(partitionName, code);
1253
1254         if (dp->lock_fd != INVALID_FD)
1255             break;
1256         if (errno == ENOENT)
1257             code |= O_CREAT;
1258         pausing.tv_sec = 0;
1259         pausing.tv_usec = 500000;
1260         select(0, NULL, NULL, NULL, &pausing);
1261     }
1262     osi_Assert(retries != 0);
1263
1264 #if defined (AFS_HPUX_ENV)
1265
1266     osi_Assert(getprivgrp(privGrpList) == 0);
1267
1268     /*
1269      * In general, it will difficult and time-consuming ,if not impossible,
1270      * to try to find the privgroup to which this process belongs that has the
1271      * smallest membership, to minimise the security hole.  So, we use the privgrp
1272      * to which everybody belongs.
1273      */
1274     /* first, we have to find the global mask */
1275     for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1276          globalMaskIndex++) {
1277         if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1278             globalMask =
1279                 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1280             break;
1281         }
1282     }
1283
1284     if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1285         /* allow everybody to set a lock on a read-only file descriptor */
1286         (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1287         osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1288                == 0);
1289
1290         lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1291
1292         /* remove the privilege granted to everybody to lock a read-only fd */
1293         (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1294         osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1295                == 0);
1296     } else {
1297         /* in this case, we should be able to do this with impunity, anyway */
1298         lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1299     }
1300
1301     osi_Assert(lockfRtn != -1);
1302 #else
1303 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1304     osi_Assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1305 #else
1306     osi_Assert(flock(dp->lock_fd, LOCK_EX) == 0);
1307 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1308 #endif
1309 }
1310
1311 void
1312 VUnlockPartition_r(char *name)
1313 {
1314     struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1315     if (!dp)
1316         return;                 /* no partition, will fail later */
1317     close(dp->lock_fd);
1318     dp->lock_fd = INVALID_FD;
1319 }
1320
1321 #endif /* AFS_NT40_ENV */
1322
1323 void
1324 VLockPartition(char *name)
1325 {
1326     VOL_LOCK;
1327     VLockPartition_r(name);
1328     VOL_UNLOCK;
1329 }
1330
1331 void
1332 VUnlockPartition(char *name)
1333 {
1334     VOL_LOCK;
1335     VUnlockPartition_r(name);
1336     VOL_UNLOCK;
1337 }
1338
1339 #ifdef AFS_DEMAND_ATTACH_FS
1340
1341 /* new-style partition locks; these are only to have some mutual exclusion
1342  * between the VGC scanner and volume utilies creating/altering vol headers
1343  */
1344
1345 /**
1346  * lock a partition's vol headers.
1347  *
1348  * @param[in] dp       the partition to lock
1349  * @param[in] locktype READ_LOCK or WRITE_LOCK
1350  *
1351  * @return operation status
1352  *  @retval 0 success
1353  */
1354 int
1355 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1356 {
1357     int code;
1358
1359     /* block on acquiring the lock */
1360     int nonblock = 0;
1361
1362     code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1363     if (code) {
1364         Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1365             VPartitionPath(dp));
1366     }
1367     return code;
1368 }
1369
1370 /**
1371  * unlock a partition's vol headers.
1372  *
1373  * @param[in] dp       the partition to unlock
1374  * @param[in] locktype READ_LOCK or WRITE_LOCK
1375  */
1376 void
1377 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1378 {
1379     VReleaseDiskLock(&dp->headerLock, locktype);
1380 }
1381
1382 /* XXX not sure this will work on AFS_NT40_ENV
1383  * needs to be tested!
1384  */
1385
1386 /**
1387  * lookup a disk partition object by its index number.
1388  *
1389  * @param[in] id      partition index number
1390  * @param[in] abortp  see abortp usage note below
1391  *
1392  * @return disk partition object
1393  *   @retval NULL no such disk partition
1394  *
1395  * @note when abortp is non-zero, lookups which would return
1396  *       NULL will result in an assertion failure
1397  *
1398  * @pre VOL_LOCK must be held
1399  *
1400  * @internal volume package internal use only
1401  */
1402
1403 struct DiskPartition64 *
1404 VGetPartitionById_r(afs_int32 id, int abortp)
1405 {
1406     struct DiskPartition64 *dp = NULL;
1407
1408     if ((id >= 0) && (id <= VOLMAXPARTS)) {
1409         dp = DiskPartitionTable[id];
1410     }
1411
1412     if (abortp) {
1413         osi_Assert(dp != NULL);
1414     }
1415     return dp;
1416 }
1417
1418 /**
1419  * lookup a disk partition object by its index number.
1420  *
1421  * @param[in] id      partition index number
1422  * @param[in] abortp  see abortp usage note below
1423  *
1424  * @return disk partition object
1425  *   @retval NULL no such disk partition
1426  *
1427  * @note when abortp is non-zero, lookups which would return
1428  *       NULL will result in an assertion failure
1429  */
1430
1431 struct DiskPartition64 *
1432 VGetPartitionById(afs_int32 id, int abortp)
1433 {
1434     struct DiskPartition64 * dp;
1435
1436     VOL_LOCK;
1437     dp = VGetPartitionById_r(id, abortp);
1438     VOL_UNLOCK;
1439
1440     return dp;
1441 }
1442
1443 static struct DiskPartition64 *
1444 VLookupPartition_r(char * path)
1445 {
1446     afs_int32 id = volutil_GetPartitionID(path);
1447
1448     if (id < 0 || id > VOLMAXPARTS)
1449         return NULL;
1450
1451     return DiskPartitionTable[id];
1452 }
1453
1454 static void
1455 AddPartitionToTable_r(struct DiskPartition64 *dp)
1456 {
1457     osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1458     DiskPartitionTable[dp->index] = dp;
1459 }
1460
1461 #if 0
1462 static void
1463 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1464 {
1465     osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1466     DiskPartitionTable[dp->index] = NULL;
1467 }
1468 #endif
1469 #endif /* AFS_DEMAND_ATTACH_FS */