vol: check for bad partition names
[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
28 #ifdef AFS_NT40_ENV
29 #include <windows.h>
30 #include <winbase.h>
31 #include <winioctl.h>
32 #else
33
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>
39 #endif
40
41 #if !defined(AFS_SGI_ENV)
42 #ifdef  AFS_OSF_ENV
43 #include <sys/mount.h>
44 #include <ufs/fs.h>
45 #else /* AFS_OSF_ENV */
46 #ifdef AFS_VFSINCL_ENV
47 #define VFS
48 #ifdef  AFS_SUN5_ENV
49 #include <sys/fs/ufs_fs.h>
50 #else
51 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ffs/fs.h>
54 #else
55 #include <ufs/fs.h>
56 #endif
57 #endif
58 #else /* AFS_VFSINCL_ENV */
59 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
60 #include <sys/fs.h>
61 #endif
62 #endif /* AFS_VFSINCL_ENV */
63 #endif /* AFS_OSF_ENV */
64 #include <sys/file.h>
65 #ifdef  AFS_AIX_ENV
66 #include <sys/vfs.h>
67 #include <sys/lockf.h>
68 #else
69 #ifdef  AFS_HPUX_ENV
70 #include <sys/vfs.h>
71 #include <checklist.h>
72 #else
73 #if     defined(AFS_SUN_ENV)
74 #include <sys/vfs.h>
75 #ifndef AFS_SUN5_ENV
76 #include <mntent.h>
77 #endif
78 #endif
79 #ifdef AFS_SUN5_ENV
80 #include <sys/mnttab.h>
81 #include <sys/mntent.h>
82 #else
83 #ifdef AFS_LINUX22_ENV
84 #include <mntent.h>
85 #include <sys/statfs.h>
86 #else
87 #include <fstab.h>
88 #endif
89 #endif
90 #endif
91 #endif
92 #endif /* AFS_SGI_ENV */
93 #endif /* AFS_NT40_ENV */
94 #if defined(AFS_SGI_ENV)
95 #include <sys/file.h>
96 #include <mntent.h>
97 #endif
98
99 #include <afs/opr.h>
100 #ifdef AFS_PTHREAD_ENV
101 # include <opr/lock.h>
102 #endif
103 #include <afs/afsint.h>
104 #include <rx/rx_queue.h>
105 #include "nfs.h"
106 #include <afs/errors.h>
107 #include "lock.h"
108 #include "lwp.h"
109 #include <afs/afssyscalls.h>
110 #include "ihandle.h"
111 #include "common.h"
112 #ifdef AFS_NAMEI_ENV
113 #ifdef AFS_NT40_ENV
114 #include "ntops.h"
115 #else
116 #include "namei_ops.h"
117 #endif /* AFS_NT40_ENV */
118 #endif /* AFS_NAMEI_ENV */
119 #include "vnode.h"
120 #include "volume.h"
121 #include "partition.h"
122
123 #if defined(AFS_HPUX_ENV)
124 #include <sys/privgrp.h>
125 #endif /* defined(AFS_HPUX_ENV) */
126
127 #ifdef AFS_AIX42_ENV
128 #include <jfs/filsys.h>
129 #endif
130
131 #ifdef AFS_NT40_ENV
132 extern int VValidVPTEntry(struct vptab *vptp);
133 #endif
134
135 int aixlow_water = 8;           /* default 8% */
136 struct DiskPartition64 *DiskPartitionList;
137
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"
142
143 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
144
145 static struct DiskPartition64 * VLookupPartition_r(char * path);
146 static void AddPartitionToTable_r(struct DiskPartition64 *);
147 #endif /* AFS_DEMAND_ATTACH_FS */
148
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
153  * the XFS inode.
154  */
155 #include <afs/xfsattrs.h>
156 static int
157 VerifyXFSInodeSize(char *part, char *fstype)
158 {
159     afs_xfs_attr_t junk;
160     int length = SIZEOF_XFS_ATTR_T;
161     int fd = 0;
162     int code = -1;
163     struct fsxattr fsx;
164
165     if (strcmp("xfs", fstype))
166         return 0;
167
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)) {
171
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");
175             } else
176                 code = 0;
177         }
178
179         if (fd > 0)
180             close(fd);
181         (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
182     }
183     return code;
184 }
185 #endif /* AFS_SGI_XFS_IOPS_ENV */
186
187 int
188 VInitPartitionPackage(void)
189 {
190 #ifdef AFS_DEMAND_ATTACH_FS
191     memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
192 #endif /* AFS_DEMAND_ATTACH_FS */
193     return 0;
194 }
195
196 static void
197 VInitPartition_r(char *path, char *devname, Device dev)
198 {
199     struct DiskPartition64 *dp, *op;
200
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) {
204         if (!op->next)
205             break;
206     }
207     if (op)
208         op->next = dp;
209     else
210         DiskPartitionList = dp;
211     dp->next = 0;
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;
224 #else
225     dp->devName = strdup(devname);
226     dp->device = dev;
227 #endif
228     dp->lock_fd = INVALID_FD;
229     dp->flags = 0;
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));
234 #endif
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;
242     {
243         char lockpath[MAXPATHLEN+1];
244         snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
245         lockpath[MAXPATHLEN] = '\0';
246         VLockFileInit(&dp->headerLockFile, lockpath);
247
248         snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
249         lockpath[MAXPATHLEN] = '\0';
250         VLockFileInit(&dp->volLockFile, lockpath);
251     }
252     VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
253 #endif /* AFS_DEMAND_ATTACH_FS */
254 }
255
256 static void
257 VInitPartition(char *path, char *devname, Device dev)
258 {
259     VOL_LOCK;
260     VInitPartition_r(path, devname, dev);
261     VOL_UNLOCK;
262 }
263
264 #ifndef AFS_NT40_ENV
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
268  * list.
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.
272  *
273  * AFS_NAMEI_ENV
274  * No specific user space file system checks, since we don't know what
275  * is being used for vice partitions.
276  *
277  * Use partition name as devname.
278  */
279 static int
280 VCheckPartition(char *part, char *devname, int logging)
281 {
282     struct afs_stat_st status;
283 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
284     char AFSIDatPath[MAXPATHLEN];
285 #endif
286
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)) {
290         return 0;
291     }
292     if (volutil_GetPartitionID(part) == -1) {
293         Log("Warning: %s is a bad partition name; ignored.\n", part);
294         return 0;
295     }
296     if (afs_stat(part, &status) < 0) {
297         Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
298         return 0;
299     }
300     if (logging) {
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);
307         return -1;
308     }
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);
316             return -1;
317         }
318     }
319 #endif
320
321 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
322     strcpy(AFSIDatPath, part);
323     strcat(AFSIDatPath, "/AFSIDat");
324 #ifdef AFS_NAMEI_ENV
325     if (afs_stat(AFSIDatPath, &status) < 0) {
326         DIR *dirp;
327         struct dirent *dp;
328
329         dirp = opendir(part);
330         opr_Assert(dirp);
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);
334                 closedir(dirp);
335                 return -1;
336             }
337         }
338         closedir(dirp);
339     }
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);
343         return -1;
344     }
345
346 #ifdef AFS_SGI_XFS_IOPS_ENV
347     if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
348         return -1;
349 #endif
350 #endif /* AFS_NAMEI_ENV */
351 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
352
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);
356         return 0;
357     }
358 #endif
359
360     VInitPartition(part, devname, status.st_dev);
361
362     return 0;
363 }
364
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
368  * always returns 0.
369  *
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.
375  */
376 static int
377 VIsAlwaysAttach(char *part, int *awouldattach)
378 {
379 #ifdef AFS_NAMEI_ENV
380     struct afs_stat_st st;
381     char checkfile[256];
382     int ret;
383 #endif /* AFS_NAMEI_ENV */
384
385     if (awouldattach) {
386         *awouldattach = 0;
387     }
388
389 #ifdef AFS_NAMEI_ENV
390     if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
391         return 0;
392
393     if (awouldattach) {
394         *awouldattach = 1;
395     }
396
397     strncpy(checkfile, part, 100);
398     strcat(checkfile, OS_DIRSEP);
399     strcat(checkfile, VICE_ALWAYSATTACH_FILE);
400
401     ret = afs_stat(checkfile, &st);
402     return (ret < 0) ? 0 : 1;
403 #else /* AFS_NAMEI_ENV */
404     return 0;
405 #endif /* AFS_NAMEI_ENV */
406 }
407
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.
411  */
412 static int
413 VIsNeverAttach(char *part)
414 {
415     struct afs_stat_st st;
416     char checkfile[256];
417     int ret;
418
419     if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
420         return 0;
421
422     strncpy(checkfile, part, 100);
423     strcat(checkfile, OS_DIRSEP);
424     strcat(checkfile, VICE_NEVERATTACH_FILE);
425
426     ret = afs_stat(checkfile, &st);
427     return (ret < 0) ? 0 : 1;
428 }
429
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.
434  */
435 static void
436 VAttachPartitions2(void)
437 {
438 #ifdef AFS_NAMEI_ENV
439     DIR *dirp;
440     struct dirent *de;
441     char pname[32];
442     int wouldattach;
443
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';
449
450         /* Only keep track of "/vicepx" partitions since automounter
451          * may hose us */
452         if (VIsAlwaysAttach(pname, &wouldattach)) {
453             VCheckPartition(pname, "", 0);
454         } else {
455             struct afs_stat_st st;
456             if (wouldattach && VGetPartition(pname, 0) == NULL &&
457                 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
458
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
463                  * being attached.
464                  *
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. */
470
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",
474                     pname, pname);
475             }
476         }
477     }
478     closedir(dirp);
479 #endif /* AFS_NAMEI_ENV */
480 }
481 #endif /* AFS_NT40_ENV */
482
483 #ifdef AFS_SUN5_ENV
484 int
485 VAttachPartitions(void)
486 {
487     int errors = 0;
488     struct mnttab mnt;
489     FILE *mntfile;
490
491     if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
492         Log("Can't open %s\n", MNTTAB);
493         perror(MNTTAB);
494         exit(-1);
495     }
496     while (!getmntent(mntfile, &mnt)) {
497         int logging = 0;
498         /* Ignore non ufs or non read/write partitions */
499         /* but allow zfs too if we're in the NAMEI environment */
500         if (
501 #ifdef AFS_NAMEI_ENV
502             (((strcmp(mnt.mnt_fstype, "ufs") &&
503                 strcmp(mnt.mnt_fstype, "zfs"))))
504 #else
505             (strcmp(mnt.mnt_fstype, "ufs") != 0)
506 #endif
507             || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
508             continue;
509
510         /* Skip this Partition? */
511         if (VIsNeverAttach(mnt.mnt_mountp))
512             continue;
513
514         /* If we're going to always attach this partition, do it later. */
515         if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
516             continue;
517
518 #ifndef AFS_NAMEI_ENV
519         if (hasmntopt(&mnt, "logging") != NULL) {
520             logging = 1;
521         }
522 #endif /* !AFS_NAMEI_ENV */
523
524         if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special, logging) < 0)
525             errors++;
526     }
527
528     (void)fclose(mntfile);
529
530     /* Process the always-attach partitions, if any. */
531     VAttachPartitions2();
532
533     return errors;
534 }
535
536 #endif /* AFS_SUN5_ENV */
537 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
538 int
539 VAttachPartitions(void)
540 {
541     int errors = 0;
542     FILE *mfd;
543     struct mntent *mntent;
544
545     if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
546         Log("Problems in getting mount entries(setmntent)\n");
547         exit(-1);
548     }
549     while (mntent = getmntent(mfd)) {
550         if (!hasmntopt(mntent, MNTOPT_RW))
551             continue;
552
553         /* Skip this Partition? */
554         if (VIsNeverAttach(mntent->mnt_dir))
555             continue;
556
557         /* If we're going to always attach this partition, do it later. */
558         if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
559             continue;
560
561         if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
562             errors++;
563     }
564
565     endmntent(mfd);
566
567     /* Process the always-attach partitions, if any. */
568     VAttachPartitions2();
569
570     return errors;
571 }
572 #endif
573 #ifdef AFS_AIX_ENV
574 /*
575  * (This function was grabbed from df.c)
576  */
577 int
578 getmount(struct vmount **vmountpp)
579 {
580     int size;
581     struct vmount *vm;
582     int nmounts;
583
584     /* set initial size of mntctl buffer to a MAGIC NUMBER */
585     size = BUFSIZ;
586
587     /* try the operation until ok or a fatal error */
588     while (1) {
589         if ((vm = malloc(size)) == NULL) {
590             /* failed getting memory for mount status buf */
591             perror("FATAL ERROR: get_stat malloc failed\n");
592             exit(-1);
593         }
594
595         /*
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.
600          */
601         if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
602             /* OK, got it, now return */
603             *vmountpp = vm;
604             return (nmounts);
605
606         } else if (nmounts == 0) {
607             /* the buffer wasn't big enough .... */
608             /* .... get required buffer size */
609             size = *(int *)vm;
610             free(vm);
611
612         } else {
613             /* some other kind of error occurred */
614             free(vm);
615             return (-1);
616         }
617     }
618 }
619
620 int
621 VAttachPartitions(void)
622 {
623     int errors = 0;
624     int nmounts;
625     struct vmount *vmountp;
626
627     if ((nmounts = getmount(&vmountp)) <= 0) {
628         Log("Problems in getting # of mount entries(getmount)\n");
629         exit(-1);
630     }
631     for (; nmounts;
632          nmounts--, vmountp =
633          (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
634         char *part = vmt2dataptr(vmountp, VMT_STUB);
635
636         if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
637             continue;           /* Ignore any "special" partitions */
638
639 #ifdef AFS_AIX42_ENV
640 #ifndef AFS_NAMEI_ENV
641         {
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.
645              */
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);
649                 continue;
650             }
651             if (IsBigFilesFileSystem(&fs)) {
652                 if (programType == fileServer)
653                     Log("%s is a big files filesystem, ignoring it.\n", part);
654                 continue;
655             }
656         }
657 #endif
658 #endif
659
660         /* Skip this Partition? */
661         if (VIsNeverAttach(part))
662             continue;
663
664         /* If we're going to always attach this partition, do it later. */
665         if (VIsAlwaysAttach(part, NULL))
666             continue;
667
668         if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT), 0) < 0)
669             errors++;
670     }
671
672     /* Process the always-attach partitions, if any. */
673     VAttachPartitions2();
674
675     return errors;
676 }
677 #endif
678 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
679 int
680 VAttachPartitions(void)
681 {
682     int errors = 0;
683     struct fstab *fsent;
684
685     if (setfsent() < 0) {
686         Log("Error listing filesystems.\n");
687         exit(-1);
688     }
689
690     while ((fsent = getfsent())) {
691         if (strcmp(fsent->fs_type, "rw") != 0)
692             continue;
693
694         /* Skip this Partition? */
695         if (VIsNeverAttach(fsent->fs_file))
696             continue;
697
698         /* If we're going to always attach this partition, do it later. */
699         if (VIsAlwaysAttach(fsent->fs_file, NULL))
700             continue;
701
702         if (VCheckPartition(fsent->fs_file, fsent->fs_spec, 0) < 0)
703             errors++;
704     }
705     endfsent();
706
707     /* Process the always-attach partitions, if any. */
708     VAttachPartitions2();
709
710     return errors;
711 }
712 #endif
713
714 #ifdef AFS_NT40_ENV
715 /* VValidVPTEntry
716  *
717  * validate names in vptab.
718  *
719  * Return value:
720  * 1 valid entry
721  * 0 invalid entry
722  */
723
724 static int
725 VValidVPTEntry(struct vptab *vpe)
726 {
727     int len = strlen(vpe->vp_name);
728     int i;
729
730     if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
731         return 0;
732     if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
733         return 0;
734
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",
738                 vpe->vp_name);
739             return 0;
740         }
741     }
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');
745         if (i > 255) {
746             Log("Invalid partition name %s in registry, ignoring it.\n",
747                 vpe->vp_name);
748             return 0;
749         }
750     }
751
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",
756             vpe->vp_dev);
757         return 0;
758     }
759
760     return 1;
761 }
762
763 static int
764 VCheckPartition(char *partName)
765 {
766     char volRoot[4];
767     char volFsType[64];
768     DWORD dwDummy;
769     int err;
770
771     /* partName is presumed to be of the form "X:" */
772     (void)sprintf(volRoot, "%c:\\", *partName);
773
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);
784         return -1;
785     }
786
787     if (strcmp(volFsType, "NTFS")) {
788         Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
789         return -1;
790     }
791
792     return 0;
793 }
794
795
796 int
797 VAttachPartitions(void)
798 {
799     struct DiskPartition64 *partP, *prevP, *nextP;
800     struct vpt_iter iter;
801     struct vptab entry;
802
803     if (vpt_Start(&iter) < 0) {
804         Log("No partitions to attach.\n");
805         return 0;
806     }
807
808     while (0 == vpt_NextEntry(&iter, &entry)) {
809         if (!VValidVPTEntry(&entry)) {
810             continue;
811         }
812
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.
816          */
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. */
822             }
823         }
824         if (partP)
825             continue;           /* found a duplicate */
826
827         if (VCheckPartition(entry.vp_dev) < 0)
828             continue;
829         /* This test allows for manually inserting the FORCESALVAGE flag
830          * and thereby invoking the salvager. scandisk obviously won't be
831          * doing this for us.
832          */
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);
840                 exit(1);
841             }
842         }
843         VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
844     }
845     vpt_Finish(&iter);
846
847     /* Run through partition list and clear out the dupes. */
848     prevP = nextP = NULL;
849     for (partP = DiskPartitionList; partP; partP = nextP) {
850         nextP = partP->next;
851         if (partP->flags == PART_DUPLICATE) {
852             if (prevP)
853                 prevP->next = partP->next;
854             else
855                 DiskPartitionList = partP->next;
856             free(partP);
857         } else
858             prevP = partP;
859     }
860
861     return 0;
862 }
863 #endif
864
865 #ifdef AFS_LINUX22_ENV
866 int
867 VAttachPartitions(void)
868 {
869     int errors = 0;
870     FILE *mfd;
871     struct mntent *mntent;
872
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");
876             exit(-1);
877         }
878     }
879     while ((mntent = getmntent(mfd))) {
880         /* Skip this Partition? */
881         if (VIsNeverAttach(mntent->mnt_dir))
882             continue;
883
884         /* If we're going to always attach this partition, do it later. */
885         if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
886             continue;
887
888         if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
889             errors++;
890     }
891     endmntent(mfd);
892
893     /* Process the always-attach partitions, if any. */
894     VAttachPartitions2();
895
896     return errors;
897 }
898 #endif /* AFS_LINUX22_ENV */
899
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.
902  */
903 char *
904 VPartitionPath(struct DiskPartition64 *part)
905 {
906 #ifdef AFS_NT40_ENV
907     return part->devName;
908 #else
909     return part->name;
910 #endif
911 }
912
913 /* get partition structure, abortp tells us if we should abort on failure */
914 struct DiskPartition64 *
915 VGetPartition_r(char *name, int abortp)
916 {
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)
923             break;
924     }
925 #endif /* AFS_DEMAND_ATTACH_FS */
926     if (abortp)
927         opr_Assert(dp != NULL);
928     return dp;
929 }
930
931 struct DiskPartition64 *
932 VGetPartition(char *name, int abortp)
933 {
934     struct DiskPartition64 *retVal;
935     VOL_LOCK;
936     retVal = VGetPartition_r(name, abortp);
937     VOL_UNLOCK;
938     return retVal;
939 }
940
941 #ifdef AFS_NT40_ENV
942 void
943 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
944 {
945     ULARGE_INTEGER free_user, total, free_total;
946     int ufree, tot, tfree;
947
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,
951                GetLastError());
952         return;
953     }
954
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);
959
960     dp->minFree = tfree - ufree;        /* only used in VPrintDiskStats_r */
961     dp->totalUsable = tot;
962     dp->free = tfree;
963 }
964
965 #else
966 void
967 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
968 {
969     int bsize, code;
970     afs_int64 totalblks, free, used, availblks;
971     int reserved;
972 #ifdef afs_statvfs
973     struct afs_statvfs statbuf;
974 #else
975     struct afs_statfs statbuf;
976 #endif
977
978     if (dp->flags & PART_DONTUPDATE)
979         return;
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 */
984 #ifdef afs_statvfs
985     code = afs_statvfs(dp->name, &statbuf);
986 #else
987     code = afs_statfs(dp->name, &statbuf);
988 #endif
989     if (code < 0) {
990         Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
991             dp->name, errno);
992         return;
993     }
994     if (statbuf.f_blocks == -1) {       /* Undefined; skip stats.. */
995         Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
996         return;
997     }
998     totalblks = statbuf.f_blocks;
999     free = statbuf.f_bfree;
1000     reserved = free - statbuf.f_bavail;
1001 #ifdef afs_statvfs
1002     bsize = statbuf.f_frsize;
1003 #else
1004     bsize = statbuf.f_bsize;
1005 #endif
1006     availblks = totalblks - reserved;
1007     dp->f_files = statbuf.f_files;      /* max # of files in partition */
1008
1009     /* Now free and totalblks are in fragment units, but we want them in
1010      * 1K units.
1011      */
1012     if (bsize >= 1024) {
1013         free *= (bsize / 1024);
1014         totalblks *= (bsize / 1024);
1015         availblks *= (bsize / 1024);
1016         reserved *= (bsize / 1024);
1017     } else {
1018         free /= (1024 / bsize);
1019         totalblks /= (1024 / bsize);
1020         availblks /= (1024 / bsize);
1021         reserved /= (1024 / bsize);
1022     }
1023     /* now compute remaining figures */
1024     used = totalblks - free;
1025
1026     dp->minFree = reserved;     /* only used in VPrintDiskStats_r */
1027     dp->totalUsable = availblks;
1028     dp->free = availblks - used;        /* this is exactly f_bavail */
1029 }
1030 #endif /* AFS_NT40_ENV */
1031
1032 void
1033 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
1034 {
1035     VOL_LOCK;
1036     VSetPartitionDiskUsage_r(dp);
1037     VOL_UNLOCK;
1038 }
1039
1040 void
1041 VResetDiskUsage_r(void)
1042 {
1043     struct DiskPartition64 *dp;
1044     for (dp = DiskPartitionList; dp; dp = dp->next) {
1045         VSetPartitionDiskUsage_r(dp);
1046 #ifndef AFS_PTHREAD_ENV
1047         IOMGR_Poll();
1048 #endif /* !AFS_PTHREAD_ENV */
1049     }
1050 }
1051
1052 void
1053 VResetDiskUsage(void)
1054 {
1055     VOL_LOCK;
1056     VResetDiskUsage_r();
1057     VOL_UNLOCK;
1058 }
1059
1060 void
1061 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1062                    afs_sfsize_t checkBlocks)
1063 {
1064     *ec = 0;
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
1068      * blocks. */
1069     if (blocks > 0) {
1070 #ifdef  AFS_AIX32_ENV
1071         afs_int32 rem, minavail;
1072
1073         if ((rem = vp->partition->free - checkBlocks) < (minavail =
1074                                                          (vp->partition->
1075                                                           totalUsable *
1076                                                           aixlow_water) /
1077                                                          100))
1078 #else
1079         if (vp->partition->free - checkBlocks < 0)
1080 #endif
1081             *ec = VDISKFULL;
1082         else if (V_maxquota(vp)
1083                  && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1084             *ec = VOVERQUOTA;
1085     }
1086     vp->partition->free -= blocks;
1087     V_diskused(vp) += blocks;
1088 }
1089
1090 void
1091 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1092                  afs_sfsize_t checkBlocks)
1093 {
1094     VOL_LOCK;
1095     VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1096     VOL_UNLOCK;
1097 }
1098
1099 int
1100 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1101 {
1102     if (blocks > 0) {
1103 #ifdef  AFS_AIX32_ENV
1104         afs_int32 rem, minavail;
1105
1106         if ((rem = vp->partition->free - blocks) < (minavail =
1107                                                     (vp->partition->
1108                                                      totalUsable *
1109                                                      aixlow_water) / 100))
1110 #else
1111         if (vp->partition->free - blocks < 0)
1112 #endif
1113             return (VDISKFULL);
1114     }
1115     vp->partition->free -= blocks;
1116     return 0;
1117 }
1118
1119 int
1120 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1121 {
1122     int retVal;
1123     VOL_LOCK;
1124     retVal = VDiskUsage_r(vp, blocks);
1125     VOL_UNLOCK;
1126     return retVal;
1127 }
1128
1129 void
1130 VPrintDiskStats_r(void)
1131 {
1132     struct DiskPartition64 *dp;
1133     for (dp = DiskPartitionList; dp; dp = dp->next) {
1134         if (dp->free < 0) {
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);
1139         } else {
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);
1144         }
1145     }
1146 }
1147
1148 void
1149 VPrintDiskStats(void)
1150 {
1151     VOL_LOCK;
1152     VPrintDiskStats_r();
1153     VOL_UNLOCK;
1154 }
1155
1156 #ifdef AFS_NT40_ENV
1157 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1158 #define LOCKFILE "LOCKFILE"
1159 void
1160 VLockPartition_r(char *name)
1161 {
1162     struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1163     OVERLAPPED lap;
1164
1165     if (!dp)
1166         return;
1167     if (dp->lock_fd == INVALID_FD) {
1168         char path[64];
1169         int rc;
1170         (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1171         dp->lock_fd =
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);
1176
1177         memset(&lap, 0, sizeof(lap));
1178         rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1179                         0, &lap);
1180         opr_Assert(rc);
1181     }
1182 }
1183
1184 void
1185 VUnlockPartition_r(char *name)
1186 {
1187     struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1188     OVERLAPPED lap;
1189
1190     if (!dp)
1191         return;                 /* no partition, will fail later */
1192     memset(&lap, 0, sizeof(lap));
1193
1194     UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1195     CloseHandle((HANDLE) dp->lock_fd);
1196     dp->lock_fd = INVALID_FD;
1197 }
1198 #else /* AFS_NT40_ENV */
1199
1200 #if defined(AFS_HPUX_ENV)
1201 #define BITS_PER_CHAR   (8)
1202 #define BITS(type)      (sizeof(type) * BITS_PER_CHAR)
1203
1204 #define LOCKRDONLY_OFFSET       ((PRIV_LOCKRDONLY - 1) / BITS(int))
1205 #endif /* defined(AFS_HPUX_ENV) */
1206
1207 void
1208 VLockPartition_r(char *name)
1209 {
1210     struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1211     char *partitionName;
1212     int retries, code;
1213     struct timeval pausing;
1214 #if defined(AFS_HPUX_ENV)
1215     int lockfRtn;
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];
1226
1227     sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1228     partitionName = (char *)&LockFileName;
1229 #endif
1230 #endif
1231
1232     if (!dp)
1233         return;                 /* no partition, will fail later */
1234     if (dp->lock_fd != INVALID_FD)
1235         return;
1236
1237 #if    defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1238 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1239     partitionName = dp->devName;
1240 #endif
1241     code = O_RDWR;
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;
1246 #else
1247     partitionName = dp->name;
1248     code = O_RDONLY;
1249 #endif
1250
1251     for (retries = 25; retries; retries--) {
1252         if (code & O_CREAT)
1253             dp->lock_fd = afs_open(partitionName, code, 0644);
1254         else
1255             dp->lock_fd = afs_open(partitionName, code);
1256
1257         if (dp->lock_fd != INVALID_FD)
1258             break;
1259         if (errno == ENOENT)
1260             code |= O_CREAT;
1261         pausing.tv_sec = 0;
1262         pausing.tv_usec = 500000;
1263         select(0, NULL, NULL, NULL, &pausing);
1264     }
1265     opr_Assert(retries != 0);
1266
1267 #if defined (AFS_HPUX_ENV)
1268
1269     opr_Verify(getprivgrp(privGrpList) == 0);
1270
1271     /*
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.
1276      */
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) {
1281             globalMask =
1282                 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1283             break;
1284         }
1285     }
1286
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);
1292
1293         lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1294
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);
1299     } else {
1300         /* in this case, we should be able to do this with impunity, anyway */
1301         lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1302     }
1303
1304     opr_Assert(lockfRtn != -1);
1305 #else
1306 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1307     opr_Verify(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1308 #else
1309     opr_Verify(flock(dp->lock_fd, LOCK_EX) == 0);
1310 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1311 #endif
1312 }
1313
1314 void
1315 VUnlockPartition_r(char *name)
1316 {
1317     struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1318     if (!dp)
1319         return;                 /* no partition, will fail later */
1320     close(dp->lock_fd);
1321     dp->lock_fd = INVALID_FD;
1322 }
1323
1324 #endif /* AFS_NT40_ENV */
1325
1326 void
1327 VLockPartition(char *name)
1328 {
1329     VOL_LOCK;
1330     VLockPartition_r(name);
1331     VOL_UNLOCK;
1332 }
1333
1334 void
1335 VUnlockPartition(char *name)
1336 {
1337     VOL_LOCK;
1338     VUnlockPartition_r(name);
1339     VOL_UNLOCK;
1340 }
1341
1342 #ifdef AFS_DEMAND_ATTACH_FS
1343
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
1346  */
1347
1348 /**
1349  * lock a partition's vol headers.
1350  *
1351  * @param[in] dp       the partition to lock
1352  * @param[in] locktype READ_LOCK or WRITE_LOCK
1353  *
1354  * @return operation status
1355  *  @retval 0 success
1356  */
1357 int
1358 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1359 {
1360     int code;
1361
1362     /* block on acquiring the lock */
1363     int nonblock = 0;
1364
1365     code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1366     if (code) {
1367         Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1368             VPartitionPath(dp));
1369     }
1370     return code;
1371 }
1372
1373 /**
1374  * unlock a partition's vol headers.
1375  *
1376  * @param[in] dp       the partition to unlock
1377  * @param[in] locktype READ_LOCK or WRITE_LOCK
1378  */
1379 void
1380 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1381 {
1382     VReleaseDiskLock(&dp->headerLock, locktype);
1383 }
1384
1385 /* XXX not sure this will work on AFS_NT40_ENV
1386  * needs to be tested!
1387  */
1388
1389 /**
1390  * lookup a disk partition object by its index number.
1391  *
1392  * @param[in] id      partition index number
1393  * @param[in] abortp  see abortp usage note below
1394  *
1395  * @return disk partition object
1396  *   @retval NULL no such disk partition
1397  *
1398  * @note when abortp is non-zero, lookups which would return
1399  *       NULL will result in an assertion failure
1400  *
1401  * @pre VOL_LOCK must be held
1402  *
1403  * @internal volume package internal use only
1404  */
1405
1406 struct DiskPartition64 *
1407 VGetPartitionById_r(afs_int32 id, int abortp)
1408 {
1409     struct DiskPartition64 *dp = NULL;
1410
1411     if ((id >= 0) && (id <= VOLMAXPARTS)) {
1412         dp = DiskPartitionTable[id];
1413     }
1414
1415     if (abortp) {
1416         opr_Assert(dp != NULL);
1417     }
1418     return dp;
1419 }
1420
1421 /**
1422  * lookup a disk partition object by its index number.
1423  *
1424  * @param[in] id      partition index number
1425  * @param[in] abortp  see abortp usage note below
1426  *
1427  * @return disk partition object
1428  *   @retval NULL no such disk partition
1429  *
1430  * @note when abortp is non-zero, lookups which would return
1431  *       NULL will result in an assertion failure
1432  */
1433
1434 struct DiskPartition64 *
1435 VGetPartitionById(afs_int32 id, int abortp)
1436 {
1437     struct DiskPartition64 * dp;
1438
1439     VOL_LOCK;
1440     dp = VGetPartitionById_r(id, abortp);
1441     VOL_UNLOCK;
1442
1443     return dp;
1444 }
1445
1446 static struct DiskPartition64 *
1447 VLookupPartition_r(char * path)
1448 {
1449     afs_int32 id = volutil_GetPartitionID(path);
1450
1451     if (id < 0 || id > VOLMAXPARTS)
1452         return NULL;
1453
1454     return DiskPartitionTable[id];
1455 }
1456
1457 static void
1458 AddPartitionToTable_r(struct DiskPartition64 *dp)
1459 {
1460     opr_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1461     DiskPartitionTable[dp->index] = dp;
1462 }
1463
1464 #if 0
1465 static void
1466 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1467 {
1468     opr_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1469     DiskPartitionTable[dp->index] = NULL;
1470 }
1471 #endif
1472 #endif /* AFS_DEMAND_ATTACH_FS */