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