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