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