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